Select to view content in your preferred language

Exporting points from S123 feature service with arcpy

401
2
Jump to solution
03-05-2024 07:07 AM
AmyRoust
Frequent Contributor

I have a S123 set up with the web builder to collect points. By default, S123 creates an attachment layer to collect photos and files, but we do not need that functionality.

Here's my code snippet (note that I have already defined the max_date variable in a previous step not in this section):

feature_layer = FeatureLayer(feature_layer_url, gis=gis)
given_date = max_date
query = f"submission_date_and_time > timestamp '{given_date}'"
selected_features = feature_layer.query(where=query)

print(f"Number of features selected: {len(selected_features.features)}")

if len(selected_features.features)>0:
    output_feature_class = r'C:\path\to\Default.gdb\SelectedFeatures'
    copy_features = arcpy.management.CopyFeatures(selected_features, output_feature_class)

  

No matter what I try in line 10 - CopyFeatures, ExportFeatures, truncate an existing table and append the selected records - I cannot get those selected features out of the S123 feature layer. 

I suspect that the attachments functionality is complicating the process, but I can't isolate a specific issue to troubleshoot. Does anyone have suggestions on how I can get these points exported to a FGDB?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
AmyRoust
Frequent Contributor

@TylerGraham2, thank you for your comment! It put me down the right path to find a solution.

From what I read in the Python API documentation, the only way to export features from a hosted feature layer is to export them into a new item in your ArcGIS Online account (or presumably Enterprise Portal, if that's your environment). I didn't want to manage the overhead of creating a new item, downloading it, and then deleting the web item, so I decided to rewrite the script to download the entire hosted feature layer using arcpy.conversion.FeatureClassToFeatureClass() function. Thankfully, that arcpy function worked even though I was pulling hosted feature layer data into a file geodatabase on my desktop.

Once I had a copy of the entire point feature class, I used a SQL query in arcpy.management.MakeFeatureLayer() to select the records that I wanted to put into my master feature class.

For those who don't mind creating a new item in your portal, you can use arcgis.features.manage.data.extract_data to export the results of a query into a separate file geodatabase (or shapefile, or csv, etc).

View solution in original post

0 Kudos
2 Replies
TylerGraham2
Frequent Contributor

I see you're using the arcpy module to call CopyFeatures, but my experience is that you can't access hosted layers with the arcpy module.  It looks like you're using the arcgis module to set up you FeatureLayer, so I think you're well on the way to success.   

There's a section in the ArcGIS for Python API that talks about how to call geoprocessing tools.  I think your answer may be there.  

https://developers.arcgis.com/python/guide/an-introduction-to-geoprocessing/

AmyRoust
Frequent Contributor

@TylerGraham2, thank you for your comment! It put me down the right path to find a solution.

From what I read in the Python API documentation, the only way to export features from a hosted feature layer is to export them into a new item in your ArcGIS Online account (or presumably Enterprise Portal, if that's your environment). I didn't want to manage the overhead of creating a new item, downloading it, and then deleting the web item, so I decided to rewrite the script to download the entire hosted feature layer using arcpy.conversion.FeatureClassToFeatureClass() function. Thankfully, that arcpy function worked even though I was pulling hosted feature layer data into a file geodatabase on my desktop.

Once I had a copy of the entire point feature class, I used a SQL query in arcpy.management.MakeFeatureLayer() to select the records that I wanted to put into my master feature class.

For those who don't mind creating a new item in your portal, you can use arcgis.features.manage.data.extract_data to export the results of a query into a separate file geodatabase (or shapefile, or csv, etc).

0 Kudos