在 Python 中将 csv 转换为 geojson
Converting csv to geojson in Python
我正在尝试将文件转换为 python 中的 Geojson,并且我有以下代码。我尝试在代码中添加行索引,但错误仍然存在。
import csv
import json
from collections import OrderedDict
li = []
with open('sample.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for lat,long in reader:
d = OrderedDict()
d['type'] = 'Feature'
d['geometry'] = {
'type': 'Point',
'coordinates': [float(lat), float(long)]
}
li.append(d)
d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
我遇到了以下错误,
"too many values to unpack (expected 2)"
示例数据
event_id time_0 time_1 signal description signal_name_value_desc product long lat
a 6/30/2018 18:39 6/30/2018 18:39 1 description1 signal_name1 product-1 -84.52694 46.931625
a 6/30/2018 18:39 6/30/2018 18:39 1 description1 signal_name1 Product - 1 -84.52684 46.931725
a 10/15/2018 21:10 10/15/2018 21:11 1 description1 signal_name1 Product - 2 -94.25526333 42.71689167
a 10/15/2018 21:10 10/15/2018 21:11 1 description1 signal_name1 Product - 2 -94.25524667 42.71689333
a 10/15/2018 21:10 10/15/2018 21:11 1 description1 signal_name1 Product - 2 -94.25519167 42.716895
b 10/15/2018 21:10 10/15/2018 21:11 1 description1 signal_name1 Product - 2 -94.25505167 42.71690833
b 10/15/2018 21:12 10/15/2018 21:13 1 description1 signal_name1 Product - 2 -94.25531167 42.71687167
b 10/15/2018 21:12 10/15/2018 21:13 1 description1 signal_name1
这是我期待的输出
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -84.52694,46.931625 ]
},
"properties": {
"event_id":"a",
"time_0":"6/30/2018 18:39",
"time_1":"6/30/2018 18:39",
"signal":"1",
"description":"description1",
"signal_name_value_desc":"signal_name1",
"product":"product-1",
}
}
如何将 csv 转换为 GeoJson。提前致谢
既然你说你的 .CSV 文件中有 13 列,问题就在这里:
for lat,long in reader:
该行需要 reader
行有两列。示例:
>>> lat,long = 1,2,3,4,5,6,7,8,9,10,11,12,13
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
您可以为所有 13 列指定变量,或使用此语法:
>>> lat,long,*the_rest = 1,2,3,4,5,6,7,8,9,10,11,12,13
>>> lat
1
>>> long
2
>>> the_rest
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
您也可以只使用以下内容并为所需的列编制索引:
for row in reader:
lat = row[0] # or whatever the columns really are
long = row[1]
如果您的数据具有 headers,如您所示,这里是使用 DictReader
按名称引用列的示例。
给定 input.csv
:
event_id,time_0,time_1,signal,description,signal_name_value_desc,product,long,lat
a,6/30/2018 18:39,6/30/2018 18:39,1,description1,signal_name1,product-1,-84.52694,46.931625
此代码:
import csv
import json
from collections import OrderedDict
li = []
with open('input.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
d = OrderedDict()
d['type'] = 'Feature'
d['geometry'] = {
'type': 'Point',
'coordinates': [float(row['lat']), float(row['long'])]
}
li.append(d)
d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
with open('output.json','w') as f:
json.dump(d,f,indent=2)
产生 output.json
:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
46.931625,
-84.52694
]
}
}
]
}
我正在尝试将文件转换为 python 中的 Geojson,并且我有以下代码。我尝试在代码中添加行索引,但错误仍然存在。
import csv
import json
from collections import OrderedDict
li = []
with open('sample.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for lat,long in reader:
d = OrderedDict()
d['type'] = 'Feature'
d['geometry'] = {
'type': 'Point',
'coordinates': [float(lat), float(long)]
}
li.append(d)
d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
我遇到了以下错误,
"too many values to unpack (expected 2)"
示例数据
event_id time_0 time_1 signal description signal_name_value_desc product long lat
a 6/30/2018 18:39 6/30/2018 18:39 1 description1 signal_name1 product-1 -84.52694 46.931625
a 6/30/2018 18:39 6/30/2018 18:39 1 description1 signal_name1 Product - 1 -84.52684 46.931725
a 10/15/2018 21:10 10/15/2018 21:11 1 description1 signal_name1 Product - 2 -94.25526333 42.71689167
a 10/15/2018 21:10 10/15/2018 21:11 1 description1 signal_name1 Product - 2 -94.25524667 42.71689333
a 10/15/2018 21:10 10/15/2018 21:11 1 description1 signal_name1 Product - 2 -94.25519167 42.716895
b 10/15/2018 21:10 10/15/2018 21:11 1 description1 signal_name1 Product - 2 -94.25505167 42.71690833
b 10/15/2018 21:12 10/15/2018 21:13 1 description1 signal_name1 Product - 2 -94.25531167 42.71687167
b 10/15/2018 21:12 10/15/2018 21:13 1 description1 signal_name1
这是我期待的输出
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -84.52694,46.931625 ]
},
"properties": {
"event_id":"a",
"time_0":"6/30/2018 18:39",
"time_1":"6/30/2018 18:39",
"signal":"1",
"description":"description1",
"signal_name_value_desc":"signal_name1",
"product":"product-1",
}
}
如何将 csv 转换为 GeoJson。提前致谢
既然你说你的 .CSV 文件中有 13 列,问题就在这里:
for lat,long in reader:
该行需要 reader
行有两列。示例:
>>> lat,long = 1,2,3,4,5,6,7,8,9,10,11,12,13
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
您可以为所有 13 列指定变量,或使用此语法:
>>> lat,long,*the_rest = 1,2,3,4,5,6,7,8,9,10,11,12,13
>>> lat
1
>>> long
2
>>> the_rest
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
您也可以只使用以下内容并为所需的列编制索引:
for row in reader:
lat = row[0] # or whatever the columns really are
long = row[1]
如果您的数据具有 headers,如您所示,这里是使用 DictReader
按名称引用列的示例。
给定 input.csv
:
event_id,time_0,time_1,signal,description,signal_name_value_desc,product,long,lat
a,6/30/2018 18:39,6/30/2018 18:39,1,description1,signal_name1,product-1,-84.52694,46.931625
此代码:
import csv
import json
from collections import OrderedDict
li = []
with open('input.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
d = OrderedDict()
d['type'] = 'Feature'
d['geometry'] = {
'type': 'Point',
'coordinates': [float(row['lat']), float(row['long'])]
}
li.append(d)
d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
with open('output.json','w') as f:
json.dump(d,f,indent=2)
产生 output.json
:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
46.931625,
-84.52694
]
}
}
]
}