@turf/point-on-feature 7.0.0-alpha.1 → 7.0.0-alpha.110
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/README.md +4 -9
- package/dist/cjs/index.cjs +111 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +32 -0
- package/dist/esm/index.d.mts +32 -0
- package/dist/esm/index.mjs +111 -0
- package/dist/esm/index.mjs.map +1 -0
- package/package.json +36 -27
- package/dist/es/index.js +0 -149
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -159
- package/index.d.ts +0 -7
package/README.md
CHANGED
|
@@ -47,26 +47,21 @@ Returns **[Feature][5]<[Point][6]>** a point on the surface of `input`
|
|
|
47
47
|
|
|
48
48
|
[6]: https://tools.ietf.org/html/rfc7946#section-3.1.2
|
|
49
49
|
|
|
50
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
51
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
52
|
-
./scripts/generate-readmes in the turf project. -->
|
|
50
|
+
<!-- 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. -->
|
|
53
51
|
|
|
54
52
|
---
|
|
55
53
|
|
|
56
|
-
This module is part of the [Turfjs project](
|
|
57
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
58
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
59
|
-
PRs and issues.
|
|
54
|
+
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.
|
|
60
55
|
|
|
61
56
|
### Installation
|
|
62
57
|
|
|
63
|
-
Install this module individually:
|
|
58
|
+
Install this single module individually:
|
|
64
59
|
|
|
65
60
|
```sh
|
|
66
61
|
$ npm install @turf/point-on-feature
|
|
67
62
|
```
|
|
68
63
|
|
|
69
|
-
Or install the
|
|
64
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
70
65
|
|
|
71
66
|
```sh
|
|
72
67
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
var _explode = require('@turf/explode');
|
|
6
|
+
var _center = require('@turf/center');
|
|
7
|
+
var _nearestpoint = require('@turf/nearest-point');
|
|
8
|
+
var _booleanpointinpolygon = require('@turf/boolean-point-in-polygon');
|
|
9
|
+
var _helpers = require('@turf/helpers');
|
|
10
|
+
function pointOnFeature(geojson) {
|
|
11
|
+
const fc = normalize(geojson);
|
|
12
|
+
const cent = _center.center.call(void 0, fc);
|
|
13
|
+
let onSurface = false;
|
|
14
|
+
let i = 0;
|
|
15
|
+
while (!onSurface && i < fc.features.length) {
|
|
16
|
+
const geom = fc.features[i].geometry;
|
|
17
|
+
let x, y, x1, y1, x2, y2;
|
|
18
|
+
let onLine = false;
|
|
19
|
+
if (geom.type === "Point") {
|
|
20
|
+
if (cent.geometry.coordinates[0] === geom.coordinates[0] && cent.geometry.coordinates[1] === geom.coordinates[1]) {
|
|
21
|
+
onSurface = true;
|
|
22
|
+
}
|
|
23
|
+
} else if (geom.type === "MultiPoint") {
|
|
24
|
+
let onMultiPoint = false;
|
|
25
|
+
let k = 0;
|
|
26
|
+
while (!onMultiPoint && k < geom.coordinates.length) {
|
|
27
|
+
if (cent.geometry.coordinates[0] === geom.coordinates[k][0] && cent.geometry.coordinates[1] === geom.coordinates[k][1]) {
|
|
28
|
+
onSurface = true;
|
|
29
|
+
onMultiPoint = true;
|
|
30
|
+
}
|
|
31
|
+
k++;
|
|
32
|
+
}
|
|
33
|
+
} else if (geom.type === "LineString") {
|
|
34
|
+
let k = 0;
|
|
35
|
+
while (!onLine && k < geom.coordinates.length - 1) {
|
|
36
|
+
x = cent.geometry.coordinates[0];
|
|
37
|
+
y = cent.geometry.coordinates[1];
|
|
38
|
+
x1 = geom.coordinates[k][0];
|
|
39
|
+
y1 = geom.coordinates[k][1];
|
|
40
|
+
x2 = geom.coordinates[k + 1][0];
|
|
41
|
+
y2 = geom.coordinates[k + 1][1];
|
|
42
|
+
if (pointOnSegment(x, y, x1, y1, x2, y2)) {
|
|
43
|
+
onLine = true;
|
|
44
|
+
onSurface = true;
|
|
45
|
+
}
|
|
46
|
+
k++;
|
|
47
|
+
}
|
|
48
|
+
} else if (geom.type === "MultiLineString") {
|
|
49
|
+
let j = 0;
|
|
50
|
+
while (j < geom.coordinates.length) {
|
|
51
|
+
onLine = false;
|
|
52
|
+
let k = 0;
|
|
53
|
+
const line = geom.coordinates[j];
|
|
54
|
+
while (!onLine && k < line.length - 1) {
|
|
55
|
+
x = cent.geometry.coordinates[0];
|
|
56
|
+
y = cent.geometry.coordinates[1];
|
|
57
|
+
x1 = line[k][0];
|
|
58
|
+
y1 = line[k][1];
|
|
59
|
+
x2 = line[k + 1][0];
|
|
60
|
+
y2 = line[k + 1][1];
|
|
61
|
+
if (pointOnSegment(x, y, x1, y1, x2, y2)) {
|
|
62
|
+
onLine = true;
|
|
63
|
+
onSurface = true;
|
|
64
|
+
}
|
|
65
|
+
k++;
|
|
66
|
+
}
|
|
67
|
+
j++;
|
|
68
|
+
}
|
|
69
|
+
} else if (geom.type === "Polygon" || geom.type === "MultiPolygon") {
|
|
70
|
+
if (_booleanpointinpolygon.booleanPointInPolygon.call(void 0, cent, geom)) {
|
|
71
|
+
onSurface = true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
i++;
|
|
75
|
+
}
|
|
76
|
+
if (onSurface) {
|
|
77
|
+
return cent;
|
|
78
|
+
} else {
|
|
79
|
+
const vertices = _helpers.featureCollection.call(void 0, []);
|
|
80
|
+
for (let f = 0; f < fc.features.length; f++) {
|
|
81
|
+
vertices.features = vertices.features.concat(
|
|
82
|
+
_explode.explode.call(void 0, fc.features[f]).features
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return _helpers.point.call(void 0, _nearestpoint.nearestPoint.call(void 0, cent, vertices).geometry.coordinates);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
__name(pointOnFeature, "pointOnFeature");
|
|
89
|
+
function normalize(geojson) {
|
|
90
|
+
if (geojson.type !== "FeatureCollection") {
|
|
91
|
+
if (geojson.type !== "Feature") {
|
|
92
|
+
return _helpers.featureCollection.call(void 0, [_helpers.feature.call(void 0, geojson)]);
|
|
93
|
+
}
|
|
94
|
+
return _helpers.featureCollection.call(void 0, [geojson]);
|
|
95
|
+
}
|
|
96
|
+
return geojson;
|
|
97
|
+
}
|
|
98
|
+
__name(normalize, "normalize");
|
|
99
|
+
function pointOnSegment(x, y, x1, y1, x2, y2) {
|
|
100
|
+
const ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
|
|
101
|
+
const ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
|
|
102
|
+
const pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
|
|
103
|
+
return ab === ap + pb;
|
|
104
|
+
}
|
|
105
|
+
__name(pointOnSegment, "pointOnSegment");
|
|
106
|
+
var turf_point_on_feature_default = pointOnFeature;
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
exports.default = turf_point_on_feature_default; exports.pointOnFeature = pointOnFeature;
|
|
111
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;;AAEA,SAAS,eAAe;AACxB,SAAS,UAAU,gBAAgB;AACnC,SAAS,oBAAoB;AAC7B,SAAS,6BAA6B;AACtC,SAAS,mBAAmB,SAAS,aAAa;AA4BlD,SAAS,eAAe,SAAqC;AAE3D,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AARS;AAUT,SAAS,eACP,GACA,GACA,IACA,IACA,IACA,IACA;AACA,QAAM,KAAK,KAAK,MAAM,KAAK,OAAO,KAAK,OAAO,KAAK,OAAO,KAAK,GAAG;AAClE,QAAM,KAAK,KAAK,MAAM,IAAI,OAAO,IAAI,OAAO,IAAI,OAAO,IAAI,GAAG;AAC9D,QAAM,KAAK,KAAK,MAAM,KAAK,MAAM,KAAK,MAAM,KAAK,MAAM,KAAK,EAAE;AAC9D,SAAO,OAAO,KAAK;AACrB;AAZS;AAeT,IAAO,gCAAQ","sourcesContent":["import type { Feature, Point } from \"geojson\";\nimport type { AllGeoJSON } from \"@turf/helpers\";\nimport { explode } from \"@turf/explode\";\nimport { center as centroid } from \"@turf/center\";\nimport { nearestPoint } from \"@turf/nearest-point\";\nimport { booleanPointInPolygon } from \"@turf/boolean-point-in-polygon\";\nimport { featureCollection, feature, point } from \"@turf/helpers\";\n\n/**\n * Takes a Feature or FeatureCollection and returns a {@link Point} guaranteed to be on the surface of the feature.\n *\n * * Given a {@link Polygon}, the point will be in the area of the polygon\n * * Given a {@link LineString}, the point will be along the string\n * * Given a {@link Point}, the point will the same as the input\n *\n * @name pointOnFeature\n * @param {GeoJSON} geojson any Feature or FeatureCollection\n * @returns {Feature<Point>} a point on the surface of `input`\n * @example\n * var polygon = turf.polygon([[\n * [116, -36],\n * [131, -32],\n * [146, -43],\n * [155, -25],\n * [133, -9],\n * [111, -22],\n * [116, -36]\n * ]]);\n *\n * var pointOnPolygon = turf.pointOnFeature(polygon);\n *\n * //addToMap\n * var addToMap = [polygon, pointOnPolygon];\n */\nfunction pointOnFeature(geojson: AllGeoJSON): Feature<Point> {\n // normalize\n const fc = normalize(geojson);\n\n // get centroid\n const cent = centroid(fc);\n\n // check to see if centroid is on surface\n let onSurface = false;\n let i = 0;\n while (!onSurface && i < fc.features.length) {\n const geom = fc.features[i].geometry;\n let x, y, x1, y1, x2, y2;\n let onLine = false;\n if (geom.type === \"Point\") {\n if (\n cent.geometry.coordinates[0] === geom.coordinates[0] &&\n cent.geometry.coordinates[1] === geom.coordinates[1]\n ) {\n onSurface = true;\n }\n } else if (geom.type === \"MultiPoint\") {\n let onMultiPoint = false;\n let k = 0;\n while (!onMultiPoint && k < geom.coordinates.length) {\n if (\n cent.geometry.coordinates[0] === geom.coordinates[k][0] &&\n cent.geometry.coordinates[1] === geom.coordinates[k][1]\n ) {\n onSurface = true;\n onMultiPoint = true;\n }\n k++;\n }\n } else if (geom.type === \"LineString\") {\n let k = 0;\n while (!onLine && k < geom.coordinates.length - 1) {\n x = cent.geometry.coordinates[0];\n y = cent.geometry.coordinates[1];\n x1 = geom.coordinates[k][0];\n y1 = geom.coordinates[k][1];\n x2 = geom.coordinates[k + 1][0];\n y2 = geom.coordinates[k + 1][1];\n if (pointOnSegment(x, y, x1, y1, x2, y2)) {\n onLine = true;\n onSurface = true;\n }\n k++;\n }\n } else if (geom.type === \"MultiLineString\") {\n let j = 0;\n while (j < geom.coordinates.length) {\n onLine = false;\n let k = 0;\n const line = geom.coordinates[j];\n while (!onLine && k < line.length - 1) {\n x = cent.geometry.coordinates[0];\n y = cent.geometry.coordinates[1];\n x1 = line[k][0];\n y1 = line[k][1];\n x2 = line[k + 1][0];\n y2 = line[k + 1][1];\n if (pointOnSegment(x, y, x1, y1, x2, y2)) {\n onLine = true;\n onSurface = true;\n }\n k++;\n }\n j++;\n }\n } else if (geom.type === \"Polygon\" || geom.type === \"MultiPolygon\") {\n if (booleanPointInPolygon(cent, geom)) {\n onSurface = true;\n }\n }\n i++;\n }\n if (onSurface) {\n return cent;\n } else {\n const vertices = featureCollection<Point>([]);\n for (let f = 0; f < fc.features.length; f++) {\n vertices.features = vertices.features.concat(\n explode(fc.features[f]).features\n );\n }\n // Remove distanceToPoint properties from nearestPoint()\n return point(nearestPoint(cent, vertices).geometry.coordinates);\n }\n}\n\n/**\n * Normalizes any GeoJSON to a FeatureCollection\n *\n * @private\n * @name normalize\n * @param {GeoJSON} geojson Any GeoJSON\n * @returns {FeatureCollection} FeatureCollection\n */\nfunction normalize(geojson: AllGeoJSON) {\n if (geojson.type !== \"FeatureCollection\") {\n if (geojson.type !== \"Feature\") {\n return featureCollection([feature(geojson)]);\n }\n return featureCollection([geojson]);\n }\n return geojson;\n}\n\nfunction pointOnSegment(\n x: number,\n y: number,\n x1: number,\n y1: number,\n x2: number,\n y2: number\n) {\n const ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));\n const ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));\n const pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));\n return ab === ap + pb;\n}\n\nexport { pointOnFeature };\nexport default pointOnFeature;\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Feature, Point } from 'geojson';
|
|
2
|
+
import { AllGeoJSON } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Takes a Feature or FeatureCollection and returns a {@link Point} guaranteed to be on the surface of the feature.
|
|
6
|
+
*
|
|
7
|
+
* * Given a {@link Polygon}, the point will be in the area of the polygon
|
|
8
|
+
* * Given a {@link LineString}, the point will be along the string
|
|
9
|
+
* * Given a {@link Point}, the point will the same as the input
|
|
10
|
+
*
|
|
11
|
+
* @name pointOnFeature
|
|
12
|
+
* @param {GeoJSON} geojson any Feature or FeatureCollection
|
|
13
|
+
* @returns {Feature<Point>} a point on the surface of `input`
|
|
14
|
+
* @example
|
|
15
|
+
* var polygon = turf.polygon([[
|
|
16
|
+
* [116, -36],
|
|
17
|
+
* [131, -32],
|
|
18
|
+
* [146, -43],
|
|
19
|
+
* [155, -25],
|
|
20
|
+
* [133, -9],
|
|
21
|
+
* [111, -22],
|
|
22
|
+
* [116, -36]
|
|
23
|
+
* ]]);
|
|
24
|
+
*
|
|
25
|
+
* var pointOnPolygon = turf.pointOnFeature(polygon);
|
|
26
|
+
*
|
|
27
|
+
* //addToMap
|
|
28
|
+
* var addToMap = [polygon, pointOnPolygon];
|
|
29
|
+
*/
|
|
30
|
+
declare function pointOnFeature(geojson: AllGeoJSON): Feature<Point>;
|
|
31
|
+
|
|
32
|
+
export { pointOnFeature as default, pointOnFeature };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Feature, Point } from 'geojson';
|
|
2
|
+
import { AllGeoJSON } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Takes a Feature or FeatureCollection and returns a {@link Point} guaranteed to be on the surface of the feature.
|
|
6
|
+
*
|
|
7
|
+
* * Given a {@link Polygon}, the point will be in the area of the polygon
|
|
8
|
+
* * Given a {@link LineString}, the point will be along the string
|
|
9
|
+
* * Given a {@link Point}, the point will the same as the input
|
|
10
|
+
*
|
|
11
|
+
* @name pointOnFeature
|
|
12
|
+
* @param {GeoJSON} geojson any Feature or FeatureCollection
|
|
13
|
+
* @returns {Feature<Point>} a point on the surface of `input`
|
|
14
|
+
* @example
|
|
15
|
+
* var polygon = turf.polygon([[
|
|
16
|
+
* [116, -36],
|
|
17
|
+
* [131, -32],
|
|
18
|
+
* [146, -43],
|
|
19
|
+
* [155, -25],
|
|
20
|
+
* [133, -9],
|
|
21
|
+
* [111, -22],
|
|
22
|
+
* [116, -36]
|
|
23
|
+
* ]]);
|
|
24
|
+
*
|
|
25
|
+
* var pointOnPolygon = turf.pointOnFeature(polygon);
|
|
26
|
+
*
|
|
27
|
+
* //addToMap
|
|
28
|
+
* var addToMap = [polygon, pointOnPolygon];
|
|
29
|
+
*/
|
|
30
|
+
declare function pointOnFeature(geojson: AllGeoJSON): Feature<Point>;
|
|
31
|
+
|
|
32
|
+
export { pointOnFeature as default, pointOnFeature };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
import { explode } from "@turf/explode";
|
|
6
|
+
import { center as centroid } from "@turf/center";
|
|
7
|
+
import { nearestPoint } from "@turf/nearest-point";
|
|
8
|
+
import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
|
|
9
|
+
import { featureCollection, feature, point } from "@turf/helpers";
|
|
10
|
+
function pointOnFeature(geojson) {
|
|
11
|
+
const fc = normalize(geojson);
|
|
12
|
+
const cent = centroid(fc);
|
|
13
|
+
let onSurface = false;
|
|
14
|
+
let i = 0;
|
|
15
|
+
while (!onSurface && i < fc.features.length) {
|
|
16
|
+
const geom = fc.features[i].geometry;
|
|
17
|
+
let x, y, x1, y1, x2, y2;
|
|
18
|
+
let onLine = false;
|
|
19
|
+
if (geom.type === "Point") {
|
|
20
|
+
if (cent.geometry.coordinates[0] === geom.coordinates[0] && cent.geometry.coordinates[1] === geom.coordinates[1]) {
|
|
21
|
+
onSurface = true;
|
|
22
|
+
}
|
|
23
|
+
} else if (geom.type === "MultiPoint") {
|
|
24
|
+
let onMultiPoint = false;
|
|
25
|
+
let k = 0;
|
|
26
|
+
while (!onMultiPoint && k < geom.coordinates.length) {
|
|
27
|
+
if (cent.geometry.coordinates[0] === geom.coordinates[k][0] && cent.geometry.coordinates[1] === geom.coordinates[k][1]) {
|
|
28
|
+
onSurface = true;
|
|
29
|
+
onMultiPoint = true;
|
|
30
|
+
}
|
|
31
|
+
k++;
|
|
32
|
+
}
|
|
33
|
+
} else if (geom.type === "LineString") {
|
|
34
|
+
let k = 0;
|
|
35
|
+
while (!onLine && k < geom.coordinates.length - 1) {
|
|
36
|
+
x = cent.geometry.coordinates[0];
|
|
37
|
+
y = cent.geometry.coordinates[1];
|
|
38
|
+
x1 = geom.coordinates[k][0];
|
|
39
|
+
y1 = geom.coordinates[k][1];
|
|
40
|
+
x2 = geom.coordinates[k + 1][0];
|
|
41
|
+
y2 = geom.coordinates[k + 1][1];
|
|
42
|
+
if (pointOnSegment(x, y, x1, y1, x2, y2)) {
|
|
43
|
+
onLine = true;
|
|
44
|
+
onSurface = true;
|
|
45
|
+
}
|
|
46
|
+
k++;
|
|
47
|
+
}
|
|
48
|
+
} else if (geom.type === "MultiLineString") {
|
|
49
|
+
let j = 0;
|
|
50
|
+
while (j < geom.coordinates.length) {
|
|
51
|
+
onLine = false;
|
|
52
|
+
let k = 0;
|
|
53
|
+
const line = geom.coordinates[j];
|
|
54
|
+
while (!onLine && k < line.length - 1) {
|
|
55
|
+
x = cent.geometry.coordinates[0];
|
|
56
|
+
y = cent.geometry.coordinates[1];
|
|
57
|
+
x1 = line[k][0];
|
|
58
|
+
y1 = line[k][1];
|
|
59
|
+
x2 = line[k + 1][0];
|
|
60
|
+
y2 = line[k + 1][1];
|
|
61
|
+
if (pointOnSegment(x, y, x1, y1, x2, y2)) {
|
|
62
|
+
onLine = true;
|
|
63
|
+
onSurface = true;
|
|
64
|
+
}
|
|
65
|
+
k++;
|
|
66
|
+
}
|
|
67
|
+
j++;
|
|
68
|
+
}
|
|
69
|
+
} else if (geom.type === "Polygon" || geom.type === "MultiPolygon") {
|
|
70
|
+
if (booleanPointInPolygon(cent, geom)) {
|
|
71
|
+
onSurface = true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
i++;
|
|
75
|
+
}
|
|
76
|
+
if (onSurface) {
|
|
77
|
+
return cent;
|
|
78
|
+
} else {
|
|
79
|
+
const vertices = featureCollection([]);
|
|
80
|
+
for (let f = 0; f < fc.features.length; f++) {
|
|
81
|
+
vertices.features = vertices.features.concat(
|
|
82
|
+
explode(fc.features[f]).features
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return point(nearestPoint(cent, vertices).geometry.coordinates);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
__name(pointOnFeature, "pointOnFeature");
|
|
89
|
+
function normalize(geojson) {
|
|
90
|
+
if (geojson.type !== "FeatureCollection") {
|
|
91
|
+
if (geojson.type !== "Feature") {
|
|
92
|
+
return featureCollection([feature(geojson)]);
|
|
93
|
+
}
|
|
94
|
+
return featureCollection([geojson]);
|
|
95
|
+
}
|
|
96
|
+
return geojson;
|
|
97
|
+
}
|
|
98
|
+
__name(normalize, "normalize");
|
|
99
|
+
function pointOnSegment(x, y, x1, y1, x2, y2) {
|
|
100
|
+
const ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
|
|
101
|
+
const ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
|
|
102
|
+
const pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
|
|
103
|
+
return ab === ap + pb;
|
|
104
|
+
}
|
|
105
|
+
__name(pointOnSegment, "pointOnSegment");
|
|
106
|
+
var turf_point_on_feature_default = pointOnFeature;
|
|
107
|
+
export {
|
|
108
|
+
turf_point_on_feature_default as default,
|
|
109
|
+
pointOnFeature
|
|
110
|
+
};
|
|
111
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import type { Feature, Point } from \"geojson\";\nimport type { AllGeoJSON } from \"@turf/helpers\";\nimport { explode } from \"@turf/explode\";\nimport { center as centroid } from \"@turf/center\";\nimport { nearestPoint } from \"@turf/nearest-point\";\nimport { booleanPointInPolygon } from \"@turf/boolean-point-in-polygon\";\nimport { featureCollection, feature, point } from \"@turf/helpers\";\n\n/**\n * Takes a Feature or FeatureCollection and returns a {@link Point} guaranteed to be on the surface of the feature.\n *\n * * Given a {@link Polygon}, the point will be in the area of the polygon\n * * Given a {@link LineString}, the point will be along the string\n * * Given a {@link Point}, the point will the same as the input\n *\n * @name pointOnFeature\n * @param {GeoJSON} geojson any Feature or FeatureCollection\n * @returns {Feature<Point>} a point on the surface of `input`\n * @example\n * var polygon = turf.polygon([[\n * [116, -36],\n * [131, -32],\n * [146, -43],\n * [155, -25],\n * [133, -9],\n * [111, -22],\n * [116, -36]\n * ]]);\n *\n * var pointOnPolygon = turf.pointOnFeature(polygon);\n *\n * //addToMap\n * var addToMap = [polygon, pointOnPolygon];\n */\nfunction pointOnFeature(geojson: AllGeoJSON): Feature<Point> {\n // normalize\n const fc = normalize(geojson);\n\n // get centroid\n const cent = centroid(fc);\n\n // check to see if centroid is on surface\n let onSurface = false;\n let i = 0;\n while (!onSurface && i < fc.features.length) {\n const geom = fc.features[i].geometry;\n let x, y, x1, y1, x2, y2;\n let onLine = false;\n if (geom.type === \"Point\") {\n if (\n cent.geometry.coordinates[0] === geom.coordinates[0] &&\n cent.geometry.coordinates[1] === geom.coordinates[1]\n ) {\n onSurface = true;\n }\n } else if (geom.type === \"MultiPoint\") {\n let onMultiPoint = false;\n let k = 0;\n while (!onMultiPoint && k < geom.coordinates.length) {\n if (\n cent.geometry.coordinates[0] === geom.coordinates[k][0] &&\n cent.geometry.coordinates[1] === geom.coordinates[k][1]\n ) {\n onSurface = true;\n onMultiPoint = true;\n }\n k++;\n }\n } else if (geom.type === \"LineString\") {\n let k = 0;\n while (!onLine && k < geom.coordinates.length - 1) {\n x = cent.geometry.coordinates[0];\n y = cent.geometry.coordinates[1];\n x1 = geom.coordinates[k][0];\n y1 = geom.coordinates[k][1];\n x2 = geom.coordinates[k + 1][0];\n y2 = geom.coordinates[k + 1][1];\n if (pointOnSegment(x, y, x1, y1, x2, y2)) {\n onLine = true;\n onSurface = true;\n }\n k++;\n }\n } else if (geom.type === \"MultiLineString\") {\n let j = 0;\n while (j < geom.coordinates.length) {\n onLine = false;\n let k = 0;\n const line = geom.coordinates[j];\n while (!onLine && k < line.length - 1) {\n x = cent.geometry.coordinates[0];\n y = cent.geometry.coordinates[1];\n x1 = line[k][0];\n y1 = line[k][1];\n x2 = line[k + 1][0];\n y2 = line[k + 1][1];\n if (pointOnSegment(x, y, x1, y1, x2, y2)) {\n onLine = true;\n onSurface = true;\n }\n k++;\n }\n j++;\n }\n } else if (geom.type === \"Polygon\" || geom.type === \"MultiPolygon\") {\n if (booleanPointInPolygon(cent, geom)) {\n onSurface = true;\n }\n }\n i++;\n }\n if (onSurface) {\n return cent;\n } else {\n const vertices = featureCollection<Point>([]);\n for (let f = 0; f < fc.features.length; f++) {\n vertices.features = vertices.features.concat(\n explode(fc.features[f]).features\n );\n }\n // Remove distanceToPoint properties from nearestPoint()\n return point(nearestPoint(cent, vertices).geometry.coordinates);\n }\n}\n\n/**\n * Normalizes any GeoJSON to a FeatureCollection\n *\n * @private\n * @name normalize\n * @param {GeoJSON} geojson Any GeoJSON\n * @returns {FeatureCollection} FeatureCollection\n */\nfunction normalize(geojson: AllGeoJSON) {\n if (geojson.type !== \"FeatureCollection\") {\n if (geojson.type !== \"Feature\") {\n return featureCollection([feature(geojson)]);\n }\n return featureCollection([geojson]);\n }\n return geojson;\n}\n\nfunction pointOnSegment(\n x: number,\n y: number,\n x1: number,\n y1: number,\n x2: number,\n y2: number\n) {\n const ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));\n const ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));\n const pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));\n return ab === ap + pb;\n}\n\nexport { pointOnFeature };\nexport default pointOnFeature;\n"],"mappings":";;;;AAEA,SAAS,eAAe;AACxB,SAAS,UAAU,gBAAgB;AACnC,SAAS,oBAAoB;AAC7B,SAAS,6BAA6B;AACtC,SAAS,mBAAmB,SAAS,aAAa;AA4BlD,SAAS,eAAe,SAAqC;AAE3D,QAAM,KAAK,UAAU,OAAO;AAG5B,QAAM,OAAO,SAAS,EAAE;AAGxB,MAAI,YAAY;AAChB,MAAI,IAAI;AACR,SAAO,CAAC,aAAa,IAAI,GAAG,SAAS,QAAQ;AAC3C,UAAM,OAAO,GAAG,SAAS,CAAC,EAAE;AAC5B,QAAI,GAAG,GAAG,IAAI,IAAI,IAAI;AACtB,QAAI,SAAS;AACb,QAAI,KAAK,SAAS,SAAS;AACzB,UACE,KAAK,SAAS,YAAY,CAAC,MAAM,KAAK,YAAY,CAAC,KACnD,KAAK,SAAS,YAAY,CAAC,M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GED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/point-on-feature",
|
|
3
|
-
"version": "7.0.0-alpha.
|
|
3
|
+
"version": "7.0.0-alpha.110+1411d63a7",
|
|
4
4
|
"description": "turf point-on-feature module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,44 +24,53 @@
|
|
|
24
24
|
"surface",
|
|
25
25
|
"polygon"
|
|
26
26
|
],
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"type": "commonjs",
|
|
28
|
+
"main": "dist/cjs/index.cjs",
|
|
29
|
+
"module": "dist/esm/index.mjs",
|
|
30
|
+
"types": "dist/cjs/index.d.ts",
|
|
29
31
|
"exports": {
|
|
30
32
|
"./package.json": "./package.json",
|
|
31
33
|
".": {
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/esm/index.d.mts",
|
|
36
|
+
"default": "./dist/esm/index.mjs"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/cjs/index.d.ts",
|
|
40
|
+
"default": "./dist/cjs/index.cjs"
|
|
41
|
+
}
|
|
35
42
|
}
|
|
36
43
|
},
|
|
37
|
-
"types": "index.d.ts",
|
|
38
44
|
"sideEffects": false,
|
|
39
45
|
"files": [
|
|
40
|
-
"dist"
|
|
41
|
-
"index.d.ts"
|
|
46
|
+
"dist"
|
|
42
47
|
],
|
|
43
48
|
"scripts": {
|
|
44
|
-
"bench": "tsx bench.
|
|
45
|
-
"build": "
|
|
46
|
-
"docs": "tsx ../../scripts/generate-readmes",
|
|
47
|
-
"test": "npm-run-all test:*",
|
|
48
|
-
"test:tape": "tsx test.
|
|
49
|
+
"bench": "tsx bench.ts",
|
|
50
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
51
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
52
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
53
|
+
"test:tape": "tsx test.ts"
|
|
49
54
|
},
|
|
50
55
|
"devDependencies": {
|
|
51
|
-
"@turf/meta": "^7.0.0-alpha.
|
|
52
|
-
"@turf/truncate": "^7.0.0-alpha.
|
|
53
|
-
"benchmark": "
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
56
|
+
"@turf/meta": "^7.0.0-alpha.110+1411d63a7",
|
|
57
|
+
"@turf/truncate": "^7.0.0-alpha.110+1411d63a7",
|
|
58
|
+
"@types/benchmark": "^2.1.5",
|
|
59
|
+
"@types/tape": "^4.2.32",
|
|
60
|
+
"benchmark": "^2.1.4",
|
|
61
|
+
"npm-run-all": "^4.1.5",
|
|
62
|
+
"tape": "^5.7.2",
|
|
63
|
+
"tsup": "^8.0.1",
|
|
64
|
+
"tsx": "^4.6.2",
|
|
65
|
+
"typescript": "^5.2.2"
|
|
58
66
|
},
|
|
59
67
|
"dependencies": {
|
|
60
|
-
"@turf/boolean-point-in-polygon": "^7.0.0-alpha.
|
|
61
|
-
"@turf/center": "^7.0.0-alpha.
|
|
62
|
-
"@turf/explode": "^7.0.0-alpha.
|
|
63
|
-
"@turf/helpers": "^7.0.0-alpha.
|
|
64
|
-
"@turf/nearest-point": "^7.0.0-alpha.
|
|
68
|
+
"@turf/boolean-point-in-polygon": "^7.0.0-alpha.110+1411d63a7",
|
|
69
|
+
"@turf/center": "^7.0.0-alpha.110+1411d63a7",
|
|
70
|
+
"@turf/explode": "^7.0.0-alpha.110+1411d63a7",
|
|
71
|
+
"@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
|
|
72
|
+
"@turf/nearest-point": "^7.0.0-alpha.110+1411d63a7",
|
|
73
|
+
"tslib": "^2.6.2"
|
|
65
74
|
},
|
|
66
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
|
|
67
76
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import explode from '@turf/explode';
|
|
2
|
-
import centroid from '@turf/center';
|
|
3
|
-
import nearestPoint from '@turf/nearest-point';
|
|
4
|
-
import booleanPointInPolygon from '@turf/boolean-point-in-polygon';
|
|
5
|
-
import { featureCollection, point, feature } from '@turf/helpers';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Takes a Feature or FeatureCollection and returns a {@link Point} guaranteed to be on the surface of the feature.
|
|
9
|
-
*
|
|
10
|
-
* * Given a {@link Polygon}, the point will be in the area of the polygon
|
|
11
|
-
* * Given a {@link LineString}, the point will be along the string
|
|
12
|
-
* * Given a {@link Point}, the point will the same as the input
|
|
13
|
-
*
|
|
14
|
-
* @name pointOnFeature
|
|
15
|
-
* @param {GeoJSON} geojson any Feature or FeatureCollection
|
|
16
|
-
* @returns {Feature<Point>} a point on the surface of `input`
|
|
17
|
-
* @example
|
|
18
|
-
* var polygon = turf.polygon([[
|
|
19
|
-
* [116, -36],
|
|
20
|
-
* [131, -32],
|
|
21
|
-
* [146, -43],
|
|
22
|
-
* [155, -25],
|
|
23
|
-
* [133, -9],
|
|
24
|
-
* [111, -22],
|
|
25
|
-
* [116, -36]
|
|
26
|
-
* ]]);
|
|
27
|
-
*
|
|
28
|
-
* var pointOnPolygon = turf.pointOnFeature(polygon);
|
|
29
|
-
*
|
|
30
|
-
* //addToMap
|
|
31
|
-
* var addToMap = [polygon, pointOnPolygon];
|
|
32
|
-
*/
|
|
33
|
-
function pointOnFeature(geojson) {
|
|
34
|
-
// normalize
|
|
35
|
-
var fc = normalize(geojson);
|
|
36
|
-
|
|
37
|
-
// get centroid
|
|
38
|
-
var cent = centroid(fc);
|
|
39
|
-
|
|
40
|
-
// check to see if centroid is on surface
|
|
41
|
-
var onSurface = false;
|
|
42
|
-
var i = 0;
|
|
43
|
-
while (!onSurface && i < fc.features.length) {
|
|
44
|
-
var geom = fc.features[i].geometry;
|
|
45
|
-
var x, y, x1, y1, x2, y2, k;
|
|
46
|
-
var onLine = false;
|
|
47
|
-
if (geom.type === "Point") {
|
|
48
|
-
if (
|
|
49
|
-
cent.geometry.coordinates[0] === geom.coordinates[0] &&
|
|
50
|
-
cent.geometry.coordinates[1] === geom.coordinates[1]
|
|
51
|
-
) {
|
|
52
|
-
onSurface = true;
|
|
53
|
-
}
|
|
54
|
-
} else if (geom.type === "MultiPoint") {
|
|
55
|
-
var onMultiPoint = false;
|
|
56
|
-
k = 0;
|
|
57
|
-
while (!onMultiPoint && k < geom.coordinates.length) {
|
|
58
|
-
if (
|
|
59
|
-
cent.geometry.coordinates[0] === geom.coordinates[k][0] &&
|
|
60
|
-
cent.geometry.coordinates[1] === geom.coordinates[k][1]
|
|
61
|
-
) {
|
|
62
|
-
onSurface = true;
|
|
63
|
-
onMultiPoint = true;
|
|
64
|
-
}
|
|
65
|
-
k++;
|
|
66
|
-
}
|
|
67
|
-
} else if (geom.type === "LineString") {
|
|
68
|
-
k = 0;
|
|
69
|
-
while (!onLine && k < geom.coordinates.length - 1) {
|
|
70
|
-
x = cent.geometry.coordinates[0];
|
|
71
|
-
y = cent.geometry.coordinates[1];
|
|
72
|
-
x1 = geom.coordinates[k][0];
|
|
73
|
-
y1 = geom.coordinates[k][1];
|
|
74
|
-
x2 = geom.coordinates[k + 1][0];
|
|
75
|
-
y2 = geom.coordinates[k + 1][1];
|
|
76
|
-
if (pointOnSegment(x, y, x1, y1, x2, y2)) {
|
|
77
|
-
onLine = true;
|
|
78
|
-
onSurface = true;
|
|
79
|
-
}
|
|
80
|
-
k++;
|
|
81
|
-
}
|
|
82
|
-
} else if (geom.type === "MultiLineString") {
|
|
83
|
-
var j = 0;
|
|
84
|
-
while (j < geom.coordinates.length) {
|
|
85
|
-
onLine = false;
|
|
86
|
-
k = 0;
|
|
87
|
-
var line = geom.coordinates[j];
|
|
88
|
-
while (!onLine && k < line.length - 1) {
|
|
89
|
-
x = cent.geometry.coordinates[0];
|
|
90
|
-
y = cent.geometry.coordinates[1];
|
|
91
|
-
x1 = line[k][0];
|
|
92
|
-
y1 = line[k][1];
|
|
93
|
-
x2 = line[k + 1][0];
|
|
94
|
-
y2 = line[k + 1][1];
|
|
95
|
-
if (pointOnSegment(x, y, x1, y1, x2, y2)) {
|
|
96
|
-
onLine = true;
|
|
97
|
-
onSurface = true;
|
|
98
|
-
}
|
|
99
|
-
k++;
|
|
100
|
-
}
|
|
101
|
-
j++;
|
|
102
|
-
}
|
|
103
|
-
} else if (geom.type === "Polygon" || geom.type === "MultiPolygon") {
|
|
104
|
-
if (booleanPointInPolygon(cent, geom)) {
|
|
105
|
-
onSurface = true;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
i++;
|
|
109
|
-
}
|
|
110
|
-
if (onSurface) {
|
|
111
|
-
return cent;
|
|
112
|
-
} else {
|
|
113
|
-
var vertices = featureCollection([]);
|
|
114
|
-
for (i = 0; i < fc.features.length; i++) {
|
|
115
|
-
vertices.features = vertices.features.concat(
|
|
116
|
-
explode(fc.features[i]).features
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
// Remove distanceToPoint properties from nearestPoint()
|
|
120
|
-
return point(nearestPoint(cent, vertices).geometry.coordinates);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Normalizes any GeoJSON to a FeatureCollection
|
|
126
|
-
*
|
|
127
|
-
* @private
|
|
128
|
-
* @name normalize
|
|
129
|
-
* @param {GeoJSON} geojson Any GeoJSON
|
|
130
|
-
* @returns {FeatureCollection} FeatureCollection
|
|
131
|
-
*/
|
|
132
|
-
function normalize(geojson) {
|
|
133
|
-
if (geojson.type !== "FeatureCollection") {
|
|
134
|
-
if (geojson.type !== "Feature") {
|
|
135
|
-
return featureCollection([feature(geojson)]);
|
|
136
|
-
}
|
|
137
|
-
return featureCollection([geojson]);
|
|
138
|
-
}
|
|
139
|
-
return geojson;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function pointOnSegment(x, y, x1, y1, x2, y2) {
|
|
143
|
-
var ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
|
|
144
|
-
var ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
|
|
145
|
-
var pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
|
|
146
|
-
return ab === ap + pb;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export default pointOnFeature;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var explode = require('@turf/explode');
|
|
4
|
-
var centroid = require('@turf/center');
|
|
5
|
-
var nearestPoint = require('@turf/nearest-point');
|
|
6
|
-
var booleanPointInPolygon = require('@turf/boolean-point-in-polygon');
|
|
7
|
-
var helpers = require('@turf/helpers');
|
|
8
|
-
|
|
9
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
|
-
|
|
11
|
-
var explode__default = /*#__PURE__*/_interopDefaultLegacy(explode);
|
|
12
|
-
var centroid__default = /*#__PURE__*/_interopDefaultLegacy(centroid);
|
|
13
|
-
var nearestPoint__default = /*#__PURE__*/_interopDefaultLegacy(nearestPoint);
|
|
14
|
-
var booleanPointInPolygon__default = /*#__PURE__*/_interopDefaultLegacy(booleanPointInPolygon);
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Takes a Feature or FeatureCollection and returns a {@link Point} guaranteed to be on the surface of the feature.
|
|
18
|
-
*
|
|
19
|
-
* * Given a {@link Polygon}, the point will be in the area of the polygon
|
|
20
|
-
* * Given a {@link LineString}, the point will be along the string
|
|
21
|
-
* * Given a {@link Point}, the point will the same as the input
|
|
22
|
-
*
|
|
23
|
-
* @name pointOnFeature
|
|
24
|
-
* @param {GeoJSON} geojson any Feature or FeatureCollection
|
|
25
|
-
* @returns {Feature<Point>} a point on the surface of `input`
|
|
26
|
-
* @example
|
|
27
|
-
* var polygon = turf.polygon([[
|
|
28
|
-
* [116, -36],
|
|
29
|
-
* [131, -32],
|
|
30
|
-
* [146, -43],
|
|
31
|
-
* [155, -25],
|
|
32
|
-
* [133, -9],
|
|
33
|
-
* [111, -22],
|
|
34
|
-
* [116, -36]
|
|
35
|
-
* ]]);
|
|
36
|
-
*
|
|
37
|
-
* var pointOnPolygon = turf.pointOnFeature(polygon);
|
|
38
|
-
*
|
|
39
|
-
* //addToMap
|
|
40
|
-
* var addToMap = [polygon, pointOnPolygon];
|
|
41
|
-
*/
|
|
42
|
-
function pointOnFeature(geojson) {
|
|
43
|
-
// normalize
|
|
44
|
-
var fc = normalize(geojson);
|
|
45
|
-
|
|
46
|
-
// get centroid
|
|
47
|
-
var cent = centroid__default['default'](fc);
|
|
48
|
-
|
|
49
|
-
// check to see if centroid is on surface
|
|
50
|
-
var onSurface = false;
|
|
51
|
-
var i = 0;
|
|
52
|
-
while (!onSurface && i < fc.features.length) {
|
|
53
|
-
var geom = fc.features[i].geometry;
|
|
54
|
-
var x, y, x1, y1, x2, y2, k;
|
|
55
|
-
var onLine = false;
|
|
56
|
-
if (geom.type === "Point") {
|
|
57
|
-
if (
|
|
58
|
-
cent.geometry.coordinates[0] === geom.coordinates[0] &&
|
|
59
|
-
cent.geometry.coordinates[1] === geom.coordinates[1]
|
|
60
|
-
) {
|
|
61
|
-
onSurface = true;
|
|
62
|
-
}
|
|
63
|
-
} else if (geom.type === "MultiPoint") {
|
|
64
|
-
var onMultiPoint = false;
|
|
65
|
-
k = 0;
|
|
66
|
-
while (!onMultiPoint && k < geom.coordinates.length) {
|
|
67
|
-
if (
|
|
68
|
-
cent.geometry.coordinates[0] === geom.coordinates[k][0] &&
|
|
69
|
-
cent.geometry.coordinates[1] === geom.coordinates[k][1]
|
|
70
|
-
) {
|
|
71
|
-
onSurface = true;
|
|
72
|
-
onMultiPoint = true;
|
|
73
|
-
}
|
|
74
|
-
k++;
|
|
75
|
-
}
|
|
76
|
-
} else if (geom.type === "LineString") {
|
|
77
|
-
k = 0;
|
|
78
|
-
while (!onLine && k < geom.coordinates.length - 1) {
|
|
79
|
-
x = cent.geometry.coordinates[0];
|
|
80
|
-
y = cent.geometry.coordinates[1];
|
|
81
|
-
x1 = geom.coordinates[k][0];
|
|
82
|
-
y1 = geom.coordinates[k][1];
|
|
83
|
-
x2 = geom.coordinates[k + 1][0];
|
|
84
|
-
y2 = geom.coordinates[k + 1][1];
|
|
85
|
-
if (pointOnSegment(x, y, x1, y1, x2, y2)) {
|
|
86
|
-
onLine = true;
|
|
87
|
-
onSurface = true;
|
|
88
|
-
}
|
|
89
|
-
k++;
|
|
90
|
-
}
|
|
91
|
-
} else if (geom.type === "MultiLineString") {
|
|
92
|
-
var j = 0;
|
|
93
|
-
while (j < geom.coordinates.length) {
|
|
94
|
-
onLine = false;
|
|
95
|
-
k = 0;
|
|
96
|
-
var line = geom.coordinates[j];
|
|
97
|
-
while (!onLine && k < line.length - 1) {
|
|
98
|
-
x = cent.geometry.coordinates[0];
|
|
99
|
-
y = cent.geometry.coordinates[1];
|
|
100
|
-
x1 = line[k][0];
|
|
101
|
-
y1 = line[k][1];
|
|
102
|
-
x2 = line[k + 1][0];
|
|
103
|
-
y2 = line[k + 1][1];
|
|
104
|
-
if (pointOnSegment(x, y, x1, y1, x2, y2)) {
|
|
105
|
-
onLine = true;
|
|
106
|
-
onSurface = true;
|
|
107
|
-
}
|
|
108
|
-
k++;
|
|
109
|
-
}
|
|
110
|
-
j++;
|
|
111
|
-
}
|
|
112
|
-
} else if (geom.type === "Polygon" || geom.type === "MultiPolygon") {
|
|
113
|
-
if (booleanPointInPolygon__default['default'](cent, geom)) {
|
|
114
|
-
onSurface = true;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
i++;
|
|
118
|
-
}
|
|
119
|
-
if (onSurface) {
|
|
120
|
-
return cent;
|
|
121
|
-
} else {
|
|
122
|
-
var vertices = helpers.featureCollection([]);
|
|
123
|
-
for (i = 0; i < fc.features.length; i++) {
|
|
124
|
-
vertices.features = vertices.features.concat(
|
|
125
|
-
explode__default['default'](fc.features[i]).features
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
// Remove distanceToPoint properties from nearestPoint()
|
|
129
|
-
return helpers.point(nearestPoint__default['default'](cent, vertices).geometry.coordinates);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Normalizes any GeoJSON to a FeatureCollection
|
|
135
|
-
*
|
|
136
|
-
* @private
|
|
137
|
-
* @name normalize
|
|
138
|
-
* @param {GeoJSON} geojson Any GeoJSON
|
|
139
|
-
* @returns {FeatureCollection} FeatureCollection
|
|
140
|
-
*/
|
|
141
|
-
function normalize(geojson) {
|
|
142
|
-
if (geojson.type !== "FeatureCollection") {
|
|
143
|
-
if (geojson.type !== "Feature") {
|
|
144
|
-
return helpers.featureCollection([helpers.feature(geojson)]);
|
|
145
|
-
}
|
|
146
|
-
return helpers.featureCollection([geojson]);
|
|
147
|
-
}
|
|
148
|
-
return geojson;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function pointOnSegment(x, y, x1, y1, x2, y2) {
|
|
152
|
-
var ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
|
|
153
|
-
var ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
|
|
154
|
-
var pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
|
|
155
|
-
return ab === ap + pb;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
module.exports = pointOnFeature;
|
|
159
|
-
module.exports.default = pointOnFeature;
|