5

Is it possible to use the arcpy library with hosted feature service layers from ArcGIS Online?

I am aware of the ArcGIS API for Python, but the API lacks many of arcpy's features.

In my case, I'd like to script populating a new field of a point layer with the layer's XY attributes (latitude and longitude). This is known as 'Calculate Geometry' in ArcMap and 'Add Geometry Attributes' in ArcGIS Pro.

I need this to be scripted, rather than done manually, because I am working with a layer that frequently has additional points added to it via Survey123. Survey123 does not automatically record the longitude and latitude of points added to it through its form, and again, the ArcGIS API for Python does not cover 'Calculate Geometry' to my knowledge. Finally, I would prefer not to download the ArcGIS Online layer, run arcpy scripts on it locally, then re-publish the layer, since this tends to reset symbology, field names, etc. - but if anyone who knows more about ArcGIS Online can help with this strategy, that could solve the problem as well.

The arcpy code would be something as simple as:

import arcpy
arcpy.env.workspace = <some file gdb>
arcpy.AddGeometryAttributes_management(<name of the layer as a string>, "POINT_X_Y_Z_M")

But in my case, instead of a feature class in file geodatabase stored locally, all I have is a hosted feature service. I've tried using the REST endpoint url as the workspace, but it doesn't seem to work. I've also tried extracting the feature layer itself via the ArcGIS API for Python, then using that in the arcpy code:

search_result = gis.content.search(query="<my layer name>", max_items=1, item_type="Feature Service")
# or search_result = gis.content.get(<item ID of my layer>)
item = search_result[0]
item_layers = item.layers
layer_fset = item_layers[0].query()
layer_features = layer_fset.features
layer_flayer = item_layers[0]

But that doesn't seem to work, either. Either I'm missing something or perhaps it's not possible.

1

The arcpy library was designed and built long before feature layers were a thing. Well technically arcgisscripting was built first, and arcpy is a wrapper on arcgisscripting, but that's beside the point. The point is arcpy is a wrapper on tools, and all * tools were built at a time to work against local datasets (fgdb, shape, etc) and enterprise geodatabase layers, not to work against hosted feature layers.

Now I put the ** beside the all comment because from ArcGIS Pro, some tools will accept a feature layer input and modify it. Add Field, Delete Field, Calc, and Append are all tools I'm mostly certain you can use with hosted feature services. Now this is specific to the tools inside ArcGIS Pro. You should be able to use arcpy (ie. the wrapper on the tool) to work against the feature layers.

All of these points considered, to the best of my knowledge, the Add Geometry Attributes in ArcGIS Pro does not work against feature layers. So to answer your question:

  • A lot of tools can read feature layers as input (think buffer, copy, etc)
  • A very small set of tools can act upon or update feature layers (add field, calc field)
  • In your particular case, asking about Add Geometry Attributes, you'll need to make pure HTTP REST calls, or make use of the ArcGIS Python API.
  • I forgot to specify in my original question that it is possible to use 'Add Geometry Attributes' in ArcGIS Pro with hosted feature service layers through manual Geoprocessing--therefore, it should be possible to use the arcpy wrapper to do the same. The ArcGIS Python API does not support adding geometry attributes as far as I know, but I will look into pure HTTP rest calls. Thanks for the answer. – JMNC Jan 7 at 18:12
  • I'm a little surprised, but my knowledge on what each tool can do is quickly going out of date. Question @JMNC when you run the tool does it actually make changes to the feature layer, or does the tool just say "successful"? – KHibma Jan 7 at 18:19
  • Here is a screenshot of the 'Add Geometry Attributes' tool being executed, and the POINT_X and POINT_Y fields being added (though the coordinates are in northing/easting rather than decimal degrees - a separate issue). (I'm not sure what the best practice for sharing images in comments on stackexchange is - correct me if I can improve.) – JMNC Jan 7 at 18:27
  • Also, if I can get Add Field, Append, etc. to work with hosted feature service layers in arcpy, I should theoretically be able to do the same with Calculate/Add Geometry Attributes. My main problem is getting arcpy to refer to a hosted feature service layer - or perhaps that's not possible. – JMNC Jan 7 at 18:34
  • Can you try something like this: (updating for your own layer) arcpy.management.AddGeometryAttributes("https://services1.arcgis.com/vY6WuhLW0HkFe6Fl/arcgis/rest/services/rndypoints/FeatureServer/0", "POINT_X_Y_Z_M", None, None, None) – KHibma Jan 7 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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