GeoPands学习之--构造GeoDataFrame

 

根据python的对象数组,构造DataFrame,然后构造GeoDataFrame

原始的数组字段数据类型结构

转换为DataFrame

geofenceDf = pandas.DataFrame(geofence)

 

构造geometry列

geometryList=[]
for i in range(0, len(geofenceDf)):
    if geofenceDf.iloc[i]['geometry_type']=='POLYGON':
        geofencePolygon = Polygon(geofenceDf.iloc[i]['geometry']['rings'][0])
        geometryList.append(geofencePolygon)
    if geofenceDf.iloc[i]['geometry_type']=='POLYLINE':
        geofencePolyline = LineString(geofenceDf.iloc[i]['geometry']['paths'][0]).buffer(1)
        geometryList.append(geofencePolyline)
    if geofenceDf.iloc[i]['geometry_type'] == 'POINT':
        geofencePoint = Point(geofenceDf.iloc[i]['geometry']['x'],geofenceDf.iloc[i]['geometry']['y']).buffer(1)
        geometryList.append(geofencePoint)

 

构建GeoDataFrame

gdf = geopandas.GeoDataFrame(geofenceDf, geometry=geometryList)

 

完整代码如下:

import pandas
import geopandas
from shapely.geometry import Polygon,Point,LineString
geofence=[
    {'id': '1', 'geometry_type': 'POLYGON', 'geometry':{'rings' : [
          [
            [
              12743260.674682617,
              3582656.0642700195
            ],
            [
              12743258.209472656,
              3582653.5993041992
            ],
            [
              12743255.35369873,
              3582651.5996704102
            ],
            [
              12743252.194091797,
              3582650.1262817383
            ],
            [
              12743248.826721191,
              3582649.2238769531
            ],
            [
              12743245.202697754,
              3582648.9213256836
            ],
            [
              12742829.075927734,
              3582655.205078125
            ],
            [
              12742705.611083984,
              3582655.6840820313
            ],
            [
              12742662.495483398,
              3582653.2354736328
            ],
            [
              12742199.483093262,
              3582546.3685302734
            ],
            [
              12742198.458129883,
              3582546.1599121094
            ],
            [
              12742194.985107422,
              3582545.8560791016
            ],
            [
              12742191.512084961,
              3582546.1599121094
            ],
            [
              12742188.144714355,
              3582547.0623168945
            ],
            [
              12742184.985107422,
              3582548.5357055664
            ],
            [
              12742182.129272461,
              3582550.5352783203
            ],
            [
              12742179.664306641,
              3582553.0003051758
            ],
            [
              12742177.664672852,
              3582555.8560791016
            ],
            [
              12742176.19128418,
              3582559.0156860352
            ],
            [
              12742175.288879395,
              3582562.3831176758
            ],
            [
              12742174.985107422,
              3582565.8560791016
            ],
            [
              12742175.288879395,
              3582569.3291015625
            ],
            [
              12742176.19128418,
              3582572.696472168
            ],
            [
              12742177.664672852,
              3582575.8560791016
            ],
            [
              12742179.664306641,
              3582578.7119140625
            ],
            [
              12742182.129272461,
              3582581.1768798828
            ],
            [
              12742184.985107422,
              3582583.1766967773
            ],
            [
              12742188.144714355,
              3582584.6499023438
            ],
            [
              12742190.487304687,
              3582585.3436889648
            ],
            [
              12742655.161315918,
              3582692.5942993164
            ],
            [
              12742656.186279297,
              3582692.8026733398
            ],
            [
              12742658.525085449,
              3582693.0742797852
            ],
            [
              12742703.947692871,
              3582695.6541137695
            ],
            [
              12742705.120727539,
              3582695.6862792969
            ],
            [
              12742829.467102051,
              3582695.2033081055
            ],
            [
              12743245.655883789,
              3582688.9179077148
            ],
            [
              12743248.826721191,
              3582688.6162719727
            ],
            [
              12743252.194091797,
              3582687.7139282227
            ],
            [
              12743255.35369873,
              3582686.2407226563
            ],
            [
              12743258.209472656,
              3582684.2410888672
            ],
            [
              12743260.674682617,
              3582681.7758789063
            ],
            [
              12743262.674316406,
              3582678.9201049805
            ],
            [
              12743264.147521973,
              3582675.7604980469
            ],
            [
              12743265.049926758,
              3582672.3931274414
            ],
            [
              12743265.35369873,
              3582668.9201049805
            ],
            [
              12743265.049926758,
              3582665.4470825195
            ],
            [
              12743264.147521973,
              3582662.0797119141
            ],
            [
              12743262.674316406,
              3582658.9201049805
            ],
            [
              12743260.674682617,
              3582656.0642700195
            ]
          ]
        ]}},
    {'id': '2', 'geometry_type': 'POLYLINE', 'geometry': {'paths': [
        [
            [
                12743260.674682617,
                3582656.0642700195
            ],
            [
                12743258.209472656,
                3582653.5993041992
            ],
            [
                12743255.35369873,
                3582651.5996704102
            ],
            [
                12743252.194091797,
                3582650.1262817383
            ],
            [
                12743248.826721191,
                3582649.2238769531
            ],
            [
                12743245.202697754,
                3582648.9213256836
            ],
            [
                12742829.075927734,
                3582655.205078125
            ],
            [
                12742705.611083984,
                3582655.6840820313
            ],
            [
                12742662.495483398,
                3582653.2354736328
            ],
            [
                12742199.483093262,
                3582546.3685302734
            ],
            [
                12742198.458129883,
                3582546.1599121094
            ],
            [
                12742194.985107422,
                3582545.8560791016
            ],
            [
                12742191.512084961,
                3582546.1599121094
            ],
            [
                12742188.144714355,
                3582547.0623168945
            ],
            [
                12742184.985107422,
                3582548.5357055664
            ],
            [
                12742182.129272461,
                3582550.5352783203
            ],
            [
                12742179.664306641,
                3582553.0003051758
            ],
            [
                12742177.664672852,
                3582555.8560791016
            ],
            [
                12742176.19128418,
                3582559.0156860352
            ],
            [
                12742175.288879395,
                3582562.3831176758
            ],
            [
                12742174.985107422,
                3582565.8560791016
            ],
            [
                12742175.288879395,
                3582569.3291015625
            ],
            [
                12742176.19128418,
                3582572.696472168
            ],
            [
                12742177.664672852,
                3582575.8560791016
            ],
            [
                12742179.664306641,
                3582578.7119140625
            ],
            [
                12742182.129272461,
                3582581.1768798828
            ],
            [
                12742184.985107422,
                3582583.1766967773
            ],
            [
                12742188.144714355,
                3582584.6499023438
            ],
            [
                12742190.487304687,
                3582585.3436889648
            ],
            [
                12742655.161315918,
                3582692.5942993164
            ],
            [
                12742656.186279297,
                3582692.8026733398
            ],
            [
                12742658.525085449,
                3582693.0742797852
            ],
            [
                12742703.947692871,
                3582695.6541137695
            ],
            [
                12742705.120727539,
                3582695.6862792969
            ],
            [
                12742829.467102051,
                3582695.2033081055
            ],
            [
                12743245.655883789,
                3582688.9179077148
            ],
            [
                12743248.826721191,
                3582688.6162719727
            ],
            [
                12743252.194091797,
                3582687.7139282227
            ],
            [
                12743255.35369873,
                3582686.2407226563
            ],
            [
                12743258.209472656,
                3582684.2410888672
            ],
            [
                12743260.674682617,
                3582681.7758789063
            ],
            [
                12743262.674316406,
                3582678.9201049805
            ],
            [
                12743264.147521973,
                3582675.7604980469
            ],
            [
                12743265.049926758,
                3582672.3931274414
            ],
            [
                12743265.35369873,
                3582668.9201049805
            ],
            [
                12743265.049926758,
                3582665.4470825195
            ],
            [
                12743264.147521973,
                3582662.0797119141
            ],
            [
                12743262.674316406,
                3582658.9201049805
            ],
            [
                12743260.674682617,
                3582656.0642700195
            ]
        ]
    ]}},
    {'id': '3', 'geometry_type': 'POINT', 'geometry': {'x':12740442.57,'y':3582111.135}},
    {'id': '4', 'geometry_type': 'POLYLINE', 'geometry':{"paths" : [
        [
            [
              12743045.955505371,
              3581102.0302734375
            ],
            [
              12743042.48248291,
              3581101.7265014648
            ],
            [
              12743039.009521484,
              3581102.0302734375
            ],
            [
              12743035.642089844,
              3581102.9326782227
            ],
            [
              12743032.48248291,
              3581104.4061279297
            ],
            [
              12743029.626708984,
              3581106.4057006836
            ],
            [
              12743027.161682129,
              3581108.8707275391
            ],
            [
              12743025.162109375,
              3581111.7265014648
            ],
            [
              12743023.688720703,
              3581114.8861083984
            ],
            [
              12743023.030273437,
              3581117.0775146484
            ],
            [
              12742984.541503906,
              3581278.1188964844
            ]]]}},
]
a=[[0, 0], [1, 0], [1, 1]]
p1 = Polygon(a)
b=[(0, 0), (1, 0), (1, 1)]
p2=LineString(b)
print p1
geofenceDf = pandas.DataFrame(geofence)
print geofenceDf.iloc[3]['geometry']['paths'][0]
geometryList=[]
for i in range(0, len(geofenceDf)):
    if geofenceDf.iloc[i]['geometry_type']=='POLYGON':
        geofencePolygon = Polygon(geofenceDf.iloc[i]['geometry']['rings'][0])
        geometryList.append(geofencePolygon)
    if geofenceDf.iloc[i]['geometry_type']=='POLYLINE':
        geofencePolyline = LineString(geofenceDf.iloc[i]['geometry']['paths'][0]).buffer(1)
        geometryList.append(geofencePolyline)
    if geofenceDf.iloc[i]['geometry_type'] == 'POINT':
        geofencePoint = Point(geofenceDf.iloc[i]['geometry']['x'],geofenceDf.iloc[i]['geometry']['y']).buffer(1)
        geometryList.append(geofencePoint)

