ArcGIS Python API – How to Specify Default Display Style for FeatureLayer

arcgis-onlinearcgis-python-apifeature-layer

Is there a way to edit the default display style of FeatureLayer on ArcGIS Online via the (Python) API?

Currently I can use the gis module to publish a GeoJSON to ArcGIS Online.

The GeoJSON in question has several attributes—latitude, longitude, object_type, length, date, etc.

If one opens up the FeatureLayer right after I publish it, ArcGIS Online would show the length attribute with circle sizes corresponding to the values of length; i.e., the longer an object is, the larger the circle.

What I would like is for ArcGIS Online to default to showing the object_type attribute, with different colors for different object types.

I can manually edit and save the display style via ArcGIS Online. However, I would like to know how I can do that with the (Python) API.

In case it helps, after I manually edited the display style, the drawingInfo attribute of the FeatureLayer properties shows the following:

{
  "renderer": {
    "type": "uniqueValue",
    "field1": "object_type",
    "uniqueValueInfos": [
      {
        "value": "a",
        "label": "Type-A object",
        "symbol": {
          "color": [
            237,
            81,
            81,
            255
          ],
          "size": 6,
          "angle": 0,
          "xoffset": 0,
          "yoffset": 0,
          "type": "esriSMS",
          "style": "esriSMSCircle",
          "outline": {
            "color": [
              153,
              153,
              153,
              64
            ],
            "width": 0.75,
            "type": "esriSLS",
            "style": "esriSLSSolid"
          }
        }
      },
      {
        "value": "b",
        "label": "Type-B object",
        "symbol": {
          "color": [
            20,
            158,
            206,
            255
          ],
          "size": 6,
          "angle": 0,
          "xoffset": 0,
          "yoffset": 0,
          "type": "esriSMS",
          "style": "esriSMSCircle",
          "outline": {
            "color": [
              153,
              153,
              153,
              64
            ],
            "width": 0.75,
            "type": "esriSLS",
            "style": "esriSLSSolid"
          }
        }
      },
      {
        "value": "c",
        "label": "Type-C object",
        "symbol": {
          "color": [
            167,
            198,
            54,
            255
          ],
          "size": 6,
          "angle": 0,
          "xoffset": 0,
          "yoffset": 0,
          "type": "esriSMS",
          "style": "esriSMSCircle",
          "outline": {
            "color": [
              153,
              153,
              153,
              64
            ],
            "width": 0.75,
            "type": "esriSLS",
            "style": "esriSLSSolid"
          }
        }
      }
    ]
  },
  "transparency": 0
}

This would seem to suggest that I need to tweak the renderer but I cannot find an example of how to use generate_renderer. I have tried the following:

layer = FeatureLayer(layer_url, gis=gis)
unique_class = {
    "type": "uniqueValueDef",
    "uniqueValueFields": ["object_type"],
    "fieldDelimiter": ",",
}
layer.generate_renderer(definition=unique_class)

However, that gives me the following exception:

Exception: 
The requested layer (layerId: generateRenderer) was not found.
(Error Code: 400)

Best Answer

I managed to update the default display style in three steps.

Step 1: Set layer.properties.drawingInfo to the dictionary/JSON object I shared in my original question.

Step 2: Set lyr.properties.editingInfo.lastEditDate = " " (See https://support.esri.com/en/technical-article/000014622)

Step 3: Use layer manager to update the layer definition.

manager = layer.manager
manager.update_definition(layer.properties)
Related Question