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

Fix longitudes outside of [-180,180] range #194

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

ka7eh
Copy link

@ka7eh ka7eh commented Dec 24, 2021

This is not necessarily an issue with supercluster (assuming it expects all longitudes to be within the [-180, 180] range), but it causes issues with maplibre-gl and mapbox-gl.

For example, a point with coordinates [-181, 0] renders fine in a non-cluster mode in the two mentioned libraries, but it doesn't render in a clustered geojson source. Here's an example showing this issue: https://codepen.io/kaveh/pen/mdBqrYq. The red layer is for non-clustered points (shows 2 rendered points) and green is for clustered (shows only 1).

I could fix my coordinates before adding them to source in those libraries, but I thought if the same data works in non-clustered mode, it should work with clustered mode too and that's why I submitted the fix here.

Copy link
Member

@mourner mourner left a comment

Choose a reason for hiding this comment

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

Thank you for the fix! Can you also add a minimal test for this?

@@ -387,7 +387,7 @@ function getClusterProperties(cluster) {

// longitude/latitude to spherical mercator in [0..1] range
function lngX(lng) {
return lng / 360 + 0.5;
return lng / 360 + 0.5 + (lng > 180 || lng < -180 ? -Math.sign(lng) : 0);
Copy link
Member

Choose a reason for hiding this comment

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

A slightly simpler / more reliable way to wrap the value:

Suggested change
return lng / 360 + 0.5 + (lng > 180 || lng < -180 ? -Math.sign(lng) : 0);
return ((lng / 360 + 0.5) % 1 + 1) % 1;

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the tip on the alternative math. I tested it and unfortunately, it breaks the tests.

With the suggested approach, the returned values from lngX are different from the values from the first solution at their ~14-15 decimal digit, which causes the tests to fail. So I kept the original solution for now.

I've also added a test for this change in 99c877f.

Copy link
Author

Choose a reason for hiding this comment

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

@mourner just a quick follow-up on this PR to see if there are any other changes you'd like me to include. :)

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.

2 participants