Is it possible to script the removal of points from a hosted feature service? I have a hosted feature service that supports a geoform that we're using to collect customer information. Once the customer submits the geoform I have a process that downloads a replica and moves the data to our ArcGIS Server. Once that's done I'd like to clean out the feature service so that each person is presented with a "clean" map. Is this possible? I'd also be open to other methods to accomplish this "clean" look.
Thanks,
Brandon
Solved! Go to Solution.
Hi Brandon,
You can use python and the REST API to execute a delete command:
This PDF provides some more info on interacting with the server using Python:
http://proceedings.esri.com/library/userconf/proc14/tech-workshops/tw_187.pdf
Kind regards, Xander
Hi Brandon,
You can use python and the REST API to execute a delete command:
This PDF provides some more info on interacting with the server using Python:
http://proceedings.esri.com/library/userconf/proc14/tech-workshops/tw_187.pdf
Kind regards, Xander
Hi Xander,
Sorry for the slow response. I'm new to using JSON inside of python; can you give me an idea of how I would use the delete command inside of a python script? Or another resource to consult? I'm also curious how I would list out the object id's inside of the feature service. Is there something similar to a search cursor that I could use?
Thanks for your help!
Brandon
Below a small example of a POST request. This did not work on my computer due to an authentication error, but that just might be my proxy (the server supposedly does not require a token (but you may in your case).
More on getting a token here (include it in the params dictionary):
import urllib
import urllib2
# a list of non existing object id's (to avoid deletion of features)
oids = [37,101,462]
ids = ",".join([str(oid) for oid in oids])
print ids
# encode the parameters, only specifying the list of objectids (simple case)
params = urllib.urlencode({'f': 'json', 'objectIds':ids})
print params
# taking the sample HomelandSecurity URL (hoping this doesn't affect my visit to the DevSummit next year)
queryURL = "https://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServe..."
# use the request method on the urllib2 library
req = urllib2.Request(queryURL, params) # forces POST request (deleteFeatures can only be done with a POST request)
# get the response
response = urllib2.urlopen(req)
# assuming this query URL
jsonResult = json.load(response)
# do something with the response
print jsonResults
This will return a json list indicating the result of the deletion for each object id:
{
"deleteResults" : [
{
"objectId" : 37,
"globalId" : null,
"success" : false,
"error" :
{
"code" : -1,
"description" : "Delete for the object was not attempted. Object may not exist."
}
},
{
"objectId" : 101,
"globalId" : null,
"success" : false,
"error" :
{
"code" : -1,
"description" : "Delete for the object was not attempted. Object may not exist."
}
},
{
"objectId" : 462,
"globalId" : null,
"success" : false,
"error" :
{
"code" : -1,
"description" : "Delete for the object was not attempted. Object may not exist."
}
}
]}
Kind regards, Xander
Thanks for all the help Xander. I'm excited to give it a try!
Best,
Brandon
Hi Xander,
Finally got around to trying the process and I'm getting a traceback error that 'json' is not defined which I think is related to this line from your example.
# assuming this query URL
jsonResult = json.load(response)
Is json part of another module or is it a variable I didn't define correctly?
Thanks,
Brandon
Hi Brandon,
I missed a line in the beginning... json is a module that needs to be imported.
add the line:
import json
...in the beginning of the code
Sorry, Xander
Hi Xander,
Thanks for the quick response and no worries on the missed line. Your fix worked but now it seems we're stuck on the token issue you mentioned briefly above. I found the API reference for getting a token but it hasn't worked for me yet. Thanks for all your help. Once I have it working I'll post back here in case anyone is interested.
Best,
Brandon