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

Make polygon comparison compare shell and holes #397

Closed

Conversation

brendan-ward
Copy link
Contributor

This PR makes sure that `Polygon::compareToSameClass`` compares holes as well as outer shell and adds a
corresponding test.

Previously, the comparison was only based on comparing the outer shell.

Resolves #1099

This was adapted from JTS. Note that the first variant of JTS compareToSameClass has the same issue as was present here.

I adapted another test in that unit test file to create the tests here; it was not obvious how to reduce the boilerplate in this case (i.e., simpler ways to instantiate from WKT here).

src/geom/Polygon.cpp Outdated Show resolved Hide resolved
src/geom/Polygon.cpp Outdated Show resolved Hide resolved
auto geo = reader_.read("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))");
ensure(geo != nullptr);

PolygonPtr poly = dynamic_cast<PolygonPtr>(geo.get());
Copy link
Member

Choose a reason for hiding this comment

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

You're testing the Polygon implementation of Geometry::compareToSameClass, so if you want to reduce boilerplate, you can avoid lines like 630-631 (which really test the WKTReader). I would also take out lines like 622 (again, the goal is not to test the WKTReader) but it really comes down to preference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry to be dense, but I'm not seeing how to avoid these two lines here?

auto geo = reader_.read("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))");
PolygonPtr poly = dynamic_cast<PolygonPtr>(geo.get());

(total noob to C++ managed pointers)

Copy link
Member

Choose a reason for hiding this comment

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

Well, you can't avoid the first :)

But you can call geo->compareToSameClass(geo2.get()) directly; you don't need to explicitly cast it to a Polygon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like I need to cast the left side, otherwise compareToSameClass is inaccessible. But no need to cast the function parameter, so that simplifies a little.

Copy link
Member

Choose a reason for hiding this comment

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

That's interesting -- compareToSameClass is protected in Geometry but public in Polygon. I can't think of a good reason for that off hand -- I wonder if it's an oversight.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, Point::compareToSameClass is also protected, so the Polygon variant being public is probably a 15-year old oversight. The alternative is to just call geo->compareTo(geo2.get()) which is really the public interface. Not asking you to change anything, just trying to explain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @dbaston for helping me understand! The comment on it implies that it was changed intentionally for Polygon class?

I think since we're specifically trying to test compareToSameClass here, the cast is OK to leave as is.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's interesting -- compareToSameClass is protected in Geometry but public in Polygon. I can't think of a good reason for that off hand -- I wonder if it's an oversight.

Sure seems like an oversight/misunderstanding. Can this be fixed?

Copy link
Member

Choose a reason for hiding this comment

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

Sure seems like an oversight/misunderstanding. Can this be fixed?

Do you mean make it protected and then test compareTo instead of compareToSameClass ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean make it protected and then test compareTo instead of compareToSameClass ?

Yes.

src/geom/Polygon.cpp Outdated Show resolved Hide resolved
tests/unit/geom/PolygonTest.cpp Outdated Show resolved Hide resolved
@brendan-ward
Copy link
Contributor Author

@dbaston thanks for the fast review and guidance here!

@dbaston
Copy link
Member

dbaston commented Feb 2, 2021

Squashed and committed as 587049c. Thanks!

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

Successfully merging this pull request may close these issues.

None yet

3 participants