3

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.

2
  • If you scroll down on the referenced REST API Documentation page, you'll find sections for Publisher parameters JSON object for CSV and Excel and Publish parameters JSON object for shapefiles, feature collections, and file geodatabases (both include examples) - are these what you're looking for?
    – GavinR
    Commented Jan 9, 2019 at 18:33
  • These are part of the ArcGIS REST API, and it would seem that the dictionary parameters for the ArcGIS Python API would be the same. However, all of my efforts to leverage the dictionary keys shown in the former for use in the latter always fail to publish features correctly. Specifically as one example, when using the {"locationType" : "coordinates", "latitudeFieldName" : "myLatField", "longitudeFieldName" : "myLongField"} key value pairs, the features appears to be published on AGOL, but will be empty.
    – GeoJohn
    Commented Jan 11, 2019 at 20:20

1 Answer 1

4

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):
    try:
        lyr = arcgis.features.FeatureLayer(target, gis)
        lyr.manager.truncate()
        print ("Successfully truncated layer: " + str(target))
    except:
        print("Failed truncating: " + str(target))
        sys.exit()

#create connection to AGOL portal GIS
mygis = arcgis.gis.GIS(r"https://aportal.maps.arcgis.com", "someUser", "somePassword")

#the URL of a single feature layer within a collection in an AGOL portal
publishedWebLayer = r"https://services6.arcgis.com/XXXXXXXXXXXX/arcgis/rest/services/XXXX/FeatureServer/0"

#a feature class on the local system with the same schema as the portal layer
updateFeatures = r"G:\DataData\Data.gdb\Data"

#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.

3
  • I wish I could upvote this 10 times. I've just spent a whole week trying to figure this out from looking at ESRI blogs and GeoNet and nothing was working. Thank you so much.
    – HFBrowning
    Commented Feb 26, 2020 at 2:17
  • Here is another solution gis.stackexchange.com/a/358844/4230
    – Patty Jula
    Commented Apr 21, 2020 at 14:50
  • Agreed that this seems to be a great, simple solution. There is no easy ESRI documentation equivalent to this that I have found. There are some additional more involved methods from the 2019 ESRI Dev conference: github.com/albrowngeo/Python/tree/master/GeoDev_2019 Commented Jul 24, 2020 at 20:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.