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/06/27 08:35:57 UTC
[GitHub] [lucene-solr] iverase commented on a change in pull request #709:
LUCENE-8850: Calculate the area of a polygon and throw error when values
are invalid
iverase 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_r298065063
##########
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:
I rename the method to getArea() and make it protected. I have no use case outside of validation so it is ok to make it package private.
----------------------------------------------------------------
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