之前自己在做基于Lucene的內容檢索過程中,了解到Lucene可以實現對文本信息,數值信息的內容檢索,對于空間距離好像并為為源碼中實現;最近半年自己接觸到Solr,里面有一個空間距離檢索(經緯度),最近對其中的實現做了下學習,了解到在實現空間距離檢索的有一個比較常用的技術――GeoHash,下面就介紹下GeoHash。
GeoHash特點
因此我們再去做距離檢索的時候,只需要對GeoHash進行前綴匹配即可,具體的原因在后面內容進行介紹。
GeoHash原理
GeoHash最簡單的解釋就是將一個位置信息轉化成一個可以排序、可以比較的字符串編碼。下面就詳細介紹以下其實現過程:
首先我們將緯度(-90, 90)平均分成兩個區間(-90, 0)、(0, 90),如果坐標位置的緯度值在第一區間,則編碼是0,否則編碼為1。我們用 40.222012 舉例,由于40.222012 屬于 (0, 90),所以編碼為1,然后我們繼續將(0, 90)分成(0, 45)、(45, 90)兩個區間,而40.222012 位于(0, 45),所以編碼是0,依次類推,我們進行20次拆分,最后計算40.222012 的編碼是 10111001001101000110。
對于經度采用同樣的的方法,得到 116.248283 的編碼是 11010010101010100101。
接下來我們對經緯度的編碼合并,奇數為是緯度,偶數為是經度,得到的編碼是 1110011101001001100011011001100000110110(這里需要特別注意,這里說的奇數、偶數是值數組的下標,從0開始的);
最后用base32編碼,二進制串對應的十進制分別為 28, 29, 4, 24, 27, 6, 1, 22,轉化為base32是wx4sv61q,因此就 得到(40.222012, 116.248283) 的編碼為 wx4sv61q。(下圖介紹了base32的對應關系)

編碼 wx4sv61q 在地圖上對應的位置如下圖:

這里我們GeoHash的編碼長度為8,這時精度在19米,下表列出了不同的編碼長度對應的精度:


由上面的精度可知,如果要選取和我(40.222012, 116.248283)相距2km內的物品,我們只需要查找物品坐標對應的GeoHash以wx4sv為前綴的即可。
GeoHash延伸
到目前為止我們對空間索引有了一定的了解,但是上面介紹的內容對下面的一種情況就無法實現:

