Thursday, 21 January 2016

QGIS python processing.runalg Clip returns None


I'm writing a script in pyQGIS, which would emulate measurement of a line by "walking" along it with a compass with constant length. I have a problem with the Clip algorythm from processing module, namely it returns None, regardless of whether I add layers as inputs or selected features. What do I do wrong? I believe, that should the clip result in an empty layer, it would still be there, just empty? Below is the piece of code with problems,


while d.measureLine(QgsPoint(coorFin[0],coorFin[1]),QgsPoint(coor[0],coor[1]))>step: # If the distance between current and last point is more than chosen step
# Create buffer polygon around a point
bufCirc = point.geometry().buffer(step,10) # do a buffer from existing point

buforek = QgsFeature()
buforek.setGeometry(bufCirc)

# Create layer and put the polygon inside
bufferLayer = QgsVectorLayer("Polygon", "temporary_polygons", "memory")
pz = bufferLayer.dataProvider()
bufferLayer.setCrs(my_crs)
pz.addFeatures([buforek])
bufferLayer.commitChanges()


# select the buffer
buforSel = bufferLayer.setSelectedFeatures([buforek])

# Clip coastline with the buffer
liniaWarstwa = processing.runandload("qgis:clip", selectedCoastLine, buforSel, "memory:liniaWarstwa")
print liniaWarstwa
layer = QgsMapLayerRegistry.instance().mapLayersByName("memory:liniaWarstwa")[0]

The print prints "none", QgsMapLayerRegistry says "index out of range".



Answer




In the newer versions of the Processing plugin, it would seem that you can no longer define the name of the output memory layer. The name of the algorithm is used instead (see similar post).


Therefore, you should replace the last section of your code with something similar:


# Clip coastline with the buffer
processing.runandload("qgis:clip", selectedCoastLine, buforSel, None)
layer = QgsMapLayerRegistry.instance().mapLayersByName("Clipped")[0]

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...