我使用pandas DataFrame使用multi-index重构了数据集,现在该数据集的格式如下。
In [1]: df.head(12)
Out [1]:

为了将其转换成GeoJSON LineString格式并在地图上可视化,我需要在每一点和每一行上,通过数百万个卫星观测点,编写一个Python loop。为了参考,下面的示例指定了一个GeoJSON LineString。
{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }但是,并不总是如图中所示,一条线由前三行的4个点组成,该数据集中的一条特定行的点数完全是随机的,从4到数百。
我很困惑如何编写一个Python loop,它可以帮助我通过使用multi-index将坐标转换成GeoJSON LineString类型。
In [2]: df.Longitude[1][4]
Out [2]: 128耽误您时间,实在对不起!
发布于 2016-02-20 00:07:44
groupby和to_json的结合似乎运行得很好。
import pandas as pd
import numpy as np
import pprint
arrays = [np.array([1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4]),
np.array([1, 2, 3, 1, 2, 1, 2, 3, 4, 5, 1, 2])]
df = pd.DataFrame(np.arange(24).reshape(12,2),
index=arrays, columns=['Longitude', 'Lattitude'])
dd = {"type":"Feature",
"geometry":{"type":"Linestring",
"coordinates":None
},
"properties":{"prop0":'red',
"prop1":'dashed'
}
}
for _, group in df.groupby(level=0):
dd["geometry"]["coordinates"] = group.to_json(orient='values')
pprint.pprint(dd)产出:
{'geometry': {'coordinates': '[[0,1],[2,3],[4,5]]',
'type': 'Linestring'},
'properties': {'prop0': 'red',
'prop1': 'dashed'},
'type': 'Feature'}
{'geometry': {'coordinates': '[[6,7],[8,9]]',
'type': 'Linestring'},
'properties': {'prop0': 'red',
'prop1': 'dashed'},
'type': 'Feature'}
{'geometry': {'coordinates': '[[10,11],[12,13],[14,15],[16,17],[18,19]]',
'type': 'Linestring'},
'properties': {'prop0': 'red',
'prop1': 'dashed'},
'type': 'Feature'}
{'geometry': {'coordinates': '[[20,21],[22,23]]',
'type': 'Linestring'},
'properties': {'prop0': 'red',
'prop1': 'dashed'},
'type': 'Feature'}https://stackoverflow.com/questions/35515539
复制相似问题