Generate GEOJSON using pandas datframe

有些话、适合烂在心里 提交于 2021-01-29 14:36:19

问题


I have a pandas dataframe that contains Longitude,Latitude, City, State,Timestamp. I have created one additional column as geometry that is a combination of [Latitude,Longitude].

I wanted to generate a geojson file that contains geomatory information on basis of city something like that https://github.com/adilmoujahid/kaggle-talkingdata-visualization/blob/master/input/geojson/china_provinces_en.json


回答1:


You can iterate through all rows of the dataframe and build up a dict with the geojson structure. Afterwards, you can dump the dict to a geojson file.

import json

geojson = {"type": "FeatureCollection", "features": []}

for _, row in df.iterrows():
    feature = {"type": "Feature", "geometry": {"type": "Point", "coordinates": [row['Longitude'], row['Latitude']]}, "properties": {"city": row['city']}}
    geojson['features'].append(feature)

with open('result.geojson', 'w') as fp:
    json.dump(geojson, fp)    


来源:https://stackoverflow.com/questions/60382659/generate-geojson-using-pandas-datframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!