-
Notifications
You must be signed in to change notification settings - Fork 300
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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); |
There was a problem hiding this comment.
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:
return lng / 360 + 0.5 + (lng > 180 || lng < -180 ? -Math.sign(lng) : 0); | |
return ((lng / 360 + 0.5) % 1 + 1) % 1; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :)
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
andmapbox-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.