import * as turf from '@turf/turf' export const PRIMARY_AREA_BOUNDS = Object.freeze([ 110.328749941417, 28.3917230659234, 110.465779226663, 28.4913038992907, ]) const primaryArea = turf.bboxPolygon(PRIMARY_AREA_BOUNDS) export function isWithinPrimaryArea(feature, bounds = PRIMARY_AREA_BOUNDS) { return turf.booleanWithin(feature, turf.bboxPolygon(bounds)) } export function clipFeatureToBounds(feature, bounds = PRIMARY_AREA_BOUNDS) { const type = feature.geometry?.type let clipped if (type === 'LineString' || type === 'MultiLineString') { clipped = turf.bboxClip(feature, bounds) } else if (type === 'Polygon' || type === 'MultiPolygon') { clipped = turf.intersect(turf.featureCollection([feature, turf.bboxPolygon(bounds)])) } else { clipped = turf.booleanWithin(feature, primaryArea) ? feature : null } if (!clipped) return null const coordinates = clipped.geometry?.coordinates if (Array.isArray(coordinates) && coordinates.length === 0) return null if (Array.isArray(coordinates) && coordinates.every((part) => Array.isArray(part) && part.length === 0)) return null if (turf.area(clipped) === 0 && type !== 'LineString' && type !== 'MultiLineString') return null return { ...clipped, properties: { ...feature.properties } } } export function isInsidePrimaryArea(point) { return point.length === 2 && Number.isFinite(point[0]) && Number.isFinite(point[1]) && point[0] >= PRIMARY_AREA_BOUNDS[0] && point[0] <= PRIMARY_AREA_BOUNDS[2] && point[1] >= PRIMARY_AREA_BOUNDS[1] && point[1] <= PRIMARY_AREA_BOUNDS[3] }