Is it possible to create a duplicate layer, only displaying the selection from the layer you are duplicating in QuantumGIS? Similar to ArcGIS -> Create Layer from Selected Features.

I looked at "How to duplicate a layer"

iface = qgis.utils.iface
vl = iface.activeLayer()
iface.addVectorLayer( vl.source(), vl.name(), vl.providerType() )

but can't see how to duplicate only the selected features?

1 Answer 1


You can retrieve the selected features with

vl = iface.activeLayer()
selectedFeatures = vl.selectedFeatures()

You then need to add a new vector layer. If you want your duplicate layer only in memory (i.e. not persistent) use a memory layer (see the pyqgis cookbook) If you want your layer to be persistent, refer to the section about writing vector layers.

Use then the QgsVectorFileWriter.addFeature or QgsVectorLayer.addFeatures method on the retrieved layer / layerwriter to add the features.

In case you have really many features, it might be prudent to use selectedFeaturesIds() and then query and iterate over these to not have to copy all the features into the memory.

  • 2
    You can also use "save selection as". First you select the feature(s),then select the layer, right click and choose save selection as. Here you can define the type of file, shape for instance. Works like data export data /selected features in ArcMap Jun 21, 2013 at 18:11
  • Good point. It's a bit unclear from the question, if python is involved or a UI only solution is looked for. Jun 22, 2013 at 10:22
  • There is a little typo in the code: it should say activeLayer, not acitveLayer. Unfortunately I cannot edit just one character. Jun 14, 2015 at 9:32

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.