feature_service_item = sedf.spatial.insert_layer(feature_service = feature_service_item,service_name=new_sublayer_name)<div><div><span>However, there happens some type casting under the hood, which I do not want at the moment (e.g. Int32 will be converted to esriFieldTypeString).<div> <div><div><div><span>Is there a possibility to insert a FeatureSet as a new FeatureLayer inside a Feature Service? Like my abovementioned example for a sedf but just for fsets?</span></div></div></div></div></span></div></div>
I have successfully used the below workflow.
Code and workflow commented...
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.features import FeatureLayerCollection
################################################################################
## USER INPUTS #################################################################
## the item id for the feature service that contains the layer to query
fs_item_id = "FS_ITEM_ID"
## the index of the layer to query (0 if only one layer or the first layer)
lyr_idx = 0
## the item id for the feature service to add the new feature layer to
add_lyr_item_id = "FS_ITEM_ID"
## the name of the new feature layer
lyr_name = "NEW_FEATURE_LAYER"
## the feature layer query
fl_query = "SQL_QUERY"
################################################################################
## ACCESS ARCGIS ONLINE ########################################################
agol = GIS("home")
################################################################################
## GET FEATURE SET INFORMATION #################################################
## get the feature service item that contains the layer to query
fs_item = agol.content.get(fs_item_id)
## get the layer of interest to query
fl = FeatureLayer.fromitem(fs_item, lyr_idx)
## get the feature set
fs = fl.query(fl_query)
################################################################################
## CREATE NEW FEATURE LAYER ####################################################
fl_properties = dict(fl.properties)
## get the feature service item to create new feature layer for
add_lyr_item = agol.content.get(add_lyr_item_id)
## create feature layer definition
fl_definition = {
"type" : "Feature Layer",
"name" : lyr_name,
"geometryType": fl_properties["geometryType"],
"drawingInfo" : fl_properties["drawingInfo"],
"fields" : fl_properties["fields"],
"indexes": fl_properties["indexes"],
"objectIdField": fl_properties["objectIdField"],
"uniqueIdField": fl_properties["uniqueIdField"],
}
## create FLC object
flc = FeatureLayerCollection.fromitem(add_lyr_item)
## update the JSON definition fof the feature service to include the layer
flc.manager.add_to_definition({"layers": [fl_definition]})
################################################################################
## ADD RECORDS #################################################################
## re-get the updated item object
add_lyr_item = agol.content.get(add_lyr_item_id)
## get the layer of interest
fl = [lyr for lyr in add_lyr_item.layers if lyr.properties.name == lyr_name][0]
## populate with data
fl.edit_features(adds=fs.features)
################################################################################
print("\nSCRIPT COMPLETE")
Depending on the size of the Feature Set, you might want to break the edit_features into chunks of 2000 records at at time.