You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sedona.apache.org by "prantogg (via GitHub)" <gi...@apache.org> on 2024/12/08 02:30:04 UTC

[PR] [SEDONA-686] Add ST_Anchor [sedona]

prantogg opened a new pull request, #1712:
URL: https://github.com/apache/sedona/pull/1712

   ## Did you read the Contributor Guide?
   
   - Yes, I have read the [Contributor Rules](https://sedona.apache.org/latest-snapshot/community/rule/) and [Contributor Development Guide](https://sedona.apache.org/latest-snapshot/community/develop/)
   
   ## Is this PR related to a JIRA ticket?
   
   - Yes, the URL of the associated JIRA ticket is https://issues.apache.org/jira/browse/SEDONA-686. The PR name follows the format `[SEDONA-XXX] my subject`.
   
   
   ## What changes were proposed in this PR?
   
   - This PR introduces ST_Anchor - Calculates and returns an anchor point for a given geometry. The anchor point is determined as a point inside the geometry, prioritizing proximity to the centroid and distance from the boundaries. For collections, the largest polygon by area is used.
   
   ## How was this patch tested?
   
   -  Passes new and existing tests
   
   ## Did this PR include necessary documentation updates?
   
   - Yes, I am adding a new API. I am using the [current SNAPSHOT version number](https://github.com/apache/sedona/blob/99239524f17389fc4ae9548ea88756f8ea538bb9/pom.xml#L29) in `vX.Y.Z` format.
   


-- 
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: dev-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_Anchor [sedona]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1879147274


##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);
+  }
+
+  public static Geometry anchor(Geometry geometry, double stepSize) {
+    return anchor(geometry, stepSize, 0.2);
+  }
+
+  public static Geometry anchor(Geometry geometry, double stepSize, double goodnessThreshold) {
+    if (geometry.getArea() <= 0) {
+      throw new IllegalArgumentException("Geometry must have a positive area");
+    }
+
+    GeometryFactory geometryFactory = new GeometryFactory();
+
+    // If the geometry is a collection, find the largest polygon
+    if (geometry instanceof GeometryCollection) {
+      GeometryCollection gc = (GeometryCollection) geometry;
+      Geometry largestPolygon = null;
+      double maxArea = Double.MIN_VALUE;
+
+      for (int i = 0; i < gc.getNumGeometries(); i++) {
+        Geometry geom = gc.getGeometryN(i);
+        double area = geom.getArea();
+        if (area > maxArea) {
+          largestPolygon = geom;
+          maxArea = area;
+        }
+      }
+
+      // Recursively process the largest polygon
+      if (largestPolygon instanceof Polygon) {

Review Comment:
   +1. This function needs to be recursive.



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1887361133


##########
docs/api/flink/Function.md:
##########
@@ -164,6 +164,82 @@ Input: `POLYGON ((1 0 1, 1 1 1, 2 2 2, 1 0 1))`
 
 Output: `POLYGON Z((2 3 1, 4 5 1, 7 8 2, 2 3 1))`
 
+## ST_LabelPoint
+
+Introduction: `ST_LabelPoint` computes and returns a label point for a given polygon or geometry collection. The label point is chosen to:
+
+- Be near the center of the polygon.
+- Stay far from boundaries for optimal placement.
+
+The algorithm is inspired by Tippecanoe’s `polygon_to_anchor`, an approximate solution for label point generation, designed to be faster than iterative algorithms like `polylabel`. It may not produce the globally optimal result, but it ensures a “good enough” label point within a limited number of iterations. For geometry collections, only the largest polygon by area is considered. While `ST_Centroid` is a fast algorithm to calculate the center of mass of a polygon, assuming uniform density, it may place the point outside the polygon or near a boundary, especially for irregular shapes or polygons with holes.
+
+`ST_LabelPoint` takes upto 3 arguments,

Review Comment:
   up to is two words



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_Anchor [sedona]

Posted by "prantogg (via GitHub)" <gi...@apache.org>.
prantogg commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1882682646


##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);

Review Comment:
   @james-willis By width you mean width of the grid/tile? If so, how would one access the width of the grid?



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1887524255


##########
common/src/test/java/org/apache/sedona/common/FunctionsTest.java:
##########
@@ -63,6 +63,42 @@ protected int compareCoordinate(
 
   private final WKTReader wktReader = new WKTReader();
 
+  @Test
+  public void labelPoint() throws Exception {
+    Geometry geom =
+        Constructors.geomFromWKT(
+            "POLYGON ((-112.637484 33.440546, -112.546852 33.477209, -112.489177 33.550488, -112.41777 33.751684, -111.956371 33.719707, -111.766868 33.616843, -111.775107 33.527595, -111.640533 33.504695, -111.440044 33.463462, -111.415326 33.374055, -111.514197 33.309809, -111.643279 33.222542, -111.893203 33.174278, -111.96461 33.250109, -112.123903 33.261593, -112.252985 33.35341, -112.406784 33.346527, -112.667694 33.316695, -112.637484 33.440546))",
+            4326);
+    System.out.println();

Review Comment:
   Please remove println



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1887525985


##########
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:
   Are you sure?
   
   So both `MultiPolygon` and `GeometryCollection` return true for `InstanceOf GeometryCollection`?



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1887523729


##########
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:
   A MultiPolygon is a GeometryCollection.



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_Anchor [sedona]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on PR #1712:
URL: https://github.com/apache/sedona/pull/1712#issuecomment-2532606879

   > prioritizing proximity to the centroid and distance from the boundaries. For collections, the largest polygon by area is used.
   
   It would be more accurate to say it is lookin for a point with enough distance from the boundaries. It doesnt care about the centroid beyond considering it first as a reasonable option


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1882811941


##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);

Review Comment:
   width of the extent of the geometry



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1889083648


##########
common/src/test/java/org/apache/sedona/common/FunctionsTest.java:
##########
@@ -63,6 +63,42 @@ protected int compareCoordinate(
 
   private final WKTReader wktReader = new WKTReader();
 
+  @Test
+  public void labelPoint() throws Exception {
+    Geometry geom =
+        Constructors.geomFromWKT(
+            "POLYGON ((-112.637484 33.440546, -112.546852 33.477209, -112.489177 33.550488, -112.41777 33.751684, -111.956371 33.719707, -111.766868 33.616843, -111.775107 33.527595, -111.640533 33.504695, -111.440044 33.463462, -111.415326 33.374055, -111.514197 33.309809, -111.643279 33.222542, -111.893203 33.174278, -111.96461 33.250109, -112.123903 33.261593, -112.252985 33.35341, -112.406784 33.346527, -112.667694 33.316695, -112.637484 33.440546))",
+            4326);
+    System.out.println();

Review Comment:
   ```suggestion
   ```



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1887520655


##########
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:
   What happens to `MultiPolygon`?
   
   @prantogg @james-willis 



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1887361616


##########
docs/api/flink/Function.md:
##########
@@ -164,6 +164,82 @@ Input: `POLYGON ((1 0 1, 1 1 1, 2 2 2, 1 0 1))`
 
 Output: `POLYGON Z((2 3 1, 4 5 1, 7 8 2, 2 3 1))`
 
+## ST_LabelPoint
+
+Introduction: `ST_LabelPoint` computes and returns a label point for a given polygon or geometry collection. The label point is chosen to:
+
+- Be near the center of the polygon.
+- Stay far from boundaries for optimal placement.
+
+The algorithm is inspired by Tippecanoe’s `polygon_to_anchor`, an approximate solution for label point generation, designed to be faster than iterative algorithms like `polylabel`. It may not produce the globally optimal result, but it ensures a “good enough” label point within a limited number of iterations. For geometry collections, only the largest polygon by area is considered. While `ST_Centroid` is a fast algorithm to calculate the center of mass of a polygon, assuming uniform density, it may place the point outside the polygon or near a boundary, especially for irregular shapes or polygons with holes.
+
+`ST_LabelPoint` takes upto 3 arguments,
+
+- `geometry`: input geometry (e.g., a polygon or GeometryCollection) for which the anchor point is to be calculated.

Review Comment:
   Polygon should be capitalized



##########
docs/api/flink/Function.md:
##########
@@ -164,6 +164,82 @@ Input: `POLYGON ((1 0 1, 1 1 1, 2 2 2, 1 0 1))`
 
 Output: `POLYGON Z((2 3 1, 4 5 1, 7 8 2, 2 3 1))`
 
+## ST_LabelPoint
+
+Introduction: `ST_LabelPoint` computes and returns a label point for a given polygon or geometry collection. The label point is chosen to:
+
+- Be near the center of the polygon.
+- Stay far from boundaries for optimal placement.
+
+The algorithm is inspired by Tippecanoe’s `polygon_to_anchor`, an approximate solution for label point generation, designed to be faster than iterative algorithms like `polylabel`. It may not produce the globally optimal result, but it ensures a “good enough” label point within a limited number of iterations. For geometry collections, only the largest polygon by area is considered. While `ST_Centroid` is a fast algorithm to calculate the center of mass of a polygon, assuming uniform density, it may place the point outside the polygon or near a boundary, especially for irregular shapes or polygons with holes.
+
+`ST_LabelPoint` takes upto 3 arguments,
+
+- `geometry`: input geometry (e.g., a polygon or GeometryCollection) for which the anchor point is to be calculated.
+- `gridResolution` (Optional, default is 16): Controls the resolution of the search grid for refining the label point. A higher resolution increases the grid density, providing more accurate results but at the cost of additional computation. For example, a gridResolution of 16 divides the bounding box of the polygon into a 16x16 grid.
+- `goodnessThreshold` (Optional, default is 0.2): Determines the minimum acceptable “goodness” value for the anchor point. Higher thresholds prioritize points farther from boundaries but may require more computation.
+
+!!!note
+    - `ST_LabelPoint` throws an `IllegalArgumentException` if the input geometry has an area of zero or less.
+    - Holes within polygons are respected, and the anchor is always placed inside the outer boundary, not inside a hole.

Review Comment:
   Im not sure this is strictly true. There are certain parameters where you will run out of iterations before finding a point not in a hole



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_Anchor [sedona]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1878652644


##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);
+  }
+
+  public static Geometry anchor(Geometry geometry, double stepSize) {
+    return anchor(geometry, stepSize, 0.2);
+  }
+
+  public static Geometry anchor(Geometry geometry, double stepSize, double goodnessThreshold) {
+    if (geometry.getArea() <= 0) {
+      throw new IllegalArgumentException("Geometry must have a positive area");
+    }
+
+    GeometryFactory geometryFactory = new GeometryFactory();
+
+    // If the geometry is a collection, find the largest polygon
+    if (geometry instanceof GeometryCollection) {
+      GeometryCollection gc = (GeometryCollection) geometry;
+      Geometry largestPolygon = null;
+      double maxArea = Double.MIN_VALUE;
+
+      for (int i = 0; i < gc.getNumGeometries(); i++) {
+        Geometry geom = gc.getGeometryN(i);
+        double area = geom.getArea();
+        if (area > maxArea) {
+          largestPolygon = geom;
+          maxArea = area;
+        }
+      }
+
+      // Recursively process the largest polygon
+      if (largestPolygon instanceof Polygon) {
+        return polygonToAnchor(
+            (Polygon) largestPolygon, stepSize, goodnessThreshold, geometryFactory);
+      } else {
+        throw new IllegalArgumentException("GeometryCollection must contain at least one Polygon");
+      }
+    }
+
+    // If the geometry is a polygon, process it
+    if (geometry instanceof Polygon) {
+      return polygonToAnchor((Polygon) geometry, stepSize, goodnessThreshold, geometryFactory);
+    }
+
+    throw new IllegalArgumentException(
+        "Geometry must be a Polygon or a GeometryCollection containing Polygons");
+  }
+
+  private static Point polygonToAnchor(
+      Polygon polygon, double stepSize, double goodnessThreshold, GeometryFactory geometryFactory) {
+    if (polygon.getArea() <= 0) {
+      throw new IllegalArgumentException("Polygon must have a positive area");
+    }
+
+    Envelope env = polygon.getEnvelopeInternal();
+    double xmin = env.getMinX();
+    double ymin = env.getMinY();
+    double xmax = env.getMaxX();
+    double ymax = env.getMaxY();
+
+    Point centroid = polygon.getCentroid();
+    double radius = Math.sqrt(polygon.getArea() / Math.PI);
+    goodnessThreshold = radius * goodnessThreshold;
+
+    double bestGoodness = labelGoodness(polygon, centroid);
+    //    System.out.println(bestGoodness + ", " + goodnessThreshold);

Review Comment:
   remove comment



##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);

Review Comment:
   val stepSize = width / gridResolution
   
   something like this. not sure if we need to handle width and length seperately.



##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);
+  }
+
+  public static Geometry anchor(Geometry geometry, double stepSize) {
+    return anchor(geometry, stepSize, 0.2);
+  }
+
+  public static Geometry anchor(Geometry geometry, double stepSize, double goodnessThreshold) {
+    if (geometry.getArea() <= 0) {
+      throw new IllegalArgumentException("Geometry must have a positive area");
+    }
+
+    GeometryFactory geometryFactory = new GeometryFactory();
+
+    // If the geometry is a collection, find the largest polygon
+    if (geometry instanceof GeometryCollection) {
+      GeometryCollection gc = (GeometryCollection) geometry;
+      Geometry largestPolygon = null;
+      double maxArea = Double.MIN_VALUE;
+
+      for (int i = 0; i < gc.getNumGeometries(); i++) {
+        Geometry geom = gc.getGeometryN(i);
+        double area = geom.getArea();
+        if (area > maxArea) {
+          largestPolygon = geom;
+          maxArea = area;
+        }
+      }
+
+      // Recursively process the largest polygon
+      if (largestPolygon instanceof Polygon) {

Review Comment:
   I think a gemoetry collection can contain a geometry collection. we might need to recurse the area finding code.



##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);

Review Comment:
   isntead of a step size argument, lets do a grid resolution kind of argument. this makes the runtime and the search quality scale invariant. 



##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);
+  }
+
+  public static Geometry anchor(Geometry geometry, double stepSize) {
+    return anchor(geometry, stepSize, 0.2);
+  }
+
+  public static Geometry anchor(Geometry geometry, double stepSize, double goodnessThreshold) {
+    if (geometry.getArea() <= 0) {
+      throw new IllegalArgumentException("Geometry must have a positive area");
+    }
+
+    GeometryFactory geometryFactory = new GeometryFactory();
+
+    // If the geometry is a collection, find the largest polygon
+    if (geometry instanceof GeometryCollection) {
+      GeometryCollection gc = (GeometryCollection) geometry;
+      Geometry largestPolygon = null;
+      double maxArea = Double.MIN_VALUE;
+
+      for (int i = 0; i < gc.getNumGeometries(); i++) {
+        Geometry geom = gc.getGeometryN(i);
+        double area = geom.getArea();
+        if (area > maxArea) {
+          largestPolygon = geom;
+          maxArea = area;
+        }
+      }
+
+      // Recursively process the largest polygon
+      if (largestPolygon instanceof Polygon) {
+        return polygonToAnchor(
+            (Polygon) largestPolygon, stepSize, goodnessThreshold, geometryFactory);
+      } else {
+        throw new IllegalArgumentException("GeometryCollection must contain at least one Polygon");
+      }
+    }
+
+    // If the geometry is a polygon, process it
+    if (geometry instanceof Polygon) {
+      return polygonToAnchor((Polygon) geometry, stepSize, goodnessThreshold, geometryFactory);
+    }
+
+    throw new IllegalArgumentException(
+        "Geometry must be a Polygon or a GeometryCollection containing Polygons");
+  }
+
+  private static Point polygonToAnchor(
+      Polygon polygon, double stepSize, double goodnessThreshold, GeometryFactory geometryFactory) {
+    if (polygon.getArea() <= 0) {
+      throw new IllegalArgumentException("Polygon must have a positive area");
+    }
+
+    Envelope env = polygon.getEnvelopeInternal();
+    double xmin = env.getMinX();
+    double ymin = env.getMinY();
+    double xmax = env.getMaxX();
+    double ymax = env.getMaxY();
+
+    Point centroid = polygon.getCentroid();
+    double radius = Math.sqrt(polygon.getArea() / Math.PI);
+    goodnessThreshold = radius * goodnessThreshold;
+
+    double bestGoodness = labelGoodness(polygon, centroid);
+    //    System.out.println(bestGoodness + ", " + goodnessThreshold);
+
+    if (bestGoodness < goodnessThreshold) {
+      for (int sub = 2; sub < 32; sub++) {
+        //        System.out.println("Loop " + (sub - 1));

Review Comment:
   remove comment



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu merged PR #1712:
URL: https://github.com/apache/sedona/pull/1712


-- 
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: dev-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "prantogg (via GitHub)" <gi...@apache.org>.
prantogg commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1882855078


##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);

Review Comment:
   How about diagonal of the MBR as a measure of extent?



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "prantogg (via GitHub)" <gi...@apache.org>.
prantogg commented on code in PR #1712:
URL: https://github.com/apache/sedona/pull/1712#discussion_r1882855078


##########
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 anchor(Geometry geometry) {
+    return anchor(geometry, 2, 0.2);

Review Comment:
   How about diagonal of the MBR as a measure of extent?



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [SEDONA-686] Add ST_LabelPoint [sedona]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on PR #1712:
URL: https://github.com/apache/sedona/pull/1712#issuecomment-2541939341

   @prantogg fix the CI again?


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org