Select to view content in your preferred language

API for python spatial.to_featureclass - maintain attachments?

427
3
Jump to solution
03-23-2024 12:55 PM
kmsmikrud
Regular Contributor

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

 

1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

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)

  

~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
3 Replies
Clubdebambos
Occasional Contributor III

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)

  

~ learn.finaldraftmapping.com
0 Kudos
kmsmikrud
Regular Contributor

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

Clubdebambos
Occasional Contributor III

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.  

~ learn.finaldraftmapping.com