2016-08-18 79 views
1

我有2個數據框df1和df2與不同的Latitutde和經度以及相應的地理雜亂。現在對於df1中的每個geohash,我想在dataframe df2中找到最接近的geohash。我不確定是否有方法可以比較geohashes。例如,對於df1中的id 121,df2中最接近的geohash將是9muc3rr,對於df2中的id 122,最接近的geohash將是9wv97m1。比較兩個大熊貓數據框之間的地理加密

數據幀DF1

Id Latitude Longitude Geohash 
121 32.815130 -117.151695 9mudwju 
122 37.920948 -108.005043 9wepwr3 

數據幀DF2

Id Latitude Longitude Geohash 

124 32.604187 -117.005745 9muc3rr 
127 37.920948 -108.005043 9wv97m1 
135 39.70122 -104.876976 9xj3v7q 
128 38.844032 -104.718307 9wvscp6 
+1

你看過'geopandas'嗎? –

+0

據我所知,我沒有看到任何方法在geopandas比較geohashes。 – user3447653

+0

最近的經緯度與最短的距離是不是最接近的geohash? –

回答

0

如果你是重新發明輪子一點點OK,你可以在(緯度,經度)對轉換爲笛卡爾單位向量,然後只是比較那些使用點積的產品。由於點積基本上是一個矢量投影到另一個矢量的度量,所以與1最接近的乘積(最大值)將是兩個矢量之間的最佳匹配。

下面的示例計算基於this answer。我將讓您在WGS84橢球提供大地座標(因爲什麼GPS使用)的假設,而橢球以上的高度,是對所有點零:

from math import radians, sin, cos 
import numpy as np 

# WGS 84 parameters. Any other ellipsoid can be plugged in by changing 
# the following lines. All parameters are taken from Wikipedia at 
# https://en.wikipedia.org/wiki/Geodetic_datum#Parameters_for_some_geodetic_systems 
invFlat = 298.257222101 # Inverse flattening (1/f), unitless 
# Derived parameters 
e2 = 6694.37999014 # First eccentricity squared. Unitless. Can be computed from 2*f − f**2 

# Note that the radius is irrelevant since we are going to 
# normalize the result anyway. 

def cartesianUnitVector(lat, lon, isdeg=True): 
    if isdeg: 
     lat, lon = radians(lat), radians(lon) 
    vec = np.array([ 
     cos(lat) * cos(lon), 
     cos(lat) * sin(lon), 
     (1 - e2) * sin(lat) 
    ]) 
    norm = np.linalg.norm(vec) 
    return vec/norm 

target = (32.815130, -117.151695) 
candidates = [ 
    (32.604187, -117.005745), 
    (37.920948, -108.005043), 
    (39.70122, -104.876976), 
    (38.844032, -104.718307) 
] 

max(candidates, key=lambda x: np.dot(cartesianUnitVector(*x), cartesianUnitVector(*target))) 

的大地到ECEF公式可以在Wikipedia找到。這個例子展示瞭如何在迭代的lat-lon對上進行操作。我不完全確定如何適應熊貓,但你的問題是如何做比較,我想我已經提供了一個答案。我確信,一旦你定義了轉換函數和使用它的比較鍵,你就可以毫無困難地將它應用於熊貓。