Feature request #5899

Duplicate layer

Added by Olivier redlead almost 12 years ago. Updated over 11 years ago.

Status:Closed
Priority:Normal
Assignee:Giuseppe Sucameli
Category:-
Pull Request or Patch supplied:No Resolution:fixed
Easy fix?:No Copied to github as #:15348

Description

Hi !

Often you want to display one Shapefile with several layers (example : for the shapefile LandUse.shp, show column "land_use_current" on one layer, and "land_use_projet" on another layer)

When you want to do so, you have to add the Shapefile several times, meaning you have to go in your file hierarchy to find it again each time you want a new layer from your data.

It would be really nice to have a little "duplicate layer" button !

Thanks !

History

#1 Updated by Giuseppe Sucameli almost 12 years ago

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())

You can also create a little plugin using the previous snippet.

#2 Updated by Giuseppe Sucameli almost 12 years ago

Sorry, I've misunderstood the subject. 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.

#3 Updated by Bernd Vogelgesang over 11 years ago

I already found several past feature requests which weren't processed or where the relevance of the topic didn't get clear enough.
#1483
#3554
Working with complex datasets nearly always requires building views for different aspects of the underlying data.
Having to add the same dataset through the file wizard all the time is a tedious task when you need multiple of these views (e.g. queries, different labeling, colors etc)

Use case: I have 13 groups of monitoring plots, where i have to produce overview maps of. For this, i need as basis a point, line and area layer which i then query for the group-id and put those layers in a group within the TOC for quick on/off of the features.

Having the possibility to quickly duplicate the original set for performing queries and changing additional setting would be a real helper.
Shouldn't mention it, but in ArcGIS, this is a basic feature for a long time (right-click action on layer in TOC to duplicate it).

Coding python scripts themselves is beyond average users abilities i fear...

#4 Updated by Alister Hood over 11 years ago

Olivier redlead wrote:

Often you want to display one Shapefile with several layers (example : for the shapefile LandUse.shp, show column "land_use_current" on one layer, and "land_use_projet" on another layer)

Am I missing something here? I thought a shapefile could only contain a single layer...
Do you mean you want to display different labels depending on the contents of different columns? This is possible with the new expression based labelling, but they will all be formatted the same. If you want to format them differently, then you need rule-based labelling, and there is already a ticket open for that.
Or do you mean you want to run several queries on the layer, based on different columns, and use different symbols for each one? This is possible with the rule-based renderer.

#5 Updated by Alister Hood over 11 years ago

Bernd Vogelgesang wrote:

put those layers in a group within the TOC for quick on/off of the features.

Ah, OK. That's the use case.
Yes, I've found myself wanting to toggle the visibility of features per rule when using the rule based renderer. As long as that isn't possible then I'm sure plenty of people would appreciate a "duplicate layer" plugin.

#6 Updated by Alister Hood over 11 years ago

Should this be merged with #1483?
N.B. copy and paste (#1483) would be slightly better than duplicate, as you could paste into a different QGIS project.

#7 Updated by Giovanni Manghi over 11 years ago

  • Assignee set to Giuseppe Sucameli
  • Target version set to Version 2.0.0

Alister Hood wrote:

Should this be merged with #1483?
N.B. copy and paste (#1483) would be slightly better than duplicate, as you could paste into a different QGIS project.

I'm not sure that #1483 is about copy/paste layers between projects, seems really a duplicate of this ticket.

By the way the one line code given by Giuseppe works like a charm (it works also for rasters with a trivial change) and I really would like to see a "clone layer" button (or "clone layer" right click dialog entry) dialog to be added to the standard toolbar. I already asked this privately to Giuseppe, let's see what he says.

#8 Updated by James Stott over 11 years ago

The duplicate layer button/option on the right click menu would be a great addition.

A slight extension of this would be to enable you to add a layer for each value in a column of a dataset.

For example, say I have a table with land uses (say 10 different types), and I want to create a separate layer with each land use in it. At the moment you would have to add the layer 10 number of times, and then do right click > query 10 times. A simple way to add a layer for each category in the land use type column would save a lot of time in this case.

#9 Updated by Alexander Bruy over 11 years ago

  • Status changed from Open to Closed
  • Resolution set to fixed

Implemented in c11df1aa79

#10 Updated by Giovanni Manghi over 11 years ago

Alexander Bruy wrote:

Implemented in c11df1aa79

great addition Alexander! Thanks!

Also available in: Atom PDF