我們從圖中可以看出,紅點與上方的綠點距離較近,與下方的綠點距離較遠,但是紅點與下方的綠點的編碼字符串一樣,都是wx4g0。對于GeoHash這種邊界問題解決思路也十分簡單,我們在做檢索或者查詢的時候,對周圍的八個區域進行匹配,這樣就很好的解決了邊界問題。下面我們就對GeoHash用Java進行實現。
JAVA實現
在實現之前,我們首先定義一個LocationBean,用它來表示經緯度信息:
/** *@Description: 存儲經緯度信息 */ package com.lulei.geo.bean; public class LocationBean { public static final double MINLAT = -90; public static final double MAXLAT = 90; public static final double MINLNG = -180; public static final double MAXLNG = 180; private double lat;//緯度[-90,90] private double lng;//經度[-180,180] public LocationBean(double lat, double lng) { this.lat = lat; this.lng = lng; } public double getLat() { return lat; } public void setLat(double lat) { this.lat = lat; } public double getLng() { return lng; } public void setLng(double lng) { this.lng = lng; } } 然后我們編寫一個類,來實現GeoHash,在實現GeoHash的過程中,我們需要用定義一些常量以及經緯度信息,具體如下:
public class GeoHash { private LocationBean location; /** * 1 2500km;2 630km;3 78km;4 30km * 5 2.4km; 6 610m; 7 76m; 8 19m */ private int hashLength = 8; //經緯度轉化為geohash長度 private int latLength = 20; //緯度轉化為二進制長度 private int lngLength = 20; //經度轉化為二進制長度 private double minLat;//每格緯度的單位大小 private double minLng;//每個經度的倒下 private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; } 在GeoHash實例化時,我們需要對一些屬性進行賦值:
public GeoHash(double lat, double lng) { location = new LocationBean(lat, lng); setMinLatLng(); } public int gethashLength() { return hashLength; } /** * @Author:lulei * @Description: 設置經緯度的最小單位 */ private void setMinLatLng() { minLat = LocationBean.MAXLAT - LocationBean.MINLAT; for (int i = 0; i < latLength; i++) { minLat /= 2.0; } minLng = LocationBean.MAXLNG - LocationBean.MINLNG; for (int i = 0; i < lngLength; i++) { minLng /= 2.0; } } 我們在使用GeoHash的時候,需要設置最終編碼的長度,因此編寫一個方法實現對GeoHash長度的設置
public boolean sethashLength(int length) { if (length < 1) { return false; } hashLength = length; latLength = (length * 5) / 2; if (length % 2 == 0) { lngLength = latLength; } else { lngLength = latLength + 1; } setMinLatLng(); return true; } 有了這些設置之后,我們需要將經度、緯度轉化為對應的二進制編碼
private boolean[] getHashArray(double value, double min, double max, int length) { if (value < min || value > max) { return null; } if (length < 1) { return null; } boolean[] result = new boolean[length]; for (int i = 0; i < length; i++) { double mid = (min + max) / 2.0; if (value > mid) { result[i] = true; min = mid; } else { result[i] = false; max = mid; } } return result; } 分別獲取經緯度的二進制編碼后,我們需要將兩個二進制字符串合并成一個
private boolean[] merge(boolean[] latArray, boolean[] lngArray) { if (latArray == null || lngArray == null) { return null; } boolean[] result = new boolean[lngArray.length + latArray.length]; Arrays.fill(result, false); for (int i = 0; i < lngArray.length; i++) { result[2 * i] = lngArray[i]; } for (int i = 0; i < latArray.length; i++) { result[2 * i + 1] = latArray[i]; } return result; } 最后我們需要將獲得的二進制轉進行base32轉化
/** * @param lat * @param lng * @return * @Author:lulei * @Description: 獲取經緯度的base32字符串 */ private String getGeoHashBase32(double lat, double lng) { boolean[] bools = getGeoBinary(lat, lng); if (bools == null) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < bools.length; i = i + 5) { boolean[] base32 = new boolean[5]; for (int j = 0; j < 5; j++) { base32[j] = bools[i + j]; } char cha = getBase32Char(base32); if (' ' == cha) { return null; } sb.append(cha); } return sb.toString(); } /** * @param base32 * @return * @Author:lulei * @Description: 將五位二進制轉化為base32 */ private char getBase32Char(boolean[] base32) { if (base32 == null || base32.length != 5) { return ' '; } int num = 0; for (boolean bool : base32) { num <<= 1; if (bool) { num += 1; } } return CHARS[num % CHARS.length]; } 對于如何獲取周圍八個區域的GeoHash值這個問題我們可以做如下轉化,我們已經知道當前點的經緯度值,我們也知道每一個區域內的經度、緯度的寬度,如果經度加上或減去這個寬度,我們就可以位于該區域左側和右側區域的經度,如果緯度加上或減去這個寬度,我們就可以獲取該區域上部和下部的緯度,這樣我們就可以分別獲取到該區域周圍八個區域內的一個點的坐標,我們分別計算這八個點的坐標,也就是八個區域對應的GeoHash編碼。
public List<String> getGeoHashBase32For9() { double leftLat = location.getLat() - minLat; double rightLat = location.getLat() + minLat; double upLng = location.getLng() - minLng; double downLng = location.getLng() + minLng; List<String> base32For9 = new ArrayList<String>(); //左側從上到下 3個 String leftUp = getGeoHashBase32(leftLat, upLng); if (!(leftUp == null || "".equals(leftUp))) { base32For9.add(leftUp); } String leftMid = getGeoHashBase32(leftLat, location.getLng()); if (!(leftMid == null || "".equals(leftMid))) { base32For9.add(leftMid); } String leftDown = getGeoHashBase32(leftLat, downLng); if (!(leftDown == null || "".equals(leftDown))) { base32For9.add(leftDown); } //中間從上到下 3個 String midUp = getGeoHashBase32(location.getLat(), upLng); if (!(midUp == null || "".equals(midUp))) { base32For9.add(midUp); } String midMid = getGeoHashBase32(location.getLat(), location.getLng()); if (!(midMid == null || "".equals(midMid))) { base32For9.add(midMid); } String midDown = getGeoHashBase32(location.getLat(), downLng); if (!(midDown == null || "".equals(midDown))) { base32For9.add(midDown); } //右側從上到下 3個 String rightUp = getGeoHashBase32(rightLat, upLng); if (!(rightUp == null || "".equals(rightUp))) { base32For9.add(rightUp); } String rightMid = getGeoHashBase32(rightLat, location.getLng()); if (!(rightMid == null || "".equals(rightMid))) { base32For9.add(rightMid); } String rightDown = getGeoHashBase32(rightLat, downLng); if (!(rightDown == null || "".equals(rightDown))) { base32For9.add(rightDown); } return base32For9; } 運行結果

完整代碼
上面的博客中已經有完整的LoacationBean代碼,這里就不再寫了。
/** *@Description: GeoHash實現經緯度的轉化 */ package com.lulei.geo; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.lulei.geo.bean.LocationBean; import com.lulei.util.JsonUtil; public class GeoHash { private LocationBean location; /** * 1 2500km;2 630km;3 78km;4 30km * 5 2.4km; 6 610m; 7 76m; 8 19m */ private int hashLength = 8; //經緯度轉化為geohash長度 private int latLength = 20; //緯度轉化為二進制長度 private int lngLength = 20; //經度轉化為二進制長度 private double minLat;//每格緯度的單位大小 private double minLng;//每個經度的倒下 private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; public GeoHash(double lat, double lng) { location = new LocationBean(lat, lng); setMinLatLng(); } public int gethashLength() { return hashLength; } /** * @Author:lulei * @Description: 設置經緯度的最小單位 */ private void setMinLatLng() { minLat = LocationBean.MAXLAT - LocationBean.MINLAT; for (int i = 0; i < latLength; i++) { minLat /= 2.0; } minLng = LocationBean.MAXLNG - LocationBean.MINLNG; for (int i = 0; i < lngLength; i++) { minLng /= 2.0; } } /** * @return * @Author:lulei * @Description: 求所在坐標點及周圍點組成的九個 */ public List<String> getGeoHashBase32For9() { double leftLat = location.getLat() - minLat; double rightLat = location.getLat() + minLat; double upLng = location.getLng() - minLng; double downLng = location.getLng() + minLng; List<String> base32For9 = new ArrayList<String>(); //左側從上到下 3個 String leftUp = getGeoHashBase32(leftLat, upLng); if (!(leftUp == null || "".equals(leftUp))) { base32For9.add(leftUp); } String leftMid = getGeoHashBase32(leftLat, location.getLng()); if (!(leftMid == null || "".equals(leftMid))) { base32For9.add(leftMid); } String leftDown = getGeoHashBase32(leftLat, downLng); if (!(leftDown == null || "".equals(leftDown))) { base32For9.add(leftDown); } //中間從上到下 3個 String midUp = getGeoHashBase32(location.getLat(), upLng); if (!(midUp == null || "".equals(midUp))) { base32For9.add(midUp); } String midMid = getGeoHashBase32(location.getLat(), location.getLng()); if (!(midMid == null || "".equals(midMid))) { base32For9.add(midMid); } String midDown = getGeoHashBase32(location.getLat(), downLng); if (!(midDown == null || "".equals(midDown))) { base32For9.add(midDown); } //右側從上到下 3個 String rightUp = getGeoHashBase32(rightLat, upLng); if (!(rightUp == null || "".equals(rightUp))) { base32For9.add(rightUp); } String rightMid = getGeoHashBase32(rightLat, location.getLng()); if (!(rightMid == null || "".equals(rightMid))) { base32For9.add(rightMid); } String rightDown = getGeoHashBase32(rightLat, downLng); if (!(rightDown == null || "".equals(rightDown))) { base32For9.add(rightDown); } return base32For9; } /** * @param length * @return * @Author:lulei * @Description: 設置經緯度轉化為geohash長度 */ public boolean sethashLength(int length) { if (length < 1) { return false; } hashLength = length; latLength = (length * 5) / 2; if (length % 2 == 0) { lngLength = latLength; } else { lngLength = latLength + 1; } setMinLatLng(); return true; } /** * @return * @Author:lulei * @Description: 獲取經緯度的base32字符串 */ public String getGeoHashBase32() { return getGeoHashBase32(location.getLat(), location.getLng()); } /** * @param lat * @param lng * @return * @Author:lulei * @Description: 獲取經緯度的base32字符串 */ private String getGeoHashBase32(double lat, double lng) { boolean[] bools = getGeoBinary(lat, lng); if (bools == null) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < bools.length; i = i + 5) { boolean[] base32 = new boolean[5]; for (int j = 0; j < 5; j++) { base32[j] = bools[i + j]; } char cha = getBase32Char(base32); if (' ' == cha) { return null; } sb.append(cha); } return sb.toString(); } /** * @param base32 * @return * @Author:lulei * @Description: 將五位二進制轉化為base32 */ private char getBase32Char(boolean[] base32) { if (base32 == null || base32.length != 5) { return ' '; } int num = 0; for (boolean bool : base32) { num <<= 1; if (bool) { num += 1; } } return CHARS[num % CHARS.length]; } /** * @param lat * @param lng * @return * @Author:lulei * @Description: 獲取坐標的geo二進制字符串 */ private boolean[] getGeoBinary(double lat, double lng) { boolean[] latArray = getHashArray(lat, LocationBean.MINLAT, LocationBean.MAXLAT, latLength); boolean[] lngArray = getHashArray(lng, LocationBean.MINLNG, LocationBean.MAXLNG, lngLength); return merge(latArray, lngArray); } /** * @param latArray * @param lngArray * @return * @Author:lulei * @Description: 合并經緯度二進制 */ private boolean[] merge(boolean[] latArray, boolean[] lngArray) { if (latArray == null || lngArray == null) { return null; } boolean[] result = new boolean[lngArray.length + latArray.length]; Arrays.fill(result, false); for (int i = 0; i < lngArray.length; i++) { result[2 * i] = lngArray[i]; } for (int i = 0; i < latArray.length; i++) { result[2 * i + 1] = latArray[i]; } return result; } /** * @param value * @param min * @param max * @return * @Author:lulei * @Description: 將數字轉化為geohash二進制字符串 */ private boolean[] getHashArray(double value, double min, double max, int length) { if (value < min || value > max) { return null; } if (length < 1) { return null; } boolean[] result = new boolean[length]; for (int i = 0; i < length; i++) { double mid = (min + max) / 2.0; if (value > mid) { result[i] = true; min = mid; } else { result[i] = false; max = mid; } } return result; } public static void main(String[] args) { // TODO Auto-generated method stub GeoHash g = new GeoHash(40.222012, 116.248283); System.out.println(g.getGeoHashBase32()); System.out.println(JsonUtil.parseJson(g.getGeoHashBase32For9())); } } 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答