|
3 | 3 | import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
|
4 | 4 | import org.locationtech.spatial4j.shape.Shape;
|
5 | 5 |
|
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collection; |
| 8 | + |
6 | 9 | /**
|
7 | 10 | * 参考:
|
8 | 11 | * 1. https://gist.github.com/jp1017/71bd0976287ce163c11a7cb963b04dd8
|
@@ -46,6 +49,21 @@ public class GeoUtils {
|
46 | 49 | */
|
47 | 50 | public static final String[] GEO_STRING_FORMAT = new String[]{"wkt", "geojson"};
|
48 | 51 |
|
| 52 | + /** |
| 53 | + * Copied from org.elasticsearch.geometry.utils.Geohash |
| 54 | + */ |
| 55 | + private static final char[] BASE_32 = new char[]{'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'}; |
| 56 | + |
| 57 | + /** |
| 58 | + * Copied from org.elasticsearch.geometry.utils.Geohash |
| 59 | + */ |
| 60 | + private static final String BASE_32_STRING; |
| 61 | + |
| 62 | + static { |
| 63 | + // Copied from org.elasticsearch.geometry.utils.Geohash |
| 64 | + BASE_32_STRING = new String(BASE_32); |
| 65 | + } |
| 66 | + |
49 | 67 | /**
|
50 | 68 | * Description:
|
51 | 69 | * 根据 GCJ-02 逻辑计算坐标是否位于中国之外
|
@@ -217,4 +235,75 @@ public static Shape readShape(String shapeString, String shapeStringFormat) {
|
217 | 235 |
|
218 | 236 | return shape;
|
219 | 237 | }
|
| 238 | + |
| 239 | + /** |
| 240 | + * Copied from org.elasticsearch.geometry.utils.Geohash |
| 241 | + */ |
| 242 | + public static Collection<? extends CharSequence> getNeighbors(String geohash) { |
| 243 | + return addNeighborsAtLevel(geohash, geohash.length(), new ArrayList(8)); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Copied from org.elasticsearch.geometry.utils.Geohash |
| 248 | + */ |
| 249 | + public static <E extends Collection<? super String>> void addNeighbors(String geohash, E neighbors) { |
| 250 | + addNeighborsAtLevel(geohash, geohash.length(), neighbors); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Copied from org.elasticsearch.geometry.utils.Geohash |
| 255 | + */ |
| 256 | + public static <E extends Collection<? super String>> E addNeighborsAtLevel(String geohash, int level, E neighbors) { |
| 257 | + String south = getNeighbor(geohash, level, 0, -1); |
| 258 | + String north = getNeighbor(geohash, level, 0, 1); |
| 259 | + if (north != null) { |
| 260 | + neighbors.add(getNeighbor(north, level, -1, 0)); |
| 261 | + neighbors.add(north); |
| 262 | + neighbors.add(getNeighbor(north, level, 1, 0)); |
| 263 | + } |
| 264 | + |
| 265 | + neighbors.add(getNeighbor(geohash, level, -1, 0)); |
| 266 | + neighbors.add(getNeighbor(geohash, level, 1, 0)); |
| 267 | + if (south != null) { |
| 268 | + neighbors.add(getNeighbor(south, level, -1, 0)); |
| 269 | + neighbors.add(south); |
| 270 | + neighbors.add(getNeighbor(south, level, 1, 0)); |
| 271 | + } |
| 272 | + |
| 273 | + return neighbors; |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Copied from org.elasticsearch.geometry.utils.Geohash |
| 278 | + */ |
| 279 | + public static String getNeighbor(String geohash, int level, int dx, int dy) { |
| 280 | + int cell = BASE_32_STRING.indexOf(geohash.charAt(level - 1)); |
| 281 | + int x0 = cell & 1; |
| 282 | + int y0 = cell & 2; |
| 283 | + int x1 = cell & 4; |
| 284 | + int y1 = cell & 8; |
| 285 | + int x2 = cell & 16; |
| 286 | + int x = x0 + x1 / 2 + x2 / 4; |
| 287 | + int y = y0 / 2 + y1 / 4; |
| 288 | + if (level != 1) { |
| 289 | + int nx = level % 2 == 1 ? x + dx : x + dy; |
| 290 | + int ny = level % 2 == 1 ? y + dy : y + dx; |
| 291 | + if (nx >= 0 && nx <= 7 && ny >= 0 && ny <= 3) { |
| 292 | + String var10000 = geohash.substring(0, level - 1); |
| 293 | + return var10000 + encodeBase32(nx, ny); |
| 294 | + } else { |
| 295 | + String neighbor = getNeighbor(geohash, level - 1, dx, dy); |
| 296 | + return neighbor != null ? neighbor + encodeBase32(nx, ny) : neighbor; |
| 297 | + } |
| 298 | + } else { |
| 299 | + return (dy >= 0 || y != 0) && (dy <= 0 || y != 3) ? Character.toString(encodeBase32(x + dx, y + dy)) : null; |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + /** |
| 304 | + * Copied from org.elasticsearch.geometry.utils.Geohash |
| 305 | + */ |
| 306 | + private static char encodeBase32(int x, int y) { |
| 307 | + return BASE_32[((x & 1) + (y & 1) * 2 + (x & 2) * 2 + (y & 2) * 4 + (x & 4) * 4) % 32]; |
| 308 | + } |
220 | 309 | }
|
0 commit comments