There is a Web Map that has a Sketch Layer with a Text in it.
How can I modify the Text by using ArcGIS API for Python?
Solved! Go to Solution.
Hi @KazFukuoka,
Here's how you can do it...
from arcgis.gis import GIS
## access ArcGIS Online
agol = GIS("home")
## the name of the Sketch layer in the layer list
sketch_lyr_name = "Sketch"
## get the WebMap as an item object
item = agol.content.get("WM_ITEM_ID")
## get the WebMap JSON definition as a dictionary
item_data = item.get_data()
## for each layer in the operationalLayers
for lyr in item_data["operationalLayers"]:
## when we hit the Sketch layer of interest
if lyr["title"] == sketch_lyr_name:
## iterate over the layers in the Sketch layer eg. Polygons, Polylines, Points, Text
for sketch_lyr in lyr["featureCollection"]["layers"]:
## we are interested in the Text layer of the Sketch
if sketch_lyr["layerDefinition"]["name"] == "Text":
## for each feature in the Text layer of the skatch
## at this point if there is only one text you could use
## indexing instead sketch_lyr["featureSet"]["features"][0]
for f in sketch_lyr["featureSet"]["features"]:
#print(f["symbol"]["text"])
## if the current text begins with what we are looking for
if f["symbol"]["text"].startswith("UNIQUE START OF TEXT"): # UPDATE CODE HERE
## update the text
f["symbol"]["text"] = "UPDATED TEXT" # UPDATE CODE HERE
## update the WebMap Item definition
updated_properties = {"text" : item_data}
item.update(item_properties=updated_properties)
Update the text in lines 29 and 31.
Please let us know if this works for you.
All the best,
Glen
Hi @KazFukuoka,
Here's how you can do it...
from arcgis.gis import GIS
## access ArcGIS Online
agol = GIS("home")
## the name of the Sketch layer in the layer list
sketch_lyr_name = "Sketch"
## get the WebMap as an item object
item = agol.content.get("WM_ITEM_ID")
## get the WebMap JSON definition as a dictionary
item_data = item.get_data()
## for each layer in the operationalLayers
for lyr in item_data["operationalLayers"]:
## when we hit the Sketch layer of interest
if lyr["title"] == sketch_lyr_name:
## iterate over the layers in the Sketch layer eg. Polygons, Polylines, Points, Text
for sketch_lyr in lyr["featureCollection"]["layers"]:
## we are interested in the Text layer of the Sketch
if sketch_lyr["layerDefinition"]["name"] == "Text":
## for each feature in the Text layer of the skatch
## at this point if there is only one text you could use
## indexing instead sketch_lyr["featureSet"]["features"][0]
for f in sketch_lyr["featureSet"]["features"]:
#print(f["symbol"]["text"])
## if the current text begins with what we are looking for
if f["symbol"]["text"].startswith("UNIQUE START OF TEXT"): # UPDATE CODE HERE
## update the text
f["symbol"]["text"] = "UPDATED TEXT" # UPDATE CODE HERE
## update the WebMap Item definition
updated_properties = {"text" : item_data}
item.update(item_properties=updated_properties)
Update the text in lines 29 and 31.
Please let us know if this works for you.
All the best,
Glen
Thank you for the detailed answer. This is amazing.
I am almost there.
- I fould f["symbol"]["text"] and modified.
- item.update() returned True.
- But the change does not happen online.
Is there any "commit" or "writeback" necessary?
itemWebMap = gis.content.get(IdWebMapContacts)
dataWebMap = itemWebMap.get_data()
for layerOp in dataWebMap['operationalLayers']:
if layerOp['title'] != NameSketch: continue
for layerFeature in layerOp['featureCollection']['layers']:
if layerFeature['layerDefinition']['name'] != 'Text': continue
if len(layerFeature['featureSet']['features']) != 1:
ErrorExit('There must be one feature')
for feature in layerFeature['featureSet']['features']:
feature['symbol']['text'] += '<<UPDATED TEXT>>'
dataNew = {'text' : dataWebMap}
result = itemWebMap.update(item_properties=dataNew)
if not result:
ErrorExit('Update failed')
Hi @KazFukuoka,
Do not use <> in your testing text on line 14, it probably thinks this is a tag (html). It does not work with <> but I removed and worked just fine.
Wow, I never thought about it. Everything is working now.
Thanks a lot for the detailed info 😀