Skip to content

Instantly share code, notes, and snippets.

@jkao
Created August 13, 2019 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkao/02a3325fe6690c25c2799c5e90c03457 to your computer and use it in GitHub Desktop.
Save jkao/02a3325fe6690c25c2799c5e90c03457 to your computer and use it in GitHub Desktop.
Clustering Users by S2 Cells
const s2 = require('@radarlabs/s2');
const user1LongLat = [-73.95772933959961, 40.71623280185081];
const user2LongLat = [-73.95927429199219, 40.71629785715124];
const user3LongLat = [-73.99206161499023, 40.688708709249646];
const user1S2 = ["user1", new s2.CellId(new s2.LatLng(user1LongLat[1], user1LongLat[0])).parent(13)];
const user2S2 = ["user2", new s2.CellId(new s2.LatLng(user2LongLat[1], user2LongLat[0])).parent(13)];
const user3S2 = ["user3", new s2.CellId(new s2.LatLng(user3LongLat[1], user3LongLat[0])).parent(13)];
const groups = {};
[user1S2, user2S2, user3S2].forEach(([userId, cellId]) => {
const group = groups[cellId.token()] || [];
group.push(userId);
groups[cellId.token()] = group;
});
console.log(groups); // { '89c2595c': [ 'user1', 'user2' ], '89c25a4c': [ 'user3' ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment