问题
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?
回答1:
You can read it like any json:
import json
data = json.loads(datastring)
data['features'][0]['geometry'] #Your first point
回答2:
import geojson
with open(path_to_file) as f:
gj = geojson.load(f)
features = gj['features'][0]
回答3:
You can also use geopandas:
import geopandas as gpd
earthquake = gpd.read_file('earthquake.geojson')
print(earthquake.head())
回答4:
You can read it with json import, and file open:
import json
with open(path) as f:
data = json.load(f)
for feature in data['features']:
print(feature['properties'])
回答5:
You can use pandas library directly
import pandas as pd
data = pd.read_json('File.geojson')
The important this is to understand the structure of this json file and manipulate the dictionaries in there
来源:https://stackoverflow.com/questions/42753745/how-can-i-parse-geojson-with-python