How to Update attributes in an Online Feature Layer using Selection

2728
8
Jump to solution
08-11-2021 02:55 PM
ChuckBenton
Occasional Contributor

I have a large feature layer hosted in ArcGIS Online. I want to select a subset by matching a field attribute to a value, and then update another field to specific value. Ideally I'd prefer not to iterate through any loops as the overall layer is > 150M records. The selection field is indexed and performs quickly, I just can't figure out how to modify/update the other field based on the selection. I've seen arcpy solutions that work with shapefiles and fgdbs, but nothing for Online.

Thanks in advance for any advice or pointers! 

0 Kudos
1 Solution

Accepted Solutions
DavinWalker2
Esri Contributor

Hi Chuck,

I think  FeatureLayer.calculate is what you are looking for. 

View solution in original post

8 Replies
DavinWalker2
Esri Contributor

Hi Chuck,

I think  FeatureLayer.calculate is what you are looking for. 

ChuckBenton
Occasional Contributor

I'm sure this is a simple issue, but it's stumped me.

The following line executes without issue:

fs_layer.calculate(where="FIPS=18039", calc_expression={"field":"VERSION", "value": "07/04/2021"})

This fails:

where_str = '"FIPS=18039"'
calc_str = '"field":"VERSION", "value":"07/10/2021"'
fs_layer.calculate(where=where_str, calc_expression={calc_str})

I've multiple ways to build the parameters to be passed, building the entire argument, varying quote schemes. What am I missing?

0 Kudos
ConradSchaefer__DOIT_
New Contributor III

Chuck,

looks like the difference is a dict versus a set containing a string.

This is a valid dict (key: value) and looks to be an acceptable parameter type for calculate

  • {"field":"VERSION", "value": "07/04/2021"}

Simplifying your code your second option is:

 

 

0 Kudos
MirHashmi
Occasional Contributor

@DavinWalker2 Hi Davin, it not does work with me when I am passing my own value to be updated for an attribute. However, it works if I pass the column name  ? 

This does not work:  fs_layer.calculate(where="FIPS=18039", calc_expression={"field":"FIELDNAME1", "value":"newattributevalue"}) // this returns an error PerformEdit() failure. ColumnName='newattributevalue' not found in field name map. (Error Code: 400)

This works: fs_layer.calculate(where="FIPS=18039", calc_expression={"field":"FIELDNAME1", "value":"FIELDNAME2"}) // this one copies the value from FIELDNAME2 to FIELDNAME1. Is this how calculate works?

 

Thank you

0 Kudos
emedina
New Contributor III

For this kind of thing, I'd recommend familiarizing yourself with Spatially Enabled DataFrames: https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/

 

Your general process will look like this:

  1. Create a FeatureLayer object from the layer you want to update
  2. Create an SEDF from the FeatureLayer
  3. Perform your selections/manipulations in the SEDF. When satisfied with the result, optionally create a version of the dataframe that only includes the field(s) you're updating and the unique identifier used for the update (ObjectID/GlobalID).
  4. Convert the result dataframe to a feature set.
  5. Execute an update operation with the FeatureLayer object's  "edit_features" method (your update is the feature set you created).

Hope this helps.

FrancisCorbett
New Contributor II

This worked great - thanks for the workflow

0 Kudos
ChuckBenton
Occasional Contributor

Thanks for the responses! featurelayer.calculate fit the bill. I'd skipped over it in my searches, because I'm not doing any "calculations". Once found it was an easy implementation. Thanks!!!

Chuck

ConradSchaefer__DOIT_
New Contributor III

.calculate() proved to be very simple and precise. Didn't know it existed before but definitely valuable for very precise editing of individual field(s).

0 Kudos