Using Python to Write Features to a Feature Service

Recently I was tasked to automate a process to update a feature service with records from a file geodatabase.

This could have been achieved by using .NET ArcObjects programming to create a windows console program. A good reference is

However, my preferred method was to use python. The advantages for using python are:

–        no need to build/compile the code

–        no need to install Visual Studio and ArcObjects .NET SDK

–        more likely the code would work on different ArcGIS version (10.1 or 10.2)

–        more flexible and easier to change the codes

–        Arcpy’s SearchCursor makes it easy to connect to and read the file geodatabases

Python provides a number of modules for performing HTTP requests:

–        Requests

–        urllib

–        urllib2

–        httplib

–        httplib2

Initially, I tried to use the python ‘requests’ library to post the data.

import requests

import json


feat= {‘attributes’ : {‘Title’ : title, ‘Content’ : content}, ‘geometry’ : {‘x’ : x, ‘y’ : y}}

payload = {‘f’:’json, ‘features’ : feat}

url = “http://theserver/arcgis/rest/services/XXX/FeatureServer/0/addFeatures”

r =, data=json.dumps(payload), headers=headers)

print r.status

The status that was returned was 200, meaning successful, but it wasn’t working. When I performed a query, the features were not added. I also tried the ‘httplib’ library but I couldn’t get past a httplib.InvalidURL: nonnumeric port error.

Then I tried, this:

import requests

import json

headers = {‘content-type’:’application/json’,’Accept’:’application/json’}

payload = {‘attributes’ : {‘Title’ : title, ‘Content’ : content}, ‘geometry’ : {‘x’ : x, ‘y’ : y}}

 url = “http://theserver/arcgis/rest/services/XXX/FeatureServer/0/addFeatures?f=json&features = “+json.dumps(payload)

r =, headers=headers)

It worked! But there was an issue. The problem with the previous method is that, although it’s performing a POST, it’s posting with the content as the url string. This method will only work as long as the url string is shorter than 2048 characters. If you are sending large polygons features to the services, you will certainly exceed the maximum characters. So, how did I resolve this? I used the ‘urllib’ library. More information can be found in the ArcGIS Help at

import urllib

import json

feat =  {‘attributes’ : {‘Title’ : title, ‘Content’ : content}, ‘geometry’ : geom}

payload = {‘f’: ‘json’, ‘features’: feat}

url =”http://theserver/arcgis/rest/services/XXX/FeatureServer/0/addFeatures”

result = urllib.urlopen(url, urllib.urlencode(payload)).read()

print json.loads(result)



James Do (Professional Services)

4 thoughts on “Using Python to Write Features to a Feature Service

  1. Matej

    I’m not sure if I understand the notion of “writing features to feature service”? Are you updating SDE database that holds data for feature service from file geodatabase?

  2. Paul Cone

    I’ve been fighting to get the requests library to do this since the rest of my code is already using it, and requests is much cleaner than using urllib (or urllib2). I think the problem you hit can be worked around if you use the data= parameter in requests, instead of putting the payload in the URL, but I can’t seem to figure out how to get the payload for the POST encoded via requests so the ArcGIS REST service will like it. Any thoughts on that?

  3. Mick Huska

    Thank you for posting this. I used it as part of a process that included getting features from one feature service and posting them to another. Have you tried posting attachments?


Got something to say?

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

You are commenting using your 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