geoHash编码、解码(java实现)

该博客介绍了Geohash算法的Java实现,包括编码和解码过程。编码部分展示了如何根据经纬度和精度生成Geohash字符串,解码部分则返回了Geohash对应的地理位置范围的四个顶点坐标。通过这段代码,开发者可以理解Geohash的工作原理并应用于地理信息系统的坐标处理。
摘要由CSDN通过智能技术生成

geohash算法原理自行百度,都讲的很详细,此处只贴实现代码

Base32

	//32位编码对应字符
    final static char[] Base32 = { '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  int[] Bits = new int[] { 16, 8, 4, 2, 1 };
    public static String digits = new String(Base32);

编码

precision为精度系数

public static String Encode(double latitude, double longitude, int precision)
    {
        boolean even = true;
        int bit = 0;
        int ch = 0;
        String geohash = "";

        double[] lat = { -90.0, 90.0 };
        double[] lon = { -180.0, 180.0 };

        if (precision < 1 || precision > 20) precision = 12;

        while (geohash.length() < precision)
        {
            double mid;

            if (even)
            {
                mid = (lon[0] + lon[1]) / 2;
                if (longitude > mid)
                {
                    ch |= Bits[bit];
                    lon[0] = mid;
                }
                else
                    lon[1] = mid;
            }
            else
            {
                mid = (lat[0] + lat[1]) / 2;
                if (latitude > mid)
                {
                    ch |= Bits[bit];
                    lat[0] = mid;
                }
                else
                    lat[1] = mid;
            }

            even = !even;
            if (bit < 4)
                bit++;
            else
            {
                geohash += Base32[ch];
                bit = 0;
                ch = 0;
            }
        }
        return geohash;
    }

解码

此处返回的是geohash对应范围的四个顶点坐标点

public static List<LatLng> decode(String geoHash) {
        String _geoHash = geoHash.toLowerCase();
        String beforeLngCode = "";
        String beforeLatCode = "";
        String code = "";
        double minLng = -180;
        double maxLng = 180;
        double minLat = -90;
        double maxLat = 90;
        // 经纬度完整二进制编码
        for (int i = 0; i < _geoHash.length(); i++) {
            int index = digits.indexOf(_geoHash.charAt(i));
            // 数字转为二进制,小于16的要补0,因此所有数字加32,然后移除第一位
            String str = Integer.toBinaryString(index + 32);
            code += str.substring(1);
        }
        // 分解获得经纬度编码
        for (int i = 0; i < code.length(); i++) {
            if (i % 2 == 0) {  // 偶数位经度
                beforeLngCode += code.charAt(i);
            } else {   // 奇数位纬度
                beforeLatCode += code.charAt(i);
            }
        }
        // 经度范围
        for (char c : beforeLngCode.toCharArray()) {
            if (c == '0') {  // 左半区
                maxLng = (minLng + maxLng) / 2;
            } else {  // 右半区
                minLng = (minLng + maxLng) / 2;
            }
        }
        // 纬度范围
        for (char c : beforeLatCode.toCharArray()) {
            if (c == '0') {  // 下半区
                maxLat = (minLat + maxLat) / 2;
            } else {   // 上半区
                minLat = (minLat + maxLat) / 2;
            }
        }

        List<LatLng> latLngs = new ArrayList<>();
        latLngs.add(new LatLng(minLat, minLng));
        latLngs.add(new LatLng(minLat, maxLng));
        latLngs.add(new LatLng(maxLat, minLng));
        latLngs.add(new LatLng(maxLat, maxLng));

        return latLngs;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值