小编use*_*454的帖子

Python脚本将CSV转换为GeoJSON

我需要Python脚本将CSV数据转换为GeoJSON输出。输出应与以下格式匹配:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates":  [ -85.362709,40.466442 ]
      },
      "properties": {
        "weather":"Overcast",
        "temp":"30.2 F"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我正在使用此脚本来运行该过程,但未产生所需的输出:

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)
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏!

python csv json geojson

4
推荐指数
1
解决办法
4239
查看次数

标签 统计

csv ×1

geojson ×1

json ×1

python ×1