Manipulating ArcGIS Online services through REST with Python

This week, I faced the problem of having to delete 1.7 million features in a feature service in ArcGIS Online. Normally, I would make a remote connection to the server, access the geodatabase and delete the features with a few mouseclicks. However, ArcGIS Online has no such feature. So I had to resort to the REST interface to the feature service: deleteFeatures. The API reference can be found here. Pretty straightforward, I would say.

But after waiting for a few (or rather: a lot of) seconds, I got a server timeout. It seems that 1.7 million features is rather too much to manipulate through the REST interface. I had to do this in batches. When talking about batches, Python jumps to mind. So I constructed a very small Python script, that also can be used to demonstrate the way you can manipulate ArcGIS Online feature services through the REST interface.

Here it is:

import requests

print "this might take a while..."

# retrieving a list of objectids

url = "http://services.arcgis.com/unique_id_of_your_organisation/arcgis/rest/services/servicename/FeatureServer/0"
startid=1000
maxnum=1769894
batchnum = 5000 #size of each batch

response = requests.get(url+"/query?f=json&returnIdsOnly=true&where=OBJECTID<%i" % maxnum, auth=('user', 'passwd'))
data = response.json()

if (len(data['objectIds'])>1):

for i in range(startid, maxnum, batchnum):

tekst = "OBJECTID > %i AND OBJECTID < %i" % (i,i+batchnum+1)
response = requests.post(url+"/deleteFeatures?f=json&where=" + tekst)
print "%s" % tekst

print "ready!"

You have to provide the URL to your service of course, and the credentials (see the line with the request) in case your service is secured. You can also catch the response for your deleteFeatures call by adding the following line after the request to deleteFeatures:

print response.json()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s