把经纬度转换为Geohash(准确)

# 现在要做:
# 1:把数据库的经纬度读出生成Geohash
# 2:取Geohash的前N位
# 3:计算前N未Geohash的demand

from pygeohash import encode, decode
import plotly
import plotly.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

def mysql():
    conn = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        passwd='xu19931026',
        db='cd_taxi',
        charset='utf8'
    )
    if conn:
        print("连接成功!")
    cursor = conn.cursor()  # 获取游标
    sql = "select CAST(BeginLongitude as CHAR(11)) as BeginLongitude,CAST(BeginLatitude as CHAR(10)) as BeginLatitude FROM Order_Data where FROM_UNIXTIME(BeginTime) <'2016-11-01 00:05:00' "  # sql语句
    cursor.execute(sql)
    result=cursor.fetchall()
    df=list(result)  #将元组转换为列表
    lon = []
    lat = []
    for point in df:
        lon.append(float(point[0]))  #将字符串的经纬度转换为float格式
        lat.append(float(point[1]))
    num=len(lon) #num为数组大小
    data = [ [lon[i],lat[i]] for i in range(num) ]    #将数据制作成[lats,lons,weights]的形式
    print('**************************************')
    print('数据库处理数据完毕')
    return lon,lat,data
def get_lonandlat(geo):
    lat, lon = geohash.decode(geo)  # precision=9可以加精度
    return lat, lon
def get_geohash(lon, lat):
    #生成Geohash
    geo = geohash.encode(lat, lon)  # precision=9可以加精度
    return geo
def get_geolist(lon,lat):  #根据MySQL出的经纬度生成Geohash
    p = []
    for i in range(len(lon)):
        result = get_geohash(lon[i],lat[i])
        p.append(result)
    return p

if __name__ == '__main__':
    lon,lat,data=mysql() # 获取MySQL里的经度,纬度,经度纬度组成的列表
    geohash1=get_geolist(lon,lat) # 根据经纬度获得geohash1列表

你可能感兴趣的:(机器学习)