Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LUCENE-8850: Calculate the area of a polygon and throw error when values are invalid #709

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

iverase
Copy link
Contributor

@iverase iverase commented Jun 11, 2019

Compute area of a polygon and throw an error when values are invalid.

Copy link
Contributor

@jpountz jpountz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me. @nknize @dsmiley Any thoughts about this?

}

/** returns the number of vertex points */
public int numPoints() {
return polyLats.length;
}

/** returns the area of the polygon */
public double area() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can imagine some asking what the units are. And why not getArea? Maybe this should be marked @lucene.internal or package local even if we don't want to expose it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree w/ @dsmiley re: units. The calculation is using the shoelace method on vertices in decimal degrees. So the units are deg^2. I remember exploring this a little over a year ago in 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). I think we should wait until LUCENE-8632 lands and only provide this area calculation in XYPolygon for regular cartesian geometries in non decimal degrees.

Copy link
Contributor Author

@iverase iverase Jun 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should not provide this info to the user, +1 not to offer the calculation .

The idea behind this change was validation of the polygon. The idea of this change came from the fact that I am using this calculation to check that a Polygon tessellation is correct and I realise that:

  • 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?

  • I have seen polygons with one hole where the hole and polygon are the same, should we capture that situation and fail?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment to the code to to link to the "shoelace method" that Nick points at. I think it's important for non-trivial algorithms to incorporate references where the readers of this code can learn more. Who knows... maybe some "velcro method" will come along and be superior and then some student looking at this code will point this out to us :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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.

@iverase
Copy link
Contributor Author

iverase commented Jun 27, 2019

One of the things I notice with this change is that it breaks some of the benchmarks, in particular it breaks this one as the polygon contain invalid holes:

https://home.apache.org/~mikemccand/geobench.html#search-polyMedium

@jpountz
Copy link
Contributor

jpountz commented Jun 27, 2019

What is the particular rule that breaks?

@iverase
Copy link
Contributor Author

iverase commented Jun 27, 2019

Reading one of the polygons representing a London Borough it fails because it contains a hole where all points are coplanar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants