Select to view content in your preferred language

How can I create a hosted feature layer from scratch using ArcGIS API for Python?

244
3
Jump to solution
11-04-2024 08:54 AM
john_cartwright_noaa
Regular Contributor

Hello All,

I'm trying to create a new hosted feature layer w/o using an existing CSV, Shapefile, etc. I have an example which works to create the Feature Layer Collection, e.g.

empty_service_item = gis.content.create_service(name='awesome_python', service_type='featureService')

 but I can't seem to get the code right for adding a Feature Layer and defining it's fields. The examples I've found are either incomplete or don't work with the current API. Can someone please help me?

Thanks!

 

0 Kudos
1 Solution

Accepted Solutions
TonyAlmeida
Frequent Contributor

Try this.

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

# Connect to your GIS
gis = GIS("https://www.blah.com", "username", "password")

# Step 1: Create an empty feature service
service_name = "awesome_python"
empty_service_item = gis.content.create_service(name=service_name, service_type='featureService')
feature_layer_collection = FeatureLayerCollection.fromitem(empty_service_item)

# Step 2: Define the feature layer schema (fields, geometry type, spatial reference, etc.)
layer_definition = {
    "layers": [
        {
            "name": "MyFeatureLayer",
            "geometryType": "esriGeometryPoint",  # Change to Polyline, Polygon as needed
            "fields": [
                {
                    "name": "ObjectID",
                    "alias": "ObjectID",
                    "type": "esriFieldTypeOID"
                },
                {
                    "name": "Name",
                    "alias": "Name",
                    "type": "esriFieldTypeString",
                    "length": 50
                },
                {
                    "name": "Description",
                    "alias": "Description",
                    "type": "esriFieldTypeString",
                    "length": 255
                },
                {
                    "name": "Value",
                    "alias": "Value",
                    "type": "esriFieldTypeDouble"
                }
            ],
            "objectIdField": "ObjectID",
            "globalIdField": "",
            "displayField": "Name",
            "typeIdField": "",
            "spatialReference": {"wkid": 4326}  # WGS 84
        }
    ]
}

# Step 3: Add the layer to the feature service
feature_layer_collection.manager.add_to_definition(layer_definition)

print("Feature layer created successfully!")

View solution in original post

3 Replies
TonyAlmeida
Frequent Contributor

Try this.

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

# Connect to your GIS
gis = GIS("https://www.blah.com", "username", "password")

# Step 1: Create an empty feature service
service_name = "awesome_python"
empty_service_item = gis.content.create_service(name=service_name, service_type='featureService')
feature_layer_collection = FeatureLayerCollection.fromitem(empty_service_item)

# Step 2: Define the feature layer schema (fields, geometry type, spatial reference, etc.)
layer_definition = {
    "layers": [
        {
            "name": "MyFeatureLayer",
            "geometryType": "esriGeometryPoint",  # Change to Polyline, Polygon as needed
            "fields": [
                {
                    "name": "ObjectID",
                    "alias": "ObjectID",
                    "type": "esriFieldTypeOID"
                },
                {
                    "name": "Name",
                    "alias": "Name",
                    "type": "esriFieldTypeString",
                    "length": 50
                },
                {
                    "name": "Description",
                    "alias": "Description",
                    "type": "esriFieldTypeString",
                    "length": 255
                },
                {
                    "name": "Value",
                    "alias": "Value",
                    "type": "esriFieldTypeDouble"
                }
            ],
            "objectIdField": "ObjectID",
            "globalIdField": "",
            "displayField": "Name",
            "typeIdField": "",
            "spatialReference": {"wkid": 4326}  # WGS 84
        }
    ]
}

# Step 3: Add the layer to the feature service
feature_layer_collection.manager.add_to_definition(layer_definition)

print("Feature layer created successfully!")
john_cartwright_noaa
Regular Contributor

Thanks - just what I needed!

 

0 Kudos
TonyAlmeida
Frequent Contributor

Awesome!

0 Kudos