diff --git a/src/main/java/tech/leovan/hive/udf/geo/PolygonAreaUDF.java b/src/main/java/tech/leovan/hive/udf/geo/PolygonAreaUDF.java index e58c93d..860b8ad 100644 --- a/src/main/java/tech/leovan/hive/udf/geo/PolygonAreaUDF.java +++ b/src/main/java/tech/leovan/hive/udf/geo/PolygonAreaUDF.java @@ -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) { diff --git a/src/main/java/tech/leovan/hive/udf/geo/PolygonGeohashUDF.java b/src/main/java/tech/leovan/hive/udf/geo/PolygonGeohashUDF.java index ea27c18..78bc4f8 100644 --- a/src/main/java/tech/leovan/hive/udf/geo/PolygonGeohashUDF.java +++ b/src/main/java/tech/leovan/hive/udf/geo/PolygonGeohashUDF.java @@ -42,24 +42,26 @@ public List 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 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 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 getGeoHashes(Geometry polygon, Integer precision){ + private Set getGeoHashes(Polygon polygon, Integer precision){ Point centroid = polygon.getCentroid(); Set geohashes = new HashSet<>(); @@ -68,8 +70,7 @@ private Set 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); @@ -84,6 +85,7 @@ private Set getGeoHashes(Geometry polygon, Integer precision){ } } } + return geohashes; }