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()
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
user22892
  • 31
  • 1
  • 2

1 Answers1

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()
Matthias Kuhn
  • 27,780
  • 3
  • 88
  • 129