Select to view content in your preferred language

How can I Calculate a field in a hosted feature class using Rest API?

2067
2
06-27-2016 08:54 AM
AllanLambert
Emerging Contributor

I have a Damage Assessment feature layer hosted on AGOL with a web map.  50k points.  Along with Domain controlled fields are Estimated % of Damage, Estimated $ Amt of Damage and a prepopulated Dwelling Value field.  Field type is Double on these 3 fields.  I need to either

1.  Calc (Est $ Damage = DwellingVal * Est % Damage) on the fly as the user enters data for each point.

2.  Create a URL to Calc (Est $ Damage = DwellingVal * Est % Damage) where (Est % Damage is >0).  AGOL times out      calc-ing the 50k records so I think the Where clause is necessary to only capture the 500 or 5000 that are damaged.

3.  I'm open to ANY suggestion!  Thanks. Allan

I can't get the REST API grid to work. The error says that the calcexpression is invalid:

Where: PercentDamage > 0

"calcExpression":[{"field" : "EstDollarDamage", "sqlExpression" : "DwellingVal*PercentDamage*.01"}]

http://services5.arcgis.com/cA4ofagPaJJ7qhEz/arcgis/rest/services/DamageAssessment_O_only/FeatureSer... > 10 2”,calcExpression={“field”: “EstDollarDamage”,“sqlexpression” : "DwellingVal*PercentDamage*.01"})

0 Kudos
2 Replies
joerodmey
MVP Alum

Did you ever get this figured out? I have the same question.

0 Kudos
AllanLambert
Emerging Contributor

I called Tech Support a couple months later and luckily talked to the most helpful guy who wrote this py script in a couple hours.  I don't think ESRI "gives" this kind of help anymore.  I can't put my hands on the token documentation.  I put this script in a sched task and it runs every 5 mins during storm damage assessment.  Good Luck. Allan

import urllib
import urllib2
import httplib
import json
##Token Generation Parameters
tokenUrl = 'https://www.arcgis.com/sharing/oauth2/token'
featureServiceURL = 'https://services5.arcgis.com/xxxxxxxxx/arcgis/rest/services/MyDamageAssessmentService2019/FeatureServer/0/calculate'
clientId = 'xxxxxxxxxxxx'
clientSecret = 'xxxxxxxxxxxxxxxxxxx'
##Calculate Parameters - Only calc if a property has been assessed PercentDam > 0
whereClause = 'PercentDam > 0'
updateExpression = [{"field":"EstDollarD","sqlExpression":"Dwlgval1*PercentDam*.01"}]
def sendRequest(url, parameters):
 data = urllib.urlencode(parameters)
 req = urllib2.Request(url, data)
 response = urllib2.urlopen(req)
 return json.loads(response.read())
def generateToken():
 tokenParameters = {'client_id': clientId, 'client_secret': clientSecret, 'grant_type':'client_credentials'}
 jsonResponse = sendRequest(tokenUrl, tokenParameters)
 token = jsonResponse["access_token"]
 return token
 
def calculateValues(token):
 calculateParameters = {'where':whereClause,'calcExpression':updateExpression,'sqlFormat':'standard','rollbackOnArithmaticError':'false','token':token, 'f':'json'}
 jsonResponse = sendRequest(featureServiceURL, calculateParameters)
 if jsonResponse['success'] == True:
  print "Updated Feature Count: " + str(jsonResponse["updatedFeatureCount"])
 else:
  print "Calculate operation failed"
  print jsonResponse

token = generateToken()
calculateValues(token)