Skip to content

Commit

Permalink
Update PolygonGeohashUDF.java
Browse files Browse the repository at this point in the history
如果是单多边形区域,直接采用原深度优先遍历方式。如果是多多边形需要按照单多边形计算后再去重合并(否则可能出现质心不在多边形内导致无geohash,或者多块区域距离较远,质心在其中一个导致只计算了一个多边形覆盖的geohash)
  • Loading branch information
djsdjs authored May 29, 2024
1 parent 4cb846f commit fbeadd8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/tech/leovan/hive/udf/geo/PolygonGeohashUDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,20 @@ public List<String> evaluate(
if (!(polygon instanceof Polygon || polygon 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));
}
}
return new ArrayList<>(geohashes);
}

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

Set<String> geohashes = new HashSet<>();
Expand All @@ -71,8 +84,7 @@ public List<String> evaluate(
}
}
}

return new ArrayList<>(geohashes);
return geohashes;
}

public List<String> evaluate(
Expand Down

0 comments on commit fbeadd8

Please sign in to comment.