from datetime import datetime from ArcServer.ArcGisServer import AgsLayer from ArcServer.ArcGisServer import AgsFeatureServiceLayer from ArcServer.ArcGisServer import AgsFeature from ArcServer.ArcGisServer import AgsSpatialReference #Build a layer object to query print "\n\n****QUERYING SERVICE***" ags_fs_url=r'http://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/0' lyrObj=AgsLayer.fromUrl(ags_fs_url) #Find the record for NFL Broncos... where="rotation=42" result=lyrObj.query(where,"*",geometry=True) print "Query Result: %s"%str(result) print "Response JSON: \n%s"%str(lyrObj.json()) #Try to add the date to the description print "\n\n****TRYING TO ADD THE DATE WITH UNICODE CHARACTERS***" jdata=lyrObj.json() feature=jdata["features"][0] dt=datetime.strftime(datetime.now(),"%Y%m%d_%H:%M:%S") old_desc=feature['attributes']['description'] new_desc="Go Broncos! @ %s"%dt print "Setting description from '%s' to '%s'"%(old_desc,new_desc) feature['attributes']['description']=new_desc fslyrObj=AgsFeatureServiceLayer.fromUrl(ags_fs_url) fslyrObj.params=dict() fslyrObj.params['updates']=[feature] fslyrObj.params['f']='json' pth="/applyEdits" print "Executing 'applyEdits' via POST..." print "Host: %s"%str(fslyrObj.host) print "Path: %s"%str(fslyrObj.path+pth) print "Method: %s"%str(fslyrObj.method) print "port: %s"%str(fslyrObj.port) print "https: %s"%str(fslyrObj.https) print "headers: %s"%str(fslyrObj.headers) print "params: %s"%str(fslyrObj.params) result=fslyrObj.execute(pth) print "Query Result: %s"%str(result) print "Query Messages: %s"%str(fslyrObj.messages()) print "Query Data: %s"%str(fslyrObj.data()) #Lets try without unicode... print "\n\n****TRYING TO ADD THE DATE WITHOUT UNICODE CHARACTERS***" srObj=AgsSpatialReference.fromFeatureSetSr(jdata['spatialReference']) featureObj=AgsFeature.fromFsFeature(feature,jdata['spatialReference']) del fslyrObj fslyrObj=AgsFeatureServiceLayer.fromUrl(ags_fs_url) fslyrObj.params=dict() fslyrObj.params['updates']=featureObj.json() fslyrObj.params['f']='json' pth="/applyEdits" print "Executing 'applyEdits' via POST..." print "Host: %s"%str(fslyrObj.host) print "Path: %s"%str(fslyrObj.path+pth) print "Method: %s"%str(fslyrObj.method) print "port: %s"%str(fslyrObj.port) print "https: %s"%str(fslyrObj.https) print "headers: %s"%str(fslyrObj.headers) print "params: %s"%str(fslyrObj.params) result=fslyrObj.execute(pth) print "Query Result: %s"%str(result) print "Query Messages: %s"%str(fslyrObj.messages()) print "Query Data: %s"%str(fslyrObj.data())
****QUERYING SERVICE***
Query Result: True
Response JSON:
{u'features': [{u'geometry': {u'y': 4828807.519031979, u'x': -11690778.563894253}, u'attributes': {u'description': u'Go Broncos! @ 20131105_08:52:52', u'objectid': 1609, u'eventtype': 12, u'created_user': u'', u'created_date': 1383664073000L, u'rotation': 42, u'last_edited_date': 1383666772000L, u'eventdate': 0, u'last_edited_user': u''}}], u'fields': [{u'alias': u'OBJECTID', u'type': u'esriFieldTypeOID', u'name': u'objectid'}, {u'alias': u'Rotation', u'type': u'esriFieldTypeSmallInteger', u'name': u'rotation'}, {u'alias': u'Description', u'length': 75, u'type': u'esriFieldTypeString', u'name': u'description'}, {u'alias': u'Date', u'length': 36, u'type': u'esriFieldTypeDate', u'name': u'eventdate'}, {u'alias': u'Type', u'type': u'esriFieldTypeInteger', u'name': u'eventtype'}, {u'alias': u'created_user', u'length': 255, u'type': u'esriFieldTypeString', u'name': u'created_user'}, {u'alias': u'created_date', u'length': 36, u'type': u'esriFieldTypeDate', u'name': u'created_date'}, {u'alias': u'last_edited_user', u'length': 255, u'type': u'esriFieldTypeString', u'name': u'last_edited_user'}, {u'alias': u'last_edited_date', u'length': 36, u'type': u'esriFieldTypeDate', u'name': u'last_edited_date'}], u'spatialReference': {u'wkid': 102100, u'latestWkid': 3857}, u'geometryType': u'esriGeometryPoint', u'objectIdFieldName': u'objectid', u'globalIdFieldName': u''}
****TRYING TO ADD THE DATE WITH UNICODE CHARACTERS***
Setting description from 'Go Broncos! @ 20131105_08:52:52' to 'Go Broncos! @ 20131105_09:01:36'
Executing 'applyEdits' via POST...
Host: sampleserver6.arcgisonline.com
Path: /arcgis/rest/services/Wildfire/FeatureServer/0/applyEdits
Method: POST
port: 80
https: False
headers: {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'}
params: {'updates': [{u'geometry': {u'y': 4828807.519031979, u'x': -11690778.563894253}, u'attributes': {u'description': 'Go Broncos! @ 20131105_09:01:36', u'objectid': 1609, u'eventtype': 12, u'created_user': u'', u'created_date': 1383664073000L, u'rotation': 42, u'last_edited_date': 1383666772000L, u'eventdate': 0, u'last_edited_user': u''}}], 'f': 'json'}
Query Result: False
Query Messages: ['Response indicated there was an error, please check the obj.data()...']
Query Data: {"error":{"code":400,"message":"Unable to complete operation.","details":[]}}
****TRYING TO ADD THE DATE WITHOUT UNICODE CHARACTERS***
Executing 'applyEdits' via POST...
Host: sampleserver6.arcgisonline.com
Path: /arcgis/rest/services/Wildfire/FeatureServer/0/applyEdits
Method: POST
port: 80
https: False
headers: {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'}
params: {'updates': [{'geometry': {'y': '4828807.51903', 'x': '-11690778.5639', 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}}, 'attributes': {'DESCRIPTION': 'Go Broncos! @ 20131105_09:01:36', 'OBJECTID': 1609, 'EVENTTYPE': 12, 'CREATED_USER': '', 'CREATED_DATE': 1383664073000L, 'ROTATION': 42, 'LAST_EDITED_DATE': 1383666772000L, 'EVENTDATE': 0, 'LAST_EDITED_USER': ''}}], 'f': 'json'}
Query Result: True
Query Messages: []
Query Data: {"addResults":[],"updateResults":[{"objectId":1609,"success":true}],"deleteResults":[]}
import httplib import urllib import json from urlparse import urlparse from datetime import datetime def parseUrl(url): result=dict() up=urlparse(url) host=up.netloc #assume port 80 at first port=80 #result["host"]=host result["path"]=up.path result["https"]=False if (up.scheme.upper()=='HTTPS'): result["https"]=True #switch to port 443 if HTTPS by default port=443 #accept non-default port if provided... if (up.netloc.find(":") != -1): port=int(host[host.find(":")+1:]) host=host[0:host.find(":")] result["host"]=host result["port"]=port return result ags_fs_url=r'http://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/0' urlp=parseUrl(ags_fs_url) headers={"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"} print "\n\n****QUERYING SERVICE***" conn=httplib.HTTPConnection(urlp['host'],urlp['port']) params=dict() params['f']='json' params['where']="rotation=42" params['outFields']="objectid,description,rotation" params['returnGeometry']=False conn.request("POST",urlp['path']+"/query",urllib.urlencode(params),headers) resp=conn.getresponse() data=resp.read() jdata=json.loads(data) print "Response JSON: \n%s"%str(jdata) #Try to add the date to the description print "\n\n****TRYING TO ADD THE DATE WITH UNICODE CHARACTERS***" feature=jdata["features"][0] dt=datetime.strftime(datetime.now(),"%Y%m%d_%H:%M:%S") old_desc=feature['attributes']['description'] new_desc="Go Broncos! @ %s"%dt print "Setting description from '%s' to '%s'"%(old_desc,new_desc) feature['attributes']['description']=new_desc conn=httplib.HTTPConnection(urlp['host'],urlp['port']) params=dict() params['f']='json' params['updates']=[feature] conn.request("POST",urlp['path']+"/applyEdits",urllib.urlencode(params),headers) resp=conn.getresponse() data=resp.read() jdata=json.loads(data) print "Request Parameters: \n%s"%str(params) print "Response JSON: \n%s"%str(jdata) #Lets try without unicode... print "\n\n****TRYING TO ADD THE DATE WITHOUT UNICODE CHARACTERS***" new_feature=dict() for k in feature.keys(): new_feature[str(k)]=dict() for k in feature['attributes'].keys(): if type(feature['attributes']) == unicode: new_feature['attributes'][str(k)]=str(feature['attributes'] ) else: new_feature['attributes'][str(k)]=feature['attributes'] conn=httplib.HTTPConnection(urlp['host'],urlp['port']) params=dict() params['f']='json' params['updates']=[new_feature] conn.request("POST",urlp['path']+"/applyEdits",urllib.urlencode(params),headers) resp=conn.getresponse() data=resp.read() jdata=json.loads(data) print "Request Parameters: \n%s"%str(params) print "Response JSON: \n%s"%str(jdata)
****QUERYING SERVICE***
Response JSON:
{u'fields': [{u'alias': u'OBJECTID', u'type': u'esriFieldTypeOID', u'name': u'objectid'}, {u'alias': u'Rotation', u'type': u'esriFieldTypeSmallInteger', u'name': u'rotation'}, {u'alias': u'Description', u'length': 75, u'type': u'esriFieldTypeString', u'name': u'description'}], u'globalIdFieldName': u'', u'objectIdFieldName': u'objectid', u'features': [{u'attributes': {u'rotation': 42, u'description': u'Go Broncos! @ 20131105_12:33:55', u'objectid': 1609}}]}
****TRYING TO ADD THE DATE WITH UNICODE CHARACTERS***
Setting description from 'Go Broncos! @ 20131105_12:33:55' to 'Go Broncos! @ 20131105_12:38:14'
Request Parameters:
{'updates': [{u'attributes': {u'rotation': 42, u'description': 'Go Broncos! @ 20131105_12:38:14', u'objectid': 1609}}], 'f': 'json'}
Response JSON:
{u'error': {u'message': u'Unable to complete operation.', u'code': 400, u'details': []}}
****TRYING TO ADD THE DATE WITHOUT UNICODE CHARACTERS***
Request Parameters:
{'updates': [{'attributes': {'rotation': 42, 'description': 'Go Broncos! @ 20131105_12:38:14', 'objectid': 1609}}], 'f': 'json'}
Response JSON:
{u'addResults': [], u'deleteResults': [], u'updateResults': [{u'success': True, u'objectId': 1609}]}