geoHash的原理不多叙述。这一流程的难点在于在热力图上找到正确的矩形中心点。
从geoHash的编码值获得中心点坐标(转载自https://blog.csdn.net/qq1195365047/article/details/93720533):
__all__ = ['encode','decode','bbox','neighbors']
_base32 = '0123456789bcdefghjkmnpqrstuvwxyz'
#10进制和32进制转换,32进制去掉了ailo
_decode_map = {}
_encode_map = {}
for i in range(len(_base32)):
_decode_map[_base32[i]] = i
_encode_map[i]=_base32[i]
del i
def bbox(geohash):
lat_range, lon_range = [-90.0, 90.0], [-180.0, 180.0]
is_lon=True
for letter in geohash:
code=str(bin(_decode_map[letter]))[2:].rjust(5,'0')
for bi in code:
if is_lon and bi=='0':
lon_range[1]=sum(lon_range)/2
elif is_lon and bi=='1':
lon_range[0]=sum(lon_range)/2
elif (not is_lon) and bi=='0':
lat_range[1]=sum(lat_range)/2
elif (not is_lon) and bi=='1':
lat_range[0]=sum(lat_range)/2
is_lon=not is_lon
#左上、右下;(lat_max,lon_min),(lat_min,lon_max)
return [(lat_range[1],lon_range[0]),(lat_range[0],lon_range[1])]
如:
bbox('wqve') ,可以得到 [(38.671875, 108.984375), (38.49609375, 109.3359375)]
如果使用folium的rectangle绘制,可以得到矩形区域
for k,v in gh_ratio2.items():
bounds = gh_codes_bds[k]
m.add_child(folium.Rectangle(
bounds=bounds, # 坐标列表, Latitude and Longitude of line (Northing, Easting)
weight=2, # 线条宽度
tooltip = str(v),
color='blue'))
之后可以求出每个rectangle的中心点坐标,画图就是
from folium.plugins import HeatMap
def get_bounds_center(bounds):
u1 = bounds[0]
u2 = bounds[1]
lat_center = (u1[0] + u2[0]) / 2
lng_center = (u1[1] + u2[1]) / 2
return [lat_center, lng_center]
# for k,v in u2_gh_count.items():
for k,v in gh_ratio2.items():
bounds = gh_codes_bds[k]
center = get_bounds_center(bounds)
res = [center[0], center[1], v/2]
heatMapDatas.append(res)
HeatMap(heatMapDatas).add_to(m)