I'm developing a plugin where I would like to get some features from one layer (a), do a difference on them with the entirety of a second layer (b), and put the results into a layer I've created in memory (c). If at all possible, I'd also like to copy a specific field from (a) to (c) when doing the difference.
To do this, I've got the following code set up:
layer_A = None
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "A":
layer_A = lyr
break
it = layer_A.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"CAT" = 27') )
layer_A.setSelectedFeatures( [ f.id() for f in it ] )
box = layer_A.boundingBoxOfSelected()
iface.mapCanvas().setExtent(box)
iface.mapCanvas().refresh()
layer_B = None
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "B":
layer_B = lyr
break
l1 = QgsVectorLayer("Polygon?field=CAT:integer", "C", "memory")
QgsMapLayerRegistry.instance().addMapLayer(l1)
As you can see, we select various features in A based on a query.
I'd like to perform a difference between these selected features, and the entirety of B, which is unfiltered and to put the resulting shapes into C (with the CAT, if at all possible, but not necessary).