@turf/clean-coords 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 +133 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{js → cjs}/index.d.ts +2 -1
- package/dist/esm/index.d.mts +23 -0
- package/dist/esm/index.mjs +133 -0
- package/dist/esm/index.mjs.map +1 -0
- package/package.json +33 -28
- package/dist/es/index.js +0 -163
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -165
package/README.md
CHANGED
|
@@ -36,26 +36,21 @@ Returns **([Geometry][1] | [Feature][2])** the cleaned input Feature/Geometry
|
|
|
36
36
|
|
|
37
37
|
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
38
38
|
|
|
39
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
40
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
41
|
-
./scripts/generate-readmes in the turf project. -->
|
|
39
|
+
<!-- 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. -->
|
|
42
40
|
|
|
43
41
|
---
|
|
44
42
|
|
|
45
|
-
This module is part of the [Turfjs project](
|
|
46
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
47
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
48
|
-
PRs and issues.
|
|
43
|
+
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.
|
|
49
44
|
|
|
50
45
|
### Installation
|
|
51
46
|
|
|
52
|
-
Install this module individually:
|
|
47
|
+
Install this single module individually:
|
|
53
48
|
|
|
54
49
|
```sh
|
|
55
50
|
$ npm install @turf/clean-coords
|
|
56
51
|
```
|
|
57
52
|
|
|
58
|
-
Or install the
|
|
53
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
59
54
|
|
|
60
55
|
```sh
|
|
61
56
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,133 @@
|
|
|
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 _helpers = require('@turf/helpers');
|
|
6
|
+
var _invariant = require('@turf/invariant');
|
|
7
|
+
function cleanCoords(geojson, options = {}) {
|
|
8
|
+
var mutate = typeof options === "object" ? options.mutate : options;
|
|
9
|
+
if (!geojson)
|
|
10
|
+
throw new Error("geojson is required");
|
|
11
|
+
var type = _invariant.getType.call(void 0, geojson);
|
|
12
|
+
var newCoords = [];
|
|
13
|
+
switch (type) {
|
|
14
|
+
case "LineString":
|
|
15
|
+
newCoords = cleanLine(geojson, type);
|
|
16
|
+
break;
|
|
17
|
+
case "MultiLineString":
|
|
18
|
+
case "Polygon":
|
|
19
|
+
_invariant.getCoords.call(void 0, geojson).forEach(function(line) {
|
|
20
|
+
newCoords.push(cleanLine(line, type));
|
|
21
|
+
});
|
|
22
|
+
break;
|
|
23
|
+
case "MultiPolygon":
|
|
24
|
+
_invariant.getCoords.call(void 0, geojson).forEach(function(polygons) {
|
|
25
|
+
var polyPoints = [];
|
|
26
|
+
polygons.forEach(function(ring) {
|
|
27
|
+
polyPoints.push(cleanLine(ring, type));
|
|
28
|
+
});
|
|
29
|
+
newCoords.push(polyPoints);
|
|
30
|
+
});
|
|
31
|
+
break;
|
|
32
|
+
case "Point":
|
|
33
|
+
return geojson;
|
|
34
|
+
case "MultiPoint":
|
|
35
|
+
var existing = {};
|
|
36
|
+
_invariant.getCoords.call(void 0, geojson).forEach(function(coord) {
|
|
37
|
+
var key = coord.join("-");
|
|
38
|
+
if (!Object.prototype.hasOwnProperty.call(existing, key)) {
|
|
39
|
+
newCoords.push(coord);
|
|
40
|
+
existing[key] = true;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
break;
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(type + " geometry not supported");
|
|
46
|
+
}
|
|
47
|
+
if (geojson.coordinates) {
|
|
48
|
+
if (mutate === true) {
|
|
49
|
+
geojson.coordinates = newCoords;
|
|
50
|
+
return geojson;
|
|
51
|
+
}
|
|
52
|
+
return { type, coordinates: newCoords };
|
|
53
|
+
} else {
|
|
54
|
+
if (mutate === true) {
|
|
55
|
+
geojson.geometry.coordinates = newCoords;
|
|
56
|
+
return geojson;
|
|
57
|
+
}
|
|
58
|
+
return _helpers.feature.call(void 0, { type, coordinates: newCoords }, geojson.properties, {
|
|
59
|
+
bbox: geojson.bbox,
|
|
60
|
+
id: geojson.id
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
__name(cleanCoords, "cleanCoords");
|
|
65
|
+
function cleanLine(line, type) {
|
|
66
|
+
var points = _invariant.getCoords.call(void 0, line);
|
|
67
|
+
if (points.length === 2 && !equals(points[0], points[1]))
|
|
68
|
+
return points;
|
|
69
|
+
var newPoints = [];
|
|
70
|
+
var secondToLast = points.length - 1;
|
|
71
|
+
var newPointsLength = newPoints.length;
|
|
72
|
+
newPoints.push(points[0]);
|
|
73
|
+
for (var i = 1; i < secondToLast; i++) {
|
|
74
|
+
var prevAddedPoint = newPoints[newPoints.length - 1];
|
|
75
|
+
if (points[i][0] === prevAddedPoint[0] && points[i][1] === prevAddedPoint[1])
|
|
76
|
+
continue;
|
|
77
|
+
else {
|
|
78
|
+
newPoints.push(points[i]);
|
|
79
|
+
newPointsLength = newPoints.length;
|
|
80
|
+
if (newPointsLength > 2) {
|
|
81
|
+
if (isPointOnLineSegment(
|
|
82
|
+
newPoints[newPointsLength - 3],
|
|
83
|
+
newPoints[newPointsLength - 1],
|
|
84
|
+
newPoints[newPointsLength - 2]
|
|
85
|
+
))
|
|
86
|
+
newPoints.splice(newPoints.length - 2, 1);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
newPoints.push(points[points.length - 1]);
|
|
91
|
+
newPointsLength = newPoints.length;
|
|
92
|
+
if ((type === "Polygon" || type === "MultiPolygon") && equals(points[0], points[points.length - 1]) && newPointsLength < 4) {
|
|
93
|
+
throw new Error("invalid polygon");
|
|
94
|
+
}
|
|
95
|
+
if (type === "LineString" && newPointsLength < 3) {
|
|
96
|
+
return newPoints;
|
|
97
|
+
}
|
|
98
|
+
if (isPointOnLineSegment(
|
|
99
|
+
newPoints[newPointsLength - 3],
|
|
100
|
+
newPoints[newPointsLength - 1],
|
|
101
|
+
newPoints[newPointsLength - 2]
|
|
102
|
+
))
|
|
103
|
+
newPoints.splice(newPoints.length - 2, 1);
|
|
104
|
+
return newPoints;
|
|
105
|
+
}
|
|
106
|
+
__name(cleanLine, "cleanLine");
|
|
107
|
+
function equals(pt1, pt2) {
|
|
108
|
+
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
109
|
+
}
|
|
110
|
+
__name(equals, "equals");
|
|
111
|
+
function isPointOnLineSegment(start, end, point) {
|
|
112
|
+
var x = point[0], y = point[1];
|
|
113
|
+
var startX = start[0], startY = start[1];
|
|
114
|
+
var endX = end[0], endY = end[1];
|
|
115
|
+
var dxc = x - startX;
|
|
116
|
+
var dyc = y - startY;
|
|
117
|
+
var dxl = endX - startX;
|
|
118
|
+
var dyl = endY - startY;
|
|
119
|
+
var cross = dxc * dyl - dyc * dxl;
|
|
120
|
+
if (cross !== 0)
|
|
121
|
+
return false;
|
|
122
|
+
else if (Math.abs(dxl) >= Math.abs(dyl))
|
|
123
|
+
return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
|
|
124
|
+
else
|
|
125
|
+
return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
|
|
126
|
+
}
|
|
127
|
+
__name(isPointOnLineSegment, "isPointOnLineSegment");
|
|
128
|
+
var turf_clean_coords_default = cleanCoords;
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
exports.cleanCoords = cleanCoords; exports.default = turf_clean_coords_default;
|
|
133
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;;AACA,SAAS,eAAe;AACxB,SAAS,WAAW,eAAe;AAsBnC,SAAS,YACP,SACA,UAEI,CAAC,GACL;AAEA,MAAI,SAAS,OAAO,YAAY,WAAW,QAAQ,SAAS;AAC5D,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,qBAAqB;AACnD,MAAI,OAAO,QAAQ,OAAO;AAG1B,MAAI,YAAY,CAAC;AAEjB,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,kBAAY,UAAU,SAAS,IAAI;AACnC;AAAA,IACF,KAAK;AAAA,IACL,KAAK;AACH,gBAAU,OAAO,EAAE,QAAQ,SAAU,MAAM;AACzC,kBAAU,KAAK,UAAU,MAAM,IAAI,CAAC;AAAA,MACtC,CAAC;AACD;AAAA,IACF,KAAK;AACH,gBAAU,OAAO,EAAE,QAAQ,SAAU,UAAe;AAClD,YAAI,aAAyB,CAAC;AAC9B,iBAAS,QAAQ,SAAU,MAAkB;AAC3C,qBAAW,KAAK,UAAU,MAAM,IAAI,CAAC;AAAA,QACvC,CAAC;AACD,kBAAU,KAAK,UAAU;AAAA,MAC3B,CAAC;AACD;AAAA,IACF,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,UAAI,WAAiC,CAAC;AACtC,gBAAU,OAAO,EAAE,QAAQ,SAAU,OAAY;AAC/C,YAAI,MAAM,MAAM,KAAK,GAAG;AACxB,YAAI,CAAC,OAAO,UAAU,eAAe,KAAK,UAAU,GAAG,GAAG;AACxD,oBAAU,KAAK,KAAK;AACpB,mBAAS,GAAG,IAAI;AAAA,QAClB;AAAA,MACF,CAAC;AACD;AAAA,IAC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xC,oBAAkB,UAAU;AAG5B,OACG,SAAS,aAAa,SAAS,mBAChC,OAAO,OAAO,CAAC,GAAG,OAAO,OAAO,SAAS,CAAC,CAAC,KAC3C,kBAAkB,GAClB;AACA,UAAM,IAAI,MAAM,iBAAiB;AAAA,EACnC;AAEA,MAAI,SAAS,gBAAgB,kBAAkB,GAAG;AAChD,WAAO;AAAA,EACT;AAEA,MACE;AAAA,IACE,UAAU,kBAAkB,CAAC;AAAA,IAC7B,UAAU,kBAAkB,CAAC;AAAA,IAC7B,UAAU,kBAAkB,CAAC;AAAA,EAC/B;AAEA,cAAU,OAAO,UAAU,SAAS,GAAG,CAAC;AAE1C,SAAO;AACT;AA1DS;AAoET,SAAS,OAAO,KAAe,KAAe;AAC5C,SAAO,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,IAAI,CAAC;AAC9C;AAFS;AAcT,SAAS,qBAAqB,OAAiB,KAAe,OAAiB;AAC7E,MAAI,IAAI,MAAM,CAAC,GACb,IAAI,MAAM,CAAC;AACb,MAAI,SAAS,MAAM,CAAC,GAClB,SAAS,MAAM,CAAC;AAClB,MAAI,OAAO,IAAI,CAAC,GACd,OAAO,IAAI,CAAC;AAEd,MAAI,MAAM,IAAI;AACd,MAAI,MAAM,IAAI;AACd,MAAI,MAAM,OAAO;AACjB,MAAI,MAAM,OAAO;AACjB,MAAI,QAAQ,MAAM,MAAM,MAAM;AAE9B,MAAI,UAAU;AAAG,WAAO;AAAA,WACf,KAAK,IAAI,GAAG,KAAK,KAAK,IAAI,GAAG;AACpC,WAAO,MAAM,IAAI,UAAU,KAAK,KAAK,OAAO,QAAQ,KAAK,KAAK;AAAA;AAC3D,WAAO,MAAM,IAAI,UAAU,KAAK,KAAK,OAAO,QAAQ,KAAK,KAAK;AACrE;AAlBS;AAqBT,IAAO,4BAAQ","sourcesContent":["import { Position } from \"geojson\";\nimport { feature } from \"@turf/helpers\";\nimport { getCoords, getType } from \"@turf/invariant\";\n\n// To-Do => Improve Typescript GeoJSON handling\n\n/**\n * Removes redundant coordinates from any GeoJSON Geometry.\n *\n * @name cleanCoords\n * @param {Geometry|Feature} geojson Feature or Geometry\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated\n * @returns {Geometry|Feature} the cleaned input Feature/Geometry\n * @example\n * var line = turf.lineString([[0, 0], [0, 2], [0, 5], [0, 8], [0, 8], [0, 10]]);\n * var multiPoint = turf.multiPoint([[0, 0], [0, 0], [2, 2]]);\n *\n * turf.cleanCoords(line).geometry.coordinates;\n * //= [[0, 0], [0, 10]]\n *\n * turf.cleanCoords(multiPoint).geometry.coordinates;\n * //= [[0, 0], [2, 2]]\n */\nfunction cleanCoords(\n geojson: any,\n options: {\n mutate?: boolean;\n } = {}\n) {\n // Backwards compatible with v4.0\n var mutate = typeof options === \"object\" ? options.mutate : options;\n if (!geojson) throw new Error(\"geojson is required\");\n var type = getType(geojson);\n\n // Store new \"clean\" points in this Array\n var newCoords = [];\n\n switch (type) {\n case \"LineString\":\n newCoords = cleanLine(geojson, type);\n break;\n case \"MultiLineString\":\n case \"Polygon\":\n getCoords(geojson).forEach(function (line) {\n newCoords.push(cleanLine(line, type));\n });\n break;\n case \"MultiPolygon\":\n getCoords(geojson).forEach(function (polygons: any) {\n var polyPoints: Position[] = [];\n polygons.forEach(function (ring: Position[]) {\n polyPoints.push(cleanLine(ring, type));\n });\n newCoords.push(polyPoints);\n });\n break;\n case \"Point\":\n return geojson;\n case \"MultiPoint\":\n var existing: Record<string, true> = {};\n getCoords(geojson).forEach(function (coord: any) {\n var key = coord.join(\"-\");\n if (!Object.prototype.hasOwnProperty.call(existing, key)) {\n newCoords.push(coord);\n existing[key] = true;\n }\n });\n break;\n default:\n throw new Error(type + \" geometry not supported\");\n }\n\n // Support input mutation\n if (geojson.coordinates) {\n if (mutate === true) {\n geojson.coordinates = newCoords;\n return geojson;\n }\n return { type: type, coordinates: newCoords };\n } else {\n if (mutate === true) {\n geojson.geometry.coordinates = newCoords;\n return geojson;\n }\n return feature({ type: type, coordinates: newCoords }, geojson.properties, {\n bbox: geojson.bbox,\n id: geojson.id,\n });\n }\n}\n\n/**\n * Clean Coords\n *\n * @private\n * @param {Array<number>|LineString} line Line\n * @param {string} type Type of geometry\n * @returns {Array<number>} Cleaned coordinates\n */\nfunction cleanLine(line: Position[], type: string) {\n var points = getCoords(line);\n // handle \"clean\" segment\n if (points.length === 2 && !equals(points[0], points[1])) return points;\n\n var newPoints = [];\n var secondToLast = points.length - 1;\n var newPointsLength = newPoints.length;\n\n newPoints.push(points[0]);\n for (var i = 1; i < secondToLast; i++) {\n var prevAddedPoint = newPoints[newPoints.length - 1];\n if (\n points[i][0] === prevAddedPoint[0] &&\n points[i][1] === prevAddedPoint[1]\n )\n continue;\n else {\n newPoints.push(points[i]);\n newPointsLength = newPoints.length;\n if (newPointsLength > 2) {\n if (\n isPointOnLineSegment(\n newPoints[newPointsLength - 3],\n newPoints[newPointsLength - 1],\n newPoints[newPointsLength - 2]\n )\n )\n newPoints.splice(newPoints.length - 2, 1);\n }\n }\n }\n newPoints.push(points[points.length - 1]);\n newPointsLength = newPoints.length;\n\n // (Multi)Polygons must have at least 4 points, but a closed LineString with only 3 points is acceptable\n if (\n (type === \"Polygon\" || type === \"MultiPolygon\") &&\n equals(points[0], points[points.length - 1]) &&\n newPointsLength < 4\n ) {\n throw new Error(\"invalid polygon\");\n }\n\n if (type === \"LineString\" && newPointsLength < 3) {\n return newPoints;\n }\n\n if (\n isPointOnLineSegment(\n newPoints[newPointsLength - 3],\n newPoints[newPointsLength - 1],\n newPoints[newPointsLength - 2]\n )\n )\n newPoints.splice(newPoints.length - 2, 1);\n\n return newPoints;\n}\n\n/**\n * Compares two points and returns if they are equals\n *\n * @private\n * @param {Position} pt1 point\n * @param {Position} pt2 point\n * @returns {boolean} true if they are equals\n */\nfunction equals(pt1: Position, pt2: Position) {\n return pt1[0] === pt2[0] && pt1[1] === pt2[1];\n}\n\n/**\n * Returns if `point` is on the segment between `start` and `end`.\n * Borrowed from `@turf/boolean-point-on-line` to speed up the evaluation (instead of using the module as dependency)\n *\n * @private\n * @param {Position} start coord pair of start of line\n * @param {Position} end coord pair of end of line\n * @param {Position} point coord pair of point to check\n * @returns {boolean} true/false\n */\nfunction isPointOnLineSegment(start: Position, end: Position, point: Position) {\n var x = point[0],\n y = point[1];\n var startX = start[0],\n startY = start[1];\n var endX = end[0],\n endY = end[1];\n\n var dxc = x - startX;\n var dyc = y - startY;\n var dxl = endX - startX;\n var dyl = endY - startY;\n var cross = dxc * dyl - dyc * dxl;\n\n if (cross !== 0) return false;\n else if (Math.abs(dxl) >= Math.abs(dyl))\n return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;\n else return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;\n}\n\nexport { cleanCoords };\nexport default cleanCoords;\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes redundant coordinates from any GeoJSON Geometry.
|
|
3
|
+
*
|
|
4
|
+
* @name cleanCoords
|
|
5
|
+
* @param {Geometry|Feature} geojson Feature or Geometry
|
|
6
|
+
* @param {Object} [options={}] Optional parameters
|
|
7
|
+
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated
|
|
8
|
+
* @returns {Geometry|Feature} the cleaned input Feature/Geometry
|
|
9
|
+
* @example
|
|
10
|
+
* var line = turf.lineString([[0, 0], [0, 2], [0, 5], [0, 8], [0, 8], [0, 10]]);
|
|
11
|
+
* var multiPoint = turf.multiPoint([[0, 0], [0, 0], [2, 2]]);
|
|
12
|
+
*
|
|
13
|
+
* turf.cleanCoords(line).geometry.coordinates;
|
|
14
|
+
* //= [[0, 0], [0, 10]]
|
|
15
|
+
*
|
|
16
|
+
* turf.cleanCoords(multiPoint).geometry.coordinates;
|
|
17
|
+
* //= [[0, 0], [2, 2]]
|
|
18
|
+
*/
|
|
19
|
+
declare function cleanCoords(geojson: any, options?: {
|
|
20
|
+
mutate?: boolean;
|
|
21
|
+
}): any;
|
|
22
|
+
|
|
23
|
+
export { cleanCoords, cleanCoords as default };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
import { feature } from "@turf/helpers";
|
|
6
|
+
import { getCoords, getType } from "@turf/invariant";
|
|
7
|
+
function cleanCoords(geojson, options = {}) {
|
|
8
|
+
var mutate = typeof options === "object" ? options.mutate : options;
|
|
9
|
+
if (!geojson)
|
|
10
|
+
throw new Error("geojson is required");
|
|
11
|
+
var type = getType(geojson);
|
|
12
|
+
var newCoords = [];
|
|
13
|
+
switch (type) {
|
|
14
|
+
case "LineString":
|
|
15
|
+
newCoords = cleanLine(geojson, type);
|
|
16
|
+
break;
|
|
17
|
+
case "MultiLineString":
|
|
18
|
+
case "Polygon":
|
|
19
|
+
getCoords(geojson).forEach(function(line) {
|
|
20
|
+
newCoords.push(cleanLine(line, type));
|
|
21
|
+
});
|
|
22
|
+
break;
|
|
23
|
+
case "MultiPolygon":
|
|
24
|
+
getCoords(geojson).forEach(function(polygons) {
|
|
25
|
+
var polyPoints = [];
|
|
26
|
+
polygons.forEach(function(ring) {
|
|
27
|
+
polyPoints.push(cleanLine(ring, type));
|
|
28
|
+
});
|
|
29
|
+
newCoords.push(polyPoints);
|
|
30
|
+
});
|
|
31
|
+
break;
|
|
32
|
+
case "Point":
|
|
33
|
+
return geojson;
|
|
34
|
+
case "MultiPoint":
|
|
35
|
+
var existing = {};
|
|
36
|
+
getCoords(geojson).forEach(function(coord) {
|
|
37
|
+
var key = coord.join("-");
|
|
38
|
+
if (!Object.prototype.hasOwnProperty.call(existing, key)) {
|
|
39
|
+
newCoords.push(coord);
|
|
40
|
+
existing[key] = true;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
break;
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(type + " geometry not supported");
|
|
46
|
+
}
|
|
47
|
+
if (geojson.coordinates) {
|
|
48
|
+
if (mutate === true) {
|
|
49
|
+
geojson.coordinates = newCoords;
|
|
50
|
+
return geojson;
|
|
51
|
+
}
|
|
52
|
+
return { type, coordinates: newCoords };
|
|
53
|
+
} else {
|
|
54
|
+
if (mutate === true) {
|
|
55
|
+
geojson.geometry.coordinates = newCoords;
|
|
56
|
+
return geojson;
|
|
57
|
+
}
|
|
58
|
+
return feature({ type, coordinates: newCoords }, geojson.properties, {
|
|
59
|
+
bbox: geojson.bbox,
|
|
60
|
+
id: geojson.id
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
__name(cleanCoords, "cleanCoords");
|
|
65
|
+
function cleanLine(line, type) {
|
|
66
|
+
var points = getCoords(line);
|
|
67
|
+
if (points.length === 2 && !equals(points[0], points[1]))
|
|
68
|
+
return points;
|
|
69
|
+
var newPoints = [];
|
|
70
|
+
var secondToLast = points.length - 1;
|
|
71
|
+
var newPointsLength = newPoints.length;
|
|
72
|
+
newPoints.push(points[0]);
|
|
73
|
+
for (var i = 1; i < secondToLast; i++) {
|
|
74
|
+
var prevAddedPoint = newPoints[newPoints.length - 1];
|
|
75
|
+
if (points[i][0] === prevAddedPoint[0] && points[i][1] === prevAddedPoint[1])
|
|
76
|
+
continue;
|
|
77
|
+
else {
|
|
78
|
+
newPoints.push(points[i]);
|
|
79
|
+
newPointsLength = newPoints.length;
|
|
80
|
+
if (newPointsLength > 2) {
|
|
81
|
+
if (isPointOnLineSegment(
|
|
82
|
+
newPoints[newPointsLength - 3],
|
|
83
|
+
newPoints[newPointsLength - 1],
|
|
84
|
+
newPoints[newPointsLength - 2]
|
|
85
|
+
))
|
|
86
|
+
newPoints.splice(newPoints.length - 2, 1);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
newPoints.push(points[points.length - 1]);
|
|
91
|
+
newPointsLength = newPoints.length;
|
|
92
|
+
if ((type === "Polygon" || type === "MultiPolygon") && equals(points[0], points[points.length - 1]) && newPointsLength < 4) {
|
|
93
|
+
throw new Error("invalid polygon");
|
|
94
|
+
}
|
|
95
|
+
if (type === "LineString" && newPointsLength < 3) {
|
|
96
|
+
return newPoints;
|
|
97
|
+
}
|
|
98
|
+
if (isPointOnLineSegment(
|
|
99
|
+
newPoints[newPointsLength - 3],
|
|
100
|
+
newPoints[newPointsLength - 1],
|
|
101
|
+
newPoints[newPointsLength - 2]
|
|
102
|
+
))
|
|
103
|
+
newPoints.splice(newPoints.length - 2, 1);
|
|
104
|
+
return newPoints;
|
|
105
|
+
}
|
|
106
|
+
__name(cleanLine, "cleanLine");
|
|
107
|
+
function equals(pt1, pt2) {
|
|
108
|
+
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
109
|
+
}
|
|
110
|
+
__name(equals, "equals");
|
|
111
|
+
function isPointOnLineSegment(start, end, point) {
|
|
112
|
+
var x = point[0], y = point[1];
|
|
113
|
+
var startX = start[0], startY = start[1];
|
|
114
|
+
var endX = end[0], endY = end[1];
|
|
115
|
+
var dxc = x - startX;
|
|
116
|
+
var dyc = y - startY;
|
|
117
|
+
var dxl = endX - startX;
|
|
118
|
+
var dyl = endY - startY;
|
|
119
|
+
var cross = dxc * dyl - dyc * dxl;
|
|
120
|
+
if (cross !== 0)
|
|
121
|
+
return false;
|
|
122
|
+
else if (Math.abs(dxl) >= Math.abs(dyl))
|
|
123
|
+
return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
|
|
124
|
+
else
|
|
125
|
+
return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
|
|
126
|
+
}
|
|
127
|
+
__name(isPointOnLineSegment, "isPointOnLineSegment");
|
|
128
|
+
var turf_clean_coords_default = cleanCoords;
|
|
129
|
+
export {
|
|
130
|
+
cleanCoords,
|
|
131
|
+
turf_clean_coords_default as default
|
|
132
|
+
};
|
|
133
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Position } from \"geojson\";\nimport { feature } from \"@turf/helpers\";\nimport { getCoords, getType } from \"@turf/invariant\";\n\n// To-Do => Improve Typescript GeoJSON handling\n\n/**\n * Removes redundant coordinates from any GeoJSON Geometry.\n *\n * @name cleanCoords\n * @param {Geometry|Feature} geojson Feature or Geometry\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated\n * @returns {Geometry|Feature} the cleaned input Feature/Geometry\n * @example\n * var line = turf.lineString([[0, 0], [0, 2], [0, 5], [0, 8], [0, 8], [0, 10]]);\n * var multiPoint = turf.multiPoint([[0, 0], [0, 0], [2, 2]]);\n *\n * turf.cleanCoords(line).geometry.coordinates;\n * //= [[0, 0], [0, 10]]\n *\n * turf.cleanCoords(multiPoint).geometry.coordinates;\n * //= [[0, 0], [2, 2]]\n */\nfunction cleanCoords(\n geojson: any,\n options: {\n mutate?: boolean;\n } = {}\n) {\n // Backwards compatible with v4.0\n var mutate = typeof options === \"object\" ? options.mutate : options;\n if (!geojson) throw new Error(\"geojson is required\");\n var type = getType(geojson);\n\n // Store new \"clean\" points in this Array\n var newCoords = [];\n\n switch (type) {\n case \"LineString\":\n newCoords = cleanLine(geojson, type);\n break;\n case \"MultiLineString\":\n case \"Polygon\":\n getCoords(geojson).forEach(function (line) {\n newCoords.push(cleanLine(line, type));\n });\n break;\n case \"MultiPolygon\":\n getCoords(geojson).forEach(function (polygons: any) {\n var polyPoints: Position[] = [];\n polygons.forEach(function (ring: Position[]) {\n polyPoints.push(cleanLine(ring, type));\n });\n newCoords.push(polyPoints);\n });\n break;\n case \"Point\":\n return geojson;\n case \"MultiPoint\":\n var existing: Record<string, true> = {};\n getCoords(geojson).forEach(function (coord: any) {\n var key = coord.join(\"-\");\n if (!Object.prototype.hasOwnProperty.call(existing, key)) {\n newCoords.push(coord);\n existing[key] = true;\n }\n });\n break;\n default:\n throw new Error(type + \" geometry not supported\");\n }\n\n // Support input mutation\n if (geojson.coordinates) {\n if (mutate === true) {\n geojson.coordinates = newCoords;\n return geojson;\n }\n return { type: type, coordinates: newCoords };\n } else {\n if (mutate === true) {\n geojson.geometry.coordinates = newCoords;\n return geojson;\n }\n return feature({ type: type, coordinates: newCoords }, geojson.properties, {\n bbox: geojson.bbox,\n id: geojson.id,\n });\n }\n}\n\n/**\n * Clean Coords\n *\n * @private\n * @param {Array<number>|LineString} line Line\n * @param {string} type Type of geometry\n * @returns {Array<number>} Cleaned coordinates\n */\nfunction cleanLine(line: Position[], type: string) {\n var points = getCoords(line);\n // handle \"clean\" segment\n if (points.length === 2 && !equals(points[0], points[1])) return points;\n\n var newPoints = [];\n var secondToLast = points.length - 1;\n var newPointsLength = newPoints.length;\n\n newPoints.push(points[0]);\n for (var i = 1; i < secondToLast; i++) {\n var prevAddedPoint = newPoints[newPoints.length - 1];\n if (\n points[i][0] === prevAddedPoint[0] &&\n points[i][1] === prevAddedPoint[1]\n )\n continue;\n else {\n newPoints.push(points[i]);\n newPointsLength = newPoints.length;\n if (newPointsLength > 2) {\n if (\n isPointOnLineSegment(\n newPoints[newPointsLength - 3],\n newPoints[newPointsLength - 1],\n newPoints[newPointsLength - 2]\n )\n )\n newPoints.splice(newPoints.length - 2, 1);\n }\n }\n }\n newPoints.push(points[points.length - 1]);\n newPointsLength = newPoints.length;\n\n // (Multi)Polygons must have at least 4 points, but a closed LineString with only 3 points is acceptable\n if (\n (type === \"Polygon\" || type === \"MultiPolygon\") &&\n equals(points[0], points[points.length - 1]) &&\n newPointsLength < 4\n ) {\n throw new Error(\"invalid polygon\");\n }\n\n if (type === \"LineString\" && newPointsLength < 3) {\n return newPoints;\n }\n\n if (\n isPointOnLineSegment(\n newPoints[newPointsLength - 3],\n newPoints[newPointsLength - 1],\n newPoints[newPointsLength - 2]\n )\n )\n newPoints.splice(newPoints.length - 2, 1);\n\n return newPoints;\n}\n\n/**\n * Compares two points and returns if they are equals\n *\n * @private\n * @param {Position} pt1 point\n * @param {Position} pt2 point\n * @returns {boolean} true if they are equals\n */\nfunction equals(pt1: Position, pt2: Position) {\n return pt1[0] === pt2[0] && pt1[1] === pt2[1];\n}\n\n/**\n * Returns if `point` is on the segment between `start` and `end`.\n * Borrowed from `@turf/boolean-point-on-line` to speed up the evaluation (instead of using the module as dependency)\n *\n * @private\n * @param {Position} start coord pair of start of line\n * @param {Position} end coord pair of end of line\n * @param {Position} point coord pair of point to check\n * @returns {boolean} true/false\n */\nfunction isPointOnLineSegment(start: Position, end: Position, point: Position) {\n var x = point[0],\n y = point[1];\n var startX = start[0],\n startY = start[1];\n var endX = end[0],\n endY = end[1];\n\n var dxc = x - startX;\n var dyc = y - startY;\n var dxl = endX - startX;\n var dyl = endY - startY;\n var cross = dxc * dyl - dyc * dxl;\n\n if (cross !== 0) return false;\n else if (Math.abs(dxl) >= Math.abs(dyl))\n return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;\n else return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;\n}\n\nexport { cleanCoords };\nexport default cleanCoords;\n"],"mappings":";;;;AACA,SAAS,eAAe;AACxB,SAAS,WAAW,eAAe;AAsBnC,SAAS,YACP,SACA,UAEI,CAAC,GACL;AAEA,MAAI,SAAS,OAAO,YAAY,WAAW,QAAQ,SAAS;AAC5D,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,qBAAqB;AACnD,MAAI,OAAO,QAAQ,OAAO;AAG1B,MAAI,YAAY,CAAC;AAEjB,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,kBAAY,UAAU,SAAS,IAAI;AACnC;AAAA,IACF,KAAK;AAAA,IACL,KAAK;AACH,gBAAU,OAAO,EAAE,QAAQ,SAAU,MAAM;AACzC,kBAAU,KAAK,UAAU,MAAM,IAAI,CAAC;AAAA,MACtC,CAAC;AACD;AAAA,IACF,KAAK;AACH,gBAAU,OAAO,EAAE,QAAQ,SAAU,UAA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d,MAAI,MAAM,IAAI;AACd,MAAI,MAAM,IAAI;AACd,MAAI,MAAM,OAAO;AACjB,MAAI,MAAM,OAAO;AACjB,MAAI,QAAQ,MAAM,MAAM,MAAM;AAE9B,MAAI,UAAU;AAAG,WAAO;AAAA,WACf,KAAK,IAAI,GAAG,KAAK,KAAK,IAAI,GAAG;AACpC,WAAO,MAAM,IAAI,UAAU,KAAK,KAAK,OAAO,QAAQ,KAAK,KAAK;AAAA;AAC3D,WAAO,MAAM,IAAI,UAAU,KAAK,KAAK,OAAO,QAAQ,KAAK,KAAK;AACrE;AAlBS;AAqBT,IAAO,4BAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/clean-coords",
|
|
3
|
-
"version": "7.0.0-alpha.
|
|
3
|
+
"version": "7.0.0-alpha.110+1411d63a7",
|
|
4
4
|
"description": "turf clean-coords module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -24,47 +24,52 @@
|
|
|
24
24
|
"gis",
|
|
25
25
|
"clean-coords"
|
|
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": "dist/js/index.d.ts",
|
|
38
44
|
"sideEffects": false,
|
|
39
45
|
"files": [
|
|
40
46
|
"dist"
|
|
41
47
|
],
|
|
42
48
|
"scripts": {
|
|
43
|
-
"bench": "tsx bench.
|
|
44
|
-
"build": "
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"test": "npm-run-all test:*",
|
|
49
|
-
"test:tape": "tsx test.js",
|
|
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",
|
|
50
54
|
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
51
55
|
},
|
|
52
56
|
"devDependencies": {
|
|
53
|
-
"@turf/truncate": "^7.0.0-alpha.
|
|
54
|
-
"@types/
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
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
|
+
"load-json-file": "^7.0.1",
|
|
62
|
+
"npm-run-all": "^4.1.5",
|
|
63
|
+
"tape": "^5.7.2",
|
|
64
|
+
"tsup": "^8.0.1",
|
|
65
|
+
"tsx": "^4.6.2",
|
|
66
|
+
"typescript": "^5.2.2",
|
|
67
|
+
"write-json-file": "^5.0.0"
|
|
63
68
|
},
|
|
64
69
|
"dependencies": {
|
|
65
|
-
"@turf/helpers": "^7.0.0-alpha.
|
|
66
|
-
"@turf/invariant": "^7.0.0-alpha.
|
|
67
|
-
"tslib": "^2.
|
|
70
|
+
"@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
|
|
71
|
+
"@turf/invariant": "^7.0.0-alpha.110+1411d63a7",
|
|
72
|
+
"tslib": "^2.6.2"
|
|
68
73
|
},
|
|
69
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
|
|
70
75
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { feature } from "@turf/helpers";
|
|
2
|
-
import { getCoords, getType } from "@turf/invariant";
|
|
3
|
-
// To-Do => Improve Typescript GeoJSON handling
|
|
4
|
-
/**
|
|
5
|
-
* Removes redundant coordinates from any GeoJSON Geometry.
|
|
6
|
-
*
|
|
7
|
-
* @name cleanCoords
|
|
8
|
-
* @param {Geometry|Feature} geojson Feature or Geometry
|
|
9
|
-
* @param {Object} [options={}] Optional parameters
|
|
10
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated
|
|
11
|
-
* @returns {Geometry|Feature} the cleaned input Feature/Geometry
|
|
12
|
-
* @example
|
|
13
|
-
* var line = turf.lineString([[0, 0], [0, 2], [0, 5], [0, 8], [0, 8], [0, 10]]);
|
|
14
|
-
* var multiPoint = turf.multiPoint([[0, 0], [0, 0], [2, 2]]);
|
|
15
|
-
*
|
|
16
|
-
* turf.cleanCoords(line).geometry.coordinates;
|
|
17
|
-
* //= [[0, 0], [0, 10]]
|
|
18
|
-
*
|
|
19
|
-
* turf.cleanCoords(multiPoint).geometry.coordinates;
|
|
20
|
-
* //= [[0, 0], [2, 2]]
|
|
21
|
-
*/
|
|
22
|
-
function cleanCoords(geojson, options = {}) {
|
|
23
|
-
// Backwards compatible with v4.0
|
|
24
|
-
var mutate = typeof options === "object" ? options.mutate : options;
|
|
25
|
-
if (!geojson)
|
|
26
|
-
throw new Error("geojson is required");
|
|
27
|
-
var type = getType(geojson);
|
|
28
|
-
// Store new "clean" points in this Array
|
|
29
|
-
var newCoords = [];
|
|
30
|
-
switch (type) {
|
|
31
|
-
case "LineString":
|
|
32
|
-
newCoords = cleanLine(geojson, type);
|
|
33
|
-
break;
|
|
34
|
-
case "MultiLineString":
|
|
35
|
-
case "Polygon":
|
|
36
|
-
getCoords(geojson).forEach(function (line) {
|
|
37
|
-
newCoords.push(cleanLine(line, type));
|
|
38
|
-
});
|
|
39
|
-
break;
|
|
40
|
-
case "MultiPolygon":
|
|
41
|
-
getCoords(geojson).forEach(function (polygons) {
|
|
42
|
-
var polyPoints = [];
|
|
43
|
-
polygons.forEach(function (ring) {
|
|
44
|
-
polyPoints.push(cleanLine(ring, type));
|
|
45
|
-
});
|
|
46
|
-
newCoords.push(polyPoints);
|
|
47
|
-
});
|
|
48
|
-
break;
|
|
49
|
-
case "Point":
|
|
50
|
-
return geojson;
|
|
51
|
-
case "MultiPoint":
|
|
52
|
-
var existing = {};
|
|
53
|
-
getCoords(geojson).forEach(function (coord) {
|
|
54
|
-
var key = coord.join("-");
|
|
55
|
-
if (!Object.prototype.hasOwnProperty.call(existing, key)) {
|
|
56
|
-
newCoords.push(coord);
|
|
57
|
-
existing[key] = true;
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
break;
|
|
61
|
-
default:
|
|
62
|
-
throw new Error(type + " geometry not supported");
|
|
63
|
-
}
|
|
64
|
-
// Support input mutation
|
|
65
|
-
if (geojson.coordinates) {
|
|
66
|
-
if (mutate === true) {
|
|
67
|
-
geojson.coordinates = newCoords;
|
|
68
|
-
return geojson;
|
|
69
|
-
}
|
|
70
|
-
return { type: type, coordinates: newCoords };
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
if (mutate === true) {
|
|
74
|
-
geojson.geometry.coordinates = newCoords;
|
|
75
|
-
return geojson;
|
|
76
|
-
}
|
|
77
|
-
return feature({ type: type, coordinates: newCoords }, geojson.properties, {
|
|
78
|
-
bbox: geojson.bbox,
|
|
79
|
-
id: geojson.id,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Clean Coords
|
|
85
|
-
*
|
|
86
|
-
* @private
|
|
87
|
-
* @param {Array<number>|LineString} line Line
|
|
88
|
-
* @param {string} type Type of geometry
|
|
89
|
-
* @returns {Array<number>} Cleaned coordinates
|
|
90
|
-
*/
|
|
91
|
-
function cleanLine(line, type) {
|
|
92
|
-
var points = getCoords(line);
|
|
93
|
-
// handle "clean" segment
|
|
94
|
-
if (points.length === 2 && !equals(points[0], points[1]))
|
|
95
|
-
return points;
|
|
96
|
-
var newPoints = [];
|
|
97
|
-
var secondToLast = points.length - 1;
|
|
98
|
-
var newPointsLength = newPoints.length;
|
|
99
|
-
newPoints.push(points[0]);
|
|
100
|
-
for (var i = 1; i < secondToLast; i++) {
|
|
101
|
-
var prevAddedPoint = newPoints[newPoints.length - 1];
|
|
102
|
-
if (points[i][0] === prevAddedPoint[0] &&
|
|
103
|
-
points[i][1] === prevAddedPoint[1])
|
|
104
|
-
continue;
|
|
105
|
-
else {
|
|
106
|
-
newPoints.push(points[i]);
|
|
107
|
-
newPointsLength = newPoints.length;
|
|
108
|
-
if (newPointsLength > 2) {
|
|
109
|
-
if (isPointOnLineSegment(newPoints[newPointsLength - 3], newPoints[newPointsLength - 1], newPoints[newPointsLength - 2]))
|
|
110
|
-
newPoints.splice(newPoints.length - 2, 1);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
newPoints.push(points[points.length - 1]);
|
|
115
|
-
newPointsLength = newPoints.length;
|
|
116
|
-
// (Multi)Polygons must have at least 4 points, but a closed LineString with only 3 points is acceptable
|
|
117
|
-
if ((type === "Polygon" || type === "MultiPolygon") &&
|
|
118
|
-
equals(points[0], points[points.length - 1]) &&
|
|
119
|
-
newPointsLength < 4) {
|
|
120
|
-
throw new Error("invalid polygon");
|
|
121
|
-
}
|
|
122
|
-
if (isPointOnLineSegment(newPoints[newPointsLength - 3], newPoints[newPointsLength - 1], newPoints[newPointsLength - 2]))
|
|
123
|
-
newPoints.splice(newPoints.length - 2, 1);
|
|
124
|
-
return newPoints;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Compares two points and returns if they are equals
|
|
128
|
-
*
|
|
129
|
-
* @private
|
|
130
|
-
* @param {Position} pt1 point
|
|
131
|
-
* @param {Position} pt2 point
|
|
132
|
-
* @returns {boolean} true if they are equals
|
|
133
|
-
*/
|
|
134
|
-
function equals(pt1, pt2) {
|
|
135
|
-
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Returns if `point` is on the segment between `start` and `end`.
|
|
139
|
-
* Borrowed from `@turf/boolean-point-on-line` to speed up the evaluation (instead of using the module as dependency)
|
|
140
|
-
*
|
|
141
|
-
* @private
|
|
142
|
-
* @param {Position} start coord pair of start of line
|
|
143
|
-
* @param {Position} end coord pair of end of line
|
|
144
|
-
* @param {Position} point coord pair of point to check
|
|
145
|
-
* @returns {boolean} true/false
|
|
146
|
-
*/
|
|
147
|
-
function isPointOnLineSegment(start, end, point) {
|
|
148
|
-
var x = point[0], y = point[1];
|
|
149
|
-
var startX = start[0], startY = start[1];
|
|
150
|
-
var endX = end[0], endY = end[1];
|
|
151
|
-
var dxc = x - startX;
|
|
152
|
-
var dyc = y - startY;
|
|
153
|
-
var dxl = endX - startX;
|
|
154
|
-
var dyl = endY - startY;
|
|
155
|
-
var cross = dxc * dyl - dyc * dxl;
|
|
156
|
-
if (cross !== 0)
|
|
157
|
-
return false;
|
|
158
|
-
else if (Math.abs(dxl) >= Math.abs(dyl))
|
|
159
|
-
return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
|
|
160
|
-
else
|
|
161
|
-
return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
|
|
162
|
-
}
|
|
163
|
-
export default cleanCoords;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const helpers_1 = require("@turf/helpers");
|
|
4
|
-
const invariant_1 = require("@turf/invariant");
|
|
5
|
-
// To-Do => Improve Typescript GeoJSON handling
|
|
6
|
-
/**
|
|
7
|
-
* Removes redundant coordinates from any GeoJSON Geometry.
|
|
8
|
-
*
|
|
9
|
-
* @name cleanCoords
|
|
10
|
-
* @param {Geometry|Feature} geojson Feature or Geometry
|
|
11
|
-
* @param {Object} [options={}] Optional parameters
|
|
12
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated
|
|
13
|
-
* @returns {Geometry|Feature} the cleaned input Feature/Geometry
|
|
14
|
-
* @example
|
|
15
|
-
* var line = turf.lineString([[0, 0], [0, 2], [0, 5], [0, 8], [0, 8], [0, 10]]);
|
|
16
|
-
* var multiPoint = turf.multiPoint([[0, 0], [0, 0], [2, 2]]);
|
|
17
|
-
*
|
|
18
|
-
* turf.cleanCoords(line).geometry.coordinates;
|
|
19
|
-
* //= [[0, 0], [0, 10]]
|
|
20
|
-
*
|
|
21
|
-
* turf.cleanCoords(multiPoint).geometry.coordinates;
|
|
22
|
-
* //= [[0, 0], [2, 2]]
|
|
23
|
-
*/
|
|
24
|
-
function cleanCoords(geojson, options = {}) {
|
|
25
|
-
// Backwards compatible with v4.0
|
|
26
|
-
var mutate = typeof options === "object" ? options.mutate : options;
|
|
27
|
-
if (!geojson)
|
|
28
|
-
throw new Error("geojson is required");
|
|
29
|
-
var type = invariant_1.getType(geojson);
|
|
30
|
-
// Store new "clean" points in this Array
|
|
31
|
-
var newCoords = [];
|
|
32
|
-
switch (type) {
|
|
33
|
-
case "LineString":
|
|
34
|
-
newCoords = cleanLine(geojson, type);
|
|
35
|
-
break;
|
|
36
|
-
case "MultiLineString":
|
|
37
|
-
case "Polygon":
|
|
38
|
-
invariant_1.getCoords(geojson).forEach(function (line) {
|
|
39
|
-
newCoords.push(cleanLine(line, type));
|
|
40
|
-
});
|
|
41
|
-
break;
|
|
42
|
-
case "MultiPolygon":
|
|
43
|
-
invariant_1.getCoords(geojson).forEach(function (polygons) {
|
|
44
|
-
var polyPoints = [];
|
|
45
|
-
polygons.forEach(function (ring) {
|
|
46
|
-
polyPoints.push(cleanLine(ring, type));
|
|
47
|
-
});
|
|
48
|
-
newCoords.push(polyPoints);
|
|
49
|
-
});
|
|
50
|
-
break;
|
|
51
|
-
case "Point":
|
|
52
|
-
return geojson;
|
|
53
|
-
case "MultiPoint":
|
|
54
|
-
var existing = {};
|
|
55
|
-
invariant_1.getCoords(geojson).forEach(function (coord) {
|
|
56
|
-
var key = coord.join("-");
|
|
57
|
-
if (!Object.prototype.hasOwnProperty.call(existing, key)) {
|
|
58
|
-
newCoords.push(coord);
|
|
59
|
-
existing[key] = true;
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
break;
|
|
63
|
-
default:
|
|
64
|
-
throw new Error(type + " geometry not supported");
|
|
65
|
-
}
|
|
66
|
-
// Support input mutation
|
|
67
|
-
if (geojson.coordinates) {
|
|
68
|
-
if (mutate === true) {
|
|
69
|
-
geojson.coordinates = newCoords;
|
|
70
|
-
return geojson;
|
|
71
|
-
}
|
|
72
|
-
return { type: type, coordinates: newCoords };
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
if (mutate === true) {
|
|
76
|
-
geojson.geometry.coordinates = newCoords;
|
|
77
|
-
return geojson;
|
|
78
|
-
}
|
|
79
|
-
return helpers_1.feature({ type: type, coordinates: newCoords }, geojson.properties, {
|
|
80
|
-
bbox: geojson.bbox,
|
|
81
|
-
id: geojson.id,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Clean Coords
|
|
87
|
-
*
|
|
88
|
-
* @private
|
|
89
|
-
* @param {Array<number>|LineString} line Line
|
|
90
|
-
* @param {string} type Type of geometry
|
|
91
|
-
* @returns {Array<number>} Cleaned coordinates
|
|
92
|
-
*/
|
|
93
|
-
function cleanLine(line, type) {
|
|
94
|
-
var points = invariant_1.getCoords(line);
|
|
95
|
-
// handle "clean" segment
|
|
96
|
-
if (points.length === 2 && !equals(points[0], points[1]))
|
|
97
|
-
return points;
|
|
98
|
-
var newPoints = [];
|
|
99
|
-
var secondToLast = points.length - 1;
|
|
100
|
-
var newPointsLength = newPoints.length;
|
|
101
|
-
newPoints.push(points[0]);
|
|
102
|
-
for (var i = 1; i < secondToLast; i++) {
|
|
103
|
-
var prevAddedPoint = newPoints[newPoints.length - 1];
|
|
104
|
-
if (points[i][0] === prevAddedPoint[0] &&
|
|
105
|
-
points[i][1] === prevAddedPoint[1])
|
|
106
|
-
continue;
|
|
107
|
-
else {
|
|
108
|
-
newPoints.push(points[i]);
|
|
109
|
-
newPointsLength = newPoints.length;
|
|
110
|
-
if (newPointsLength > 2) {
|
|
111
|
-
if (isPointOnLineSegment(newPoints[newPointsLength - 3], newPoints[newPointsLength - 1], newPoints[newPointsLength - 2]))
|
|
112
|
-
newPoints.splice(newPoints.length - 2, 1);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
newPoints.push(points[points.length - 1]);
|
|
117
|
-
newPointsLength = newPoints.length;
|
|
118
|
-
// (Multi)Polygons must have at least 4 points, but a closed LineString with only 3 points is acceptable
|
|
119
|
-
if ((type === "Polygon" || type === "MultiPolygon") &&
|
|
120
|
-
equals(points[0], points[points.length - 1]) &&
|
|
121
|
-
newPointsLength < 4) {
|
|
122
|
-
throw new Error("invalid polygon");
|
|
123
|
-
}
|
|
124
|
-
if (isPointOnLineSegment(newPoints[newPointsLength - 3], newPoints[newPointsLength - 1], newPoints[newPointsLength - 2]))
|
|
125
|
-
newPoints.splice(newPoints.length - 2, 1);
|
|
126
|
-
return newPoints;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Compares two points and returns if they are equals
|
|
130
|
-
*
|
|
131
|
-
* @private
|
|
132
|
-
* @param {Position} pt1 point
|
|
133
|
-
* @param {Position} pt2 point
|
|
134
|
-
* @returns {boolean} true if they are equals
|
|
135
|
-
*/
|
|
136
|
-
function equals(pt1, pt2) {
|
|
137
|
-
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Returns if `point` is on the segment between `start` and `end`.
|
|
141
|
-
* Borrowed from `@turf/boolean-point-on-line` to speed up the evaluation (instead of using the module as dependency)
|
|
142
|
-
*
|
|
143
|
-
* @private
|
|
144
|
-
* @param {Position} start coord pair of start of line
|
|
145
|
-
* @param {Position} end coord pair of end of line
|
|
146
|
-
* @param {Position} point coord pair of point to check
|
|
147
|
-
* @returns {boolean} true/false
|
|
148
|
-
*/
|
|
149
|
-
function isPointOnLineSegment(start, end, point) {
|
|
150
|
-
var x = point[0], y = point[1];
|
|
151
|
-
var startX = start[0], startY = start[1];
|
|
152
|
-
var endX = end[0], endY = end[1];
|
|
153
|
-
var dxc = x - startX;
|
|
154
|
-
var dyc = y - startY;
|
|
155
|
-
var dxl = endX - startX;
|
|
156
|
-
var dyl = endY - startY;
|
|
157
|
-
var cross = dxc * dyl - dyc * dxl;
|
|
158
|
-
if (cross !== 0)
|
|
159
|
-
return false;
|
|
160
|
-
else if (Math.abs(dxl) >= Math.abs(dyl))
|
|
161
|
-
return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
|
|
162
|
-
else
|
|
163
|
-
return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
|
|
164
|
-
}
|
|
165
|
-
exports.default = cleanCoords;
|