New to the site, and to geoJSON, and to python.

My goal is to read in a file with geoJSON data, and use it to generate shapes to be used in an application that is using python scripting. Python has the ability to read it in, and do some manipulating. I have found some examples, but they are not traversing the geoJSON hierarchy correctly.

I'm looking for a library, or instructions, on how to extract the geometry data from the geoJSON data file. In other words, I would like to be able to say "get the list of features..." - then, for each of the features, I'd like to get the geometry, read the type, and the get the coordinates for that particular geometry item. Repeat for each of the items in the list of features.

Any suggestions?


When you load a GeoJSON file using the json library, you get a dict that contains an entry features, which contains the list of features. Each feature in turn consists of a dict, which, among other things, contains an entry geometry. The geometry is a dict containing the entries type and coordinates. So you can traverse your GeoJSON file like this:

import json

with open('test.json') as f:
    data = json.load(f)

for feature in data['features']:
    print feature['geometry']['type']
    print feature['geometry']['coordinates']
| improve this answer | |
  • Excellent suggestion. This was far easier than what I was trying to do. I did need to change load to loads, and numpy isn't a library in my current install of python. Thanks for the quick reply. I'll be able to move on now. – Mike Weber Oct 8 '13 at 20:17
  • 1
    Numpy isn't technically needed. feature['geometry']['coordinates'] (above) is just a Python list. – sgillies Oct 8 '13 at 21:13
  • @sgillies: Good point, Sean. I've removed the use of numpy to avoid confusion. – Jake Oct 8 '13 at 21:18

My lib PyGeoj is specifically meant as a geojson file reader and writer, with a simple API that turns the file contents into objects with attributes, so you don't have to deal with the dictionaries directly. It also has some convenience methods, like calculate and add the bbox for the entire feature collection or just for each feature.

So for instance, the following code would do what the poster asked for:

import pygeoj
testfile = pygeoj.load("test.geojson")
for feature in testfile:
    print feature.geometry.type
    print feature.geometry.coordinates

The library can also import and export objects from/to other libraries via the _geo_interface_ protocol, among other things as seen in the documentation on the project's Github page.

| improve this answer | |
  • you library is great, I just started using it today – ziggy Apr 27 '16 at 18:58
  • if I wanted to start doing web-mapping with python parsing geojson, what would be the best software to use? – ziggy Apr 27 '16 at 18:59
  • 1
    For web app working with GeoJson, use Leaflet javascript library. Here is a good example thread: gis.stackexchange.com/questions/68489/… – For Comment Mar 28 '17 at 15:30

There are many geospatial Python modules that can convert GeoJSON to shapefiles (and the reverse):

see Python Geo_interface applications

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.