離(lí)我最近之geohash算法(增加周邊鄰近編号)
發表時間:2020-10-19
發布人:葵宇科技
浏覽次數:24
接着上一篇文(wén)┞仿:查找鄰近網點geohash算法及實現 (Java版本) http://blog.csdn.net/sunrise_2013/article/details/42024813
參考文(wén)檔:
http://www.slideshare.net/sandeepbhaskar2/geohash 介紹geohash道理及例子(zǐ)
http://geohash.gofreerange.com/ 演示實例
http://geohash.gofreerange.com/ 周邊8格子(zǐ)實例
因為存在朝分網點損掉的情況,為懂得決邊沿問(wèn)題,須要計算這個(gè)塊四周的八個(gè)格子(zǐ),彌補代碼,實現周邊8個(gè)編碼的比來編碼。
根據一個(gè)geohash值 獲取四周8個(gè)geohash值的代碼。
例如(rú)在方格4的左下(xià)部分的點和(hé)大年夜方格1的右下(xià)部分的點離(lí)的很近,可(kě)是它們的geohash值必定是相差的相當遠(yuǎn),因為頭一次的分塊就相差太大年夜了,很多時刻我們對geohash的值進内行單的排序比較,結不雅貌似真的可(kě)以或許找出鄰近的點,并且似乎照樣按照距離(lí)的遠(yuǎn)近分列的,可(kě)是實際上會有一些點被漏掉落了。
上述這個(gè)問(wèn)題,可(kě)以經由過程搜刮一個(gè)格子(zǐ),四周八個(gè)格子(zǐ)的數據,同一獲取後再進行過濾。如(rú)許就在編碼條懂得決了這個(gè)問(wèn)題。
[img]http://img.blog.csdn.net/20150105090803059?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VucmlzZV8yMDEz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
全部代碼實例:
Geohase.java
package com.DistTest; //Geohash.java //Geohash library for Java //ported from David Troy's Geohash library for Javascript //- http://github.com/davetroy/geohash-js/tree/master //(c) 2008 David Troy //(c) 2008 Tom Carden //Distributed under the MIT License public class Geohash { public static int BITS[] = { 16, 8, 4, 2, 1 }; public static String BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz"; public static int RIGHT = 0; public static int LEFT = 1; public static int TOP = 2; public static int BOTTOM = 3; public static int EVEN = 0; public static int ODD = 1; public static String[][] NEIGHBORS; public static String[][] BORDERS; static { NEIGHBORS = new String[4][2]; BORDERS = new String[4][2]; NEIGHBORS[BOTTOM][EVEN] = "bc01fg45238967deuvhjyznpkmstqrwx"; NEIGHBORS[TOP][EVEN] = "238967debc01fg45kmstqrwxuvhjyznp"; NEIGHBORS[LEFT][EVEN] = "p0r21436x8zb9dcf5h7kjnmqesgutwvy"; NEIGHBORS[RIGHT][EVEN] = "14365h7k9dcfesgujnmqp0r2twvyx8zb"; BORDERS[BOTTOM][EVEN] = "bcfguvyz"; BORDERS[TOP][EVEN] = "0145hjnp"; BORDERS[LEFT][EVEN] = "prxz"; BORDERS[RIGHT][EVEN] = "028b"; NEIGHBORS[BOTTOM][ODD] = NEIGHBORS[LEFT][EVEN]; NEIGHBORS[TOP][ODD] = NEIGHBORS[RIGHT][EVEN]; NEIGHBORS[LEFT][ODD] = NEIGHBORS[BOTTOM][EVEN]; NEIGHBORS[RIGHT][ODD] = NEIGHBORS[TOP][EVEN]; BORDERS[BOTTOM][ODD] = BORDERS[LEFT][EVEN]; BORDERS[TOP][ODD] = BORDERS[RIGHT][EVEN]; BORDERS[LEFT][ODD] = BORDERS[BOTTOM][EVEN]; BORDERS[RIGHT][ODD] = BORDERS[TOP][EVEN]; } private static void refine_interval(double[] interval, int cd, int mask) { if ((cd & mask) > 0) { interval[0] = (interval[0] + interval[1]) / 2.0; } else { interval[1] = (interval[0] + interval[1]) / 2.0; } } public static String calculateAdjacent(String srcHash, int dir) { srcHash = srcHash.toLowerCase(); char lastChr = srcHash.charAt(srcHash.length() - 1); int type = (srcHash.length() % 2) == 1 ? ODD : EVEN; String base = srcHash.substring(0, srcHash.length() - 1); if (BORDERS[dir][type].indexOf(lastChr) != -1) { base = calculateAdjacent(base, dir); } return base + BASE32.charAt(NEIGHBORS[dir][type].indexOf(lastChr)); } public static String[] getGeoHashExpand(String geohash) { try { String geohashTop = calculateAdjacent(geohash, TOP); String geohashBottom = calculateAdjacent(geohash, BOTTOM); String geohashRight = calculateAdjacent(geohash, RIGHT); String geohashLeft = calculateAdjacent(geohash, LEFT); String geohashTopLeft = calculateAdjacent(geohashLeft, TOP); String geohashTopRight = calculateAdjacent(geohashRight, TOP); String geohashBottomRight = calculateAdjacent(geohashRight, BOTTOM); String geohashBottomLeft = calculateAdjacent(geohashLeft, BOTTOM); String[] expand = { geohash, geohashTop, geohashBottom, geohashRight, geohashLeft, geohashTopLeft, geohashTopRight, geohashBottomRight, geohashBottomLeft }; return expand; } catch (Exception e) { //logger.error("GeoHash Error",e); return null; } } public static double[][] decode(String geohash) { boolean is_even = true; double[] lat = new double[3]; double[] lon = new double[3]; lat[0] = -90.0; lat[1] = 90.0; lon[0] = -180.0; lon[1] = 180.0; double lat_err = 90.0; double lon_err = 180.0; for (int i = 0; i < geohash.length(); i++) { char c = geohash.charAt(i); int cd = BASE32.indexOf(c); for (int j = 0; j < BITS.length; j++) { int mask = BITS[j]; if (is_even) { lon_err /= 2.0; refine_interval(lon, cd, mask); } else { lat_err /= 2.0; refine_interval(lat, cd, mask); } is_even = !is_even; } } lat[2] = (lat[0] + lat[1]) / 2.0; lon[2] = (lon[0] + lon[1]) / 2.0; return new double[][] { lat, lon }; } public static String encode(double latitude, double longitude) { boolean is_even = true; int i = 0; double lat[] = new double[3]; double lon[] = new double[3]; int bit = 0; int ch = 0; int precision = 12; String geohash = ""; lat[0] = -90.0; lat[1] = 90.0; lon[0] = -180.0; lon[1] = 180.0; while (geohash.length() < precision) { if (is_even) { double mid = (lon[0] + lon[1]) / 2.0; if (longitude > mid) { ch |= BITS[bit]; lon[0] = mid; } else { lon[1] = mid; } } else { double mid = (lat[0] + lat[1]) / 2.0; if (latitude > mid) { ch |= BITS[bit]; lat[0] = mid; } else { lat[1] = mid; } } is_even = !is_even; if (bit < 4) { bit++; } else { geohash += BASE32.charAt(ch); bit = 0; ch = 0; } } return geohash; } }
測試實例:
package com.DistTest; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Connection; import java.sql.Statement; public class sqlTest { public static void main(String[] args) throws Exception { Connection conn = null; String sql; String sql1; String url = "jdbc:mysql://132.97.194.62/test?" + "user=root&password=sheng&useUnicode=true&characterEncoding=UTF8"; try { Class.forName("com.mysql.jdbc.Driver");// 動(dòng)态加載mysql驅動(dòng) // System.out.println("成功加載MySQL驅動(dòng)法度榜樣"); // 一個(gè)Connection代表一個(gè)數據庫連接 conn = DriverManager.getConnection(url); // Statement琅绫擎帶有很多辦法,比如(rú)executeUpdate可(kě)以實現插入,更新和(hé)删除等 Statement stmt = conn.createStatement(); double lon1=109.0145193757; double lat1=34.236080797698; //根據地輿坐(zuò)标,生成geohash編碼 // Geohash ghash = new Geohash(); // String gcode=Geohash.encode(lat1, lon1).substring(0, 4); String gcode="wqj6z"; String[] neargcode=Geohash.getGeoHashExpand(gcode); for(int i=0;i<neargcode.length;i++) { System.out.println(neargcode[i]); } sql = "select * from retailersinfotable where geohash like " + "'"+neargcode[0]+"%' or '"+neargcode[1]+"%' or '"+neargcode[2]+"%' " + "or '"+neargcode[3]+"%' or '"+neargcode[4]+"%' or '"+neargcode[5]+"%' " + "or '"+neargcode[6]+"%' or '"+neargcode[7]+"%' or '"+neargcode[8]+"%'"; System.out.println(sql); ResultSet rs = stmt.executeQuery(sql);// executeQuery會返回結不雅的集合,不然返回空值 rs.last(); int rowCount = rs.getRow(); //獲得ResultSet的總行數 System.out.println(rowCount); rs.beforeFirst(); System.out.println("當前地位:經度"+lon1+" 維度:"+lat1); int i=0; String[][] array = new String[rowCount][3]; while (rs.next()){ //大年夜數據庫掏出地輿坐(zuò)标 double lon2=Double.parseDouble(rs.getString("Longitude")); double lat2=Double.parseDouble(rs.getString("Latitude")); //根據地輿坐(zuò)标,生成geohash編碼 Geohash geohash = new Geohash(); String geocode=geohash.encode(lat2, lon2).substring(0, 9); //計算兩點間的距離(lí) int dist=(int) Test.GetDistance(lon1, lat1, lon2, lat2); array[i][0]=String.valueOf(i); array[i][1]=geocode; array[i][2]=Integer.toString(dist); i++; // System.out.println(lon2+"---"+lat2+"---"+geocode+"---"+dist); } array=sqlTest.getOrder(array); //二維數組排序 sqlTest.showArray(array); //打印數組 } catch (SQLException e) { System.out.println("MySQL操作缺點"); e.printStackTrace(); } finally { conn.close(); } } /* * 二維數組排序,比較array[][2]的值,返回二維數組 * */ public static String[][] getOrder(String[][] array){ for (int j = 0; j < array.length ; j++) { for (int bb = 0; bb < array.length - 1; bb++) { String[] ss; int a1=Integer.valueOf(array[bb][2]); //轉化成int型比較大年夜小 int a2=Integer.valueOf(array[bb+1][2]); if (a1>a2) { ss = array[bb]; array[bb] = array[bb + 1]; array[bb + 1] = ss; } } } return array; } /*打印數組*/ public static void showArray(String[][] array){ for(int a=0;a<array.length;a++){ for(int j=0;j<array[0].length;j++) System.out.print(array[a][j]+" "); System.out.println(); } } /* public static void main(String[] args) { String aaa="wxsfdf"; Geohash geohash = new Geohash(); String[] str = geohash.getGeoHashExpand(aaa); for(int i=0;i<str.length;i++) { System.out.println(str[i]); } }*/ }