Select to view content in your preferred language

Overwrite AGOL feature service without overwriting field aliases and descriptions

639
4
Jump to solution
05-28-2024 05:35 PM
Labels (1)
Trippetoe
Frequent Contributor

Hello. I am using the FeatureLayerCollection.manager.overwrite() function to overwrite an AGOL hosted feature service with the contents of a csv file. Something like this:

lookup_table = connection_to_agol.content.get(file['agolItemId'])     
feature_layer_collection = FeatureLayerCollection.fromitem(lookup_table)
response = feature_layer_collection.manager.overwrite(csv_file)

 

This is working as expected, but all field aliases and descriptions for the hosted feature service are also overwritten when i overwrite the data. I'd like to not have to re-create that information every time the feature service is updated programmatically. Is there a different approach i can use to overwrite the data from a csv file while preserving the field aliases and descriptions?
 
thank you
0 Kudos
1 Solution

Accepted Solutions
EarlMedina
Esri Regular Contributor

You have two options: 

1. Keep doing what you're doing but add an additional step that updates the feature layer definition to reapply the aliases and descriptions. For this, you'll need to access the FeatureLayer you want to update from the FeatureLayerCollection and then save the fields properties somewhere before you overwrite (FeatureLayer.properties.fields). On overwrite, you will use the FeatureLayer.update_definition() and supply the fields.

 

2. Truncate/append instead of overwriting. I like this option better because it's faster and results in less service downtime.

This example assumes you have geometry to parse. If you don't then use "df" instead of "sedf" :

 

 

import pandas as pd
from arcgis import GIS
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from arcgis.features import FeatureLayer

gis = GIS()
url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Earthquakes_Since1970/FeatureServer/0"
fl = FeatureLayer(url, gis)

csv_path = "/path/to/xy.csv"
df = pd.read_csv(csv_path)
sedf = pd.DataFrame.spatial.from_xy(df=df, x_column="LONGITUDE", y_column="LATITUDE", sr=4326)

adds = sedf.spatial.to_featureset()
fl.manager.truncate()
fl.edit_features(adds=adds)

 

 

 

Note for this to work you have to point to individual layer you want to update with new data.

View solution in original post

4 Replies
EarlMedina
Esri Regular Contributor

You have two options: 

1. Keep doing what you're doing but add an additional step that updates the feature layer definition to reapply the aliases and descriptions. For this, you'll need to access the FeatureLayer you want to update from the FeatureLayerCollection and then save the fields properties somewhere before you overwrite (FeatureLayer.properties.fields). On overwrite, you will use the FeatureLayer.update_definition() and supply the fields.

 

2. Truncate/append instead of overwriting. I like this option better because it's faster and results in less service downtime.

This example assumes you have geometry to parse. If you don't then use "df" instead of "sedf" :

 

 

import pandas as pd
from arcgis import GIS
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from arcgis.features import FeatureLayer

gis = GIS()
url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Earthquakes_Since1970/FeatureServer/0"
fl = FeatureLayer(url, gis)

csv_path = "/path/to/xy.csv"
df = pd.read_csv(csv_path)
sedf = pd.DataFrame.spatial.from_xy(df=df, x_column="LONGITUDE", y_column="LATITUDE", sr=4326)

adds = sedf.spatial.to_featureset()
fl.manager.truncate()
fl.edit_features(adds=adds)

 

 

 

Note for this to work you have to point to individual layer you want to update with new data.

Trippetoe
Frequent Contributor

Thank you @EarlMedina . Option 2 sounds way better. I'll give that a try and let you know how it goes.

 

0 Kudos
Trippetoe
Frequent Contributor

Hi @EarlMedina 

Thanks again for your suggestion. I ended up doing a truncate / append. Instead of using the edit_features() method on the FeatureLayer, i am using the append() method on my Table (my data has no spatial component). There's a bit more futzing around with uploading and deleting the source csv file, but its working for now.

If you've got some extra time (and no worries if you don't!), could you explain more about how i could of done this with a pandas data frame. I was intrigued by your comment: 

This example assumes you have geometry to parse. If you don't then use "df" instead of "sedf" 

I poked around a bit, but couldn't figure out how to use the data frame as the source for the 'edit_features()'. I

Thank you.

0 Kudos
EarlMedina
Esri Regular Contributor

Oh, so you would basically just do this and I think it should work:

 

 

 

sedf = pd.DataFrame.spatial.from_df(df)
adds = sedf.spatial.to_featureset()
fl.manager.truncate()
fl.edit_features(adds=adds)

 

 

 

 

I don't remember if the result includes a SHAPE column, but if it does you could simply drop it before creating the featureset. There is a Table object you can use (although you can actually also just use FeatureLayer without a problem) which also has edit_features.

 

@Trippetoe Sorry, I realized I was using "updates" instead of "adds" before. Updated all the samples accordingly. You would use "updates" in the case that you were simply updating attributes in the data.