@turf/point-to-polygon-distance 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +61 -0
- package/dist/cjs/index.cjs +49 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +23 -0
- package/dist/esm/index.d.ts +23 -0
- package/dist/esm/index.js +49 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 TurfJS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# @turf/point-to-polygon-distance
|
|
2
|
+
|
|
3
|
+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
|
4
|
+
|
|
5
|
+
## pointToPolygonDistance
|
|
6
|
+
|
|
7
|
+
Calculates the distance from a point to the edges of a polygon or multi-polygon.
|
|
8
|
+
Returns negative values for points inside the polygon.
|
|
9
|
+
Handles polygons with holes and multi-polygons.
|
|
10
|
+
A hole is treated as the exterior of the polygon.
|
|
11
|
+
|
|
12
|
+
### Parameters
|
|
13
|
+
|
|
14
|
+
* `point` **([Feature][1]<[Point][2]> | [Point][2] | [Position][3])** Input point
|
|
15
|
+
* `polygonOrMultiPolygon` **([Feature][1]<([Polygon][4] | [MultiPolygon][5])> | [Polygon][4] | [MultiPolygon][5])** Input polygon or multipolygon
|
|
16
|
+
* `options` **[Object][6]** Optional parameters (optional, default `{}`)
|
|
17
|
+
|
|
18
|
+
* `options.units` **Units** Units of the result e.g. "kilometers", "miles", "meters"
|
|
19
|
+
* `options.method` **(`"geodesic"` | `"planar"`)** Method of the result
|
|
20
|
+
|
|
21
|
+
<!---->
|
|
22
|
+
|
|
23
|
+
* Throws **[Error][7]** If input geometries are invalid
|
|
24
|
+
|
|
25
|
+
Returns **[number][8]** Distance in meters (negative values for points inside the polygon)
|
|
26
|
+
|
|
27
|
+
[1]: https://tools.ietf.org/html/rfc7946#section-3.2
|
|
28
|
+
|
|
29
|
+
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.2
|
|
30
|
+
|
|
31
|
+
[3]: https://developer.mozilla.org/docs/Web/API/Position
|
|
32
|
+
|
|
33
|
+
[4]: https://tools.ietf.org/html/rfc7946#section-3.1.6
|
|
34
|
+
|
|
35
|
+
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.7
|
|
36
|
+
|
|
37
|
+
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
38
|
+
|
|
39
|
+
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
|
40
|
+
|
|
41
|
+
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
|
42
|
+
|
|
43
|
+
<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
|
|
48
|
+
|
|
49
|
+
### Installation
|
|
50
|
+
|
|
51
|
+
Install this single module individually:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
$ npm install @turf/point-to-polygon-distance
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
$ npm install @turf/turf
|
|
61
|
+
```
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
|
|
2
|
+
var _booleanpointinpolygon = require('@turf/boolean-point-in-polygon');
|
|
3
|
+
var _pointtolinedistance = require('@turf/point-to-line-distance');
|
|
4
|
+
var _polygontoline = require('@turf/polygon-to-line');
|
|
5
|
+
var _invariant = require('@turf/invariant');
|
|
6
|
+
var _meta = require('@turf/meta');
|
|
7
|
+
var _helpers = require('@turf/helpers');
|
|
8
|
+
function pointToPolygonDistance(point, polygonOrMultiPolygon, options = {}) {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
const method = (_a = options.method) != null ? _a : "geodesic";
|
|
11
|
+
const units = (_b = options.units) != null ? _b : "kilometers";
|
|
12
|
+
if (!point) throw new Error("point is required");
|
|
13
|
+
if (!polygonOrMultiPolygon)
|
|
14
|
+
throw new Error("polygon or multi-polygon is required");
|
|
15
|
+
const geom = _invariant.getGeom.call(void 0, polygonOrMultiPolygon);
|
|
16
|
+
if (geom.type === "MultiPolygon") {
|
|
17
|
+
const distances = geom.coordinates.map(
|
|
18
|
+
(coords) => pointToPolygonDistance(point, _helpers.polygon.call(void 0, coords), { method, units })
|
|
19
|
+
);
|
|
20
|
+
return Math.min(...distances.map(Math.abs)) * (_booleanpointinpolygon.booleanPointInPolygon.call(void 0, point, polygonOrMultiPolygon) ? -1 : 1);
|
|
21
|
+
}
|
|
22
|
+
if (geom.coordinates.length > 1) {
|
|
23
|
+
const [exteriorDistance, ...interiorDistances] = geom.coordinates.map(
|
|
24
|
+
(coords) => pointToPolygonDistance(point, _helpers.polygon.call(void 0, [coords]), { method, units })
|
|
25
|
+
);
|
|
26
|
+
if (exteriorDistance >= 0) return exteriorDistance;
|
|
27
|
+
const smallestInteriorDistance = Math.min(...interiorDistances);
|
|
28
|
+
if (smallestInteriorDistance < 0) return Math.abs(smallestInteriorDistance);
|
|
29
|
+
return Math.min(smallestInteriorDistance, Math.abs(exteriorDistance));
|
|
30
|
+
}
|
|
31
|
+
const lines = _polygontoline.polygonToLine.call(void 0, geom);
|
|
32
|
+
let minDistance = Infinity;
|
|
33
|
+
_meta.flattenEach.call(void 0, lines, (feature) => {
|
|
34
|
+
minDistance = Math.min(
|
|
35
|
+
minDistance,
|
|
36
|
+
_pointtolinedistance.pointToLineDistance.call(void 0, point, feature, {
|
|
37
|
+
method,
|
|
38
|
+
units
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
return _booleanpointinpolygon.booleanPointInPolygon.call(void 0, point, geom) ? -minDistance : minDistance;
|
|
43
|
+
}
|
|
44
|
+
var turf_point_to_polygon_distance_default = pointToPolygonDistance;
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
exports.default = turf_point_to_polygon_distance_default; exports.pointToPolygonDistance = pointToPolygonDistance;
|
|
49
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-point-to-polygon-distance/dist/cjs/index.cjs","../../index.ts"],"names":[],"mappings":"AAAA;ACAA,uEAAsC;AAStC,mEAAoC;AACpC,sDAA8B;AAC9B,4CAAwB;AACxB,kCAA4B;AAC5B,wCAA+B;AAgBxB,SAAS,sBAAA,CACd,KAAA,EACA,qBAAA,EAIA,QAAA,EAGI,CAAC,CAAA,EACG;AAvCV,EAAA,IAAA,EAAA,EAAA,EAAA;AAwCE,EAAA,MAAM,OAAA,EAAA,CAAS,GAAA,EAAA,OAAA,CAAQ,MAAA,EAAA,GAAR,KAAA,EAAA,GAAA,EAAkB,UAAA;AACjC,EAAA,MAAM,MAAA,EAAA,CAAQ,GAAA,EAAA,OAAA,CAAQ,KAAA,EAAA,GAAR,KAAA,EAAA,GAAA,EAAiB,YAAA;AAE/B,EAAA,GAAA,CAAI,CAAC,KAAA,EAAO,MAAM,IAAI,KAAA,CAAM,mBAAmB,CAAA;AAC/C,EAAA,GAAA,CAAI,CAAC,qBAAA;AACH,IAAA,MAAM,IAAI,KAAA,CAAM,sCAAsC,CAAA;AAExD,EAAA,MAAM,KAAA,EAAO,gCAAA,qBAA6B,CAAA;AAE1C,EAAA,GAAA,CAAI,IAAA,CAAK,KAAA,IAAS,cAAA,EAAgB;AAChC,IAAA,MAAM,UAAA,EAAY,IAAA,CAAK,WAAA,CAAY,GAAA;AAAA,MAAI,CAAC,MAAA,EAAA,GACtC,sBAAA,CAAuB,KAAA,EAAO,8BAAA,MAAc,CAAA,EAAG,EAAE,MAAA,EAAQ,MAAM,CAAC;AAAA,IAClE,CAAA;AACA,IAAA,OACE,IAAA,CAAK,GAAA,CAAI,GAAG,SAAA,CAAU,GAAA,CAAI,IAAA,CAAK,GAAG,CAAC,EAAA,EAAA,CAClC,0DAAA,KAAsB,EAAO,qBAAqB,EAAA,EAAI,CAAA,EAAA,EAAK,CAAA,CAAA;AAAA,EAEhE;AAEA,EAAA,GAAA,CAAI,IAAA,CAAK,WAAA,CAAY,OAAA,EAAS,CAAA,EAAG;AAE/B,IAAA,MAAM,CAAC,gBAAA,EAAkB,GAAG,iBAAiB,EAAA,EAAI,IAAA,CAAK,WAAA,CAAY,GAAA;AAAA,MAChE,CAAC,MAAA,EAAA,GACC,sBAAA,CAAuB,KAAA,EAAO,8BAAA,CAAS,MAAM,CAAC,CAAA,EAAG,EAAE,MAAA,EAAQ,MAAM,CAAC;AAAA,IACtE,CAAA;AACA,IAAA,GAAA,CAAI,iBAAA,GAAoB,CAAA,EAAG,OAAO,gBAAA;AAElC,IAAA,MAAM,yBAAA,EAA2B,IAAA,CAAK,GAAA,CAAI,GAAG,iBAAiB,CAAA;AAE9D,IAAA,GAAA,CAAI,yBAAA,EAA2B,CAAA,EAAG,OAAO,IAAA,CAAK,GAAA,CAAI,wBAAwB,CAAA;AAE1E,IAAA,OAAO,IAAA,CAAK,GAAA,CAAI,wBAAA,EAA0B,IAAA,CAAK,GAAA,CAAI,gBAAgB,CAAC,CAAA;AAAA,EACtE;AAEA,EAAA,MAAM,MAAA,EAAQ,0CAAA,IAAkB,CAAA;AAChC,EAAA,IAAI,YAAA,EAAc,QAAA;AAClB,EAAA,+BAAA,KAAY,EAAO,CAAC,OAAA,EAAA,GAAY;AAC9B,IAAA,YAAA,EAAc,IAAA,CAAK,GAAA;AAAA,MACjB,WAAA;AAAA,MACA,sDAAA,KAAoB,EAAO,OAAA,EAAgC;AAAA,QACzD,MAAA;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH,CAAA;AAAA,EACF,CAAC,CAAA;AAED,EAAA,OAAO,0DAAA,KAAsB,EAAO,IAAI,EAAA,EAAI,CAAC,YAAA,EAAc,WAAA;AAC7D;AAEA,IAAO,uCAAA,EAAQ,sBAAA;AD7Cf;AACE;AACA;AACF,kHAAC","file":"/home/runner/work/turf/turf/packages/turf-point-to-polygon-distance/dist/cjs/index.cjs","sourcesContent":[null,"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> | Point | Position} point Input point\n * @param {Feature<Polygon | MultiPolygon> | 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> | Point | Position,\n polygonOrMultiPolygon:\n | Feature<Polygon | MultiPolygon>\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<LineString>, {\n method,\n units,\n })\n );\n });\n\n return booleanPointInPolygon(point, geom) ? -minDistance : minDistance;\n}\n\nexport default pointToPolygonDistance;\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Feature, Point, Position, Polygon, MultiPolygon } from 'geojson';
|
|
2
|
+
import { Units } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Calculates the distance from a point to the edges of a polygon or multi-polygon.
|
|
6
|
+
* Returns negative values for points inside the polygon.
|
|
7
|
+
* Handles polygons with holes and multi-polygons.
|
|
8
|
+
* A hole is treated as the exterior of the polygon.
|
|
9
|
+
*
|
|
10
|
+
* @param {Feature<Point> | Point | Position} point Input point
|
|
11
|
+
* @param {Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon} polygonOrMultiPolygon Input polygon or multipolygon
|
|
12
|
+
* @param {Object} options Optional parameters
|
|
13
|
+
* @param {Units} options.units Units of the result e.g. "kilometers", "miles", "meters"
|
|
14
|
+
* @param {"geodesic" | "planar"} options.method Method of the result
|
|
15
|
+
* @returns {number} Distance in meters (negative values for points inside the polygon)
|
|
16
|
+
* @throws {Error} If input geometries are invalid
|
|
17
|
+
*/
|
|
18
|
+
declare function pointToPolygonDistance(point: Feature<Point> | Point | Position, polygonOrMultiPolygon: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon, options?: {
|
|
19
|
+
units?: Units;
|
|
20
|
+
method?: "geodesic" | "planar";
|
|
21
|
+
}): number;
|
|
22
|
+
|
|
23
|
+
export { pointToPolygonDistance as default, pointToPolygonDistance };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Feature, Point, Position, Polygon, MultiPolygon } from 'geojson';
|
|
2
|
+
import { Units } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Calculates the distance from a point to the edges of a polygon or multi-polygon.
|
|
6
|
+
* Returns negative values for points inside the polygon.
|
|
7
|
+
* Handles polygons with holes and multi-polygons.
|
|
8
|
+
* A hole is treated as the exterior of the polygon.
|
|
9
|
+
*
|
|
10
|
+
* @param {Feature<Point> | Point | Position} point Input point
|
|
11
|
+
* @param {Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon} polygonOrMultiPolygon Input polygon or multipolygon
|
|
12
|
+
* @param {Object} options Optional parameters
|
|
13
|
+
* @param {Units} options.units Units of the result e.g. "kilometers", "miles", "meters"
|
|
14
|
+
* @param {"geodesic" | "planar"} options.method Method of the result
|
|
15
|
+
* @returns {number} Distance in meters (negative values for points inside the polygon)
|
|
16
|
+
* @throws {Error} If input geometries are invalid
|
|
17
|
+
*/
|
|
18
|
+
declare function pointToPolygonDistance(point: Feature<Point> | Point | Position, polygonOrMultiPolygon: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon, options?: {
|
|
19
|
+
units?: Units;
|
|
20
|
+
method?: "geodesic" | "planar";
|
|
21
|
+
}): number;
|
|
22
|
+
|
|
23
|
+
export { pointToPolygonDistance as default, pointToPolygonDistance };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
|
|
3
|
+
import { pointToLineDistance } from "@turf/point-to-line-distance";
|
|
4
|
+
import { polygonToLine } from "@turf/polygon-to-line";
|
|
5
|
+
import { getGeom } from "@turf/invariant";
|
|
6
|
+
import { flattenEach } from "@turf/meta";
|
|
7
|
+
import { polygon } from "@turf/helpers";
|
|
8
|
+
function pointToPolygonDistance(point, polygonOrMultiPolygon, options = {}) {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
const method = (_a = options.method) != null ? _a : "geodesic";
|
|
11
|
+
const units = (_b = options.units) != null ? _b : "kilometers";
|
|
12
|
+
if (!point) throw new Error("point is required");
|
|
13
|
+
if (!polygonOrMultiPolygon)
|
|
14
|
+
throw new Error("polygon or multi-polygon is required");
|
|
15
|
+
const geom = getGeom(polygonOrMultiPolygon);
|
|
16
|
+
if (geom.type === "MultiPolygon") {
|
|
17
|
+
const distances = geom.coordinates.map(
|
|
18
|
+
(coords) => pointToPolygonDistance(point, polygon(coords), { method, units })
|
|
19
|
+
);
|
|
20
|
+
return Math.min(...distances.map(Math.abs)) * (booleanPointInPolygon(point, polygonOrMultiPolygon) ? -1 : 1);
|
|
21
|
+
}
|
|
22
|
+
if (geom.coordinates.length > 1) {
|
|
23
|
+
const [exteriorDistance, ...interiorDistances] = geom.coordinates.map(
|
|
24
|
+
(coords) => pointToPolygonDistance(point, polygon([coords]), { method, units })
|
|
25
|
+
);
|
|
26
|
+
if (exteriorDistance >= 0) return exteriorDistance;
|
|
27
|
+
const smallestInteriorDistance = Math.min(...interiorDistances);
|
|
28
|
+
if (smallestInteriorDistance < 0) return Math.abs(smallestInteriorDistance);
|
|
29
|
+
return Math.min(smallestInteriorDistance, Math.abs(exteriorDistance));
|
|
30
|
+
}
|
|
31
|
+
const lines = polygonToLine(geom);
|
|
32
|
+
let minDistance = Infinity;
|
|
33
|
+
flattenEach(lines, (feature) => {
|
|
34
|
+
minDistance = Math.min(
|
|
35
|
+
minDistance,
|
|
36
|
+
pointToLineDistance(point, feature, {
|
|
37
|
+
method,
|
|
38
|
+
units
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
return booleanPointInPolygon(point, geom) ? -minDistance : minDistance;
|
|
43
|
+
}
|
|
44
|
+
var turf_point_to_polygon_distance_default = pointToPolygonDistance;
|
|
45
|
+
export {
|
|
46
|
+
turf_point_to_polygon_distance_default as default,
|
|
47
|
+
pointToPolygonDistance
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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> | Point | Position} point Input point\n * @param {Feature<Polygon | MultiPolygon> | 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> | Point | Position,\n polygonOrMultiPolygon:\n | Feature<Polygon | MultiPolygon>\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<LineString>, {\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,CAAC,WACC,uBAAuB,OAAO,QAAQ,CAAC,MAAM,CAAC,GAAG,EAAE,QAAQ,MAAM,CAAC;AAAA,IACtE;AACA,QAAI,oBAAoB,EAAG,QAAO;AAElC,UAAM,2BAA2B,KAAK,IAAI,GAAG,iBAAiB;AAE9D,QAAI,2BAA2B,EAAG,QAAO,KAAK,IAAI,wBAAwB;AAE1E,WAAO,KAAK,IAAI,0BAA0B,KAAK,IAAI,gBAAgB,CAAC;AAAA,EACtE;AAEA,QAAM,QAAQ,cAAc,IAAI;AAChC,MAAI,cAAc;AAClB,cAAY,OAAO,CAAC,YAAY;AAC9B,kBAAc,KAAK;AAAA,MACjB;AAAA,MACA,oBAAoB,OAAO,SAAgC;AAAA,QACzD;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AAED,SAAO,sBAAsB,OAAO,IAAI,IAAI,CAAC,cAAc;AAC7D;AAEA,IAAO,yCAAQ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@turf/point-to-polygon-distance",
|
|
3
|
+
"version": "7.2.0",
|
|
4
|
+
"description": "turf point-to-polygon-distance module",
|
|
5
|
+
"author": "Turf Authors",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Marc <@pachacamac>"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/Turfjs/turf/issues"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/Turfjs/turf",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git://github.com/Turfjs/turf.git"
|
|
17
|
+
},
|
|
18
|
+
"funding": "https://opencollective.com/turf",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"turf",
|
|
24
|
+
"gis",
|
|
25
|
+
"point",
|
|
26
|
+
"polygon",
|
|
27
|
+
"distance"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"main": "dist/cjs/index.cjs",
|
|
31
|
+
"module": "dist/esm/index.js",
|
|
32
|
+
"types": "dist/esm/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
"./package.json": "./package.json",
|
|
35
|
+
".": {
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/esm/index.d.ts",
|
|
38
|
+
"default": "./dist/esm/index.js"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./dist/cjs/index.d.cts",
|
|
42
|
+
"default": "./dist/cjs/index.cjs"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"sideEffects": false,
|
|
47
|
+
"files": [
|
|
48
|
+
"dist"
|
|
49
|
+
],
|
|
50
|
+
"scripts": {
|
|
51
|
+
"bench": "tsx bench.ts",
|
|
52
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
53
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
54
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
55
|
+
"test:tape": "tsx test.ts"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@types/benchmark": "^2.1.5",
|
|
59
|
+
"@types/tape": "^4.13.4",
|
|
60
|
+
"benchmark": "^2.1.4",
|
|
61
|
+
"load-json-file": "^7.0.1",
|
|
62
|
+
"npm-run-all": "^4.1.5",
|
|
63
|
+
"tape": "^5.9.0",
|
|
64
|
+
"tsup": "^8.3.5",
|
|
65
|
+
"tsx": "^4.19.2",
|
|
66
|
+
"typescript": "^5.5.4",
|
|
67
|
+
"write-json-file": "^5.0.0"
|
|
68
|
+
},
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"@turf/boolean-point-in-polygon": "^7.2.0",
|
|
71
|
+
"@turf/helpers": "^7.2.0",
|
|
72
|
+
"@turf/invariant": "^7.2.0",
|
|
73
|
+
"@turf/meta": "^7.2.0",
|
|
74
|
+
"@turf/point-to-line-distance": "^7.2.0",
|
|
75
|
+
"@turf/polygon-to-line": "^7.2.0",
|
|
76
|
+
"@types/geojson": "^7946.0.10",
|
|
77
|
+
"tslib": "^2.8.1"
|
|
78
|
+
},
|
|
79
|
+
"gitHead": "7b0f0374c4668cd569f8904c71e2ae7d941be867"
|
|
80
|
+
}
|