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

72 lines
2.2 KiB
TypeScript

// Inlined from ./Mbnr-2.geojson (hand-converted: .geojson cannot be imported
// directly — there is no bundler loader and resolveJsonModule only covers .json).
export const mbnr2GeoJson = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {
name: 'Mbnr-2',
},
geometry: {
type: 'Polygon',
coordinates: [
[
[77.9773739, 16.7127921],
[77.9950359, 16.7080928],
[78.0187929, 16.70999],
[78.0289444, 16.7211582],
[78.0570875, 16.7502883],
[78.0662922, 16.7519867],
[78.0652719, 16.7684088],
[78.0204341, 16.7689639],
[78.0023161, 16.770186],
[77.9984809, 16.7850848],
[77.983305, 16.782843],
[77.971574, 16.7646028],
[77.9686551, 16.7454905],
[77.9773739, 16.7127921],
],
],
},
},
],
} as const
const mbnr2Coordinates = mbnr2GeoJson.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(mbnr2Coordinates)
return (coords) =>
booleanPointInPolygon(turfHelpers.point([coords.lng, coords.lat]), polygon)
})()
}
return turfPolygonPromise
}
/**
* Returns true when the given coordinates fall inside the Mbnr-2 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)
}