Select to view content in your preferred language

How to style (color) a polygon based on a field value in a feature layer

422
1
11-11-2024 07:43 AM
Labels (1)
Ef
by
New Contributor

I have a method to create a feature layer. it consists of polygons. Each represent a unit. these units have a field of unit_type. Based on the type I would the polygon to have a different color.
I created a renderer to handle the logic

my_renderer = {
"type": "uniqueValue",
"field1": "unit_type",
"uniqueValueInfos": [
{
"value": "YES_WORK",
"symbol": {
"type": "esriSFS",
"style": "esriSFSSolid",
"color": [0, 0, 255, 128], # Blue
"outline": {
"type": "esriSLS",
"style": "esriSLSSolid",
"color": [255, 255, 255, 255],
"width": 1
}
},
"label": "Yes Work"
},
{
"value": "NO_WORK",
"symbol": {
"type": "esriSFS",
"style": "esriSFSSolid",
"color": [255, 0, 0, 128], # Red
"outline": {
"type": "esriSLS",
"style": "esriSLSSolid",
"color": [255, 255, 255, 255],
"width": 1
}
},
"label": "NO_WORK"
}
],
"defaultSymbol": {
"type": "esriSFS",
"style": "esriSFSSolid",
"color": [0, 0, 255, 128], # Default color (Blue)
"outline": {
"type": "esriSLS",
"style": "esriSLSSolid",
"color": [255, 255, 255, 255],
"width": 1
}
},
"defaultLabel": "Default"
}

 

and to pass it here to create the layer

operational_layers.append(
{
"url": f"{FEATURE_LAYER_URL}/0",
"type": "FeatureLayer",
"title": "ProjectBoundaries",
"token": SOME_TOKEN,
"opacity": 0.4,
"minScale": 0,
"maxScale": 0,
"renderer": my_renderer # Ensure the renderer is included here
}
)

export_map_task_payload = {
"operationalLayers": operational_layers,
"mapOptions": {
"extent": extent,
"spatialReference": {
"latestWkid": 3857,
"wkid": 102100
},
"showAttribution": True,
},
"exportOptions": {
"outputSize": [800, 800]
}
}

but it doesn't work. What is wrong. and if this is not not the way how to achieve the requirement

0 Kudos
1 Reply
Clubdebambos
MVP Regular Contributor

Hi @Ef,

It seems you are quite close here. From the information provided it looks like you are trying to add a Layer to a WebMap (operationalLayers) and set the symbology (renderer).

Check the commented code below for the workflow.

from arcgis.gis import GIS

## access ArcGIS Online
agol = GIS("home")

## get the WebMap Item as an Item object
wm_item = agol.content.get("WM_ITEM_ID")

## get the WebMap Definition
wm_def = wm_item.get_data()

## get the current opetaionalLayers as a list
ol = wm_def["operationalLayers"]

## create the renderer definition
my_renderer = {...}

## create the new layer definition with the renderer
## notice the path to the renderer in the layerDefinition property
new_lyr_def = {
        "url": f"{FEATURE_LAYER_URL}/0",
        "type": "FeatureLayer",
        "title": "ProjectBoundaries",
        "token": SOME_TOKEN,
        "opacity": 0.4,
        "minScale": 0,
        "maxScale": 0,
        "layerDefinition": {
            "drawingInfo" : {
                "renderer" : my_renderer
            }
        }
}

## append the new layer to the ol list
ol.append(new_lyr_def)

## test print to make sure the layer has been added
print(ol)

## if not adding try the below
#ol = ol + [new_lyr_def]

## assign the new ol to the WebMap OL Definition
wm_def["operationalLayers"] = ol

## create a dictionary to hold the new update info
update_properties = {"text" : wm_def}

## apply the update to the WebMap Item object
wm_itm.update(
    "item_properties" : update_properties
)

All the best,

Glen

 

~ learn.finaldraftmapping.com
0 Kudos