5-6位Geohash图示例
import plotly
import chart_studio. plotly as pyf
import plotly. graph_objs as go
import numpy as np
import pandas as pd
import math
from matplotlib. path import Path
import numpy as np
import plotly. offline as of
import plotly. graph_objs as go
import plotly. plotly as py
import numpy as np
import pandas as pd
import folium
import webbrowser
from folium. plugins import HeatMap
import datetime
import time
import pymysql. cursors
import decimal
import geohash
import geohash
def geo_demand ( geo_top_geo) :
conn = pymysql. connect(
host= 'localhost' ,
port= 3306 ,
user= 'root' ,
passwd= 'xu19931026' ,
db= 'hk_taxi' ,
charset= 'utf8'
)
if conn:
print ( "连接成功!" )
cursor = conn. cursor( )
sql = "SELECT Geohash,number,CAST(number/(SELECT count( 0 ) FROM haikou_1 where UNIX_TIMESTAMP(departure_time)<1498867200 AND UNIX_TIMESTAMP(departure_time)>1496246400 and left(`geo`,5)=%s) as CHAR(15))as demand FROM (select left(`geo`,6) AS Geohash,count(0) AS number from haikou_1 where UNIX_TIMESTAMP(departure_time)<1498867200 AND UNIX_TIMESTAMP(departure_time)>1496246400 and left(`geo`,5)=%s GROUP BY Geohash ORDER BY number desc) as T"
cursor. execute( sql, ( geo_top_geo, geo_top_geo) )
conn. commit( )
result = cursor. fetchall( )
df = list ( result)
conn. close( )
print ( '数据库处理数据完毕' )
geo_dict = { }
geo_list = [ ]
order_number = [ ]
demand = [ ]
for point in df:
geo_list. append( ( point[ 0 ] ) )
order_number. append( point[ 1 ] )
demand. append( float ( point[ 2 ] ) )
num = len ( geo_list)
geo_data = [ [ geo_list[ i] , demand[ i] ] for i in range ( num) ]
geo_nb = [ [ geo_list[ i] , order_number[ i] ] for i in range ( num) ]
return geo_data, geo_nb
def get_geohash ( lon, lat) :
geo = geohash. encode( lat, lon)
return geo
def jie_geohash ( a) :
b= [ ]
b= geohash. decode( a)
return b
def geo_paint ( central_points, dict ) :
lat1= [ ]
lon1= [ ]
lat1. append( central_points[ 0 ] )
lon1. append( central_points[ 1 ] )
datas = [
go. Scattermapbox(
lat= lat1,
lon= lon1,
text= 'center' ,
mode= 'markers' ,
marker= go. scattermapbox. Marker(
size= 10 ,
color= '#ff2d51' ,
opacity= 0.9
)
)
]
for key in dict . keys( ) :
borders = dict [ key]
lons = [ ]
lats = [ ]
for border in borders:
lons. append( border[ 0 ] )
lats. append( border[ 1 ] )
lons1 = list ( set ( lons) )
lats1 = list ( set ( lats) )
lats1. sort( )
lons1. sort( )
lons = [ lons1[ 0 ] , lons1[ 0 ] , lons1[ 1 ] , lons1[ 1 ] , lons1[ 0 ] ]
lats = [ lats1[ 0 ] , lats1[ 1 ] , lats1[ 1 ] , lats1[ 0 ] , lats1[ 0 ] ]
datas. append(
go. Scattermapbox(
lat= lats,
lon= lons,
mode= 'markers+lines' ,
marker= go. scattermapbox. Marker(
size= 10 ,
color= 'black' ,
opacity= 0.8
)
)
)
return datas
def tianjiahuatu7 ( j, datas, dict , new_central_point_text) :
lat1 = [ ]
lon1 = [ ]
lat1. append( j[ 0 ] )
lon1. append( j[ 1 ] )
datas. append(
go. Scattermapbox(
lat= lat1,
lon= lon1,
text= new_central_point_text,
mode= 'markers' ,
marker= go. scattermapbox. Marker(
size= 5 ,
color= '#cca4e3' ,
opacity= 0.9
)
)
)
for key in dict . keys( ) :
borders = dict [ key]
lons = [ ]
lats = [ ]
for border in borders:
lons. append( border[ 0 ] )
lats. append( border[ 1 ] )
lons1 = list ( set ( lons) )
lats1 = list ( set ( lats) )
lats1. sort( )
lons1. sort( )
lons = [ lons1[ 0 ] , lons1[ 0 ] , lons1[ 1 ] , lons1[ 1 ] , lons1[ 0 ] ]
lats = [ lats1[ 0 ] , lats1[ 1 ] , lats1[ 1 ] , lats1[ 0 ] , lats1[ 0 ] ]
datas. append(
go. Scattermapbox(
lat= lats,
lon= lons,
mode= 'markers+lines' ,
fillcolor= '#fffbf0' ,
marker= go. scattermapbox. Marker(
size= 2 ,
color= 'blue' ,
opacity= 0.5
)
)
)
return datas
def tianjiahuatu ( j, datas, dict , new_central_point_text) :
lat1 = [ ]
lon1 = [ ]
lat1. append( j[ 0 ] )
lon1. append( j[ 1 ] )
datas. append(
go. Scattermapbox(
lat= lat1,
lon= lon1,
text= new_central_point_text,
mode= 'markers' ,
marker= go. scattermapbox. Marker(
size= 5 ,
color= '#cca4e3' ,
opacity= 0.9
)
)
)
for key in dict . keys( ) :
borders = dict [ key]
lons = [ ]
lats = [ ]
for border in borders:
lons. append( border[ 1 ] )
lats. append( border[ 0 ] )
lons1 = list ( set ( lons) )
lats1 = list ( set ( lats) )
lats1. sort( )
lons1. sort( )
lons = [ lons1[ 0 ] , lons1[ 0 ] , lons1[ 1 ] , lons1[ 1 ] , lons1[ 0 ] ]
lats = [ lats1[ 0 ] , lats1[ 1 ] , lats1[ 1 ] , lats1[ 0 ] , lats1[ 0 ] ]
datas. append(
go. Scattermapbox(
lat= lats,
lon= lons,
mode= 'markers+lines' ,
fillcolor= '#fffbf0' ,
text= '' ,
marker= go. scattermapbox. Marker(
size= 2 ,
color= 'blue' ,
opacity= 0.5
)
)
)
return datas
def get_dict ( central_points, lats_jia, lons_jia) :
point_dict = { }
border_points = [ ]
lons = [ central_points[ 1 ] + lons_jia, central_points[ 1 ] - lons_jia]
lats = [ central_points[ 0 ] + lats_jia, central_points[ 0 ] - lats_jia]
for lon in lons:
for lat in lats:
border_points. append( [ lon, lat] )
point_dict[ str ( central_points) ] = border_points
return point_dict
def jieqiweigeo ( geo_data, datas, lats_jia, lons_jia) :
new_central_point = [ ]
new_central_point_text= [ ]
for i in geo_data:
new_central_point. append( list ( jie_geohash( i[ 0 ] ) ) )
a= str ( i[ 0 ] )
new_central_point_text. append( a)
k= 0
for j in new_central_point:
center_dic = get_dict( j, lats_jia, lons_jia)
new_datas= tianjiahuatu7( j, datas, center_dic, new_central_point_text[ k] )
k+= 1
return new_datas
if __name__ == '__main__' :
taxi_geo1 = 'w7w3y2cjsg98'
taxi_geo = taxi_geo1[ 0 : 5 ]
central_points = list ( jie_geohash( taxi_geo) )
lats_jia6= 0.0027
lons_jia6 = 0.0055
lats_jia5 = 0.022
lons_jia5 = 0.022
point_dict = get_dict( central_points, lats_jia5, lons_jia5)
datas= geo_paint( central_points, point_dict)
geo_data, geo_nb= geo_demand( taxi_geo)
new_datas = jieqiweigeo( geo_data, datas, lats_jia6, lons_jia6)
mapbox_access_token = '''pk.eyJ1IjoiemhhbmdwZW5neHUiLCJhIjoiY2s1ZGwxbXpzMHhjdTNlbnFteTdlcXk5eiJ9.vzYUBRLW8vw7L_H9ybiJYg'''
layout = go. Layout(
title= "chengdu" ,
autosize= True ,
hovermode= 'closest' ,
showlegend= False ,
mapbox= go. layout. Mapbox(
accesstoken= mapbox_access_token,
bearing= 0 ,
center= go. layout. mapbox. Center(
lat= 20.02 ,
lon= 110.32
) ,
pitch= 0 ,
zoom= 10 ,
style= 'light'
) ,
)
fig = go. Figure( data= new_datas, layout= layout)
of. plot( fig, filename= 'haikou_geo.html' )