Select to view content in your preferred language

How to programmatically create a service and add new layers?

1679
5
11-08-2019 01:56 PM
TedChapin
Frequent Contributor

I can create an empty service using gis.content.create_service
This empty service has
layers=[]
tables=[]
I can get the service item's FeatureLayerCollection and use  .manager.update_definition to update the definition of the service including service-level properties.

But, how to create a new feature layer (or table) and add it to the service's definition? 

5 Replies
Danik-B
Occasional Contributor

Reviving this question as I am also looking for a solution to this.

Anyone have a solution for this or a link to a sample?

0 Kudos
MarcelBeckmann
Regular Contributor
0 Kudos
Clubdebambos
Frequent Contributor

Example: create the empty service and add a point layer. Once created you can use methods to add/append data.

 

 

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

"""Access AGOL"""
agol = GIS("home")

"""paramters for the create_service method"""
name = "NAME_OF_FS"
has_static_data = False # want to be able to edit
max_record_count = 1000
capabilities = "Query,Extract,Create,Update,Editing,Delete",
service_type = "featureService"
tags = ["TEST", "Feature", "Service"]
snippet = "User generated feature service"

"""create the empty feature service"""
empty_service_item = agol.content.create_service(
    name = name,
    has_static_data = has_static_data,
    max_record_count = max_record_count,
    capabilities = capabilities,
    service_type = service_type,
    tags = tags,
    snippet = snippet
)

"""the feature layer defintion template. Note: print a feature layers properties to screen to see more definitions, see previous video Access Feature Layer and View Properties."""
fl_definition = {
    "type" : "Feature Layer",
    "name" : "Example using Point Layer",
    "description": "User created point layer",
    "geometryType": "esriGeometryPoint",
    "extent": {
        "xmin": -180.0,
        "ymin": -90.0,
        "xmax": 180.0,
        "ymax": 90.0,
        "spatialReference": {
            "wkid": 4326,
            "latestWkid": 4326
        }
    },
    "fields" : [
        {
          "name": "FID",
          "type": "esriFieldTypeOID",
          "actualType": "int",
          "alias": "FID",
          "sqlType": "sqlTypeInteger",
          "nullable": False,
          "editable": False,
        },
        {
          "name": "string_field",
          "type": "esriFieldTypeString",
          "actualType": "nvarchar",
          "alias": "String Field",
          "sqlType": "sqlTypeNVarchar",
          "length": 25,
          "nullable": True,
          "editable": True
        },
        {
          "name": "integer_field",
          "type": "esriFieldTypeSmallInteger",
          "actualType": "int",
          "alias": "Integer Field",
          "sqlType": "sqlTypeInteger",
          "nullable": True,
          "editable": True
        },
        {
          "name": "decimal_field",
          "type": "esriFieldTypeDouble",
          "actualType": "float",
          "alias": "Decimal Field",
          "sqlType": "sqlTypeFloat",
          "nullable": True,
          "editable": True
        },
        {
          "name": "date_field",
          "type": "esriFieldTypeDate",
          "alias": "Date Field",
          "sqlType": "sqlTypeOther",
          "nullable": True,
          "editable": True
        }
    ],
    "indexes": [
    {
      "name": "PK_IDX",
      "fields": "FID",
      "isAscending": True,
      "isUnique": True,
      "description": "clustered, unique, primary key"
    }
    ],
    "drawingInfo": {
        "renderer": {
            "symbol": {
                "angle": 0,
                "contentType": "image/png",
                "height": 16,
                "imageData": "iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABFElEQVQ4je3T223CMBgG0JPLEqQSSzRjAGuwBmuwBl0jLIFEliDBfUjchIpAqqrqC5by4suJ7e937o9a/oL/D655T9gFdgXH8diZdcK+oVxSz4YjmrG6sqopI35mnXHIkFCdJvC7cERzXNFQ1ZSBIuOQI+2mLgIVillwYHftdivr+1qq0C9I+x+23dzt7KsoONaUTbcb2bBDhlNo2bzxMRse4y1Vervzp+hDGAJFmBhLJqrhKRzTjxPaAZQbAv1eig/hMRqDakYL4rU8wqfKbR8DGweVUMdA0/4L7FHOghvKpAMWrdugYrWkXf/xwmb2VSypT92DqALbcfqxWgL7C5sfPemIu/OiIu7O8WfBv20v+Kt9AtubZ8n+mgQEAAAAAElFTkSuQmCC",
                "type": "esriPMS",
                "url": "a12829dd513a6935fd8b99065e2121bd",
                "width": 16,
                "xoffset": 0,
                "yoffset": 0
            },
            "type": "simple"
        }
    }
}

## access as FeatureLayerCollection
flc = FeatureLayerCollection.fromitem(empty_service_item)

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

 

 

 

 

~ learn.finaldraftmapping.com
MarcelBeckmann
Regular Contributor

Thanks, I get an Error: FeatureLayerCollection is not definded.

Is the following correct:

flc = features.FeatureLayerCollection.fromitem(empty_service_item)

When I use this I get the following Error:

Service Hosted/SERVICENAME/MapServer not found

0 Kudos
Clubdebambos
Frequent Contributor

I forgot the from arcgis.features import FeatureLayerCollection at the top of the script. I have added.

~ learn.finaldraftmapping.com