2

I get a QGIS project with more than 60 different WMS layers. The layers are in different subgroups. Now I want to get a list of all layernames and the URL of the layer.

Therefore I tried the following code from the QGIS cheat sheet:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
def get_group_layers(group):
  print('- group: ' + group.name())
  for child in group.children():
     if isinstance(child, QgsLayerTreeGroup):
    
       get_group_layers(child)
     else:
       print('  - layer: ' + child.name())

root = QgsProject.instance().layerTreeRoot()
for child in root.children():
   if isinstance(child, QgsLayerTreeGroup):
      get_group_layers(child)
   elif isinstance(child, QgsLayerTreeLayer):
      print ('- layer: ' + child.name())

How can I get the URL of my single layers? If there is a solution without PyQGIS, that would be fine too.

1 Answer 1

6

You can use layer.source() and split the source string at url=:

for layer in QgsProject.instance().mapLayers().values():
    src = layer.source()
    url = src.split('url=')[1]
    print(f"{layer.name()}:   {url}")

enter image description here

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.