mapbox/supercluster

Add an option for using Image Coordinates x/y

ryanhausen opened this issue · 1 comments

Howdy,

I am interested in using supercluster in a project that uses x/y coordinates instead of lat/lng. I looked through the code and got it working by changing the code responsible for converting lat/lng to the [0,1] range. Specifically, the functions i had to edit were

supercluster/index.js

Lines 69 to 81 in 60d13df

let minLng = ((bbox[0] + 180) % 360 + 360) % 360 - 180;
const minLat = Math.max(-90, Math.min(90, bbox[1]));
let maxLng = bbox[2] === 180 ? 180 : ((bbox[2] + 180) % 360 + 360) % 360 - 180;
const maxLat = Math.max(-90, Math.min(90, bbox[3]));
if (bbox[2] - bbox[0] >= 360) {
minLng = -180;
maxLng = 180;
} else if (minLng > maxLng) {
const easternHem = this.getClusters([minLng, minLat, 180, maxLat], zoom);
const westernHem = this.getClusters([-180, minLat, maxLng, maxLat], zoom);
return easternHem.concat(westernHem);
}

supercluster/index.js

Lines 388 to 390 in 60d13df

function lngX(lng) {
return lng / 360 + 0.5;
}

supercluster/index.js

Lines 391 to 395 in 60d13df

function latY(lat) {
const sin = Math.sin(lat * Math.PI / 180);
const y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI);
return y < 0 ? 0 : y > 1 ? 1 : y;
}

supercluster/index.js

Lines 398 to 400 in 60d13df

function xLng(x) {
return (x - 0.5) * 360;
}

supercluster/index.js

Lines 401 to 404 in 60d13df

function yLat(y) {
const y2 = (180 - y * 360) * Math.PI / 180;
return 360 * Math.atan(Math.exp(y2)) / Math.PI - 90;
}

Would you be interested in incorporating this kind of functionality if I submit a PR?

My initial thoughts would be to move the [lat,lng][x,y] functions into the class so they can be overridden by the user, but the first code snippet would likely be more complex and take a flag passed to the constructor or the function call.

I don't do a lot of programming in javascript so I am not sure if any of this would be an anti pattern of some sort.

Thanks for making this!

I no longer need this functionality, so I am going to close this.