Skip to content

Commit

Permalink
删除 elasticsearch-geo 依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
leovan committed Feb 12, 2024
1 parent 9bd25ed commit 08d2889
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
<hive.version>2.3.8</hive.version>
<hadoop.version>2.10.1</hadoop.version>

<elasticsearch-geo.version>7.17.18</elasticsearch-geo.version>
<gt-main.version>30.1</gt-main.version>
<gt-epsg-hsql.version>30.2</gt-epsg-hsql.version>
<gson.version>2.10.1</gson.version>
Expand Down Expand Up @@ -94,11 +93,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-geo</artifactId>
<version>${elasticsearch-geo.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tech.leovan.hive.udf.geo;

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

List<String> geohashNeighbors = new ArrayList<>();
Geohash.addNeighbors(geohash, geohashNeighbors);
GeoUtils.addNeighbors(geohash, geohashNeighbors);

for (String geohashNeighbor : geohashNeighbors) {
if (!geohashes.contains(geohashNeighbor) && !testingGeohashes.contains(geohashNeighbor)) {
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/tech/leovan/hive/udf/utils/GeoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
import org.locationtech.spatial4j.shape.Shape;

import java.util.ArrayList;
import java.util.Collection;

/**
* 参考:
* 1. https://gist.github.com/jp1017/71bd0976287ce163c11a7cb963b04dd8
Expand Down Expand Up @@ -46,6 +49,21 @@ public class GeoUtils {
*/
public static final String[] GEO_STRING_FORMAT = new String[]{"wkt", "geojson"};

/**
* Copied from org.elasticsearch.geometry.utils.Geohash
*/
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'};

/**
* Copied from org.elasticsearch.geometry.utils.Geohash
*/
private static final String BASE_32_STRING;

static {
// Copied from org.elasticsearch.geometry.utils.Geohash
BASE_32_STRING = new String(BASE_32);
}

/**
* Description:
* 根据 GCJ-02 逻辑计算坐标是否位于中国之外
Expand Down Expand Up @@ -217,4 +235,75 @@ public static Shape readShape(String shapeString, String shapeStringFormat) {

return shape;
}

/**
* Copied from org.elasticsearch.geometry.utils.Geohash
*/
public static Collection<? extends CharSequence> getNeighbors(String geohash) {
return addNeighborsAtLevel(geohash, geohash.length(), new ArrayList(8));
}

/**
* Copied from org.elasticsearch.geometry.utils.Geohash
*/
public static <E extends Collection<? super String>> void addNeighbors(String geohash, E neighbors) {
addNeighborsAtLevel(geohash, geohash.length(), neighbors);
}

/**
* Copied from org.elasticsearch.geometry.utils.Geohash
*/
public static <E extends Collection<? super String>> E addNeighborsAtLevel(String geohash, int level, E neighbors) {
String south = getNeighbor(geohash, level, 0, -1);
String north = getNeighbor(geohash, level, 0, 1);
if (north != null) {
neighbors.add(getNeighbor(north, level, -1, 0));
neighbors.add(north);
neighbors.add(getNeighbor(north, level, 1, 0));
}

neighbors.add(getNeighbor(geohash, level, -1, 0));
neighbors.add(getNeighbor(geohash, level, 1, 0));
if (south != null) {
neighbors.add(getNeighbor(south, level, -1, 0));
neighbors.add(south);
neighbors.add(getNeighbor(south, level, 1, 0));
}

return neighbors;
}

/**
* Copied from org.elasticsearch.geometry.utils.Geohash
*/
public static String getNeighbor(String geohash, int level, int dx, int dy) {
int cell = BASE_32_STRING.indexOf(geohash.charAt(level - 1));
int x0 = cell & 1;
int y0 = cell & 2;
int x1 = cell & 4;
int y1 = cell & 8;
int x2 = cell & 16;
int x = x0 + x1 / 2 + x2 / 4;
int y = y0 / 2 + y1 / 4;
if (level != 1) {
int nx = level % 2 == 1 ? x + dx : x + dy;
int ny = level % 2 == 1 ? y + dy : y + dx;
if (nx >= 0 && nx <= 7 && ny >= 0 && ny <= 3) {
String var10000 = geohash.substring(0, level - 1);
return var10000 + encodeBase32(nx, ny);
} else {
String neighbor = getNeighbor(geohash, level - 1, dx, dy);
return neighbor != null ? neighbor + encodeBase32(nx, ny) : neighbor;
}
} else {
return (dy >= 0 || y != 0) && (dy <= 0 || y != 3) ? Character.toString(encodeBase32(x + dx, y + dy)) : null;
}
}

/**
* Copied from org.elasticsearch.geometry.utils.Geohash
*/
private static char encodeBase32(int x, int y) {
return BASE_32[((x & 1) + (y & 1) * 2 + (x & 2) * 2 + (y & 2) * 4 + (x & 4) * 4) % 32];
}
}

0 comments on commit 08d2889

Please sign in to comment.