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.

I'd like to display the same vector data in different ways as different layers. While it's very handy to be able to copy and paste the layer style in 1.8+, it would be nice to be able to select a layer and right-click to copy or duplicate it directly.

Any easy way of doing this, like a Python command?


Update: @dakcarto's suggestion works perfectly. Since I needed to add many duplicate layers, I adapted it to loop as many times as needed:

for n in range(1,5): 
    iface = qgis.utils.iface; vl = iface.activeLayer(); iface.addVectorLayer(vl.source(), vl.name() + "_clone" + str(n), vl.providerType())

The only problem is that the new layer becomes the activeLayer, so it adds "_clone" and the number to the previous name, ending up with example_clone1, example_clone1_clone2, up to the unwieldy example_clone1_clone2_clone3_clone4_clone5. Need to figure out how to duplicate from the originally selected layer, now.

share|improve this question
    
This isn't really an answer, but QGIS doesn't mind if you add the same vector layer to a project twice. –  L_Holcombe Oct 31 '12 at 21:05
    
yes, I've added many a duplicate later, but only by "add layer" and browsing to select the layer each time. Minimum 4 clicks and some scrolling, which gets tiresome with needing to duplicate a layer 20 times. –  Simbamangu Nov 1 '12 at 3:35
add comment

1 Answer 1

up vote 5 down vote accepted

There are two feature requests regarding this (#5899 and #1483). This is certainly a doable feature, and could likely be included in version 2.0, if a developer were interested in adding it; or, a Python plugin developer gave it some consideration.

The #5899 issue also includes some Python code for duplicating a layer (submitted by developer Giuseppe Sucameli):

In the meantime, select the vector layer then open the QGis python console and run:

iface = qgis.utils.iface; vl = iface.activeLayer(); iface.addVectorLayer(vl.source(), vl.name() + "_clone", vl.providerType())

The previous code adds to the map the same sublayer.

It's difficult to do it using a one-line python script like the previous one, but if you know the sublayer name you can just replace vl.source() with vl.source().split("|")[0] + "|layername=my_sublayer_name" where my_sublayer_name is the name of your sublayer.

It appears after running the duplication code, you will have to copy/paste the original layer's style.

share|improve this answer
    
Works perfectly. By adding 'v' as the key to paste the layer style, duplication of layer and style takes only a few seconds now. –  Simbamangu Nov 1 '12 at 3:38
    
This feature is implemented in revision c11df1aa79. hub.qgis.org/projects/quantum-gis/repository/revisions/… –  Vladimir Nov 2 '12 at 18:41
1  
The Python binding for that commit to master branch works like so: iface.setActiveLayer( my_layer );iface.actionDuplicateLayer().trigger() where my_layer is a QgsMapLayer that is set to the active, selected layer in the legend; then, the actionDuplicateLayer() is triggered to work on the selected layers in the legend (its default). –  dakcarto Nov 2 '12 at 22:09
add comment

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.