2

I'm trying to find the closest point on a line to another point as in this post. However when I try to write the result as a shapefile I get an error:

TypeError: QgsVectorFileWriter.addFeature(QgsFeature, QgsFeatureRendererV2 renderer=None, QGis.UnitType outputUnit=QGis.Meters): argument 1 has unexpected type 'Point'

import shapely
from shapely.wkt import *
from shapely.geometry import *
from qgis.gui import *
from qgis.core import *
from PyQt4.QtCore import Qt

lineLayer = iface.mapCanvas().layer(0)
pointLayer =  iface.mapCanvas().layer(1)
canvas =  iface.mapCanvas()
spIndex = QgsSpatialIndex()
lineIter =  lineLayer.getFeatures()

provider = pointLayer.dataProvider()
fields = provider.fields()
writer = QgsVectorFileWriter('Test.shp', 'CP1250', fields, provider.geometryType(), provider.crs(), "ESRI Shapefile")
for lineFeature in lineIter:
    spIndex.insertFeature(lineFeature)
pointIter =  pointLayer.getFeatures()
for feature in pointIter:
    ptGeom = feature.geometry()
    pt = feature.geometry().asPoint()
    nearestIds = spIndex.nearestNeighbor(pt,1)
    featureId = nearestIds[0]
    nearestIterator = lineLayer.getFeatures(QgsFeatureRequest().setFilterFid(featureId))
    nearFeature = QgsFeature()
    nearestIterator.nextFeature(nearFeature)
    shplyLineString = shapely.wkt.loads(nearFeature.geometry().exportToWkt())
    shplyPoint = shapely.wkt.loads(ptGeom.exportToWkt())
    dist = shplyLineString.distance(shplyPoint)
    shplySnapPoint = shplyLineString.interpolate(shplyLineString.project(shplyPoint))
    snapGeometry = QgsGeometry.fromWkt(shapely.wkt.dumps(shplySnapPoint))
    r = QgsRubberBand(canvas,QGis.Point)
    r.setColor(Qt.red)
    r.setToGeometry(snapGeometry,pointLayer)
    writer.addFeature(shplySnapPoint)
del writer
jburrfischer
  • 399
  • 1
  • 3
  • 5
  • After a quick browse of your code it seems like the object shplySnapPoint from shplyLineString.interpolate is of type shapely.geometry.point.Point. The write.addFeature takes a QgsFeature / QgsPoint as argument. Try parsing the shapely geometry to a QgsPoint and use that object in the addFeature method. Pseudocode (have not used shapely before): qgsSnapPoint = QgsPoint(shplySnapPoint.x(),shplySnapPoint.y()) – Jakob Mar 24 '15 at 07:29

2 Answers2

2

After a quick browse of your code it seems like the object shplySnapPoint from shplyLineString.interpolate is of type shapely.geometry.point.Point. The write.addFeature takes a QgsFeature / QgsPoint as argument. Try parsing the shapely geometry to a QgsPoint and use that object in the addFeature method. Pseudocode (have not used shapely before):

qgsSnapPoint = QgsPoint(shplySnapPoint.x(),shplySnapPoint.y())
Jakob
  • 7,471
  • 1
  • 23
  • 41
1

If you type a help (Python Console) of QgsVectorFileWriter.addFeature:

help(QgsVectorFileWriter.addFeature)
Help on built-in function addFeature:

addFeature(...)
    QgsVectorFileWriter.addFeature(QgsFeature, QgsFeatureRendererV2 renderer=None, QGis.UnitType outputUnit=QGis.Meters) -> bool

you get something equivalent to 'your error'. So, the object shplySnapPoint is not a QgsFeature class object in:

writer.addFeature(shplySnapPoint)

You have to fix this.

xunilk
  • 29,891
  • 4
  • 41
  • 80