Skip to content

Map.content.update_layer() cannot apply labeling to FeatureCollection/FeatureSet layers #2465

@mso-modelity

Description

@mso-modelity

Describe the bug

When adding a FeatureCollection created from a FeatureSet to a Map using the ArcGIS Python API, labels defined via labeling_info are not displayed.

The renderer modification works correctly (point symbols appear), but the labels never render.

Additionally, using the options parameter in update_layer() (as suggested in #2265) raises an AttributeError because the FeatureCollection object does not expose a layer_definition attribute.


To Reproduce

Steps to reproduce the behavior:

from arcgis.map import Map
from arcgis.features import FeatureSet, FeatureCollection

# Sample annotation data
annotations = [
    {"lat": 48.8566, "lon": 2.3522, "text": "Random Paris"},
    {"lat": 48.8584, "lon": 2.2945, "text": "Bidule Tower"},
]

def create_point_feature_set_with_labels(points_annotation, spatial_ref=4326):
    return FeatureSet.from_dict(
        {
            "features": [
                {
                    "geometry": {
                        "x": point["lon"],
                        "y": point["lat"],
                    },
                    "attributes": {"label": point["text"]},
                }
                for point in points_annotation
            ],
            "spatialReference": {"wkid": spatial_ref},
        }
    )


def add_annotations_to_map(webmap):

    point_feature_set = create_point_feature_set_with_labels(annotations)

    feature_collection = FeatureCollection.from_featureset(point_feature_set)

    # Change renderer color to verify layer updates correctly
    feature_collection.properties["layers"][0]["layerDefinition"]["drawingInfo"][
        "renderer"
    ]["symbol"]["color"] = [255, 255, 0, 127]

    labeling_info = [
        {
            "labelExpression": "[label]",
            "labelExpressionInfo": {"expression": "$feature.label"},
            "maxScale": 0,
            "minScale": 0,
            "symbol": {
                "color": [104, 104, 104, 255],
                "type": "esriTS",
                "font": {
                    "decoration": "none",
                    "family": "Arial",
                    "size": 8,
                    "style": "normal",
                    "weight": "bold",
                },
            },
        }
    ]

    webmap.content.add(feature_collection)

    # Attempt to enable labeling
    webmap.content.update_layer(
        index=0,
        labeling_info=labeling_info
    )


# Create map and run example
webmap = Map()
add_annotations_to_map(webmap)

Points render as defined but labels do not appear.
I tried a second attempt to use the fix and solution suggested in issue #2265 
```python
options_dict = {
    "labelingInfo": labeling_info
}

webmap.content.update_layer(
    index=0,
    options=options_dict
)

error:

Traceback (most recent call last):
  File "...arcgis_map_maker.py", line 552, in add_annotes_to_map
    webmap.content.update_layer(index=0, options=options_dict)

  File "...site-packages\arcgis\map\map_widget.py", line 2150, in update_layer
    self._helper.update_layer(

  File "...site-packages\arcgis\map\_utils.py", line 1861, in update_layer
    existing_layer_definition = layer.layer_definition

AttributeError: 'FeatureCollection' object has no attribute 'layer_definition'

Expected behavior
The labels defined in labeling_info should be rendered for the features using the label attribute.

Additionally, the options parameter in update_layer() should not raise an AttributeError when used with a FeatureCollection.

Platform (please complete the following information):

  • OS: Windows 11
  • Python ArcGIS API Version: 2.4.2
  • arcgis-mapping version: 4.33.1

Additional context
I am trying using the print service in order to see how my updates behave and I have no labels drawn

Image

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions