Update Feature with ArcGIS API for Python

3234
8
Jump to solution
08-27-2018 10:10 PM
PierreWeisse1
New Contributor II

Hello, I work with ArcGIS API for Python on a service hosted in AGOL. I would like to update a field based on another field. I have tried many times, I do not understand how to do it. The example in the API is based on the update between two documents.

Someone to help me ?

Thank you

1 Solution

Accepted Solutions
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Hope the following code snippet gives you some clue:

from arcgis.gis import GIS
from arcgis.features import FeatureSet, Feature

gis = GIS(url="",username="",password="")
fs=gis.content.get(itemId)

# assume the first layer is the layer you want to update
fl=fs.layers[0]

# set return_geometry false will save you time and data 
features=fl.query(where=sql_where,out_fields='OBJECTID,FieldName'
                   ,return_geometry=False)

for feature in features:
   # Calculate the new value and assign it to the field in the current record
   feature.set_value('FieldName',NewValue)

# update the features in AGOL
results = fl.edit_features(updates=features)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

8 Replies
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Hope the following code snippet gives you some clue:

from arcgis.gis import GIS
from arcgis.features import FeatureSet, Feature

gis = GIS(url="",username="",password="")
fs=gis.content.get(itemId)

# assume the first layer is the layer you want to update
fl=fs.layers[0]

# set return_geometry false will save you time and data 
features=fl.query(where=sql_where,out_fields='OBJECTID,FieldName'
                   ,return_geometry=False)

for feature in features:
   # Calculate the new value and assign it to the field in the current record
   feature.set_value('FieldName',NewValue)

# update the features in AGOL
results = fl.edit_features(updates=features)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
PierreWeisse1
New Contributor II

Thank you Simo Xu

Have a good day

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

You are welcome, thank you

0 Kudos
PierreWeisse1
New Contributor II

Hello Simo,

Maybe you can help me on this point. I'm still working with this web service, hosted under AGOL and with ArcGIS API for Python.

On a field of type 'date', I would like to empty the field of its contents.

However when I try, here is the result 1/1/1900 11h05

Do you have an idea ?

Thank you

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

I don't know how you did it, but could you test whether or not the following code works? 

feature.set_value('DateFieldName',None)
0 Kudos
PierreWeisse1
New Contributor II

With your proposal it does not change. The content of the field remains the same.

Before asking for your help I tried this:


feature.set_value('DateFieldName',"")
RESULT >> 1/1/1900 11:05 AM


The field settings have this parameter.
Allow null values is set to Yes

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

You are right.

I tried SQL in Map Viewer, it didn't work either.

I have to manually do it in AGOL Map Viewer using Arcade expression to set the Date column to NULL. But I am not sure this is what you want.

.

0 Kudos
JonathanPollack1
New Contributor III

Very nicely done.  I wish the Esri help documentation on this topic was this straightforward and to the point.

0 Kudos