Hello,
I recently just updated geoprocessing python script tools to use more of the API for python for processing AGOL hosted feature layers that are also in the scripts downloaded to local file geodatabases.
Previously I had used the arcpy tool below which maintains attachments.
arcpy.FeatureClassToFeatureClass_conversion
However in the following updated snippet I do not see the photo attachments in the downloaded feature class. Is there an additional setting I need to set to maintain attachments? Or maybe there just aren't attachments in a spatially enabled dataframe to even export?
fn_df.spatial.to_featureclass(location=out_features, sanitize_columns=False)
I'm using ArcGIS API for Python 2.0.1 with ArcGIS Pro 3.0.3
Thanks,
Kathy
Solved! Go to Solution.
Hi Kathy,
sdf to feature class will not maintain attachments. You can use arcpy similar to below or try other avenues listed here.
import arcpy
arcpy.env.maintainAttachments = True
## output feature class name
out_fc = "exported_layer"
## url to the feature layer
## this could also be to a table
fl_url = "https://services-eu1.arcgis.com/*******/arcgis/rest/services/FS_Name/FeatureServer/0"
## file geodatabase path
## NOTE you can use arcpy to create a geodatabase rather than having an existing
gdb = r"C:\Path\to\Documents\fgdb.gdb"
## path for saving output feature class
out_fc_path = "{0}\\{1}".format(gdb, out_fc)
## create feature class from feature layer
## you can also use FeatureClassToFeatureClass in older versions of ArcGIS Pro
## if exporting a table use arcpy.conversion.ExportTable()
arcpy.conversion.ExportFeatures(fl_url, out_fc_path)
Hi Kathy,
sdf to feature class will not maintain attachments. You can use arcpy similar to below or try other avenues listed here.
import arcpy
arcpy.env.maintainAttachments = True
## output feature class name
out_fc = "exported_layer"
## url to the feature layer
## this could also be to a table
fl_url = "https://services-eu1.arcgis.com/*******/arcgis/rest/services/FS_Name/FeatureServer/0"
## file geodatabase path
## NOTE you can use arcpy to create a geodatabase rather than having an existing
gdb = r"C:\Path\to\Documents\fgdb.gdb"
## path for saving output feature class
out_fc_path = "{0}\\{1}".format(gdb, out_fc)
## create feature class from feature layer
## you can also use FeatureClassToFeatureClass in older versions of ArcGIS Pro
## if exporting a table use arcpy.conversion.ExportTable()
arcpy.conversion.ExportFeatures(fl_url, out_fc_path)
Thanks @Clubdebambos for the clarification on the attachments.
Its been an interesting and frustrating learning experience in trying to make us of the ArcGIS API for python and move away from arcpy (as suggested by Esri solution engineers) only to find out objectives can't be met with the API.
For instance, earlier in the scripts I updated the code from using arcpy and the UpdateCursor to use the edit_features like shown below. This works great to update attributes in the AGOL hosted feature layers, but then if you use arcpy and either the arcpy.conversion.ExportFeatures or the arcpy.FeatureClassToFeatureClass conversion, any records updated with the API are exported with blank values. When I would view the data in AGOL the data is all updated, but for some reason exporting using arcpy did not recognize those record updates. So then I was able to use the spatial.to_featureclass only to find out this won't work either because we need the attachments to download. So apparently you can't mix and match or do you know why the arcpy exports blank records updated with the API.
updates_key_df.append(feature)
# Perform the update
result = lyr.edit_features(updates=updates_key_df)
Thanks again for the response.
Kathy
The ArcGIS API for Python is still quite in its infancy if you ask me. If you find something doesn't work or have an idea I suggest you file an issue or the idea on github. Esri are really good for getting back to you on that platform. You might dig around and find similar for ArcPy. It is strange behaviour with the blanks and one that should be reported.
The arcgis python API is a rest interface, not a relational database. Everything is atomic and restful. This means you cannot edit a single attribute in a record. You have to retrieve the whole record, edit the attributes you want and post the whole record back into the online feature layer. Because the REST interface is Stateless. It does not remember anything you have done, which record you edited etc. Its like a whole new phone call to a new support caller without a log. You have to explain the issue all over again.
I liken it to having string puppets digging a hole with a shovel instead of you just doing it with a shovel yourself. The puppet is clumsy and very poor at digging holes.
You cannot use the arcpy package to edit online data. You have to use the arcgis package.