geohash地图好帮手

正经学徒,佛系记录,不搞事情

详细的解释交给专业的大佬,我就用用:

来自大佬的详解    geohash的距离估算

工具类:

包含坐标和geohash的转换;查询geohash周围九格的geohash(一个“搜索附近”功能的解决方法)

import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;

public class GeoHash {
    private static int numbits = 6 * 5; //经纬度单独编码长度  
    //32位编码对应字符
    final static char[] digits = { '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 static String BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz"; 
    //定义编码映射关系  
    final static HashMap lookup = new HashMap();  
    //初始化编码映射内容
    static {  
        int i = 0;  
        for (char c : digits)  
            lookup.put(c, i++);  
    }  
    public GeoHash(){
	setMap();
    }
    //对编码后的字符串解码
    public double[] decode(String geohash) {  
        StringBuilder buffer = new StringBuilder();  
        for (char c : geohash.toCharArray()) {  
            int i = lookup.get(c) + 32;  
            buffer.append( Integer.toString(i, 2).substring(1) );  
        }
        BitSet lonset = new BitSet();  
        BitSet latset = new BitSet();  
        //偶数位,经度
        int j =0;  
        for (int i=0; i< numbits*2;i+=2) {  
            boolean isSet = false;  
            if ( i < buffer.length() )  
              isSet = buffer.charAt(i) == '1';  
            lonset.set(j++, isSet);  
        }  
        //奇数位,纬度 
        j=0;  
        for (int i=1; i< numbits*2;i+=2) {  
            boolean isSet = false;  
            if ( i < buffer.length() )  
              isSet = buffer.charAt(i) == '1';  
            latset.set(j++, isSet);  
        }  
        double lon = decode(lonset, -180, 180);  
        double lat = decode(latset, -90, 90);  
        return new double[] {lat, lon};       
    }  
    
    //根据二进制和范围解码
    private double decode(BitSet bs, double floor, double ceiling) {  
        double mid = 0;  
        for (int i=0; i= mid) {  
                buffer.set(i);  
                floor = mid;  
            } else {  
                ceiling = mid;  
            }  
        }  
        return buffer;  
    }  
  
    //将经纬度合并后的二进制进行指定的32位编码
    private String base32(long i) {  
        char[] buf = new char[65];  
        int charPos = 64;  
        boolean negative = (i < 0);  
        if (!negative)  
            i = -i;  
        while (i <= -32) {  
            buf[charPos--] = digits[(int) (-(i % 32))];  
            i /= 32;  
        }  
        buf[charPos] = digits[(int) (-i)];  
        if (negative)  
            buf[--charPos] = '-';  
        return new String(buf, charPos, (65 - charPos));  
    }  
    
    /***********************获取九个的矩形编码****************************************/
	
    public static Map BORDERS = new HashMap();
    public static Map NEIGHBORS = new HashMap();
	
    public static void setMap() {
	NEIGHBORS.put("right:even", "bc01fg45238967deuvhjyznpkmstqrwx");
	NEIGHBORS.put("left:even", "238967debc01fg45kmstqrwxuvhjyznp");
	NEIGHBORS.put("top:even", "p0r21436x8zb9dcf5h7kjnmqesgutwvy");
	NEIGHBORS.put("bottom:even", "14365h7k9dcfesgujnmqp0r2twvyx8zb");
		
	NEIGHBORS.put("right:odd", "p0r21436x8zb9dcf5h7kjnmqesgutwvy");
	NEIGHBORS.put("left:odd", "14365h7k9dcfesgujnmqp0r2twvyx8zb");
	NEIGHBORS.put("top:odd", "bc01fg45238967deuvhjyznpkmstqrwx");
	NEIGHBORS.put("bottom:odd", "238967debc01fg45kmstqrwxuvhjyznp");
		
	BORDERS.put("right:even", "bcfguvyz");
	BORDERS.put("left:even", "0145hjnp");
	BORDERS.put("top:even", "prxz");
	BORDERS.put("bottom:even", "028b");
	
	BORDERS.put("right:odd", "prxz");
	BORDERS.put("left:odd", "028b");
	BORDERS.put("top:odd", "bcfguvyz");
	BORDERS.put("bottom:odd", "0145hjnp");
    }

    /**获取九个点的矩形编码
     * @param geohash
     * @return
     */
    public static String[] getGeoHashExpand(String geohash){
	try {
	    System.out.println("=========	"+geohash);
	    String geohashTop = calculateAdjacent(geohash, "top");
	    System.out.println("=========	"+geohashTop);
			
	    String geohashBottom = calculateAdjacent(geohash, "bottom");
	    System.out.println("=========	"+geohashBottom);
			
	    String geohashRight = calculateAdjacent(geohash, "right");
	    System.out.println("=========	"+geohashRight);
			
	    String geohashLeft = calculateAdjacent(geohash, "left");
	    System.out.println("=========	"+geohashLeft);
			
	    String geohashTopLeft = calculateAdjacent(geohashLeft, "top");
	    System.out.println("=========	"+geohashTopLeft);
			
	    String geohashTopRight = calculateAdjacent(geohashRight, "top");
	    System.out.println("=========	"+geohashTopRight);
			
	    String geohashBottomRight = calculateAdjacent(geohashRight, "bottom");
	    System.out.println("=========	"+geohashBottomRight);
			
	    String geohashBottomLeft = calculateAdjacent(geohashLeft, "bottom");
	    System.out.println("=========	"+geohashBottomLeft);
	    String[] expand = {geohash, geohashTop, geohashBottom, geohashRight, geohashLeft, geohashTopLeft, geohashTopRight, geohashBottomRight, geohashBottomLeft};
	    return expand;
        } catch (Exception e) {
	    System.out.println(e.getMessage());
	    return null;
        }
    }
	
    /**分别计算每个点的矩形编码
     * @param srcHash
     * @param dir
     * @return
     */
    public static String calculateAdjacent(String srcHash, String dir) {
	srcHash = srcHash.toLowerCase();
	char lastChr = srcHash.charAt(srcHash.length()-1);
	int a = srcHash.length()%2;
	String type = (a>0)?"odd":"even";
	String base = srcHash.substring(0,srcHash.length()-1);
	if (BORDERS.get(dir+":"+type).indexOf(lastChr)!=-1){
		base = calculateAdjacent(base, dir);
	}
	base = base + BASE32.toCharArray()[(NEIGHBORS.get(dir+":"+type).indexOf(lastChr))];
	return base;
    } 
	
    public static void main(String[] args)  throws Exception{  
        GeoHash geohash = new GeoHash();
        String s = geohash.encode(45, 125);
        System.out.println(s);
        double[] geo = geohash.decode(s);
        System.out.println(geo[0]+" "+geo[1]);
        String[] points = getGeoHashExpand(s);
    }  
}

你可能感兴趣的:(java)