3

i tried (i never tried before...) to write a simple python code like action in QGIS on my "XXX" vector layer that:

  1. open a layer "YYY"
  2. copy the selected feature on my "XXX"layer
  3. paste the selected feature in the "YYY" layer

Point 1) work ok, but point 2) don't work

Here is my code:

qgis.utils.iface.addVectorLayer("/home/stefano/Documenti/YYY.shp", "YYY", "ogr");
qgis.utils.iface.actionCopyFeatures()

1 Answer 1

6

Besides actionCopyFeatures() there is also an actionPasteFeatures(). They both work on the active layer. actionCopyFeatures() works on the current feature selection. First make sure you have the two references to your source and destination Layer.

# only QGIS < 2.0
iface = qgis.utils.iface
# Make the source layer active
iface.setActiveLayer( sourceLayer )
# Set the selection on the source layer (Could also be done manually with the selection tools
sourceLayer.setSelectedFeatures( [ 1, 5, 10 ] )
# Copy
iface.actionCopyFeatures().trigger()
# Set destination layer active
iface.setActiveLayer( destinationLayer )
# Turn on editing on destination layer, so we can paste
destinationLayer.startEditing()
# Paste features
iface.actionPasteFeatures().trigger()
# Uncomment to automatically save edits
# destinationLayer.commitChanges()

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.