Skip to main content
24 votes
Accepted

Showing the number of currently selected layers in QGIS

You can use this script. It adds a toolbar as seen in the image and shows how many layers are selected. label = QLabel("Selected Layers Count:") count = QLabel() tree_view = iface....
Kadir Şahbaz's user avatar
19 votes

Adding layer to group in layers panel using PyQGIS?

Use addMapLayer(layer,False), which will add a layer without showing it memlay = QgsVectorLayer("Point","myLayer", "memory") QgsProject.instance().addMapLayer(memlay, False) #False is the key ...
JAS's user avatar
  • 495
17 votes

Showing the number of currently selected layers in QGIS

To extend the great solution by @Kadir Şahbaz, you could do something like this to get a count per group: p = QgsProject.instance() root = p.layerTreeRoot() view = iface.layerTreeView() ## TOOLBAR tb ...
Matt's user avatar
  • 17.9k
12 votes
Accepted

Getting layer within a specific group using PyQGIS

You can define a method for this. def select_layer(layer_name, group_name): root = QgsProject.instance().layerTreeRoot() group = root.findGroup(group_name) if group is not None: ...
Kadir Şahbaz's user avatar
11 votes
Accepted

Removing all layers from group using PyQGIS

dump() method of QgsLayerTreeLayer should be only used for debug purpose. You can do it much nicer this way: root = QgsProject.instance().layerTreeRoot() group = root.findGroup(groupName) if group is ...
wawka's user avatar
  • 256
11 votes
Accepted

Moving layers in the Layer Order Panel using PyQGIS

