import java.util.ArrayList;
import java.util.List;
public class SpaceCoordinateHelper {
public final double Max_Lat = 90;
public final double Min_Lat = -90;
public final double Max_Lng = 180;
public final double Min_Lng = -180;
/**
* 纬度二值串
*/
public static List<String> latList = new ArrayList<>();
/**
* 经度二值串
*/
public static List<String> lngList = new ArrayList<>();
/**
* 纬度估计值
*/
public static int lat;
/**
* 经度估计值
*/
public static int lng;
private final String[] base32Lookup = {
"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 double[] getSpaceCoordinate(String geoHashCode) {
List<Integer> list = base32Decode(geoHashCode);
System.out.println(list);
String str = convertToIndex(list);
System.out.println(str);
splitLatAndLng(str);
System.out.println("latList: " + latList);
System.out.println("lngList: " + lngList);
System.out.println(revert(Min_Lat, Max_Lat, latList));
System.out.println(revert(Min_Lng, Max_Lng, lngList));
return new double[]{lat, lng};
}
/**
* 将二值串转换为经纬度值
*
* @param min 区间最小值
* @param max 区间最大值
* @param list 二值串列表
*/
public double revert(double min, double max, List<String> list) {
double value = 0;
double mid;
if (list.size() <= 0) {
return (max + min) / 2.0;
}
for (String flag : list) {
mid = (max + min) / 2;
if ("0".equals(flag)) {
max = mid;
}
if ("1".equals(flag)) {
min = mid;
}
value = (max + min) / 2;
}
return Double.parseDouble(String.format("%.6f", value));
}
/**
* 分离经度与纬度串
*
* @param latAndLngStr 经纬度二值串
*/
public void splitLatAndLng(String latAndLngStr) {
for (int i = 0; i < latAndLngStr.length(); i++) {
// 奇数位,纬度
if (i % 2 == 1) {
latList.add(String.valueOf(latAndLngStr.charAt(i)));
} else {
// 偶数位,经度
lngList.add(String.valueOf(latAndLngStr.charAt(i)));
}
}
}
/**
* 将十进制数转为五个二进制数
*
* @param nums 十进制数
* @return 五个二进制数
*/
private String convertToIndex(List<Integer> nums) {
StringBuilder str = new StringBuilder();
for (Integer num : nums) {
StringBuilder sb = new StringBuilder(Integer.toBinaryString(num));
int length = sb.length();
if (length < 5) {
for (int i = 0; i < 5 - length; i++) {
sb.insert(0, "0");
}
}
str.append(sb);
}
return str.toString();
}
/**
* 将base32串转为合并的二值串
*
* @param str base32串
* @return 合并的二值串
*/
private List<Integer> base32Decode(String str) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
String ch = String.valueOf(str.charAt(i));
for (int j = 0; j < base32Lookup.length; j++) {
if (base32Lookup[j].equals(ch)) {
list.add(j);
}
}
}
return list;
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
SpaceCoordinateHelper spaceCoordinateHelper = new SpaceCoordinateHelper();
spaceCoordinateHelper.getSpaceCoordinate("wx4g0s8");
System.out.println("耗时 " + (System.currentTimeMillis() - start) + " ms");
}
}
GeoHash转经纬度
最新推荐文章于 2021-07-07 09:23:18 发布