Skip to content

Instantly share code, notes, and snippets.

@dfellis
Created September 15, 2021 01:15
Show Gist options
  • Save dfellis/c20e596e466a3ab4a06eb1079a27ca83 to your computer and use it in GitHub Desktop.
Save dfellis/c20e596e466a3ab4a06eb1079a27ca83 to your computer and use it in GitHub Desktop.
Remote Execution Fourth Example
const degsToRads = (deg) => Math.PI / 180 * deg;
const haversine = (origin, dest) => Math.acos(
Math.sin(degsToRads(origin.lat)) * Math.sin(degsToRads(dest.lat)) +
Math.cos(degsToRads(origin.lat)) * Math.cos(degsToRads(dest.lat)) * Math.cos(degsToRads(dest.lng - origin.lng))
);
let bbox = 0.1;
for (; (await query(`
select count(*) from notablePlaces
where notablePlaces.lat < $0 + $2
and notablePlaces.lat > $0 - $2
and notablePlaces.lng < $1 + $2
and notablePlaces.lng > $1 - $2
`, [trip.destLat, trip.destLng, bbox]))[0].count < 4; bbox *= 2) {}
const closeNotablePlaces = await query(`
select * from notablePlaces
where notablePlaces.lat < $0 + $2
and notablePlaces.lat > $0 - $2
and notablePlaces.lng < $1 + $2
and notablePlaces.lng > $1 - $2
`, [trip.destLat, trip.destLng, bbox]);
const relevantPlaces = closeNotablePlaces
.map((p) => ({
name: p.name,
img: p.img,
desc: p.desc,
distance: haversine({
lat: p.lat,
lng: p.lng,
}, {
lat: trip.destLat,
lng: trip.destLng,
}),
}))
.sort((a, b) => b.distance - a.distance)
.slice(0, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment