+-
我需要一个 Python脚本来将CSV数据转换为GeoJSON输出.输出应符合以下格式:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -85.362709,40.466442 ]
},
"properties": {
"weather":"Overcast",
"temp":"30.2 F"
}
}
]
}
我使用此脚本来运行该过程,但它不会产生所需的输出:
import csv, json
li = []
with open('CurrentObs.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for latitude, longitude, weather, temp in reader:
li.append({
"latitude": latitude,
"longitude": longitude,
"weather": weather,
"temp": temp,
"geo": {
"__type": "GeoPoint",
"latitude": latitude,
"longitude": longitude,
}
})
with open("GeoObs.json", "w") as f:
json.dump(li, f)
任何帮助是极大的赞赏!
最佳答案
可以直接使用geojson包:
import csv, json
from geojson import Feature, FeatureCollection, Point
features = []
with open('CurrentObs.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for latitude, longitude, weather, temp in reader:
latitude, longitude = map(float, (latitude, longitude))
features.append(
Feature(
geometry = Point((longitude, latitude)),
properties = {
'weather': weather,
'temp': temp
}
)
)
collection = FeatureCollection(features)
with open("GeoObs.json", "w") as f:
f.write('%s' % collection)
点击查看更多相关文章
转载注明原文:用于将CSV转换为GeoJSON的Python脚本 - 乐贴网