Search code examples
pythonparsinggeojson

How can I parse GeoJSON with Python


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?


Solution

  • You can read it like any json:

    import json
    data = json.loads(datastring)
    data['features'][0]['geometry'] #Your first point