Python API add group layer to webmap?

158
1
Jump to solution
10-28-2021 07:58 AM
RichardReinicke
Occasional Contributor II
map_item["operationalLayers"]

Hi,

I have a very general question this time.

The Esri arcgis API is meant to be THE tool to automate workflows and to interact with ArcGIS Portal. But there seem to be a lot of features that are not implemented yet, in my case map authorization.

I have the new webmaps (next gen) and map viewer beta. I can interact with web maps v2 also from the Python API. But I can't create a group layer with API methods.

I can do: 

map_item.get_data()


I see my GroupLayer in the map layers:

map_item["operationalLayers"]

 

[
    {
        "id": "htsews6tyu0-layer-10010",
        "title": "DummyGroup",
        "layers": [
            {
                "id": "17cc733308b-layer-3",
                "title": "FeatureLayer1",
                "url": "some-feature-server-url",
                "itemId": "d77d046fb6de424da2258a08b376f1fc",
                "layerType": "ArcGISFeatureLayer",
                "popupInfo": {
                    "popupElements": [
                        {
                            "type": "fields"
                        }
                    ],
                    "showAttachments": True,
                    "fieldInfos": [
                        {
                            "fieldName": "objectid",
                            "label": "OBJECTID",
                            "visible": False
                        }
                    ],
                    "title": "Titel1"
                }
            },
            {
                "id": "17cc7332022-layer-2",
                "title": "FeatureLayer2",
                "url": "some-feature-server-url",
                "itemId": "3efe6ee7327e49e59a37406f622db492",
                "layerType": "ArcGISFeatureLayer",
                "popupInfo": {
                    "popupElements": [
                        {
                            "type": "fields"
                        }
                    ],
                    "showAttachments": True,
                    "fieldInfos": [
                        {
                            "fieldName": "objectid",
                            "label": "OBJECTID",
                            "visible": False
                        }
                    ],
                    "title": "Titel2"
                }
            }
        ],
        "layerType": "GroupLayer"
    }
]


Now I can get portal Feature Service items and it's FeatureLayer items and I want to:
1. add another Feature Layer to map_item["operationalLayers"][0]["layers"]

2. or add another group layer with x feature layers

We really need operations for the Python API to compose a webmap and its layers from existing layer items.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardReinicke
Occasional Contributor II

I've been able now to accomplish my goal and to add a new group layer with existing feature layers to my web map item. Unfortunatly for this you need to write dictionaries that correspond to Esri web map specification (GroupLayer, FeatureLayer ... )
https://developers.arcgis.com/web-map-specification/objects/groupLayer/

Here are the major parts:

def create_layer_id(layerIndex):
    return ''.join(random.choices(string.ascii_lowercase + \
        string.digits, k=11)) + "-layer-" + str(layerIndex)


# This is my base webmap (template)
map_template = gis.content.get('3095cb51e2a44e818b90608c487f8b3b')

# Copy template to new web map item into subfolder
project_map = WebMap(map_template).save({
    "title": "project map",
    "snippet": "some description",
    "tags": "maps"}, folder="project_folder")

# Create a new group layer following esri web map specification
group_layer = {
    "id": create_layer_id(random.randint(10000, 99999)),
    "layers": [],
    "layerType": "GroupLayer",
    "title": "my new group layer" }

# Get an existing feature service / layer collection item
# get_item() is standard portal item query logic
lyr_item = get_item(lyr_name, "Feature Service", "test")

# Create feature layer following esri web map specification
feature_layer = {
    "id": create_layer_id(random.randint(10000, 99999)),
    "url": lyr_item.layers[0].url,
    "title": lyr_item.layers[0].properties.name,
    "itemId": lyr_item.id,
    "layerType": "ArcGISFeatureLayer" }

# Add feature layer to group layer
group_layer["layers"].append(feature_layer )

# Update project map with new group layer
map_def = project_map.get_data()
map_def["operationalLayers"].append(group_layer)
project_map.update({'text': str(map_def)})

 

Maybe this will help someone :)

View solution in original post

0 Kudos
1 Reply
RichardReinicke
Occasional Contributor II

I've been able now to accomplish my goal and to add a new group layer with existing feature layers to my web map item. Unfortunatly for this you need to write dictionaries that correspond to Esri web map specification (GroupLayer, FeatureLayer ... )
https://developers.arcgis.com/web-map-specification/objects/groupLayer/

Here are the major parts:

def create_layer_id(layerIndex):
    return ''.join(random.choices(string.ascii_lowercase + \
        string.digits, k=11)) + "-layer-" + str(layerIndex)


# This is my base webmap (template)
map_template = gis.content.get('3095cb51e2a44e818b90608c487f8b3b')

# Copy template to new web map item into subfolder
project_map = WebMap(map_template).save({
    "title": "project map",
    "snippet": "some description",
    "tags": "maps"}, folder="project_folder")

# Create a new group layer following esri web map specification
group_layer = {
    "id": create_layer_id(random.randint(10000, 99999)),
    "layers": [],
    "layerType": "GroupLayer",
    "title": "my new group layer" }

# Get an existing feature service / layer collection item
# get_item() is standard portal item query logic
lyr_item = get_item(lyr_name, "Feature Service", "test")

# Create feature layer following esri web map specification
feature_layer = {
    "id": create_layer_id(random.randint(10000, 99999)),
    "url": lyr_item.layers[0].url,
    "title": lyr_item.layers[0].properties.name,
    "itemId": lyr_item.id,
    "layerType": "ArcGISFeatureLayer" }

# Add feature layer to group layer
group_layer["layers"].append(feature_layer )

# Update project map with new group layer
map_def = project_map.get_data()
map_def["operationalLayers"].append(group_layer)
project_map.update({'text': str(map_def)})

 

Maybe this will help someone :)

0 Kudos