首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写Python循环将大量坐标转换为GeoJSON LineString格式?

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

Stack Overflow用户
提问于 2016-02-19 21:38:29
回答 1查看 793关注 0票数 0

我使用pandas DataFrame使用multi-index重构了数据集,现在该数据集的格式如下。

代码语言:javascript
复制
In [1]: df.head(12)
Out [1]:

为了将其转换成GeoJSON LineString格式并在地图上可视化,我需要在每一点和每一行上,通过数百万个卫星观测点,编写一个Python loop。为了参考,下面的示例指定了一个GeoJSON LineString

代码语言:javascript
复制
{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }

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

我很困惑如何编写一个Python loop,它可以帮助我通过使用multi-index将坐标转换成GeoJSON LineString类型。

代码语言:javascript
复制
In [2]: df.Longitude[1][4]
Out [2]: 128

耽误您时间,实在对不起!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-20 00:07:44

groupbyto_json的结合似乎运行得很好。

代码语言:javascript
复制
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)

产出:

代码语言:javascript
复制
{'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'}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35515539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档