如何编写Python循环将大量坐标转换为GeoJSON LineString格式?

2024-06-16 14:57:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用multi-indexpandasDataFrame中重建了我的数据集,现在它的格式如下。在

In [1]: df.head(12)
Out [1]:

enter image description here

为了把它变成GeoJSONLineString的格式并在地图上可视化,我需要在数百万个卫星观测点的每一点和每一条线上写一个Pythonloop。作为参考,下面的示例指定了一个GeoJSONLineString。在

^{pr2}$

然而,并不总是如图中所示,一条直线的前三条直线由4个点组成,该数据集中特定直线的点数是完全随机的,从4到数百。在

我很困惑如何编写一个Pythonloop,它可以帮助我通过使用multi-index将坐标放入GeoJSONLineString类型,例如

In [2]: df.Longitude[1][4]
Out [2]: 128

谢谢你的时间!在


Tags: 数据indfindex可视化格式地图out
1条回答
网友
1楼 · 发布于 2024-06-16 14:57:28

groupby和{}的组合似乎工作得很好。在

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)

输出:

^{pr2}$

相关问题 更多 >