9

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.

  • 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
  • 1
    @Simbamangu There are different views on this, I went through GIS wiki. I definitelly appreciate your view, let me share mine. I consider question title as a direction quide about gist of the question. Since it's all about questions, then intros "How do I ...", "Why it is ..." add nothing new since it's clear that this is a question. It might make question more readable, but anyhow you have to look at the body of the question to see what it's about. To see whole page of questions that start with "How do I ..." is at least for me unproductive. – TomazicM Sep 16 at 18:50
  • 1
    And the reason I changed title of your old question? It bumped up at GIS home page because of somebodys action and I admit a have a (bad) habit of removing "How" and "Why" parts of questions. – TomazicM Sep 16 at 18:53
  • 1
    @Simbamangu I personnaly prefer questions without "How" and "Why" because these words add nothing to the content. They may make title more readabale, but I don't see any problem if title is a bit ambigous, since one has to read body of question anyway to see what's it all about. And I admit that I started removing those word after I saw PolyGeo doing it and then reading some meta articles about good practices when posting/editing questions. – TomazicM Sep 17 at 8:45
10

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.

  • 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
  • 2
    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
1

The recent versions of QGIS now have a "Duplicate Layer" option. Right-click the layer you want to duplicate, and it'll insert a new copy just below.

Side note: It seems you need to hit View --> Refresh for changes made in the one layer to propagate to the next.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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