我是靠谱客的博主 高大哈密瓜,最近开发中收集的这篇文章主要介绍5-6位Geohash图示例,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

5-6位Geohash图示例

#可以显示乘客的定位及其对应的六位和七位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):  #按照时间获取每个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]))  # 将字符串的经纬度转换为float格式
        order_number.append(point[1])  # 50610之类的本身就是int
        demand.append(float(point[2]))  # Decimal转为float
    num = len(geo_list)  # num为数组大小
    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 #返回Geohash对应的订单量与订单量百分比即demand
def get_geohash(lon, lat):
    #生成Geohash
    geo = geohash.encode(lat, lon)  # precision=9可以加精度
    return geo
def jie_geohash(a): #解码a
    #生成Geohash
    b=[]
    b=geohash.decode(a) # precision=9可以加精度
    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',
            # hoverinfo='text', #如果hoverinfo包含'text', 并且'hovertext'未设置, 则这些文本元素将显示在悬停标签中https://zhuanlan.zhihu.com/p/87163211?utm_source=qq
            marker=go.scattermapbox.Marker(
                size=10,
                color='#ff2d51',  # 000045
                opacity=0.9 #不透明度
            )
        )
    ]
    # 遍历每一个中心点,并画出对应矩形范围(一个一个区域画)
    for key in dict.keys():
        borders = dict[key]
        lons = [] #为new_dict[key]例的经度
        lats = []
        for border in borders:  #border 为每个key后对应的四个坐标经纬度
            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]] #画成一个封闭的四方形 lons经度为102.95小数点后两位
        lats = [lats1[0], lats1[1], lats1[1], lats1[0], lats1[0]]
        # lons = pd.DataFrame(lons)
        # lats = pd.DataFrame(lats)
        datas.append(
            go.Scattermapbox(
                lat=lats,
                lon=lons,
                # text=geo,
                mode='markers+lines',
                # hoverinfo='text',
                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',
            # hoverinfo='text', #如果hoverinfo包含'text', 并且'hovertext'未设置, 则这些文本元素将显示在悬停标签中https://zhuanlan.zhihu.com/p/87163211?utm_source=qq
            marker=go.scattermapbox.Marker(
                size=5,
                color='#cca4e3',  # 000045
                opacity=0.9
            )
        )
    )
    for key in dict.keys():
        borders = dict[key]
        lons = [] #为new_dict[key]例的经度
        lats = []
        for border in borders:  #border 为每个key后对应的四个坐标经纬度
            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]] #画成一个封闭的四方形 lons经度为102.95小数点后两位
        lats = [lats1[0], lats1[1], lats1[1], lats1[0], lats1[0]]
        # lons = pd.DataFrame(lons)
        # lats = pd.DataFrame(lats)
        datas.append(
            go.Scattermapbox(
                lat=lats,
                lon=lons,
                # text=geo,
                mode='markers+lines',
                fillcolor='#fffbf0',
                # hoverinfo='text',
                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',
            # hoverinfo='text', #如果hoverinfo包含'text', 并且'hovertext'未设置, 则这些文本元素将显示在悬停标签中https://zhuanlan.zhihu.com/p/87163211?utm_source=qq
            marker=go.scattermapbox.Marker(
                size=5,
                color='#cca4e3',  # 000045
                opacity=0.9
            )
        )
    )
    for key in dict.keys():
        borders = dict[key]
        lons = [] #为new_dict[key]例的经度
        lats = []
        for border in borders:  #border 为每个key后对应的四个坐标经纬度
            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]] #画成一个封闭的四方形 lons经度为102.95小数点后两位
        lats = [lats1[0], lats1[1], lats1[1], lats1[0], lats1[0]]
        # lons = pd.DataFrame(lons)
        # lats = pd.DataFrame(lats)
        datas.append(
            go.Scattermapbox(
                lat=lats,
                lon=lons,
                # text=geo,
                mode='markers+lines',
                fillcolor='#fffbf0',
                # hoverinfo='text',
                text='',
                marker=go.scattermapbox.Marker(
                    size=2,
                    color='blue',
                    opacity=0.5
                )
            )
        )
    return datas
# get_dict得到分块信息字典,键为各个中心点坐标,值为各个中心点形成的矩形区域的四个顶点。
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): #解码七位geo并添加到地图datas
    new_central_point = []
    new_central_point_text=[]
    for i in geo_data: #先一个个计算central_points
        new_central_point.append(list(jie_geohash(i[0]))) #每个Geohash的中心坐标保存在new_central_point列表里
        a=str(i[0])
        new_central_point_text.append(a) #每个中心点显示对应的7位Geohash
    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'  # 司机位置坐标所在的的geo
    taxi_geo = taxi_geo1[0:5]  # 取司机的前5位Geohash
    central_points = list(jie_geohash(taxi_geo))  # 生成中心点并转为列表(30.623703002929688, 104.03160095214844)
    lats_jia6= 0.0027 # geohash为6时候误差
    lons_jia6 = 0.0055
    lats_jia5 = 0.022
    lons_jia5 = 0.022
    point_dict = get_dict(central_points,lats_jia5,lons_jia5)#生产5位大Geohash图
    datas=geo_paint(central_points,point_dict)#生成Geohash6的图即第一个中心点和对应的大边框的datas
    geo_data,geo_nb= geo_demand(taxi_geo)
    new_datas = jieqiweigeo(geo_data, datas,lats_jia6, lons_jia6) # 返回MySQL里的长度为司机长度前六位的七位Geohash对应的订单量与订单量百分比即demand
    #接下来要画Geohash为6的图
    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')


最后

以上就是高大哈密瓜为你收集整理的5-6位Geohash图示例的全部内容,希望文章能够帮你解决5-6位Geohash图示例所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部