10

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.

Simbamangu
  • 14,773
  • 6
  • 59
  • 93
  • 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 01 '12 at 03:35
  • @TomazicM, your edits, changing the title from a clear question ("How do I ..") to a gerund, leads to ambiguity - is the question now about a problem with duplicate layers, a way of making duplicates, or some other quality about duplicate layers? Around the time this was asked, the community largely agreed to write clear titles-as-questions, even having a campaign to fix ambiguous titles. This edit does not seem to add to clarity or value. – Simbamangu Sep 16 '19 at 18:33
  • 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 '19 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 '19 at 18:53
  • @TomazicM - figured I'd check with the community on Meta, let's see what the discussion looks like! I think that the potential ambiguity is problematic - and edits "... are expected to be substantial and to leave the post better than you found it." (emphasis mine). – Simbamangu Sep 16 '19 at 19:24
  • @TomazicM, good discussion on this on meta from last year was pointed out to me. – Simbamangu Sep 17 '19 at 05:46
  • 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 '19 at 08:45

2 Answers2

11

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.

dakcarto
  • 7,754
  • 26
  • 34
  • 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 01 '12 at 03:38
  • This feature is implemented in revision c11df1aa79. http://hub.qgis.org/projects/quantum-gis/repository/revisions/c11df1aa793c56c4ed9359b7caebea58be8bf76c – Vladimir Nov 02 '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 02 '12 at 22:09
4

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.

user94812
  • 497
  • 2
  • 12