Is it possible to change the transparency of a layer in a web map with ArcGIS API for Python?

657
3
Jump to solution
02-13-2020 03:27 PM
ThomasColson
MVP Frequent Contributor

Is it possible to change the transparency of a layer in a web map with ArcGIS API for Python? I see I can add a layer Working with web maps and web scenes | ArcGIS for Developers  but need to adjust the transparency after adding it. 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
ThomasColson
MVP Frequent Contributor

And the answer is yes. Below snippet updates a web layers symbology every 5 mniutes based on some numpy arrays (generated elsewhere). 

 with open(out_fold+'/JSON/NEXRAD_SYMBOLS.json','r') as f:
 data = json.load(f)
 with open(out_fold+'/JSON/NEXRAD_SYMBOLS.json','w') as f:
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["value"] = stMinValue
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["value"] = x
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["value"] = y
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["value"] = z
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["value"] = stMaxValue
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["label"] = "<"+str(round(stMinValue,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["label"] = str(round(x,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["label"] = str(round(y,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["label"] = str(round(z,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["label"] = ">"+str(round(stMaxValue,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["minSliderValue"] = round(stMinValue,3)
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["maxSliderValue"] = round(stMaxValue,3)
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["value"] = prMinValue
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["value"] = c
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["value"] = d
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["value"] = e
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["value"] = prMaxValue
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["label"] = "<"+str(round(prMinValue,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["label"] = str(round(c,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["label"] = str(round(d,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["label"] = str(round(e,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["label"] = ">"+str(round(prMaxValue,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["minSliderValue"] = round(prMinValue,3)
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["maxSliderValue"] = round(prMaxValue,3)
 f.write(json.dumps(data))
 f.close()
 def search_item(conn,layer_name):
 search_results = conn.content.search(layer_name, item_type='Web Map')
 proper_index = [i for i, s in enumerate(search_results) if 
 '"'+layer_name+'"' in str(s)]
 found_item = search_results[proper_index[0]]
 get_item = conn.content.get(found_item.id)
 return get_item
 def update_wm_layerdef(item):
 item_data = item.get_data()
 print("*******************ORIGINAL DEFINITION*********************")
 print(json.dumps(item_data, indent=4, sort_keys=True))
 with open(out_fold+'/JSON/NEXRAD_SYMBOLS.json') as json_data:
 data = json.load(json_data)
 item_properties = {"text": json.dumps(data)}
 item.update(item_properties=item_properties)
 new_item_data = item.get_data()
 print("***********************NEW DEFINITION**********************")
 print(json.dumps(new_item_data, indent=4, sort_keys=True))
 print(datetime.now()-starttime)
 def main():
 conn = GIS("https://www.arcgis.com", "user", "pass")
 item = search_item(conn, 'GRSM_STORM_TOTAL_PRECIPITATION_MAP')
 update_wm_layerdef(item)
 if __name__ == '__main__':
 sys.exit(main())‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

Thomas Colson‌ I moved this question to ArcGIS API for Python‌ to see if it gets any attention

0 Kudos
ThomasColson
MVP Frequent Contributor

And the answer is yes. Below snippet updates a web layers symbology every 5 mniutes based on some numpy arrays (generated elsewhere). 

 with open(out_fold+'/JSON/NEXRAD_SYMBOLS.json','r') as f:
 data = json.load(f)
 with open(out_fold+'/JSON/NEXRAD_SYMBOLS.json','w') as f:
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["value"] = stMinValue
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["value"] = x
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["value"] = y
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["value"] = z
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["value"] = stMaxValue
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["label"] = "<"+str(round(stMinValue,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["label"] = str(round(x,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["label"] = str(round(y,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["label"] = str(round(z,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["label"] = ">"+str(round(stMaxValue,2))
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["minSliderValue"] = round(stMinValue,3)
 data["operationalLayers"][1]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["maxSliderValue"] = round(stMaxValue,3)
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["value"] = prMinValue
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["value"] = c
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["value"] = d
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["value"] = e
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["value"] = prMaxValue
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["label"] = "<"+str(round(prMinValue,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["label"] = str(round(c,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["label"] = str(round(d,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["label"] = str(round(e,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["label"] = ">"+str(round(prMaxValue,2))
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["minSliderValue"] = round(prMinValue,3)
 data["operationalLayers"][2]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["maxSliderValue"] = round(prMaxValue,3)
 f.write(json.dumps(data))
 f.close()
 def search_item(conn,layer_name):
 search_results = conn.content.search(layer_name, item_type='Web Map')
 proper_index = [i for i, s in enumerate(search_results) if 
 '"'+layer_name+'"' in str(s)]
 found_item = search_results[proper_index[0]]
 get_item = conn.content.get(found_item.id)
 return get_item
 def update_wm_layerdef(item):
 item_data = item.get_data()
 print("*******************ORIGINAL DEFINITION*********************")
 print(json.dumps(item_data, indent=4, sort_keys=True))
 with open(out_fold+'/JSON/NEXRAD_SYMBOLS.json') as json_data:
 data = json.load(json_data)
 item_properties = {"text": json.dumps(data)}
 item.update(item_properties=item_properties)
 new_item_data = item.get_data()
 print("***********************NEW DEFINITION**********************")
 print(json.dumps(new_item_data, indent=4, sort_keys=True))
 print(datetime.now()-starttime)
 def main():
 conn = GIS("https://www.arcgis.com", "user", "pass")
 item = search_item(conn, 'GRSM_STORM_TOTAL_PRECIPITATION_MAP')
 update_wm_layerdef(item)
 if __name__ == '__main__':
 sys.exit(main())‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DanPatterson_Retired
MVP Emeritus

See... a change of venue and you came up with it