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

ENH: Add GEOSHilbertCode to C API #556

Merged
merged 8 commits into from Feb 5, 2022

Conversation

brendan-ward
Copy link
Contributor

closes #553

Exposes HilbertEncoder to C API.

HilbertEncoder will return seemingly valid values for geometries that fall outside the extent used for scaling prior to calling
HilbertCode. Should this instead be handled as an error? For now I noted that it is the caller's responsibility to ensure the geometry falls in the extent.

I used @jorisvandenbossche 's GeoPandas PR #2297 to validate the Hilbert Codes used in the test here, since both use the same calculations.

@brendan-ward brendan-ward changed the title ENH: Add GEOSHilbert to C API ENH: Add GEOSHilbertCode to C API Feb 1, 2022
@brendan-ward
Copy link
Contributor Author

Note: I think that we don't actually need GEOSHilbert_xy as proposed in #553; caller can construct and pass in points that represent whatever they want (midpoint, point on surface, something else entirely) so long as the fall within the extent.

ensure_equals(encoder.encode(geoms[i]->getEnvelopeInternal()), expected[i]);

// cleanup geom
factory_->destroyGeometry(geoms[i]);
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 found something like this in one of the other tests, but I'm not confident this is the right way to handle this; without it there is a memory leak.

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 fine. GEOSGeom_destroy is OK too. It's awkward because the interface of HilbertEncoder is forcing you to work with raw pointers.

@@ -65,7 +65,7 @@ HilbertEncoder::sort(std::vector<geom::Geometry*>& geoms)
bool
operator()(const geom::Geometry* a, const geom::Geometry* b)
{
return enc.encode(a->getEnvelopeInternal()) > enc.encode(b->getEnvelopeInternal());
return enc.encode(a->getEnvelopeInternal()) < enc.encode(b->getEnvelopeInternal());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pramsey I changed the sort direction to better match expected results; std::sort returns items in ascending order, and the few other places I checked in GEOS that were sorting objects with a comparator like this were also doing so in ascending order. Presuming the descending Hilbert sort order was not intentional, of course.

I didn't see any other usage of this function in GEOS. Does this merit a note in the NEWS?

Copy link
Member

Choose a reason for hiding this comment

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

It's possible nobody uses this, but anybody who does will be pretty pissed off. Is there any reason not to just leave it as is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was only changing out of interest of consistency / expected results; since this PR added tests based on expected behavior, seemed like a good time to do so.

Definitely don't want to piss anyone off, just thought it was odd that a sort that normally would produce ascending order did the opposite here.

Would you like me to revert this change?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think so. One of the privileges of being a widely used library is being stuck with your history, particularly in the case of behaviour.

Copy link
Member

@dbaston dbaston Feb 3, 2022

Choose a reason for hiding this comment

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

The new C API function could do an ascending sort, no? Never mind, the new function is operating on a single input.

Copy link
Member

Choose a reason for hiding this comment

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

True

@dbaston dbaston merged commit 579b186 into libgeos:main Feb 5, 2022
@brendan-ward brendan-ward deleted the add_hilbert_capi branch February 15, 2022 03:57
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.

Add Hilbert code to C API
3 participants