Skip to content

Instantly share code, notes, and snippets.

@jkao
Created August 13, 2019 17:22
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/2921d2ac89478e38833d7b53a28c8794 to your computer and use it in GitHub Desktop.
Save jkao/2921d2ac89478e38833d7b53a28c8794 to your computer and use it in GitHub Desktop.
const s2 = require('@radarlabs/s2');
const williamsburgLongLats = [
[-73.95841598510742, 40.72423412682422],
[-73.96957397460938, 40.71226430831242],
[-73.9683723449707, 40.70497727808752],
[-73.96184921264648, 40.69951148213175],
[-73.95923137664795, 40.70852329864894],
[-73.94775152206421, 40.70391994183744],
[-73.94163608551025, 40.71145106322093],
[-73.94335269927979, 40.71834706657437],
[-73.9469575881958, 40.719778223045275],
[-73.94970417022705, 40.722217623379684],
[-73.95056247711182, 40.721892375167045],
[-73.95193576812744, 40.72335597960796],
[-73.95425319671631, 40.72312830991985],
];
const lls = williamsburgLongLats.map((lnglat) => {
const [lng, lat] = lnglat;
return new s2.LatLng(lat, lng);
});
const s2Level = 14; // ~0.32 km^2
const neighborhoodS2Cells =
new Set(s2.RegionCoverer.getCoveringTokens(
lls,
{ min: s2Level, max: s2Level }
));
const user1LongLat = [-73.95429611206055, 40.71369559554873]; // in williamsburg
const user2LongLat = [-73.9266586303711, 40.71616774648679]; // not in williamsburg
const user1S2 = new s2.CellId(new s2.LatLng(user1LongLat[1], user1LongLat[0])).parent(14); // level 14 to ensure 1:1 mapping to cover
const user2S2 = new s2.CellId(new s2.LatLng(user2LongLat[1], user2LongLat[0])).parent(14); // level 14 to ensure 1:1 mapping to cover
console.log(neighborhoodS2Cells.has(user1S2.token())); // true - O(1)
console.log(neighborhoodS2Cells.has(user2S2.token())); // false - O(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment