1. package com.koubei.collect_script.demo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. /**
  8. * @author kelin.ll
  9. * @date on 2018/6/13
  10. */
  11. public class GeoHashDemo {
  12. private LocationBean location;
  13. /**
  14. * 1 2500km;2 630km;3 78km;4 30km
  15. * 5 2.4km; 6 610m; 7 76m; 8 19m
  16. */
  17. private int hashLength = 8; //经纬度转化为geohash长度
  18. private int latLength = 20; //纬度转化为二进制长度
  19. private int lngLength = 20; //经度转化为二进制长度
  20.  
  21. private double minLat;//每格纬度的单位大小
  22. private double minLng;//每个经度的倒下
  23. private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', '7',
  24. '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n',
  25. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  26.  
  27. public GeoHashDemo(double lat, double lng) {
  28. location = new LocationBean(lat, lng);
  29. setMinLatLng();
  30. }
  31.  
  32. public int gethashLength() {
  33. return hashLength;
  34. }
  35.  
  36. /**
  37. * @Author:lulei
  38. * @Description: 设置经纬度的最小单位
  39. */
  40. private void setMinLatLng() {
  41. minLat = LocationBean.MAXLAT - LocationBean.MINLAT;
  42. for (int i = 0; i < latLength; i++) {
  43. minLat /= 2.0;
  44. }
  45. minLng = LocationBean.MAXLNG - LocationBean.MINLNG;
  46. for (int i = 0; i < lngLength; i++) {
  47. minLng /= 2.0;
  48. }
  49. }
  50.  
  51. /**
  52. * @return
  53. * @Author:lulei
  54. * @Description: 求所在坐标点及周围点组成的九个
  55. */
  56. public List<String> getGeoHashBase32For9() {
  57. double leftLat = location.getLat() - minLat;
  58. double rightLat = location.getLat() + minLat;
  59. double upLng = location.getLng() - minLng;
  60. double downLng = location.getLng() + minLng;
  61. List<String> base32For9 = new ArrayList<String>();
  62. //左侧从上到下 3个
  63. String leftUp = getGeoHashBase32(leftLat, upLng);
  64. if (!(leftUp == null || "".equals(leftUp))) {
  65. base32For9.add(leftUp);
  66. }
  67. String leftMid = getGeoHashBase32(leftLat, location.getLng());
  68. if (!(leftMid == null || "".equals(leftMid))) {
  69. base32For9.add(leftMid);
  70. }
  71. String leftDown = getGeoHashBase32(leftLat, downLng);
  72. if (!(leftDown == null || "".equals(leftDown))) {
  73. base32For9.add(leftDown);
  74. }
  75. //中间从上到下 3个
  76. String midUp = getGeoHashBase32(location.getLat(), upLng);
  77. if (!(midUp == null || "".equals(midUp))) {
  78. base32For9.add(midUp);
  79. }
  80. String midMid = getGeoHashBase32(location.getLat(), location.getLng());
  81. if (!(midMid == null || "".equals(midMid))) {
  82. base32For9.add(midMid);
  83. }
  84. String midDown = getGeoHashBase32(location.getLat(), downLng);
  85. if (!(midDown == null || "".equals(midDown))) {
  86. base32For9.add(midDown);
  87. }
  88. //右侧从上到下 3个
  89. String rightUp = getGeoHashBase32(rightLat, upLng);
  90. if (!(rightUp == null || "".equals(rightUp))) {
  91. base32For9.add(rightUp);
  92. }
  93. String rightMid = getGeoHashBase32(rightLat, location.getLng());
  94. if (!(rightMid == null || "".equals(rightMid))) {
  95. base32For9.add(rightMid);
  96. }
  97. String rightDown = getGeoHashBase32(rightLat, downLng);
  98. if (!(rightDown == null || "".equals(rightDown))) {
  99. base32For9.add(rightDown);
  100. }
  101. return base32For9;
  102. }
  103.  
  104. /**
  105. * @param length
  106. * @return
  107. * @Author:lulei
  108. * @Description: 设置经纬度转化为geohash长度
  109. */
  110. public boolean sethashLength(int length) {
  111. if (length < 1) {
  112. return false;
  113. }
  114. hashLength = length;
  115. latLength = (length * 5) / 2;
  116. if (length % 2 == 0) {
  117. lngLength = latLength;
  118. } else {
  119. lngLength = latLength + 1;
  120. }
  121. setMinLatLng();
  122. return true;
  123. }
  124.  
  125. /**
  126. * @return
  127. * @Author:lulei
  128. * @Description: 获取经纬度的base32字符串
  129. */
  130. public String getGeoHashBase32() {
  131. return getGeoHashBase32(location.getLat(), location.getLng());
  132. }
  133.  
  134. /**
  135. * @param lat
  136. * @param lng
  137. * @return
  138. * @Author:lulei
  139. * @Description: 获取经纬度的base32字符串
  140. */
  141. private String getGeoHashBase32(double lat, double lng) {
  142. boolean[] bools = getGeoBinary(lat, lng);
  143. if (bools == null) {
  144. return null;
  145. }
  146. StringBuffer sb = new StringBuffer();
  147. for (int i = 0; i < bools.length; i = i + 5) {
  148. boolean[] base32 = new boolean[5];
  149. for (int j = 0; j < 5; j++) {
  150. base32[j] = bools[i + j];
  151. }
  152. char cha = getBase32Char(base32);
  153. if (' ' == cha) {
  154. return null;
  155. }
  156. sb.append(cha);
  157. }
  158. return sb.toString();
  159. }
  160.  
  161. /**
  162. * @param base32
  163. * @return
  164. * @Author:lulei
  165. * @Description: 将五位二进制转化为base32
  166. */
  167. private char getBase32Char(boolean[] base32) {
  168. if (base32 == null || base32.length != 5) {
  169. return ' ';
  170. }
  171. int num = 0;
  172. for (boolean bool : base32) {
  173. num <<= 1;
  174. if (bool) {
  175. num += 1;
  176. }
  177. }
  178. return CHARS[num % CHARS.length];
  179. }
  180.  
  181. /**
  182. * @param lat
  183. * @param lng
  184. * @return
  185. * @Author:lulei
  186. * @Description: 获取坐标的geo二进制字符串
  187. */
  188. private boolean[] getGeoBinary(double lat, double lng) {
  189. boolean[] latArray = getHashArray(lat, LocationBean.MINLAT, LocationBean.MAXLAT, latLength);
  190. boolean[] lngArray = getHashArray(lng, LocationBean.MINLNG, LocationBean.MAXLNG, lngLength);
  191. return merge(latArray, lngArray);
  192. }
  193.  
  194. /**
  195. * @param latArray
  196. * @param lngArray
  197. * @return
  198. * @Author:lulei
  199. * @Description: 合并经纬度二进制
  200. */
  201. private boolean[] merge(boolean[] latArray, boolean[] lngArray) {
  202. if (latArray == null || lngArray == null) {
  203. return null;
  204. }
  205. boolean[] result = new boolean[lngArray.length + latArray.length];
  206. Arrays.fill(result, false);
  207. for (int i = 0; i < lngArray.length; i++) {
  208. result[2 * i] = lngArray[i];
  209. }
  210. for (int i = 0; i < latArray.length; i++) {
  211. result[2 * i + 1] = latArray[i];
  212. }
  213. return result;
  214. }
  215.  
  216. /**
  217. * @param value
  218. * @param min
  219. * @param max
  220. * @return
  221. * @Author:lulei
  222. * @Description: 将数字转化为geohash二进制字符串
  223. */
  224. private boolean[] getHashArray(double value, double min, double max, int length) {
  225. if (value < min || value > max) {
  226. return null;
  227. }
  228. if (length < 1) {
  229. return null;
  230. }
  231. boolean[] result = new boolean[length];
  232. for (int i = 0; i < length; i++) {
  233. double mid = (min + max) / 2.0;
  234. if (value > mid) {
  235. result[i] = true;
  236. min = mid;
  237. } else {
  238. result[i] = false;
  239. max = mid;
  240. }
  241. }
  242. return result;
  243. }
  244.  
  245. class LocationBean {
  246. public static final double MINLAT = -90;
  247. public static final double MAXLAT = 90;
  248. public static final double MINLNG = -180;
  249. public static final double MAXLNG = 180;
  250. private double lat;//纬度[-90,90]
  251. private double lng;//经度[-180,180]
  252.  
  253. public LocationBean(double lat, double lng) {
  254. this.lat = lat;
  255. this.lng = lng;
  256. }
  257. public double getLat() {
  258. return lat;
  259. }
  260. public void setLat(double lat) {
  261. this.lat = lat;
  262. }
  263. public double getLng() {
  264. return lng;
  265. }
  266. public void setLng(double lng) {
  267. this.lng = lng;
  268. }
  269. }
  270.  
  271. public static void main(String[] args) {
  272. // TODO Auto-generated method stub
  273. GeoHashDemo g = new GeoHashDemo(40.222012, 116.248283);
  274. g.sethashLength(12);
  275. System.out.println("当前坐标:"+g.getGeoHashBase32());
  276. //for (String str:
  277. // g.getGeoHashBase32For9()) {
  278. // System.out.println(str);
  279. //}
  280. }
  281. }

  

GeoHash(Java实现)的更多相关文章

  1. 【算法】(查找你附近的人) GeoHash核心原理解析及代码实现

    本文地址 原文地址 分享提纲: 0. 引子 1. 感性认识GeoHash 2. GeoHash算法的步骤 3. GeoHash Base32编码长度与精度 4. GeoHash算法 5. 使用注意点( ...

  2. Redis GEO ,GEOHASH,Spatial_index

    https://matt.sh/redis-geo http://antirez.com/latest/0 http://invece.org/ https://github.com/davidmot ...

  3. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  4. 查找附近网点geohash算法及实现 (Java版本号)

    參考文档: http://blog.csdn.net/wangxiafghj/article/details/9014363geohash  算法原理及实现方式 http://blog.charlee ...

  5. GeoHash解析及java实现

    GeoHash解析请参考这里: http://www.open-open.com/lib/view/open1417940079964.html java实现GeoHash,代码已注释. import ...

  6. GeoHash核心原理解析及java代码实现(转)

    原文链接:http://blog.jobbole.com/80633/ 引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按来查询一些好玩的东西.某一天机机到北海公园游玩,肚肚饿了,于是乎 ...

  7. Java应用:经纬度匹配(geohash加密)

    本文采用http://gc.ditu.aliyun.com地址进行经纬度匹配,无数量限制 如果给定经纬度进行geohash加密操作,先解密得到相应gps坐标,具体程序如下所示: import java ...

  8. JAVA实现将GeoHash转化为相应的经纬度坐标

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/50568428 http://www.llwjy.com/blogdetail/f ...

  9. Java资源大全中文版(Awesome最新版)

    Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...

随机推荐

  1. 为什么使用SLF4J?

      每个Java开发人员都知道日志记录对Java应用的重要性,尤其是对服务端应用,而且其中许多人都已经熟悉了各种记录日志的库,比如java.util.logging,Apache的log4j,logb ...

  2. Mac下的unity兼容问题,打开项目提示错误:!GetPersistentManager().IsStreamLoaded(assetPath)

    安装unity成功之后,打开unity提示如下 那么打开的unity项目很有可能出现找不到资源的错误. 在检查一下你的Mac宗卷格式,如果是APFS格式的话,基本都会不兼容低版本的32位unity应用 ...

  3. 【easy】268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  4. LeetCode 9. Palindrome Number(c语言版)

    题目: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...

  5. 微信小程序rich-text 文本首行缩进和图片居中

    微信小程序开发使用rich-text组件渲染html格式的代码,常常因为不能自定义css导致文本不能缩进,以及图片不能居中等问题,这里可以考虑使用js的replace方法,替换字符串,然后在渲染的同时 ...

  6. Ubuntu 14.04 升级 nginx/1.8.1

    参考文档:https://segmentfault.com/a/1190000008116875 https://www.ilanni.com/?p=11788 先停止nginx服务 service ...

  7. Shell执行*.sql

    > mysql -uroot -p123456 > use db_test > source /root/temp.sql

  8. linux命令详解之df命令

    df命令概述df命令作用是列出文件系统的整体磁盘空间使用情况.可以用来查看磁盘已被使用多少空间和还剩余多少空间. df命令显示系统中包含每个文件名参数的磁盘使用情况,如果没有文件名参数,则显示所有当前 ...

  9. System.TimeoutException: The operation requested on PersistentChannel timed out

    这个异常是在使用EasyNetQ时,遇到的问题,找了两个小时. 详细错误 Error:System.TimeoutException: The operation requested on Persi ...

  10. 实验二《Java面向对象程序设计》实验报告

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 1.三种代码:伪代码.测 ...