How can I parse GeoJSON with Python

stonebe picture stonebe · Mar 12, 2017 · Viewed 29.4k times · Source

I have have geojson data from a query which I now want to parse and print on screen. My current code is:

import urllib
import geojson

while True:

    url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2017-03-01&minmagnitude=4.0&maxmagnitude=9.0&minlongitude=5.95&maxlongitude=10.50&minlatitude=45.81&maxlatitude=47.81'
    uh = urllib.urlopen(url)
    data = uh.read()
    print data
    break

It seems that data is a simple string. However, I thought it could be parsed like a json parameter. How do I have to handle geojson data in order to print a single point, e.g. to extract the coordinates of the first point only?

Answer

user2270655 picture user2270655 · Dec 13, 2017
import geojson
with open(path_to_file) as f:
    gj = geojson.load(f)
features = gj['features'][0]