gdf = geopandas.GeoDataFrame(geofenceDf, geometry=geometryList)

print geofenceDf.head()

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
geopandas 是一个基于 pandas 库的地理空间数据处理库。安装 geopandas 可以在你的 Python 环境中轻松地处理和绘制地理空间数据。 要在你的 Python 环境中安装 geopandas,你需要使用 pip(Python 包管理工具)来执行安装。首先,请确保你的电脑上已经正确地安装了 Python。 打开终端或命令提示符,并输入以下命令来安装 geopandas: ``` pip install geopandas ``` 这将从 Python Package Index(PyPI)下载并安装最新版本的 geopandas。 安装过程可能需要一些时间,这取决于你的网络连接速度和系统性能。 在安装完成后,你就可以在你的 Python 程序中导入 geopandas 并开始使用它来处理地理空间数据了。 如果你在安装过程中遇到任何错误或困难,可以尝试以下解决方法: 1. 如果你使用的是 Windows 操作系统,确保已正确安装了 geopandas 运行所需的依赖库,例如 GDAL、Fiona 和 PyProj。可以从官方网站下载这些库的二进制安装包,并手动安装。 2. 如果你使用的是 macOS 或 Linux 操作系统,可能需要安装一些依赖库,例如 libgdal-dev、libproj-dev 和 python3-dev。可以使用系统包管理器(例如 apt 或 brew)来安装这些库。 除了使用 pip 安装,你还可以尝试使用 Anaconda(一个 Python 数据科学平台)来安装 geopandas。在 Anaconda 环境下,默认已经配置好了许多常用的地理空间库,包括 geopandas。你可以通过以下命令来安装 Anaconda,并在环境中使用 geopandas: ``` conda install -c conda-forge geopandas ``` 希望这些信息对你有帮助,祝你成功安装和使用 geopandas!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为了淑宝努力努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值