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!
Solved! Go to Solution.
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!")
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!")
Thanks - just what I needed!
Awesome!