How to preserve the symbology of feature layer using updating/overwriting features as described in API documentation?

729
1
02-09-2022 08:58 AM
RehanChaudhary
Occasional Contributor

I am trying to write a script where i can overwrite or update a feature layer depending on the requirements on the arcgis portal from a csv file. I found these two methods under the python api documentation on this link:

https://developers.arcgis.com/python/sample-notebooks/updating-features-in-a-feature-layer/

 

Both methods allow you to update or overwrite the contents of the feature layer but which one is able to preserve the symbology or is there any other way to preserve it?

1 Reply
Clubdebambos
Occasional Contributor III

Hi @RehanChaudhary 

Manually apply the symbology and then export the JSON. Print the JSON to screen using the snippet below.

from arcgis import GIS
import json

agol = GIS("home")

item = agol.content.get("***item_id***")

lyr = item.layers[0]

print(json.dumps(lyr.properties["drawingInfo"], indent=4))

Open a Notepad and add the below first, then copy and past the JSON output into the placeholder. Save the file with a .json file extension.

{
    "drawingInfo" :
	*** copy and paste JSON here***
}

This .json file is now your symbology template for the layer. You can apply the symbology using the code below every time you update. You can alter your current script and add similar code to update the symbology after the update.

from arcgis import GIS
import json

agol = GIS("home")

item = agol.content.get("***item_id***")

lyr = item.layers[0]

## open the JSON file
with open(r"C:\path\to\symbology.json") as json_data:
    data = json.load(json_data)

lyr.manager.update_definition(data)

 

~ learn.finaldraftmapping.com
0 Kudos