james-willis commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1887603142
##########
common/src/main/java/org/apache/sedona/common/Functions.java:
##########
@@ -80,6 +80,123 @@ public static double area(Geometry geometry) {
return geometry.getArea();
}
+ public static Geometry labelPoint(Geometry geometry) {
+ return labelPoint(geometry, 16, 0.2);
+ }
+
+ public static Geometry labelPoint(Geometry geometry, int gridResolution) {
+ return labelPoint(geometry, gridResolution, 0.2);
+ }
+
+ public static Geometry labelPoint(
+ Geometry geometry, int gridResolution, double goodnessThreshold) {
+ if (geometry.getArea() <= 0) {
+ throw new IllegalArgumentException("Geometry must have a positive area");
+ }
+
+ GeometryFactory geometryFactory = new GeometryFactory();
+
+ // Find the largest polygon
+ Polygon largestPolygon = findLargestPolygon(geometry);
+
+ if (largestPolygon == null) {
+ throw new IllegalArgumentException("Geometry must contain at least one
Polygon");
+ }
+
+ return polygonToLabel(largestPolygon, gridResolution, goodnessThreshold,
geometryFactory);
+ }
+
+ private static Polygon findLargestPolygon(Geometry geometry) {
+ if (geometry instanceof Polygon) {
+ return (Polygon) geometry;
+ }
+
+ if (geometry instanceof GeometryCollection) {
Review Comment:
Yes. I tested it.
```
import org.locationtech.jts.geom.{Coordinate, GeometryCollection,
GeometryFactory, MultiPolygon, Polygon}
val geometryFactory = new GeometryFactory()
// Define coordinates for the exterior ring of the first polygon
val outerCoords1 = Array(
new Coordinate(0, 0),
new Coordinate(10, 0),
new Coordinate(10, 10),
new Coordinate(0, 10),
new Coordinate(0, 0)
)
// Create the first polygon
val polygon1 = geometryFactory.createPolygon(outerCoords1)
// Define coordinates for the exterior ring of the second polygon
val outerCoords2 = Array(
new Coordinate(15, 15),
new Coordinate(25, 15),
new Coordinate(25, 25),
new Coordinate(15, 25),
new Coordinate(15, 15)
)
// Create the second polygon
val polygon2 = geometryFactory.createPolygon(outerCoords2)
// Create a MultiPolygon from the two polygons
val multiPolygon = geometryFactory.createMultiPolygon(Array(polygon1,
polygon2))
// Check if the MultiPolygon is an instance of GeometryCollection
println(s"Is MultiPolygon a GeometryCollection?
${multiPolygon.isInstanceOf[GeometryCollection]}")
```
gives:
```
Is MultiPolygon a GeometryCollection? true
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]