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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to