標籤:style blog sqrt adl ima ase box 長度 list
基於c#語言 geohash演算法基本實現源碼,參見: https://github.com/sharonjl/geohash-net , 源碼中具體包含如下方法:
- String CalculateAdjacent(String hash, Direction direction); //根據指定hash碼,擷取相鄰的上下左右矩形框hash碼
- double[] Decode(String geohash); //根據指定hash碼返回經緯度
- String Encode(double latitude, double longitude, int precision = 12); //根據經緯度和指定的hash碼長度擷取hash碼
但在實際應用的過程中,由於GeoHash是將地區劃分為一個個規則矩形,並對每個矩形進行編碼,這樣在查詢附近POI資訊時會導致以下問題,比如紅色的點是我們的位 置,綠色的兩個點分別是附近的兩個餐館,但是在查詢的時候會發現距離較遠餐館的GeoHash編碼與我們一樣(因為在同一個GeoHash地區塊上),而 較近餐館的GeoHash編碼與我們不一致。這個問題往往產生在邊界處,如:
解決方案:在查詢時,除了使用錨點的GeoHash編碼進行匹配外,還使用周圍8個地區的GeoHash編碼,取出相鄰的8個地區範圍內的所有餐館作為第一層過濾,然後根據當前位置計算出與8個相鄰地區內每個餐廳的距離,按距離的由小到大順序返回結果,擴充代碼如下:
- 擷取指定hash對應矩形框相鄰的8個地區範例程式碼:
/// <summary> /// 擷取九個格子 順序 本身 上、下、左、右、 左上、 右上、 左下、右下 /// </summary> /// <param name="geohash"></param> /// <returns></returns> public List<string> GetGeoHashExpand(String geohash) { String geohashTop = CalculateAdjacent(geohash, Direction.Top);//上 String geohashBottom = CalculateAdjacent(geohash, Direction.Bottom);//下 String geohashLeft = CalculateAdjacent(geohash, Direction.Left);//左 String geohashRight = CalculateAdjacent(geohash, Direction.Right);//右 String geohashTopLeft = CalculateAdjacent(geohashLeft, Direction.Top);//左上 String geohashTopRight = CalculateAdjacent(geohashRight, Direction.Top);//右上 String geohashBottomLeft = CalculateAdjacent(geohashLeft, Direction.Bottom);//左下 String geohashBottomRight = CalculateAdjacent(geohashRight, Direction.Bottom);//右下 return new List<string>() { geohash, geohashTop, geohashBottom, geohashLeft, geohashRight,
geohashTopLeft, geohashTopRight, geohashBottomLeft, geohashBottomRight }; }
- 計算兩點之間距離範例程式碼
//地球半徑,單位米 private const double EARTH_RADIUS = 6378137; /// <summary> /// 計算兩點位置的距離,返回兩點的距離,單位 米 /// 該公式為GOOGLE提供,誤差小於0.2米 /// </summary> /// <param name="lat1">第一點緯度</param> /// <param name="lng1">第一點經度</param> /// <param name="lat2">第二點緯度</param> /// <param name="lng2">第二點經度</param> /// <returns></returns> public static double GetDistance(double lat1, double lng1, double lat2, double lng2) { double radLat1 = Rad(lat1); double radLng1 = Rad(lng1); double radLat2 = Rad(lat2); double radLng2 = Rad(lng2); double a = radLat1 - radLat2; double b = radLng1 - radLng2; double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS; return result; } /// <summary> /// 經緯度轉化成弧度 /// </summary> /// <param name="d"></param> /// <returns></returns> private static double Rad(double d) { return (double)d * Math.PI / 180d; }
- 計算hash碼對應矩形框經緯度範圍代碼(左下角、右上方),參考: https://github.com/aerisweather/node-geohash/blob/master/main.js , c#實現樣本碼如下:
public double[] Decode_bbox(string hash_string) { bool isLon = true; double maxLat = 90,minLat = -90,maxLon = 180,minLon = -180,mid; int hashValue = 0; int l = hash_string.Length; for (int i = 0; i < l; i++) { string code = Convert.ToString(hash_string[i]); hashValue = BASE32_CODES_DICT[code]; for (var bits = 4; bits >= 0; bits--) { var bit = (hashValue >> bits) & 1; if (isLon) { mid = (maxLon + minLon) / 2; if (bit == 1) { minLon = mid; } else { maxLon = mid; } } else { mid = (maxLat + minLat) / 2; if (bit == 1) { minLat = mid; } else { maxLat = mid; } } isLon = !isLon; } } return new double[] { minLat, minLon, maxLat, maxLon }; }
- 基於以上的代碼,在實際應用中基本可完成對附件範圍內事物的搜尋以及在地圖中進列區域標識
geohash-net實現