You can set layer order in the Layer Order Panel manually using QgsLayerTreeCanvasBridge.setCustomLayerOrder() method, which receives an ordered list of layer IDs. For instance (assuming you just ...
Germán Carrillo's user avatar
10 votes
Accepted

Sorting layers by name in one specific group of QGIS layer tree

Nice try, you're almost there! You just need to use your group instead of the root for the ordering, like this: from collections import OrderedDict root = QgsProject.instance().layerTreeRoot() group =...
Germán Carrillo's user avatar
10 votes
Accepted

Selecting subgroup in Layers panel using PyQGIS

Setting the Qt.MatchRecursive flag as parameter enables searching the entire hierarchy of the tree view. And looking at the docs for QAbstractItemModel::match it also seems that you put the Qt....
CodeBard's user avatar
  • 3,699
9 votes

Moving layers in the Layer Order Panel using PyQGIS

For QGIS 3: I've found maybe a substitute command: bridge = iface.layerTreeCanvasBridge() bridge.rootGroup().customLayerOrder() From https://qgis.org/api/classQgsLayerTree.html#...
Breizh79's user avatar
9 votes
Accepted

Adding layer to group in layers panel using PyQGIS?

You have to make a clone of the layer and then move it and remove the original layer, try: groupName="test group" root = QgsProject.instance().layerTreeRoot() group = root.addGroup(groupName) vlayer =...
artwork21's user avatar
  • 35.2k
7 votes

Copy styles from many layers

Here is a small Python script which should do what you want. Don't worry if you're not familiar with the Python console, just open it with ctrl+alt+P or click the button in the plugins toolbar. Open ...
Ben W's user avatar
  • 23.1k
6 votes
Accepted

Getting name of group that layer is in using PyQGIS?

Instead of using the layers from the QgsMapCanvas you could better iterate the root of the layer tree view (QgsProject.instance().layerTreeRoot().children()) like I show in How to create a text file ...
Germán Carrillo's user avatar
6 votes
Accepted

Creating event in PyQGIS that notices that group of layers is selected

You can use and modify this script: from qgis.utils import iface from qgis.core import QgsLayerTreeNode view = iface.layerTreeView() def eventoClickPanelCapas(): selected = view.selectedNodes() ...
Kadir Şahbaz's user avatar
6 votes
Accepted

Cloning the map layer using PyQGIS

There are several suggestions regarding your code: adding the same layer l_out one more time to the project, even with the addToLegend = False does not make much sense to me. In the documentation for ...
Taras's user avatar
  • 34k
5 votes
Accepted

Sorting layers in specific group layer with ArcPy?

Your code is flawed in that it is not referencing layers in the group layer and for some reason your last line is to delete the group layer! Below is some code that correctly sorts and moves based ...
Hornbydd's user avatar
  • 44.6k
5 votes

Programmatically select a GROUP node using PyQGIS

The following script selects a node by name. from qgis.PyQt.QtCore import * def select(name): view = iface.layerTreeView() m = view.model() listIndexes = m.match(m.index(0, 0), Qt....
Kadir Şahbaz's user avatar
5 votes

Grouping layers in Portal for ArcGIS?

Portal does not support grouped layers. You can only have grouped layers if you share as a web service and include the service in your web map.
Zara's user avatar
  • 71
5 votes
Accepted

.on("load") event for layer groups in Leaflet

A simple way of doing this would be to count load events of individual layers and when count is equal to number of layers, it's done. Code could then look something like this: var nLayers = 0; var ...
TomazicM's user avatar
  • 27k
5 votes

Layer to KML using ArcGIS Pro doesn't show all points

If you want a tool to operate on every feature in a layer, always unselect selected features before using a tool in ArcGIS. If any feature is selected, the tool uses only selected ones.
Kadir Şahbaz's user avatar
5 votes
Accepted

Getting active group with PyQGIS

The layer tree is a QTreeView object where you can access any selected row via its selectionModel. def activeGroup(): tree_view = iface.layerTreeView() # retrieve current selected index ...
CodeBard's user avatar
  • 3,699
4 votes

Moving layers in the Layer Order Panel using PyQGIS

Based on this (old) question and some other research, I've successfully added a handler to my QGIS 3.10 project to move all newly added layers to the top of the custom layer order. Here's the relevant ...
Houska's user avatar
  • 8,416
4 votes

Dynamically add layer(s) to Layer Group

Here is what I think is the easiest and shortest way to add a Layer to LayerGroup. First add a "group" to the Map, let's call it markerGroup markerGroup = new ol.layer.Group({ ...
andrea06590's user avatar
4 votes

Removing all layers from group using PyQGIS

Update for QGIS 3. Based in @cag answer only need change this line : QgsMapLayerRegistry.instance().removeMapLayer(id) to QgsProject.instance().removeMapLayer(id) And work like a charm
Fran Raga's user avatar
  • 7,908
4 votes

Adding layer to group using PyQGIS

I got tired of forgetting whatever the inputs to the various tree layer methods are so I made a function to handle all that for me. It takes whatever thing (group name, layer id, QgsMapLayer, or ...
Lorem Ipsum's user avatar
4 votes

Grouping layers in Portal for ArcGIS?

I agree with Zara. Unfortunately, Portal doesn't support group layer as of 10.6. A workaround that I have been using is to group the layers in .mxd and publish them as one service. Then I would add ...
ctaldigital's user avatar
4 votes
Accepted

Creating new layer group in ArcMap?

It should just make a new, empty group layer in your table of contents. You should see 'New Group Layer'. You can drag other layers into this group layer and collectively toggle them on/off as needed....
tigerwoulds's user avatar
4 votes
Accepted

Adding data to subgroup layers with same name in ArcPy?

You can get the group/subgroup path using the longName attribute: ... targetGroupLayer = arcpy.mapping.ListLayers(mxd, "SubGroupLayer2", df)[0] print targetGroupLayer.longName ... So you could ...
RGfromRWBJV's user avatar
4 votes
Accepted

Removing a group from layers panel using PyQGIS?

You can use this root.removeChildNode(group)
Jhon Galindo's user avatar
4 votes
Accepted

Enabling the "Render Layers as a Group" option in QGIS through python

You are just missing a few lines. project = QgsProject.instance() root = project.layerTreeRoot() grpSEPASurfaceWater = root.insertGroup(index, 'SEPA Flood Map | Surface Water') # Set Render Layers as ...
Kalak's user avatar
  • 4,049
3 votes
Accepted

Apply a definition query to a Layer Group

You cannot list layer groups with listlayers and you do not want to list groups because you cannot apply a definition query to a layer group. You want to list individual layers and then apply the ...
Bera's user avatar
  • 77.8k

Only top scored, non community-wiki answers of a minimum length are eligible