Force style to use layer renderer after changing style in ArcGIS Online

626
1
Jump to solution
02-13-2023 05:17 AM
Nico44263
New Contributor

Hi Developers,

Background

I am developing a script to set up a Feature Layer Collection (point, polyline, polygon) including fields and styles. To set the styles, I add a renderer to the lines layer, which works fine (attached image, right side).

 

 

# Lines
unique_value_renderer = {
    "renderer": {
        "type": "uniqueValue",
        "field1": "CATEGORY",
        "defaultSymbol": {
                "color": [0, 0, 255, 255],
                "width": 5,
                "type": "esriSLS",
            },
        "uniqueValueInfos": [
        {
            "value": "Good_Route",
            "symbol": {
                "color": [0, 255, 0, 255],
                "width": 5,
                "type": "esriSLS",
            },
        },
        {
            "value": "Blocked_Route",
            "symbol": {
                "color": [255, 0, 0, 255],
                "width": 5,
                "type": "esriSLS",
            },
        }
        ]
    }
}
# Set the renderer for the feature layer
fl = FeatureLayer(url=url_lines)
fl.manager.update_definition({"drawingInfo": unique_value_renderer})

 

 

 

My Question

Using ArcGIS Online, I visualize the line feature layer in new Map Viewer, change the styles and save the layer (not a map,  but the layer). After doing so, how can I revert back to have my renderer as default style for adding the layer to maps or visualizing the data?

What I tried

  • Run above code again, no change
  • Tried to understand WebStyle and set it to none

 

 

fl.manager.update_definition({"web_style_id": None})

 

 

  • Compared fl.properties of both layers in screenshot attached (left: changed style online; right: API only), but did not find a difference, except the name.

 

Setup

  • ArcGIS Online
  • Visual Studio Code + Jupyter Notebook
  • ArcGIS Python API Version 2.1.0.2

Edit

See attached my Notebook that shows the different behavior. Dev4 section is a Feature Layer Collection to which I made changes in symbology online. My goal is to get Dev4 Variant A running the same way as Dev5.

Tags (2)
1 Solution

Accepted Solutions
Nico44263
New Contributor

I found out that manual changes of the symbology get stored in the item data behind the feature layer collection instead of into the collection or the layer. When the item data contains drawing info, the layer's drawing info will be ignored. So, I delete all item drawing info like this:

# Delete item renderers
def delete_items_renderer(url):
    
    # delete all keys in a json object
    def delete_key(obj, key):
        if isinstance(obj, dict):
            if key in obj:
                del obj[key]
            for k in obj:
                delete_key(obj[k], key)
        elif isinstance(obj, list):
            for item in obj:
                delete_key(item, key)
        return obj
    
    flc = FeatureLayerCollection(url, gis)
    itemid = flc.properties['serviceItemId']
    item = Item(gis=gis, itemid=itemid)

    data = item.get_data()
    data = delete_key(data, "renderer")

    # Update the item properties
    item.update(data=data)

delete_items_renderer(url_feature_layer_collection)

View solution in original post

0 Kudos
1 Reply
Nico44263
New Contributor

I found out that manual changes of the symbology get stored in the item data behind the feature layer collection instead of into the collection or the layer. When the item data contains drawing info, the layer's drawing info will be ignored. So, I delete all item drawing info like this:

# Delete item renderers
def delete_items_renderer(url):
    
    # delete all keys in a json object
    def delete_key(obj, key):
        if isinstance(obj, dict):
            if key in obj:
                del obj[key]
            for k in obj:
                delete_key(obj[k], key)
        elif isinstance(obj, list):
            for item in obj:
                delete_key(item, key)
        return obj
    
    flc = FeatureLayerCollection(url, gis)
    itemid = flc.properties['serviceItemId']
    item = Item(gis=gis, itemid=itemid)

    data = item.get_data()
    data = delete_key(data, "renderer")

    # Update the item properties
    item.update(data=data)

delete_items_renderer(url_feature_layer_collection)
0 Kudos