freshyo/apps/backend/src/map-assets/mbnr-1.ts
2026-10-03 10:45:42 +05:30

82 lines
2.5 KiB
TypeScript

// Inlined from ./Mbnr-1.geojson (hand-converted: .geojson cannot be imported
// directly — there is no bundler loader and resolveJsonModule only covers .json).
export const mbnr1GeoJson = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {
name: 'Polygon 4',
},
geometry: {
type: 'Polygon',
coordinates: [
[
[77.9802307, 16.7224827],
[77.9832452, 16.7186281],
[77.9885003, 16.7191643],
[78.0027771, 16.7184524],
[78.0167635, 16.717821],
[78.0176657, 16.7225884],
[78.0245685, 16.7301059],
[78.0346065, 16.7366045],
[78.0483126, 16.7469324],
[78.0558892, 16.7506815],
[78.0533183, 16.7547082],
[78.0497798, 16.7596596],
[78.0456892, 16.7640471],
[78.0383339, 16.7641426],
[78.0343234, 16.758687],
[78.021375, 16.7570244],
[78.0163704, 16.758279],
[78.0128516, 16.7590231],
[78.0120219, 16.7648881],
[78.0095524, 16.7664414],
[77.99363, 16.7660548],
[77.9720422, 16.7596124],
[77.9679337, 16.7449906],
[77.9802307, 16.7224827],
],
],
},
},
],
} as const
const mbnr1Coordinates = mbnr1GeoJson.features[0].geometry
.coordinates as unknown as number[][][]
// Turf is imported lazily so it never runs during a Worker cold start — only the
// first time isInMbnrPolygon is actually called. Cached after first use.
let turfPolygonPromise: Promise<
(coords: { lat: number; lng: number }) => boolean
> | null = null
async function getPointInPolygonChecker(): Promise<
(coords: { lat: number; lng: number }) => boolean
> {
if (!turfPolygonPromise) {
turfPolygonPromise = (async () => {
const [{ booleanPointInPolygon }, turfHelpers] = await Promise.all([
import('@turf/boolean-point-in-polygon'),
import('@turf/helpers'),
])
const polygon = turfHelpers.polygon(mbnr1Coordinates)
return (coords) =>
booleanPointInPolygon(turfHelpers.point([coords.lng, coords.lat]), polygon)
})()
}
return turfPolygonPromise
}
/**
* Returns true when the given coordinates fall inside the Mbnr-1 delivery
* polygon. GeoJSON coordinates are [longitude, latitude].
*/
export async function isInMbnrPolygon(coords: {
lat: number
lng: number
}): Promise<boolean> {
const check = await getPointInPolygonChecker()
return check(coords)
}