I have created a table join between two shapefiles which I've named 'OSMM' and 'Mastermap'. I want to copy and paste all features (polygons) without a match from the target layer (Mastermap) into the parent shapefile (OSMM). I am relativley new to PyQGIS and can't find any up-to-update code for QGIS 3.6.

Here is my code so far.

#1. loads the SHP files
uri = 'C:/Users/xx/Downloads/OSMM.shp'
vlayer = iface.addVectorLayer(uri,"","ogr")
uri = 'C:/Users/xx/Downloads/Mastermap.shp'
vlayer = iface.addVectorLayer(uri,"","ogr")

#2. Joins "InvoiceNum" from OSMM to Mastermap
layerToJoin = QgsProject().instance().mapLayersByName('OSMM')[0]
target = QgsProject().instance().mapLayersByName('Mastermap')[0]
myJoin = QgsVectorLayerJoinInfo()

#3. Selects NULL features from Mastermap
params = { 'EXPRESSION' : '\"OSMM_InvoiceNum\" is NULL', 'INPUT' : 'C:/Users/xx/Downloads/Mastermap.shp', 'METHOD' : 0 }
processing.run("qgis:selectbyexpression", params)

So how do I perform the PyQGIS version of Ctrl+C on 'Mastermap' and Ctrl+V into 'OSMM'?

Presumably I will then need to use the following code:

#5. Save the changes
  • 1
    Try the "Append Features to Layer" plugin (documentation here) and let me know if that works for you using the algorithm's GUI (it has an option to only copy selected values). If that works, I can write a full answer on how to use it from PyQGIS below. – Germán Carrillo Aug 23 at 14:41
  • @GermánCarrillo - That's a neat plugin, thanks for sharing! – Joseph Aug 23 at 14:58
  • @GermánCarrillo - Yes, thanks for sharing. I have tried it out and it works perfectly! – James B Aug 23 at 15:22
  • Good, I'll elaborate an answer now – Germán Carrillo Aug 23 at 15:27

You could use the 'Append Features to Layer' plugin. It was created to provide users with an algorithm for copying & pasting features from one layer to another based on QGIS copy&paste's code.

How to call it from PyQGIS

After installing the plugin, use a normal processing call in this way:

processing.run("etl_load:appendfeaturestolayer", {'INPUT':QgsProcessingFeatureSourceDefinition(source_layer_path, True), 'OUTPUT':target_layer})

Notice that the second parameter of the INPUT (the one with the value True) is telling the algorithm to only copy selected features from source layer.

NOTE: If you're more interested in the details, you can actually read the Python code of the algorithm, which you could reuse in your code. It of course depends on your use case. The key part is to establish a mapping between source and target layers: See the code.

  • Super stuff Germán, thank you. – James B Aug 27 at 9:19
  • There seems to be 2 open brackets here but 1 closed creating a syntax error – James B Aug 27 at 9:32
  • Solved with the following: { 'INPUT' : QgsProcessingFeatureSourceDefinition('C:/Users/xx/Downloads/Mastermap.shp', True) – James B Aug 27 at 9:49
  • Right, thanks for the feedback. – Germán Carrillo Aug 27 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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