Select to view content in your preferred language

KeyError in Creating Web Map Inventory

4826
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
14 Replies
JeffBigos
Esri Contributor

Hi Joshua,

I tested out the code that you provided and it works on my end.

It's good to see that you can access these properties in 2 different ways.

Thanks, 

Jeff

chintakandel
Regular Contributor

Thanks Jeff. It works for my other web maps. Greatly appreciated your

cooperation.

Regards,

Chintamani

*

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

A couple of different ways to alter your code to make it work.  Option #1:

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.layers
   basemap_layers = wmo.basemap['baseMapLayers']

Option #2

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.definition['operationalLayers']
   basemap_layers = wmo.definition['baseMap']['baseMapLayers']
AlistairFox
Regular Contributor

why does under 1.5 i now get this keyerror for opedrational layers. I'm using the same code and loading in the same webmap json from file. But the dictionary does not contain any operationallayer keys after the json file has been loaded in. what changed at 1.5?

for subdir, dirs, files in os.walk(rootdir3):
for file in files:
#print (os.path.join(subdir, file))
print("processing: " + file)

#modify existing web map
web_map_json = str()

# read a previously downloaded and saved web map json from text file
# store each template json file in a seperate folder from the gdb files. Loop through all the templates in each country
with open(os.path.join(subdir, file),"r") as file_handle:
web_map_json = json.load(file_handle)


# publish a new web map setting some attributes and using the json as input
# will need a title and snippet for each country to be somewhere in a list
web_map_item_properties = {'title':file[:-5],
      'type':'Web Map',
      'snippet':'This map shows a Template Map',
      'tags':'ArcGIS Python API',
      'text':json.dumps(web_map_json)}

# add to the portal
web_map_item = gis.content.add(web_map_item_properties)

# create a web map object out of the item
web_map_obj = arcgis.mapping.WebMap(web_map_item)

layer_list = web_map_obj['operationalLayers']

***get KeyError for above line

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Even using 1.4.x, I never got the approach of web_map_obj['opertionalLayers'] to work, which is why I posted two alternative methods to access the operational layers. The current arcgis.mapping module — arcgis 1.5.0 documentation for WebMap states operational layers should be accessed via the layers property (option #1 that I posted):

layers

Operational layers in the web map :return: List of Layers as dictionaries

0 Kudos