根据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()