Saturday 29 April 2017

Performing in-memory difference operation between vector layers in PyQGIS


I've been working on a project using QGIS's Python API. I need to create an in-memory layer that is defined as the difference between two polygonal vector layers, but I'm having difficulty figuring out how to do this for a standalone script.


I've looked at the processing plugin, but it seems to require a file output. I've seen that some people use the runandload() function (How to load memory output from QGIS processing?), but this seems to be specifically for the QGIS GUI console, and I'm needing to generate the output layer in memory.



Answer



Probably I don't completely understand your question.


Are you looking for something like this:


diff = processing.runalg("qgis:difference", layer1, layer2, False, None) # you can use any name intead of diff

diff_res = processing.getObject(diff['OUTPUT']) # you can use any name intead of diff_res

# And then, for example:
for feature in diff_res.getFeatures():
# some stuff

where layer1 and layer2 are your polygonal vector layers? In this way, you generate an output layer in memory without specifying a file output or loading it.


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...