数据栅格化

方形栅格渔网的生成与对应

transbigdata.rect_grids(bounds, accuracy=500)

生成研究范围内的方形栅格

输入

boundsList

生成范围的边界,[lon1,lat1,lon2,lat2] (WGS84坐标系) 其中,lon1,lat1是左下角坐标,lon2,lat2是右上角坐标

accuracynumber

栅格大小(米)

输出

gridGeoDataFrame

栅格的GeoDataFrame,其中LONCOL与LATCOL为栅格的编号,HBLON与HBLAT为栅格的中心点坐标

paramsList

栅格参数(lonStart,latStart,deltaLon,deltaLat),分别为栅格左下角坐标与单个栅格的经纬度长宽

#设定范围
bounds = [lon1,lat1,lon2,lat2]
grid,params = tbd.rect_grids(bounds,accuracy = 500)
transbigdata.grid_params(bounds, accuracy=500)

栅格参数获取

输入

boundsList

生成范围的边界,[lon1,lat1,lon2,lat2] (WGS84坐标系) 其中,lon1,lat1是左下角坐标,lon2,lat2是右上角坐标

accuracynumber

栅格大小(米)

输出

paramsList

栅格参数(lonStart,latStart,deltaLon,deltaLat),分别为栅格左下角坐标与单个栅格的经纬度长宽

bounds = [113.75194,22.447837,114.624187,22.864748]
tbd.grid_params(bounds,accuracy = 500)
transbigdata.grid_params_best(data, col=['lon', 'lat'], accuracy=500, gap=10, sample=10000)

获取最佳的栅格化参数,以基尼系数最大为标准

输入

dataDataFrame

数据

colList

经纬度列

accuracynumber

网格大小

gapnumber

精度,越大越精确,效果越好,计算量越大

samplenumber

抽样多少数据做测试

输出

paramsList

最佳的栅格参数(lonStart,latStart,deltaLon,deltaLat),分别为栅格左下角坐标与单个栅格的经纬度长宽

transbigdata.GPS_to_grids(lon, lat, params)

GPS数据对应栅格编号。输入数据的经纬度列与栅格参数,输出对应的栅格编号

输入

lonSeries

经度列

latSeries

纬度列

paramsList

栅格参数(lonStart,latStart,deltaLon,deltaLat),分别为栅格左下角坐标与单个栅格的经纬度长宽

输出

LONCOLSeries

经度栅格编号列

LATCOLSeries

纬度栅格编号列

data['LONCOL'],data['LATCOL'] = tbd.GPS_to_grids(data['Lng'],data['Lat'],params)
transbigdata.grids_centre(loncol, latcol, params)

栅格编号对应栅格中心点经纬度。输入数据的栅格编号与栅格参数,输出对应的栅格中心点

输入

LONCOLSeries

经度栅格编号列

LATCOLSeries

纬度栅格编号列

paramsList

栅格参数(lonStart,latStart,deltaLon,deltaLat),分别为栅格左下角坐标与单个栅格的经纬度长宽

输出

HBLONSeries

栅格中心点经度列

HBLATSeries

栅格中心点纬度列

data['HBLON'],data['HBLAT'] = tbd.grids_centre(data['LONCOL'],data['LATCOL'],params)
transbigdata.gridid_to_polygon(loncol, latcol, params)

栅格编号生成栅格的地理信息列。输入数据的栅格编号与栅格参数,输出对应的地理信息列

输入

LONCOLSeries

经度栅格编号列

LATCOLSeries

纬度栅格编号列

paramsList

栅格参数(lonStart,latStart,deltaLon,deltaLat),分别为栅格左下角坐标与单个栅格的经纬度长宽

输出

geometrySeries

栅格的矢量图形列

data['geometry'] = tbd.gridid_to_polygon(data['LONCOL'],data['LATCOL'],params)
transbigdata.gridid_sjoin_shape(data, shape, params, col=['LONCOL', 'LATCOL'])

输入数据(带有栅格经纬度编号两列),矢量图形与栅格化参数,输出数据栅格并对应矢量图形。

输入

dataDataFrame

数据,(带有栅格经纬度编号两列)

shapeGeoDataFrame

矢量图形

paramsList

栅格化参数

colList

列名,[经度栅格编号,纬度栅格编号]

输出

data1DataFrame

数据栅格并对应矢量图形

geohash编码

geohash是一种公共域地理编码系统,它的作用是将经纬度地理位置编码为字母和数字组成的字符串,字符串也可解码为经纬度。每个字符串代表一个网格编号,字符串的长度越长则精度越高。根据wiki,geohash字符串长度对应精度表格如下:

geohash length(precision)

lat bits

lng bits

lat error

lng error

km error

1

2

3

±23

±23

±2500

2

5

5

±2.8

±5.6

±630

3

7

8

±0.70

±0.70

±78

4

10

10

±0.087

±0.18

±20

5

12

13

±0.022

±0.022

±2.4

6

15

15

±0.0027

±0.0055

±0.61

7

17

18

±0.00068

±0.00068

±0.076

8

20

20

±0.000085

