GeoHash(Java实现)
- package com.koubei.collect_script.demo;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- /**
- * @author kelin.ll
- * @date on 2018/6/13
- */
- public class GeoHashDemo {
- 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 GeoHashDemo(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;
- }
- 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;
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- GeoHashDemo g = new GeoHashDemo(40.222012, 116.248283);
- g.sethashLength(12);
- System.out.println("当前坐标:"+g.getGeoHashBase32());
- //for (String str:
- // g.getGeoHashBase32For9()) {
- // System.out.println(str);
- //}
- }
- }
GeoHash(Java实现)的更多相关文章
- 【算法】(查找你附近的人) GeoHash核心原理解析及代码实现
本文地址 原文地址 分享提纲: 0. 引子 1. 感性认识GeoHash 2. GeoHash算法的步骤 3. GeoHash Base32编码长度与精度 4. GeoHash算法 5. 使用注意点( ...
- Redis GEO ,GEOHASH,Spatial_index
https://matt.sh/redis-geo http://antirez.com/latest/0 http://invece.org/ https://github.com/davidmot ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 查找附近网点geohash算法及实现 (Java版本号)
參考文档: http://blog.csdn.net/wangxiafghj/article/details/9014363geohash 算法原理及实现方式 http://blog.charlee ...
- GeoHash解析及java实现
GeoHash解析请参考这里: http://www.open-open.com/lib/view/open1417940079964.html java实现GeoHash,代码已注释. import ...
- GeoHash核心原理解析及java代码实现(转)
原文链接:http://blog.jobbole.com/80633/ 引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按来查询一些好玩的东西.某一天机机到北海公园游玩,肚肚饿了,于是乎 ...
- Java应用:经纬度匹配(geohash加密)
本文采用http://gc.ditu.aliyun.com地址进行经纬度匹配,无数量限制 如果给定经纬度进行geohash加密操作,先解密得到相应gps坐标,具体程序如下所示: import java ...
- JAVA实现将GeoHash转化为相应的经纬度坐标
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/50568428 http://www.llwjy.com/blogdetail/f ...
- Java资源大全中文版(Awesome最新版)
Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...
随机推荐
- 为什么使用SLF4J?
每个Java开发人员都知道日志记录对Java应用的重要性,尤其是对服务端应用,而且其中许多人都已经熟悉了各种记录日志的库,比如java.util.logging,Apache的log4j,logb ...
- Mac下的unity兼容问题,打开项目提示错误:!GetPersistentManager().IsStreamLoaded(assetPath)
安装unity成功之后,打开unity提示如下 那么打开的unity项目很有可能出现找不到资源的错误. 在检查一下你的Mac宗卷格式,如果是APFS格式的话,基本都会不兼容低版本的32位unity应用 ...
- 【easy】268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode 9. Palindrome Number(c语言版)
题目: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...
- 微信小程序rich-text 文本首行缩进和图片居中
微信小程序开发使用rich-text组件渲染html格式的代码,常常因为不能自定义css导致文本不能缩进,以及图片不能居中等问题,这里可以考虑使用js的replace方法,替换字符串,然后在渲染的同时 ...
- Ubuntu 14.04 升级 nginx/1.8.1
参考文档:https://segmentfault.com/a/1190000008116875 https://www.ilanni.com/?p=11788 先停止nginx服务 service ...
- Shell执行*.sql
> mysql -uroot -p123456 > use db_test > source /root/temp.sql
- linux命令详解之df命令
df命令概述df命令作用是列出文件系统的整体磁盘空间使用情况.可以用来查看磁盘已被使用多少空间和还剩余多少空间. df命令显示系统中包含每个文件名参数的磁盘使用情况,如果没有文件名参数,则显示所有当前 ...
- System.TimeoutException: The operation requested on PersistentChannel timed out
这个异常是在使用EasyNetQ时,遇到的问题,找了两个小时. 详细错误 Error:System.TimeoutException: The operation requested on Persi ...
- 实验二《Java面向对象程序设计》实验报告
一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 1.三种代码:伪代码.测 ...