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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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. :)

}
function latY(lat) {
const sin = Math.sin(lat * Math.PI / 180);
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,26 @@ test('makes sure unclustered point coords are not rounded', (t) => {

t.end();
});

test('returns clusters when points are outside of -180 and 180 longitude range', (t) => {
const index = new Supercluster().load([
{
type: 'Feature',
properties: null,
geometry: {
type: 'Point',
coordinates: [-190, 0]
}
}, {
type: 'Feature',
properties: null,
geometry: {
type: 'Point',
coordinates: [190, 0]
}
}
]);

t.equal(index.getClusters([-180, -90, 180, 90], 1).length, 2);
t.end();
});