±0.00017

±0.019

TransBigData包中也提供了geohash的处理功能,主要包括三个函数:

transbigdata.geohash_encode(lon, lat, precision=12)

输入经纬度与精度,输出geohash编码

输入

lonSeries

经度列

latSeries

纬度列

precisionnumber

geohash精度

输出

lonSeries

经度列

latSeries

纬度列

transbigdata.geohash_decode(geohash)

输入经纬度与精度,输出geohash编码

输入

geohashSeries

geohash编码列

输出

geohashSeries

geohash编码列

transbigdata.geohash_togrid(geohash)

输入geohash编码,输出geohash网格的地理信息图形Series列

输入

geohashSeries

geohash编码列

输出

polySeries

geohash的栅格列

相比TransBigData包中提供的方形栅格处理方法,geohash更慢,也无法提供自由定义的栅格大小。下面的示例展示如何利用这三个函数对数据进行geohash编码集计,并可视化

import transbigdata as tbd
import pandas as pd
import geopandas as gpd
#读取数据
data = pd.read_csv('TaxiData-Sample.csv',header = None)
data.columns = ['VehicleNum','time','slon','slat','OpenStatus','Speed']
#依据经纬度geohash编码,精确度选6时,栅格大小约为±0.61km
data['geohash'] = tbd.geohash_encode(data['slon'],data['slat'],precision=6)
data['geohash']
0         ws0btw
1         ws0btz
2         ws0btz
3         ws0btz
4         ws0by4
           ...
544994    ws131q
544995    ws1313
544996    ws131f
544997    ws1361
544998    ws10tq
Name: geohash, Length: 544999, dtype: object
#基于geohash编码集计
dataagg = data.groupby(['geohash'])['VehicleNum'].count().reset_index()
#geohash编码解码为经纬度
dataagg['lon_geohash'],dataagg['lat_geohash'] = tbd.geohash_decode(dataagg['geohash'])
#geohash编码生成栅格矢量图形
dataagg['geometry'] = tbd.geohash_togrid(dataagg['geohash'])
#转换为GeoDataFrame
dataagg = gpd.GeoDataFrame(dataagg)
dataagg
geohash VehicleNum lon_geohash lat_geohash geometry
0 w3uf3x 1 108. 10.28 POLYGON ((107.99561 10.27771, 107.99561 10.283...
1 webzz6 12 113.9 22.47 POLYGON ((113.87329 22.46704, 113.87329 22.472...
2 webzz7 21 113.9 22.48 POLYGON ((113.87329 22.47253, 113.87329 22.478...
3 webzzd 1 113.9 22.47 POLYGON ((113.88428 22.46704, 113.88428 22.472...
4 webzzf 2 113.9 22.47 POLYGON ((113.89526 22.46704, 113.89526 22.472...
... ... ... ... ... ...
2022 ws1d9u 1 114.7 22.96 POLYGON ((114.68628 22.96143, 114.68628 22.966...
2023 ws1ddh 6 114.7 22.96 POLYGON ((114.69727 22.96143, 114.69727 22.966...
2024 ws1ddj 2 114.7 22.97 POLYGON ((114.69727 22.96692, 114.69727 22.972...
2025 ws1ddm 4 114.7 22.97 POLYGON ((114.70825 22.96692, 114.70825 22.972...
2026 ws1ddq 7 114.7 22.98 POLYGON ((114.70825 22.97241, 114.70825 22.977...

2027 rows × 5 columns

#设定绘图边界
bounds = [113.6,22.4,114.8,22.9]
#创建图框
import matplotlib.pyplot as plt
import plot_map
fig =plt.figure(1,(8,8),dpi=280)
ax =plt.subplot(111)
plt.sca(ax)
#添加地图底图
tbd.plot_map(plt,bounds,zoom = 12,style = 4)
#绘制colorbar
cax = plt.axes([0.05, 0.33, 0.02, 0.3])
plt.title('count')
plt.sca(ax)
#绘制geohash的栅格集计
dataagg.plot(ax = ax,column = 'VehicleNum',cax = cax,legend = True)
#添加比例尺和指北针
tbd.plotscale(ax,bounds = bounds,textsize = 10,compasssize = 1,accuracy = 2000,rect = [0.06,0.03],zorder = 10)
plt.axis('off')
plt.xlim(bounds[0],bounds[2])
plt.ylim(bounds[1],bounds[3])
plt.show()
_images/output_9_0.png

六边形渔网生成

transbigdata.hexagon_grids(bounds, accuracy=500)

生成研究范围内的六边形渔网。

输入

boundsList

生成范围的边界,[lon1,lat1,lon2,lat2] (WGS84坐标系) 其中,lon1,lat1是左下角坐标,lon2,lat2是右上角坐标

accuracynumber

六边形的边长(米)

输出

hexagonGeoDataFrame

六边形渔网的矢量图形

#设定范围
bounds = [113.6,22.4,114.8,22.9]
hexagon = tbd.hexagon_grids(bounds,accuracy = 5000)
hexagon.plot()
_images/WX20211021-201747@2x.png