You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by GitBox <gi...@apache.org> on 2019/07/01 14:30:46 UTC

[GitHub] [lucene-solr] nknize commented on a change in pull request #709: LUCENE-8850: Calculate the area of a polygon and throw error when values are invalid

nknize commented on a change in pull request #709: LUCENE-8850: Calculate the area of a polygon and throw error when values are invalid
URL: https://github.com/apache/lucene-solr/pull/709#discussion_r299071383
 
 

 ##########
 File path: lucene/core/src/java/org/apache/lucene/geo/Polygon.java
 ##########
 @@ -96,34 +99,50 @@ public Polygon(double[] polyLats, double[] polyLons, Polygon... holes) {
     this.holes = holes.clone();
 
     // compute bounding box
-    double minLat = polyLats[0];
-    double maxLat = polyLats[0];
-    double minLon = polyLons[0];
-    double maxLon = polyLons[0];
+    double minLat = Double.POSITIVE_INFINITY;
+    double maxLat = Double.NEGATIVE_INFINITY;
+    double minLon = Double.POSITIVE_INFINITY;
+    double maxLon = Double.NEGATIVE_INFINITY;
 
     double windingSum = 0d;
     final int numPts = polyLats.length - 1;
-    for (int i = 1, j = 0; i < numPts; j = i++) {
+    for (int i = 0; i < numPts; i++) {
       minLat = Math.min(polyLats[i], minLat);
       maxLat = Math.max(polyLats[i], maxLat);
       minLon = Math.min(polyLons[i], minLon);
       maxLon = Math.max(polyLons[i], maxLon);
       // compute signed area
-      windingSum += (polyLons[j] - polyLons[numPts])*(polyLats[i] - polyLats[numPts])
-          - (polyLats[j] - polyLats[numPts])*(polyLons[i] - polyLons[numPts]);
+      windingSum += polyLons[i] * polyLats[i + 1] - polyLats[i] * polyLons[i + 1];
+    }
+    if (windingSum == 0) {
+      throw new IllegalArgumentException("Cannot compute the polygon / hole orientation.");
     }
     this.minLat = minLat;
     this.maxLat = maxLat;
     this.minLon = minLon;
     this.maxLon = maxLon;
     this.windingOrder = (windingSum < 0) ? GeoUtils.WindingOrder.CCW : GeoUtils.WindingOrder.CW;
+    double area = Math.abs(windingSum / 2d);
+    for (Polygon hole : holes) {
+      area -= hole.area();
+    }
+    if (area <= 0) {
+      throw new IllegalArgumentException("Polygon has an invalid area (area = " + area + ").");
+    }
+    this.area = area;
+
   }
 
   /** returns the number of vertex points */
   public int numPoints() {
     return polyLats.length;
   }
 
+  /** returns the area of the polygon */
+  public double area() {
 
 Review comment:
   > * Currently we compute the signed area to check the orientation of a polygon, if the area is 0 the polygon is considered CW, should we fail instead?
   
   +1 to fail in this situation since it is not a valid polygon. 
   
   
   > * I have seen polygons with one hole where the hole and polygon are the same, should we capture that situation and fail?
   
   +1 to fail in this situation as well. 
   

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org