Compute nearby-brewery distance in SQL instead of client-side Haversine, and add the spatial index it relies on.

This commit is contained in:
Aaron Po
2026-08-27 02:40:58 -04:00
parent dab7aa2458
commit 7330eabebe
10 changed files with 244 additions and 121 deletions

View File

@@ -1,24 +1,3 @@
const EARTH_RADIUS_METRES = 6_371_000;
export interface Coordinates {
latitude: number;
longitude: number;
}
export const haversineDistanceMetres = (a: Coordinates, b: Coordinates): number => {
const toRadians = (degrees: number) => (degrees * Math.PI) / 180;
const deltaLat = toRadians(b.latitude - a.latitude);
const deltaLng = toRadians(b.longitude - a.longitude);
const lat1 = toRadians(a.latitude);
const lat2 = toRadians(b.latitude);
const h =
Math.sin(deltaLat / 2) ** 2 +
Math.cos(lat1) * Math.cos(lat2) * Math.sin(deltaLng / 2) ** 2;
return 2 * EARTH_RADIUS_METRES * Math.asin(Math.sqrt(h));
};
export type DistanceUnit = 'km' | 'mi';
const METRES_PER_MILE = 1609.344;