Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.name(), vl.providerType() )

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

share|improve this question

1 Answer 1

You can retrieve the selected features with

vl = iface.acitveLayer()
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
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 –  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

Your Answer

 
discard

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.