Skip to content

Commit 08d2889

Browse files
committed
删除 elasticsearch-geo 依赖
1 parent 9bd25ed commit 08d2889

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<hive.version>2.3.8</hive.version>
3838
<hadoop.version>2.10.1</hadoop.version>
3939

40-
<elasticsearch-geo.version>7.17.18</elasticsearch-geo.version>
4140
<gt-main.version>30.1</gt-main.version>
4241
<gt-epsg-hsql.version>30.2</gt-epsg-hsql.version>
4342
<gson.version>2.10.1</gson.version>
@@ -94,11 +93,6 @@
9493
</exclusions>
9594
</dependency>
9695

97-
<dependency>
98-
<groupId>org.elasticsearch</groupId>
99-
<artifactId>elasticsearch-geo</artifactId>
100-
<version>${elasticsearch-geo.version}</version>
101-
</dependency>
10296
<dependency>
10397
<groupId>com.google.guava</groupId>
10498
<artifactId>guava</artifactId>

src/main/java/tech/leovan/hive/udf/geo/PolygonGeohashUDF.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package tech.leovan.hive.udf.geo;
22

3-
import org.elasticsearch.geometry.utils.Geohash;
43
import tech.leovan.hive.udf.utils.GeoUtils;
54
import org.apache.hadoop.hive.ql.exec.Description;
65
import org.apache.hadoop.hive.ql.exec.UDF;
@@ -63,7 +62,7 @@ public List<String> evaluate(
6362
geohashes.add(geohash);
6463

6564
List<String> geohashNeighbors = new ArrayList<>();
66-
Geohash.addNeighbors(geohash, geohashNeighbors);
65+
GeoUtils.addNeighbors(geohash, geohashNeighbors);
6766

6867
for (String geohashNeighbor : geohashNeighbors) {
6968
if (!geohashes.contains(geohashNeighbor) && !testingGeohashes.contains(geohashNeighbor)) {

src/main/java/tech/leovan/hive/udf/utils/GeoUtils.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
44
import org.locationtech.spatial4j.shape.Shape;
55

6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
69
/**
710
* 参考:
811
* 1. https://gist.github.com/jp1017/71bd0976287ce163c11a7cb963b04dd8
@@ -46,6 +49,21 @@ public class GeoUtils {
4649
*/
4750
public static final String[] GEO_STRING_FORMAT = new String[]{"wkt", "geojson"};
4851

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+
4967
/**
5068
* Description:
5169
* 根据 GCJ-02 逻辑计算坐标是否位于中国之外
@@ -217,4 +235,75 @@ public static Shape readShape(String shapeString, String shapeStringFormat) {
217235

218236
return shape;
219237
}
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+
}
220309
}

0 commit comments

Comments
 (0)