Java将GeoHash转化为对应的经纬度坐标实例代码
程序员文章站
2024-03-08 13:41:04
本文实例介绍了java实现将geohash转化为对应的经纬度坐标的详细代码,分享给大家供大家参考,具体内容如下
package com.lulei.geo;...
本文实例介绍了java实现将geohash转化为对应的经纬度坐标的详细代码,分享给大家供大家参考,具体内容如下
package com.lulei.geo; import java.util.arraylist; import java.util.arrays; import java.util.hashmap; 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'}; private static hashmap<character, integer> charsmap; static { charsmap = new hashmap<character, integer>(); for (int i = 0; i < chars.length; i++) { charsmap.put(chars[i], i); } } 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 i * @return * @author:lulei * @description: 将数字转化为二进制字符串 */ private string getbase32binarystring(int i) { if (i < 0 || i > 31) { return null; } string str = integer.tobinarystring(i + 32); return str.substring(1); } /** * @param geohash * @return * @author:lulei * @description: 将geohash转化为二进制字符串 */ private string getgeohashbinarystring(string geohash) { if (geohash == null || "".equals(geohash)) { return null; } stringbuffer sb = new stringbuffer(); for (int i = 0; i < geohash.length(); i++) { char c = geohash.charat(i); if (charsmap.containskey(c)) { string cstr = getbase32binarystring(charsmap.get(c)); if (cstr != null) { sb.append(cstr); } } } return sb.tostring(); } /** * @param geohash * @return * @author:lulei * @description: 返回geohash 对应的坐标 */ public locationbean getlocation(string geohash) { string geohashbinarystr = getgeohashbinarystring(geohash); if (geohashbinarystr == null) { return null; } stringbuffer lat = new stringbuffer(); stringbuffer lng = new stringbuffer(); for (int i = 0; i < geohashbinarystr.length(); i++) { if (i % 2 != 0) { lat.append(geohashbinarystr.charat(i)); } else { lng.append(geohashbinarystr.charat(i)); } } double latvalue = getgeohashmid(lat.tostring(), locationbean.minlat, locationbean.maxlat); double lngvalue = getgeohashmid(lng.tostring(), locationbean.minlng, locationbean.maxlng); locationbean location = new locationbean(latvalue, lngvalue); location.setgeohash(geohash); return location; } /** * @param binarystr * @param min * @param max * @return * @author:lulei * @description: 返回二进制对应的中间值 */ private double getgeohashmid(string binarystr, double min, double max) { if (binarystr == null || binarystr.length() < 1) { return (min + max) / 2.0; } if (binarystr.charat(0) == '1') { return getgeohashmid(binarystr.substring(1), (min + max) / 2.0, max); } else { return getgeohashmid(binarystr.substring(1), min, (min + max) / 2.0); } } /** * @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.221227, 116.24875); string geohash = g.getgeohashbase32(); system.out.println(geohash); locationbean bean = g.getlocation(geohash); system.out.println(jsonutil.parsejson(bean)); system.out.println(new geohash(bean.getlat(), bean.getlng()).getgeohashbase32()); system.out.println(distanceutil.getdistance(bean.getlat(), bean.getlng(), bean.getlat() - g.minlat, bean.getlng() - g.minlng)); } }
以上就是本文的详细内容,希望对大家的学习有所帮助。