Select to view content in your preferred language

Reference FeatureLayer from URL : Use Python to create item rendering definition

358
2
Jump to solution
01-27-2025 02:59 PM
Labels (2)
brundager
Emerging Contributor

Hey guys,

I have many 'referenced' featurelayers (referencing public GIS endpoints).

I am having to change the item display rendering as you would do through using 'Map Viewer'.

changing the transparency, visibility scales, initial off/on display...

The changes are standardized depending on geometry type (point, polygon, polylines).

I can do all of the above using python and json.  What I cannot seem to do is the initial step of creating the Item (layer definition) with python.

In Map Viewer after you change these settings, you can save the rendering/display settings to the FeatureLayer (click the elipse in the Layer contents and select 'save' from the dropdown menu). Until this is done their is no item_data object for an item (like until a popup configure is clicked the featurelayer has no popup object).  

I am trying to figure out a way of creating the item_data (layerDefinition?)  data using Python without having to manually load every featurelayer into Map Viewer and clicking save.

Anybody got any thoughts/idea/solution for this?

Thanks for listening.

BobinTN

 

 

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
MVP Regular Contributor

Hi @brundager,

This is certainly possible. Check out the WebMap specification here. The details in here will enable you to build a dictionary that represents the layer in a WebMap. You can then add this dictionary to the operationalLayers property of a WebMap definition which is a list of layer dictionary definitions. You can store each layer definition in a JSON file and read in as required.

from arcgis.gis import GIS

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

## get the WebMap as an Item object (you could also create a new one with the Map class)
wm_item = agol.content.get("WM_ITEM_ID")

## get the WebMap definitiion (as described in the WebMap specification)
wm_item_data = wm_item.get_data()

## define the layers as per the spec (or read from .json file)
layer1 = {
    ... layer definition details
}

layer2 = {
    ... layer definition details
}

## add the new layers to the operationalLayers
wm_item_data["operationalLayers"] = wm_item_data["operationalLayers"] + [layer1, layer2]

## update the WebMap item object
update_properties = {"text" : wm_item_data}
wm_item.update(update_properties)

 

How you build those layer definition dictionaries is up to you. My go to is usually do manually once and save the layers out to json file templates and reuse as needed to add to WebMaps. You could also get the information from feature services and build up each layer dictionary from the feature layer properties.

Here's how I print the operationalLayers of a WebMap to screen to assess the WebMap layer (operationalLayers) def/spec. You dont need to use every property, if you check the WebMap spec here you will see some examples. 

from arcgis.gis import GIS
import json

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

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

## Get a list of layer definitions, each definition is a dictionary
wm_item_lyrs = wm_item.get_data()["operationalLayers"]

print(json.dumps(wm_item_lyrs, indent=4))

 

I hope that helps,

Glen

~ learn.finaldraftmapping.com

View solution in original post

2 Replies
Clubdebambos
MVP Regular Contributor

Hi @brundager,

This is certainly possible. Check out the WebMap specification here. The details in here will enable you to build a dictionary that represents the layer in a WebMap. You can then add this dictionary to the operationalLayers property of a WebMap definition which is a list of layer dictionary definitions. You can store each layer definition in a JSON file and read in as required.

from arcgis.gis import GIS

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

## get the WebMap as an Item object (you could also create a new one with the Map class)
wm_item = agol.content.get("WM_ITEM_ID")

## get the WebMap definitiion (as described in the WebMap specification)
wm_item_data = wm_item.get_data()

## define the layers as per the spec (or read from .json file)
layer1 = {
    ... layer definition details
}

layer2 = {
    ... layer definition details
}

## add the new layers to the operationalLayers
wm_item_data["operationalLayers"] = wm_item_data["operationalLayers"] + [layer1, layer2]

## update the WebMap item object
update_properties = {"text" : wm_item_data}
wm_item.update(update_properties)

 

How you build those layer definition dictionaries is up to you. My go to is usually do manually once and save the layers out to json file templates and reuse as needed to add to WebMaps. You could also get the information from feature services and build up each layer dictionary from the feature layer properties.

Here's how I print the operationalLayers of a WebMap to screen to assess the WebMap layer (operationalLayers) def/spec. You dont need to use every property, if you check the WebMap spec here you will see some examples. 

from arcgis.gis import GIS
import json

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

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

## Get a list of layer definitions, each definition is a dictionary
wm_item_lyrs = wm_item.get_data()["operationalLayers"]

print(json.dumps(wm_item_lyrs, indent=4))

 

I hope that helps,

Glen

~ learn.finaldraftmapping.com
brundager
Emerging Contributor

@Clubdebambos(Glen),

Thank you so much.  I have read some of your blogs and seen your other answers in other places.

'You could also get the information from feature services and build up each layer dictionary from the feature layer properties.'

Conceptually, you helped me find the missing piece that I was not grasping.  That I need to query the endpoint of the referenced featurelayer requesting json format (?f=json) into an object.  This gives me the initial information without having to build from scratch.  I can pull out the layerdefinitions and drawinginfo I need to construct the item_data required in an automated fashion that should be consistent across the many referenced featurelayers I have to process.

 

The above is just a concept until I do it, of course.

Thanks for sharing your knowledge.

BobinTN