Select to view content in your preferred language

KeyError in Creating Web Map Inventory

4816
14
Jump to solution
06-20-2018 01:18 PM
chintakandel
Regular Contributor

 

I am trying to create a web map inventory and encountered a KeyError. 

gis = GIS("Portal url", "username", "password")
search_results = gis.content.search(query="tags:streamflow", item_type="Web Map")

l = []
for s in search_results:
   wmo = arcgis.mapping.WebMap(s)
   ops_layers = wmo['operationalLayers']
   basemap_layers = wmo['baseMap']['baseMapLayers']

KeyError: 'operationalLayers'

Any suggestions on how to handle KeyError? 

0 Kudos
1 Solution

Accepted Solutions
JeffBigos
Esri Contributor

Hi Chintamani,

In your code above, I see 2 spots where you are using the layer type key:

1. d["Layer Type"] = "Operational Layer{}".format(op_layer["layerType"])

2.j["LayerType"] = "BasemapLayer{}".format(base_layer["layerType"])

when you see the error which of those lines is the error occurring with?

Thanks,

Jeff

View solution in original post

14 Replies
JeffBigos
Esri Contributor

Hi Chintamani,

What version of the api are you using?

you can check by running the following code:

import arcgis

print(arcgis.__version__)

Recently I had something similar and figured things out in the following way:

to get the operational layers as a key from a dictionary I converted the webmap to json as you see below:

wmoJson = wmo.get_data(try_json=True)
wmo['operationalLayers']

Another access point to the operational Layers would be through the layers property
of the webmap class in the api, but access to the property in the webmap class depends on
the version of the api you have:

wmo.layers

arcgis.mapping module — arcgis 1.4.2 documentation 

Let me know if this helps.

Thanks,

Jeff
chintakandel
Regular Contributor

Hi Jeff,

Thank you for your suggestions to get the operational layers as a key from

a dictionary.

I am using 1.4.2 version.

I have tried to get the operational layers as a key from a dictionary,

still have KeyError:'layerType'.

for s in search_results:

wmo = arcgis.mapping.WebMap(s)

ops_layers = wmo.layers

basemap_layers = wmo.basemap['baseMapLayers']

for op_layer in ops_layers:

d = collections.OrderedDict()

d["Web Map Name"] = s.title

d["Web Map Item ID"] = s.itemid

d["Layer Type"] = "Operational Layer

()".format(op_layer["layerType"]) d["Layer URL"] = op_layer["url"] l.append(d) for base_layer in basemap_layers: j = collections.OrderedDict() j["Web Map Name"] = s.title j["Web Map Item ID"] = s.itemid j["Layer Type"] = "Basemap Layer ()".format(base_layer["layerType"])

j["Layer URL"] = base_layer["url"]

l.append(j)

Thanks again,

Chintamani

*

0 Kudos
JeffBigos
Esri Contributor

Chintamani,

Looking through the code you have provided I see something that could be causing empty operational layers in the resulting dictionary:

in your format function change the () to { } as you see below

d["Layer Type"] = "Operational Layer{}".format(op_layer["layerType"])

Another spot I see that is on the following line:
j["LayerType"] = "BasemapLayer{}".format(base_layer["layerType"])
Try out that change and let me know how it goes.

Thanks,

Jeff
chintakandel
Regular Contributor

Hi Jeff,

Thank you for your suggestions. I have changed my format function but still

encounter a layerType keyError.

Best regards,

Chintamani

*

0 Kudos
JeffBigos
Esri Contributor

Hi Chintamani,

In your code above, I see 2 spots where you are using the layer type key:

1. d["Layer Type"] = "Operational Layer{}".format(op_layer["layerType"])

2.j["LayerType"] = "BasemapLayer{}".format(base_layer["layerType"])

when you see the error which of those lines is the error occurring with?

Thanks,

Jeff

chintakandel
Regular Contributor

Hi Jeff,

layerType error occurs with both lines ( Operational Layer and

BasemapLayer).

Code extracts all other information except layer type.

Thanks again,

Chintamani

*

0 Kudos
JeffBigos
Esri Contributor

Hi Chintamani,

I am able to use the code you provided to get all the information you are looking for in the webmaps including the layerType key. If you try the code on another webmap are you able to get the code to work through that layer type key?

In testing the code above I have been stepping through the code in my debugger. That is a nice way to gain insight into the responses and the values being returned.

Take a look at the above and let me know what you get.

Thanks, 

Jeff




0 Kudos
chintakandel
Regular Contributor

Hi Jeff,

Thank you. I will test it to my another web map.

Thanks again,

Chintamani

*

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Have you tried either of my suggestions?  If so, did either work?