{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { booleanPointInPolygon } from \"@turf/boolean-point-in-polygon\";\nimport {\n Feature,\n Point,\n Polygon,\n MultiPolygon,\n LineString,\n Position,\n} from \"geojson\";\nimport { pointToLineDistance } from \"@turf/point-to-line-distance\";\nimport { polygonToLine } from \"@turf/polygon-to-line\";\nimport { getGeom } from \"@turf/invariant\";\nimport { flattenEach } from \"@turf/meta\";\nimport { polygon, Units } from \"@turf/helpers\";\n\n/**\n * Calculates the distance from a point to the edges of a polygon or multi-polygon.\n * Returns negative values for points inside the polygon.\n * Handles polygons with holes and multi-polygons.\n * A hole is treated as the exterior of the polygon.\n *\n * @param {Feature | Point | Position} point Input point\n * @param {Feature | Polygon | MultiPolygon} polygonOrMultiPolygon Input polygon or multipolygon\n * @param {Object} options Optional parameters\n * @param {Units} options.units Units of the result e.g. \"kilometers\", \"miles\", \"meters\"\n * @param {\"geodesic\" | \"planar\"} options.method Method of the result\n * @returns {number} Distance in meters (negative values for points inside the polygon)\n * @throws {Error} If input geometries are invalid\n */\nexport function pointToPolygonDistance(\n point: Feature | Point | Position,\n polygonOrMultiPolygon:\n | Feature\n | Polygon\n | MultiPolygon,\n options: {\n units?: Units;\n method?: \"geodesic\" | \"planar\";\n } = {}\n): number {\n const method = options.method ?? \"geodesic\";\n const units = options.units ?? \"kilometers\";\n // Input validation\n if (!point) throw new Error(\"point is required\");\n if (!polygonOrMultiPolygon)\n throw new Error(\"polygon or multi-polygon is required\");\n\n const geom = getGeom(polygonOrMultiPolygon);\n\n if (geom.type === \"MultiPolygon\") {\n const distances = geom.coordinates.map((coords) =>\n pointToPolygonDistance(point, polygon(coords), { method, units })\n );\n return (\n Math.min(...distances.map(Math.abs)) *\n (booleanPointInPolygon(point, polygonOrMultiPolygon) ? -1 : 1)\n );\n }\n\n if (geom.coordinates.length > 1) {\n // Has holes\n const [exteriorDistance, ...interiorDistances] = geom.coordinates.map(\n (coords) =>\n pointToPolygonDistance(point, polygon([coords]), { method, units })\n );\n if (exteriorDistance >= 0) return exteriorDistance;\n // point is inside the exterior polygon shape\n const smallestInteriorDistance = Math.min(...interiorDistances);\n // point is inside one of the holes?\n if (smallestInteriorDistance < 0) return Math.abs(smallestInteriorDistance);\n // find which is closer, the distance to the hole or the distance to the edge of the exterior\n return Math.min(smallestInteriorDistance, Math.abs(exteriorDistance));\n }\n // The actual distance operation - on a normal, hole-less polygon in meters\n const lines = polygonToLine(geom);\n let minDistance = Infinity;\n flattenEach(lines, (feature) => {\n minDistance = Math.min(\n minDistance,\n pointToLineDistance(point, feature as Feature, {\n method,\n units,\n })\n );\n });\n\n return booleanPointInPolygon(point, geom) ? -minDistance : minDistance;\n}\n\nexport default pointToPolygonDistance;\n"],"mappings":";AAAA,SAAS,6BAA6B;AAStC,SAAS,2BAA2B;AACpC,SAAS,qBAAqB;AAC9B,SAAS,eAAe;AACxB,SAAS,mBAAmB;AAC5B,SAAS,eAAsB;AAgBxB,SAAS,uBACd,OACA,uBAIA,UAGI,CAAC,GACG;AAvCV;AAwCE,QAAM,UAAS,aAAQ,WAAR,YAAkB;AACjC,QAAM,SAAQ,aAAQ,UAAR,YAAiB;AAE/B,MAAI,CAAC,MAAO,OAAM,IAAI,MAAM,mBAAmB;AAC/C,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,sCAAsC;AAExD,QAAM,OAAO,QAAQ,qBAAqB;AAE1C,MAAI,KAAK,SAAS,gBAAgB;AAChC,UAAM,YAAY,KAAK,YAAY;AAAA,MAAI,CAAC,WACtC,uBAAuB,OAAO,QAAQ,MAAM,GAAG,EAAE,QAAQ,MAAM,CAAC;AAAA,IAClE;AACA,WACE,KAAK,IAAI,GAAG,UAAU,IAAI,KAAK,GAAG,CAAC,KAClC,sBAAsB,OAAO,qBAAqB,IAAI,KAAK;AAAA,EAEhE;AAEA,MAAI,KAAK,YAAY,SAAS,GAAG;AAE/B,UAAM,CAAC,kBAAkB,GAAG,iBAAiB,IAAI,KAAK,YAAY;AAAA,MAChE,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