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.

  • I'm not having difficulty creating an in-memory vector layer. My issue is that I don't know how to access a difference operator that outputs to an in-memory vector layer.
    – Benjamin
    Dec 6, 2016 at 0:24

1 Answer 1


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.

  • That's exactly what I'm looking for, actually. I'll check to see how well it works for me.
    – Benjamin
    Dec 6, 2016 at 1:46

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.