1

Within QGIS 2.8.1 I found within the Processing Toolbox a script called "Points on crossing lines" which should exactly as it is named. Unfortunately it needs processing.core.VectorWriter which I guess has disappeared in this latest installment of QGIS. I tried to replace the writer with QgsVectorWriter but get the following error message:

"Algorithm [Unnamed algorithm] starting...
Index lines...
Find crossing points...
arguments did not match any overloaded call:
QgsVectorFileWriter(QString,QString, QgsFields, QGis.WkbType,
QgsCoordinateReferenceSystem, QString driverName="ESRI Shapefile",
QStringList datasourceOptions=QStringList(), QStringList
layerOptions=QStringList(), QString newFilename=None,
QgsVectorFileWriter.SymbologyExport
symbologyExport=QgsVectorFileWriter.NoSymbology): argument 3 has unexpected
type 'list' QgsVectorFileWriter(QgsVectorFileWriter): argument 1 has
unexpected type 'unicode' See log for more details" 

Can you point me at what I am doing wrong?

##Lines=vector
##Results=output vector

from qgis.core import *
from PyQt4.QtCore import *



cutLayer = processing.getObject(Lines)
cutPrder = cutLayer.dataProvider()
n = cutLayer.featureCount()
l = 0

# build spatial index of lines

index = QgsSpatialIndex()
geom_ix = {}
progress.setText("Index lines...")

for feat in processing.features(cutLayer):
progress.setPercentage(int(100*l/n))
l+=1

index.insertFeature(feat)
geom_ix[feat.id()] = feat.geometry().asWkb()



# find points on crossing lines
progress.setText("Find crossing points...")

l = 0
i = 0
ptindex = QgsSpatialIndex()
pt_ix = {}
secgeom = QgsGeometry()
featgeom = QgsGeometry()
resfeat = QgsFeature()

for feat in processing.features(cutLayer):
progress.setPercentage(int(100*l/n))
l+=1

near = index.intersects(feat.geometry().boundingBox())

for f in [x for x in near if x != feat.id()]:   # exclude self

    featgeom = feat.geometry()
    secgeom.fromWkb(geom_ix[f])

    if featgeom.crosses(secgeom):
        crosspts = feat.geometry().intersection(secgeom).asGeometryCollection()

        for pt in crosspts:
            i += 1

            # index point
            resfeat.setGeometry(pt)
            resfeat.setFeatureId(i)
            ptindex.insertFeature(resfeat)
            pt_ix[i] = pt.asPoint()

feat = QgsFeature()
fields = [QgsField("nodeid", QVariant.Int)]
writer = QgsVectorFileWriter(Results,None, fields, QGis.WKBPoint, 27700)

progress.setText("test")

# only save unique points
progress.setText("Save unique points...")
n = len(pt_ix)
featgeom = QgsGeometry()


while len(pt_ix) != 0:
progress.setPercentage(int(100*(n-len(pt_ix))/n))

i = pt_ix.keys()[0]

# write point

attrs = [i]
feat.setGeometry(featgeom.fromPoint(pt_ix[i]))
feat.setAttributes(attrs)
writer.addFeature(feat)

# delete close points
near = ptindex.intersects(buffRect(pt_ix[i], buff))

for pt in near:         
    feat.setFeatureId(pt)
    feat.setGeometry(featgeom.fromPoint(pt_ix[pt]))
    deleted = ptindex.deleteFeature(feat)
    del pt_ix[pt]

del writer

2 Answers 2

3

Try with processing.tools.vector instead of processing.core.VectorWriter.

2

The message points to variables result and fields not being what QgsVectorFileWriter expects them to be. There is a QString definition as unicode, so probably you can just cast variable Results as QString(Results). If that doesn't work, you'd get rid of the problem by specifying an Ascii filename (I suppose you specified Results as a filename).

I couldn't find any documentation about the constructor of QgsFields, but it's more than just a list, so I'd try with

  fields = QgsFields()
  fields.append(QgsField("nodeid", QVariant.Int))

  writer = QgsVectorFileWriter(QString(Results),None, fields, QGis.WKBPoint, 27700)

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.