Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to create a duplicate layer, only displaying the selection from the layer you are duplicating in QuantumGIS? Simular 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.providerType() )

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

share|improve this question

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.

share|improve this answer
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 – Gerardo Jimenez Jun 21 '13 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. – Matthias Kuhn Jun 22 '13 at 10:22
There is a little typo in the code: it should say activeLayer, not acitveLayer. Unfortunately I cannot edit just one character. – BritishSteel Jun 14 '15 at 9:32
@BritishSteel Thanks, changed – Matthias Kuhn Jun 14 '15 at 17:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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