将CSV转换为GeoJSON的Python脚本

10

我需要一份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)

非常感谢任何帮助!


1
CurrentObs.csv里面是什么?你确定需要newline=''吗? - Paulo Scardine
你说输出结果与上面的格式不匹配,但是你正在将一个完全不同的格式添加到列表中。你遇到了什么问题? - Phydeaux
你的 CSV 文件中是否包含纬度和经度列? - A.HADDAD
我建议您发布您的CSV内容,以便其他遇到相同问题/关注的人可以参考。 - A.HADDAD
2个回答

17

可以直接使用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文件中是否有纬度和经度列? 在循环中出现了错误=>我的问题 - A.HADDAD
@ewcz 你要如何修改这段代码,使其具有正确的FeatureCollection格式,例如: "type": "FeatureCollection", "features": [ { "geometry": { "type": "Point", "coordinates": [ -73.935242, 40.730610 ] }, "properties": {...} } ] - LBes

4

检查此脚本是否解决问题

import csv
import json
from collections import OrderedDict

li = []
with open('CurrentObs.csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for latitude, longitude, weather, temp in reader:
        d = OrderedDict()
        d['type'] = 'Feature'
        d['geometry'] = {
            'type': 'Point',
            'coordinates': [float(latitude), float(longitude)]
        }
        d['properties'] = {
            'weather': weather,
            'temp': temp
        }
        li.append(d)

d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
with open('GeoObs.json', 'w') as f:
    f.write(json.dumps(d, sort_keys=False, indent=4))

1
不错的解决方案,对于像OpenLayers这样的应用程序,您需要将'coordinates':[float(latitude),float(longitude)]翻转为'coordinates':[float(longitude),float(latitude)]并添加一些逻辑来捕获标题... - Jonathan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接