Skip to content

Commit

Permalink
清理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
leovan committed May 29, 2024
1 parent fbeadd8 commit 918fb04
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/main/java/tech/leovan/hive/udf/geo/PolygonAreaUDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public Double evaluate(String polygonString, String polygonStringFormat) {
return null;
}

Geometry polygon = CTX.getShapeFactory().getGeometryFrom(shape);
if (!(polygon instanceof Polygon || polygon instanceof MultiPolygon)) {
Geometry geometry = CTX.getShapeFactory().getGeometryFrom(shape);
if (!(geometry instanceof Polygon || geometry instanceof MultiPolygon)) {
return null;
}

try {
MathTransform transform = CRS.findMathTransform(SOURCE_CRS, TARGET_CRS, false);
Geometry geometryMercator = JTS.transform(polygon, transform);
Geometry geometryMercator = JTS.transform(geometry, transform);

return geometryMercator.getArea();
} catch (Exception e) {
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/tech/leovan/hive/udf/geo/PolygonGeohashUDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,26 @@ public List<String> evaluate(
return null;
}

Geometry polygon = CTX.getShapeFactory().getGeometryFrom(shape);
if (!(polygon instanceof Polygon || polygon instanceof MultiPolygon)) {
Geometry geometry = CTX.getShapeFactory().getGeometryFrom(shape);
if (!(geometry instanceof Polygon || geometry instanceof MultiPolygon)) {
return null;
}
// 如果是单多边形区域,直接采用原开源代码计算方式。如果是多多边形需要按照单多边形计算后再去重合并
Set<String> geohashes = new HashSet();
if (polygon instanceof Polygon) {
geohashes = getGeoHashes(polygon, precision);
}else if(polygon instanceof MultiPolygon){
for (int i = 0; i < polygon.getNumGeometries(); i++){
Geometry singlePolygon = polygon.getGeometryN(i);
geohashes.addAll(getGeoHashes(singlePolygon, precision));

Set<String> geohashes = new HashSet<>();

if (geometry instanceof Polygon) {
geohashes = getGeoHashes((Polygon) geometry, precision);
} else {
for (int i = 0; i < geometry.getNumGeometries(); i++){
Geometry polygon = geometry.getGeometryN(i);
geohashes.addAll(getGeoHashes((Polygon) polygon, precision));
}
}

return new ArrayList<>(geohashes);
}

private Set<String> getGeoHashes(Geometry polygon, Integer precision){
private Set<String> getGeoHashes(Polygon polygon, Integer precision){
Point centroid = polygon.getCentroid();

Set<String> geohashes = new HashSet<>();
Expand All @@ -68,8 +70,7 @@ private Set<String> getGeoHashes(Geometry polygon, Integer precision){

while (!testingGeohashes.isEmpty()) {
String geohash = testingGeohashes.poll();
Geometry geohashGeometry = CTX.getShapeFactory().getGeometryFrom(
GeohashUtils.decodeBoundary(geohash, CTX));
Geometry geohashGeometry = CTX.getShapeFactory().getGeometryFrom(GeohashUtils.decodeBoundary(geohash, CTX));

if (polygon.contains(geohashGeometry) || polygon.intersects(geohashGeometry)) {
geohashes.add(geohash);
Expand All @@ -84,6 +85,7 @@ private Set<String> getGeoHashes(Geometry polygon, Integer precision){
}
}
}

return geohashes;
}

Expand Down

0 comments on commit 918fb04

Please sign in to comment.