Select to view content in your preferred language

Adding a new feature layer to an existing feature layer collection on AGO

204
5
2 weeks ago
Labels (1)
Lydia_Kimball
New Contributor II

Hello, I am trying to add a new feature layer to an existing feature layer collection on AGO via the add_to_definition operation using arcgis api for python (adding Ropes feature layer to Breckenridge_resort_SDM feature layer collection). I'm having trouble finding good code examples for adding a feature layer in this way using a JSON object (formatting). Here is my code as well as the error I'm getting. The layer I am trying to add to the feature layer collection is a layer within another hosted feature layer collection on AGO. I got the JSON object from copy and pasting the layer information from querying the properties of the feature layer collection. I would be so grateful to get some help on where I'm going wrong. Thanks

Lydia_Kimball_0-1718433272119.png

Lydia_Kimball_1-1718433297834.png

 

 

0 Kudos
5 Replies
Clubdebambos
Occasional Contributor III

Hi @Lydia_Kimball 

true should be True and null should be None in your dictionary.

You will also need to define the fields which you can get from your other layer, and you can also set the symbology, again you can get all this from the other layer. The dictionary will look something like the below. I found that by not setting an extent, the layer opens and zooms to Null Island.

You need to get the properties of the Feature Layer object to access the fields, symbology etc.

new_layer = {
    "id" : 1, # will increment by default
    "type" : "Feature Layer",
    "name" : "NAME_OF_LAYER",
    "description": "Layer Description",
    "geometryType": "esriGeometryPolyline", # create a line layer
    "extent": {
        "xmin": -11.0,
        "ymin": -52.0,
        "xmax": -5.0,
        "ymax": 56.0,
        "spatialReference": {
            "wkid": 4326,
            "latestWkid": 4326
        }
    },
    "fields" : [
        {
          "name": "FID", ## you could call this OBJECTID
          "type": "esriFieldTypeOID",
          "actualType": "int",
          "alias": "FID",
          "sqlType": "sqlTypeInteger",
          "nullable": False,
          "editable": False,
        },
        {
          "name": "route_name",
          "type": "esriFieldTypeString",
          "actualType": "nvarchar",
          "alias": "String Field",
          "sqlType": "sqlTypeNVarchar",
          "length": 25,
          "nullable": True,
          "editable": True
        },
        {
          "name": "diff_rating",
          "type": "esriFieldTypeSmallInteger",
          "actualType": "int",
          "alias": "Integer Field",
          "sqlType": "sqlTypeInteger",
          "nullable": True,
          "editable": True
        },
        {
          "name": "highest_m",
          "type": "esriFieldTypeDouble",
          "actualType": "float",
          "alias": "Decimal Field",
          "sqlType": "sqlTypeFloat",
          "nullable": True,
          "editable": True
        }
    ],
    "indexes": [
    {
      "name": "PK_IDX", # requires a primary key field
      "fields": "FID",
      "isAscending": True,
      "isUnique": True,
      "description": "clustered, unique, primary key"
    }
    ],
    "drawingInfo": {
        "renderer": {
            "type": "simple",
            "symbol": {
                "type": "esriSLS",
                "style": "esriSLSSolid",
                "color": [
                    165,
                    83,
                    183,
                    255
                ],
                "width": 1
            }
        },
    "transparency": 0
    },
    "objectIdField": "FID", # set the objectId field
    "uniqueIdField": {
        "name": "FID",
        "isSystemMaintained": True
    }
}

 

~ learn.finaldraftmapping.com
0 Kudos
Lydia_Kimball
New Contributor II

@Clubdebambos Thank you for your reply! I retrieved the information for my json object by accessing layer.properties for the layer I want to add (Ropes) to the feature layer collection (breck_SDM_test). I copied and pasted certain sections of the layer properties to fit the formatting in the code snippet you provided. I noticed some discrepancies, such as, the fields do not have "actualType" as one of the properties. Is this ok, or do I need to add this? Regardless, I think my json is good now. I ran the code and this is the error I'm getting.

Lydia_Kimball_0-1718652539906.png

I'm wondering whats wrong with my code to give this error. Does it matter if I use gis.content.search() vs gis.content.get() to retrieve the portal item the add_to_definition operator is applied to? 

 

0 Kudos
Clubdebambos
Occasional Contributor III

Hi @Lydia_Kimball 

An item object has no manager, it is a FeatureLayerCollection object that you want to add the new layer to.

 

# add to top of the script
from arcgis.features import FeatureLayerCollection


flc = FeatureLayerCollection.fromitem(item)

# update the JSON definition fof the feature service to include the layer
flc.manager.add_to_definition({"layers": [new_layer]})

 

~ learn.finaldraftmapping.com
0 Kudos
Lydia_Kimball
New Contributor II

@Clubdebambos that helped a lot. I'm still having trouble updating the feature layer collection with the new layer on AGO. After running the script, the feature layer collection shows the new layer was added to the properties but this is not reflected on AGO when I go to the view details page. how do I update the featuren layer collection so I can view/edit on AGO? Here is my updated script and proof that the layer was added in the properties. 

Lydia_Kimball_2-1719428445326.png

Lydia_Kimball_3-1719428465687.png

 

0 Kudos
Dirk_Vandervoort
New Contributor III

Upper case True on line 29

defaultVisibility : True
0 Kudos