【问题标题】:Python Script to Convert CSV to GeoJSON将 CSV 转换为 GeoJSON 的 Python 脚本
【发布时间】:2018-02-02 16:11:26
【问题描述】:

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

非常感谢任何帮助!

【问题讨论】:

  • CurrentObs.csv 中有什么内容?你确定需要newline=''
  • 您说输出与上面的格式不匹配,但您将完全不同的格式附加到列表中。你在纠结什么?
  • 是 csv 中的纬度、经度列吗?
  • 我建议您将 csv 的内容发布给其他遇到相同问题/担忧的人

标签: python json csv geojson


【解决方案1】:

可以直接使用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 中的纬度和经度列还是什么?我在循环中遇到错误 =>my issue
  • @ewcz 你将如何修改它以获得适当的 FeatureCollection,例如:"type": "FeatureCollection", "features": [ "geometry": { "type": "Point", "coordinates ": [ -73.935242, 40.730610 ] }, "属性": {...}] }
【解决方案2】:

检查此脚本是否解析

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))

【讨论】:

  • 不错的解决方案,对于像 OpenLayers 这样的应用程序,您可能希望将“坐标”:[float(latitude), float(longitude)] 翻转为“坐标”:[float(longitude), float(纬度)] 并抛出一些逻辑来捕捉标题......
猜你喜欢
  • 2017-01-31
  • 1970-01-01
  • 2018-03-22
  • 2021-05-29
  • 2015-05-22
  • 1970-01-01
  • 2016-12-25
  • 2018-01-07
  • 2014-04-21
相关资源
最近更新 更多