Updating hosted feature layers on AGOL using the ArcGIS Python API?

Geographic Information Systems Asked on January 5, 2022

I previously asked Adding data to a feature layer on ArcGIS Online using arcrest and arcresthelper.featureservicetools? regarding the most current process for establishing and then automating feature updates on ArcGIS Online using the ArcGIS Python API. I can successfully add shapefiles to my portal content and publish them as a feature layers. However, I would like to get into the details of constructing the “publish_parameters” argument for the publish method described here: arcgis.gis.GIS content.publish() I’m not sure if the docs are incomplete, but using the supplied link for understanding the publish parameters dictionary leads you to the ArcGIS REST API which describes using JSON objects for the publish parameters. This seems to be misguided as this is not a Python API and nowhere else can I find how to construct the publish_parameters dictionary for Python.

The reason this is an issue is because some of my field types need to be specified in the published data. I have some UID fields that are all numbers but for a reason I won’t get into here, I need them to be treated as text, which seems possible if able to leverage the publish_parameters argument. This is one example of many of how I would like to take more control of how the publishing action gets handled.

Does anyone know of a resource that explicitly describes the publishing process for CSV and/or zipped Shapefiles that details the publish_parameters argument of the publish method or am I missing something else that allows control over this (besides manually changing the way fields are displayed in the portal)?

Edit: My original question regarding the publish_parameters argument was not the correct approach at all. There is no need to construct the publish_parameters argument. Local features can be converted to JSON and then pushed to AGOL using the ArcGIS Python API. This allows for local features to take the place of hosted features. This guide helped me a lot in ironing out this process.

One Answer

After a few weeks getting up to speed on the ArcGIS Python API, I have come up with work flow that works quite well. My original understanding of scripting using JSON dictionaries was a bit off as well as the structure of FeatureLayerCollection, FeatureLayer, FeaturesSet, and Feature objects from the new API. My process thus far involves manually publishing layers I need in the AGOL portal and then updating them through a process of truncation and then adding all of the updated local features to the hosted layers. Here is a simplified process that gives python access to the content of an AGOL portal, allows for truncating the layers, and adding features back to the layer from a local source.

import arcpy
import arcgis
from arcgis.gis import GIS

def TruncateWebLayer(gis=None, target=None):
        lyr = arcgis.features.FeatureLayer(target, gis)
        print ("Successfully truncated layer: " + str(target))
        print("Failed truncating: " + str(target))

#create connection to AGOL portal GIS
mygis = arcgis.gis.GIS(r"", "someUser", "somePassword")

#the URL of a single feature layer within a collection in an AGOL portal
publishedWebLayer = r""

#a feature class on the local system with the same schema as the portal layer
updateFeatures = r"G:DataDataData.gdbData"

#remove all features from the already published feature layer
TruncateWebLayer(mygis, publishedWebLayer)

#reference the empty layer as FeatureLayer object from the ArcGIS Python API
fl = arcgis.features.FeatureLayer(publishedWebLayer, mygis)

#create a JSON object from the local features
jSON = arcpy.FeaturesToJSON_conversion(updateFeatures, localJSON)

#create a FeatureSet object from the JSON
fs = arcgis.features.FeatureSet.from_json(open(localJSON).read())

#add/append the local features to the hosted feature layer.
fl.edit_features(adds = fs)

This is a simplified example of how this can be achieved. A few things to note. I have not been able to get this process to work on more complicated geometries like curve rings, rings, etc. For polygons and lines I typically add an arcpy.Generalize_edit() before creating the JSON. This converts the curves into approximated line segments.

Answered by GeoJohn on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP