Select to view content in your preferred language

Modify legend properties/layer properties with AGOL Python API

201
5
3 weeks ago
HieuTran12
Emerging Contributor

Hi all,

 

I'm having the issues with modifying the legend content. Especially the layer's title and the first title labels for instance: I want to change 5 to 0 - 5m, and the layer's title acea06 to another meaningful string.

I couldn't find any documentation on modifying the legend properties but only enable it on/off.

 

HieuTran12_0-1745938648968.png

 

Tags (4)
0 Kudos
5 Replies
Clubdebambos
MVP Regular Contributor

Hi @HieuTran12,

You can change the layer names and the values in the renderer for each layer. At the moment the Legend object only enables you to turn the layer on and off.

~ learn.finaldraftmapping.com
0 Kudos
HieuTran12
Emerging Contributor

Thanks, I tried with the renderers for each layer but it seems the imagery layer is not changed anything.

But when I list the layers there are still 2 layers.

Below is my code.

 
unique_value_infos = [
    UniqueValueInfo(
        value=5,
        label="0 - 5m",
    ),
    UniqueValueInfo(
        value=10,
        label="5 - 10m",
    ),
    UniqueValueInfo(
        value=31,
        label="10 - 31m",
    )
]
uvr_lecz = UniqueValueRenderer(
    type="uniqueValue",
    field1="Service Pixel Value",
    uniqueValueInfos=unique_value_infos,
)


lecz_option = {
    "layerDefinition" : {
        "drawingInfo" : {
            "renderer" : uvr_lecz.dict(),
        },
        "name": "Low Elevation Coastal Zones",
    },
    "opacity" : 0.5,
}
 

 

 

0 Kudos
Clubdebambos
MVP Regular Contributor

Hi @HieuTran12,

Here's an example for manipulating the JSON for a WebMap content item.

from arcgis.gis import GIS

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

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

## get the WebMap JSON definition as a dictionary
wm_item_data = wm_item.get_data()

## for each layer in the WebMap
for lyr in wm_item_data["operationalLayers"]:
    ## if the layer name is found
    if lyr["title"] == "Low Elevation Coastal Zones derived from MERIT-DEM":
        ## update the layer name
        lyr["title"] = "NEW_TITLE" # set a new name for the layer
        ## update the labels for the legend
        lyr["layerDefinition"]["drawingInfo"]["renderer"]["uniqueValueGroups"][0]["classes"][0]["label"] = "0 - 5m" # set label
        lyr["layerDefinition"]["drawingInfo"]["renderer"]["uniqueValueGroups"][0]["classes"][1]["label"] = "5 - 10m" # set label
        lyr["layerDefinition"]["drawingInfo"]["renderer"]["uniqueValueGroups"][0]["classes"][2]["label"] = "10 - 31m" # set label


## update the WebMap item object to commit the change
update_dict = {"text" : wm_item_data}

wm_item.update(
    item_properties = update_dict
)

 

Before...

Clubdebambos_1-1746521014894.png

 

After...

Clubdebambos_0-1746520928523.png

 

~ learn.finaldraftmapping.com
0 Kudos
HieuTran12
Emerging Contributor

Thanks @Clubdebambos, but I'm mapping directly from the notebook for public use, and I want to modify the properties publicly as well. If updating the webmap it will be need the WebMap owner's credentials to do so.

Do you have any other suggestions? Really appreciate your time and help!

0 Kudos
Clubdebambos
MVP Regular Contributor

Hi @HieuTran12,

Here you go...

 

from arcgis.gis import GIS
from arcgis.map import Map

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

## WebMap as Item object
## the LECZ webmap
wm_item = agol.content.get("88ca4171a38a46c79b6de271aa19d3e4")

## create a Map object
webmap = Map(wm_item)

## display the WebMap in Notebook
webmap

## enable the legend
webmap.legend.enabled = True

## get the symbology definition from the original webmap
layer_def = wm_item.get_data()["operationalLayers"][0]["layerDefinition"]

## update the label classes
layer_def["drawingInfo"]["renderer"]["uniqueValueGroups"][0]["classes"][0]["label"] = "0 - 5m"
layer_def["drawingInfo"]["renderer"]["uniqueValueGroups"][0]["classes"][1]["label"] = "0 - 5m"
layer_def["drawingInfo"]["renderer"]["uniqueValueGroups"][0]["classes"][2]["label"] = "0 - 5m"

## update the WebMap in Notebooks
webmap.content.update_layer(
    index = 0, 
    options= {
        "title": "NEW_TITLE",
        "layerDefinition" : layer_def
    }
)

 

Clubdebambos_0-1747145706508.png

 

Let us know if that works for you.

All the best,

Glen

~ learn.finaldraftmapping.com
0 Kudos