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_r298261656
##########
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 agree w/ @dsmiley re: units. The calculation is using the [shoelace
method](https://en.wikipedia.org/wiki/Shoelace_formula) on vertices in decimal
degrees. So the units are deg^2. I remember exploring this a little over a year
ago in [LUCENE-8364](https://issues.apache.org/jira/browse/LUCENE-8364) and
originally having area because it was "easy". I ended up removing it until we
had support for indexing in other projections (e.g., [equal
area](https://en.wikipedia.org/wiki/Map_projection#Equal-area)). I think we
should wait until
[LUCENE-8632](https://issues.apache.org/jira/browse/LUCENE-8632) lands and only
provide this area calculation in
[XYPolygon](lucene/sandbox/src/java/org/apache/lucene/geo/XYPolygon.java) for
regular cartesian geometries in non decimal degrees.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]