3

Vindbrukskollen has a nice map of wind turbines. From the source of that webpage, I get this link to an ArcGIS REST Server Layer:

https://ext-geodata-nationella.lansstyrelsen.se/arcgis/rest/services/

In QGIS, I click on Layer -> Add Layer -> Add ArcGIS REST Server Layer -> New, then privde this URL and give a name to the layer. After OK, I Connect to the layer. I am offered to choose from a lot of layers:

enter image description here

Layers seem to be organised into folders. I opened a few to show that there are many. In some cases, the file structure is even deeper:

enter image description here

If I add all of them, my QGIS session crashes. I don't want to manually copy-paste all of them. After clicking Plugins -> Python Console, I am able to access layer names of already added layers, that is described in this answer.

How can I use Python Console to access the name of layers offered by the REST Server, before adding these layers to canvas?

In other words: I need to access to the dropdown list shown on the above screenshot via Python.

TomazicM
  • 25,601
  • 22
  • 29
  • 39
zabop
  • 2,050
  • 7
  • 35

1 Answers1

5

Here is a script that will walk through the directory of folders, services, and layers. Ultimately, it prints the folder name, the service name, the layer name, and layer type for all available layers.

See this answer for clarification on how the URLs were derived.

import urllib.request, json

root_url = r'https://ext-geodata-nationella.lansstyrelsen.se/arcgis/rest/services/?f=pjson'

download the root json

with urllib.request.urlopen(root_url) as url: data = json.loads(url.read().decode())

iterate through the 'folders' key of the root json

for folder in data['folders']:

# construct a url to get the json of the folder
folder_url = root_url.split('?')[0] + folder + '?f=pjson'

# download the folder json
with urllib.request.urlopen(folder_url) as url:
    sub_data = json.loads(url.read().decode())

# some urls require a log in, in which case the json has a key called 'error'
if 'error' not in sub_data.keys():
    for service in sub_data['services']:

        # construct the url for the service json
        service_url = folder_url.split(folder)[0] + service['name'] + '/MapServer/?f=pjson'

        # download service json
        with urllib.request.urlopen(service_url) as url:
            sub_sub_data = json.loads(url.read().decode())

        # iterate through service json 'layers' key
        for layer in sub_sub_data['layers']:

            # do something with results
            print(folder, '|', service['name'], '|', layer['name'], '|', layer['type'])

Sample of output (the whole output is very large).

BS | BS/lst_bs_mov03 | BS Förbudsområde 1-årsregeln | Feature Layer
BS | BS/lst_bs_mov03 | BS Beviljade bearbetningskoncessioner | Feature Layer
Energimyndigheten | Energimyndigheten/lst_energigas_fak09 | Fordonsgasinfrastruktur (grupp) | Group Layer
Energimyndigheten | Energimyndigheten/lst_energigas_fak09 | Tankställen för flytande fordonsgas | Feature Layer
Energimyndigheten | Energimyndigheten/lst_energigas_fak09 | Tankställen för fordonsgas | Feature Layer
FHM | FHM/lst_fhm_fak02 | FHM Badplatser (alla) | Feature Layer
FM | FM/lst_fm_riksintressen_3_9 | FM_Riksintresse_pa_land_MB3kap9 | Feature Layer
FM | FM/lst_fm_riksintressen_3_9 | FM_Riksintresse_i_havet_sjoovningsomrade_MB3kap9 | Feature Layer
Matt
  • 16,843
  • 3
  • 21
  • 52