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
Hi Brandon,
You can use python and the REST API to execute a delete command:
ArcGIS REST API
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!
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):
ArcGIS Help 10.1
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/FeatureServer/0/deleteFeatures" # 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." } } ]}
Thanks for all the help Xander. I'm excited to give it a try!
Best,
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?
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.