I am beginning to research the best way to take a suite of existing arcgis python tools and incorporating the python api so the tools can be enhanced to also work with feature services.
The tools are currently taking input fgdbs or egdbs, reading through them, some are editing features, many compare feature attributes to other 'rules' fgdbs... The tools are pretty complex, and I am trying to figure out which way to approach the task.
Ideally the existing tools would just be enhanced with the capabilities of the python api, but I suppose it could be the other way around that new tools are started with python api and they maybe then call the arcpy portions.
Thoughts, community?
What method are you using when you say you "truncate web layers in a hosted feature service"? It looks like you need to use delete_features(), which could take a while depending on how many records you're working with. This will never match simplicity and performance of a database truncate though.
I see there is also an append() method for a FeatureLayer in the ArcGIS API for Python; I didn't know about this. Although, a note on append from the REST API doc:
Feature serviceAppendcapabilities must be enabled for theappendoperation to be used by nonadministrative users. Organization administrators, or the service owner, can use theappendoperation without having theAppendcapability enabled on the feature service. TheAppendcapability needs to be added to the service capabilities only if the service owner or organization administrators need to allow nonadministrative users to append data to a feature service.
I haven't used the ArcGIS API for Python or hosted feature layers in Portal much so I'm sorry I can't guide you much more. If you have a specific question about deleting features from multiple layers in a feature service, you should create a separate question for that.
Thanks for reply.
After signing into portal i get the itemID of the feature service using content.get(). Then assign this to a variable. Use this variable to access the layers of the feature service, once I have the layer I want to truncate, i assign it to another variable then truncate using layervariable.manager.truncate(). This works fine. It is appending that is causing issues.
Ill just keep working on it, i know i am missing something small. I am the admin/owner of the feature service so should not have problem with append permissions, I think I need to supply more parameters to the append tool even though schema matches between layers, maybe I will need to provide field mappings..
Thanks again
Cool, thanks for letting me know about the truncate operation for FeatureLayer in the REST API!
Truncate (Feature Layer)—ArcGIS REST APIs | ArcGIS Developers
Ok, So after working on this some more, I am now able to truncate and append into a hosted feature layer in Enterprise using data from disk. My approach is to use Python Api to access the layers in the portal and truncate them. Next, I use the append function in ArcPRO to update the hosted feature classes with a feature class from disk. A little different then using the append function in Python API that is looking for source data as an item published to portal vs feature class on disk.
Use Python API to query the portal for the itemid ~ gis.content.get(itemid) set to a variable.
Once you have item id you can access the feature layers ~ use above variable to access layers i.e. variable.layers.
Once you have listing of layers in the feature service you can access them by assigning each layer to a variable . variable.layers[0] for first layer, variable.layers[1] for second layer, etc.. This will return the url endpoint of that layer.
once you have the layer url you can use the truncate/append function to update the hosted layers in the service. variable.layers.manager.truncate(), then I append in ArcPRO using a feature class that is stored on disk not in portal, thus a little different than using API for existing portal item.
variable.manager.truncate()
arcpy.management.Append(
inputs=r'C:/CustomerNotification/CN.gdb/ALL',
target=variable.url,
schema_type="TEST",
field_mapping=None,
subtype="",
expression="",
match_fields=None,
update_geometry="UPDATE_GEOMETRY")
This will work to update hosted feature classes in Enterprise with data from local disk without having to overwrite the service. Now, some caveats:
- When a feature service is a hosted service and the data is copied to the GIS Server, all the field names are changed to lowercase. If your source for the append has the same schema but some field names are uppercase, the append will run without error but no features will be added...
- This method will not cause the objectids to increase, as is the case with deleting features then using add_edits to add new features back in.
- This method runs slower than using json and parsing the source data then use REST calls to delete and add/update features. using REST to update via json strings seems to be the fastest, objectids will increase.
Just my experience, hope this can help someone else out. the lowercase fieldname change when hosting the data was causing an issue for my script, once I realized that I was on my way to success.