29

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?

3 Answers 3

49

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']
3
  • 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
    Commented Oct 8, 2013 at 20:17
  • 1
    Numpy isn't technically needed. feature['geometry']['coordinates'] (above) is just a Python list.
    – sgillies
    Commented Oct 8, 2013 at 21:13
  • @sgillies: Good point, Sean. I've removed the use of numpy to avoid confusion.
    – Jake
    Commented Oct 8, 2013 at 21:18
8

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.

3
  • you library is great, I just started using it today
    – ziggy
    Commented Apr 27, 2016 at 18:58
  • if I wanted to start doing web-mapping with python parsing geojson, what would be the best software to use?
    – ziggy
    Commented Apr 27, 2016 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/… Commented Mar 28, 2017 at 15:30
5

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

see Python Geo_interface applications

0

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