Select to view content in your preferred language

Unable to manipulate and update json of web map

2074
2
08-12-2021 04:31 AM
MariamJamilah
Occasional Contributor

Hello,

I am trying to remove a set of layers from a web map and add them back later. Since the layers consist of a mix of feature services and image services, I feel the easiest way to do this would be by manipulation the json of the web map. When I use AGOL assisstant, I am able to achieve the above, however when I try to do the same using ArcGIS Python API, it does not work.

I am using the code below to achieve the above, though post executing the update command I get 'True', the layers are not removed from the web map. I have also attached screenshot for reference

 

 

web_map = source.content.get(itemid)
data = web_map.get_data()
rem_j = []
for i in data['operationalLayers']:
    if i['url'].find('mygishub') == -1:
        rem_j.append(i)
        data['operationalLayers'].remove(i)
web_map.update(item_properties=data)

 

 

MariamJamilah_0-1628766891461.png

 

I have also tried to achieve the same by using the WebMap object, but it still does not work. Have attached screenshot and code for reference.

 

 

web_map = source.content.get(itemid)
web_map_obj = WebMap(web_map)
rem_j = []
for i in web_map_obj.layers:
    if i['url'].find('mygishub') == -1:
        rem_j.append(i)
        web_map_obj.layers.remove(i)
web_map_obj.update()

 

 

 

MariamJamilah_1-1628767162559.png

 

Note: I have not used the add_layer and remove_layer methods here. I am aware that the remove layer method accepts a layer object (i.e., webmap.layers[id]), this approach works for removing layers but when it comes to adding a mixture of feature services and image services as layers using their urls, I have to find the type of service and then create a FeatureLayer or ImageryLayer object accordingly for each url and finally add them to the webmap, which I feel is not very efficient as I have a large number of layers.

0 Kudos
2 Replies
GaetanPRU
Occasional Contributor

Maybe the right methode it is remove_layer().

Otherwise, when I want update webmap properties with python, I work on json.

Eg. I use this code :

 

itemWebmap.update(item_properties={'text':new_data})

 

 where new_data is my webmap properties json with new properties.

If it's OK, this request return TRUE.

You can find more informations here : https://developers.arcgis.com/python/sample-notebooks/using-and-updating-gis-content/ 

GaetanPRU
0 Kudos
sofoo
by
Frequent Contributor

You can use get_data to return the current json on an item, then edit the json and use update as shown above. Worked for me!

0 Kudos