@turf/clean-coords 7.1.0 → 7.3.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/dist/cjs/index.cjs +35 -55
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +35 -55
- package/dist/esm/index.js.map +1 -1
- package/package.json +17 -14
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
|
|
2
2
|
var _helpers = require('@turf/helpers');
|
|
3
3
|
var _invariant = require('@turf/invariant');
|
|
4
|
+
var _booleanpointonline = require('@turf/boolean-point-on-line');
|
|
5
|
+
|
|
4
6
|
function cleanCoords(geojson, options = {}) {
|
|
5
7
|
var mutate = typeof options === "object" ? options.mutate : options;
|
|
6
|
-
if (!geojson)
|
|
7
|
-
throw new Error("geojson is required");
|
|
8
|
+
if (!geojson) throw new Error("geojson is required");
|
|
8
9
|
var type = _invariant.getType.call(void 0, geojson);
|
|
9
10
|
var newCoords = [];
|
|
10
11
|
switch (type) {
|
|
@@ -59,68 +60,47 @@ function cleanCoords(geojson, options = {}) {
|
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
function cleanLine(line, type) {
|
|
62
|
-
|
|
63
|
-
if (points.length === 2 && !equals(points[0], points[1]))
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
newPointsLength = newPoints.length;
|
|
76
|
-
if (newPointsLength > 2) {
|
|
77
|
-
if (isPointOnLineSegment(
|
|
78
|
-
newPoints[newPointsLength - 3],
|
|
79
|
-
newPoints[newPointsLength - 1],
|
|
80
|
-
newPoints[newPointsLength - 2]
|
|
81
|
-
))
|
|
82
|
-
newPoints.splice(newPoints.length - 2, 1);
|
|
83
|
-
}
|
|
63
|
+
const points = _invariant.getCoords.call(void 0, line);
|
|
64
|
+
if (points.length === 2 && !equals(points[0], points[1])) return points;
|
|
65
|
+
const newPoints = [];
|
|
66
|
+
let a = 0, b = 1, c = 2;
|
|
67
|
+
newPoints.push(points[a]);
|
|
68
|
+
while (c < points.length) {
|
|
69
|
+
if (_booleanpointonline.booleanPointOnLine.call(void 0, points[b], _helpers.lineString.call(void 0, [points[a], points[c]]))) {
|
|
70
|
+
b = c;
|
|
71
|
+
} else {
|
|
72
|
+
newPoints.push(points[b]);
|
|
73
|
+
a = b;
|
|
74
|
+
b++;
|
|
75
|
+
c = b;
|
|
84
76
|
}
|
|
77
|
+
c++;
|
|
85
78
|
}
|
|
86
|
-
newPoints.push(points[
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
79
|
+
newPoints.push(points[b]);
|
|
80
|
+
if (type === "Polygon" || type === "MultiPolygon") {
|
|
81
|
+
if (_booleanpointonline.booleanPointOnLine.call(void 0,
|
|
82
|
+
newPoints[0],
|
|
83
|
+
_helpers.lineString.call(void 0, [newPoints[1], newPoints[newPoints.length - 2]])
|
|
84
|
+
)) {
|
|
85
|
+
newPoints.shift();
|
|
86
|
+
newPoints.pop();
|
|
87
|
+
newPoints.push(newPoints[0]);
|
|
88
|
+
}
|
|
89
|
+
if (newPoints.length < 4) {
|
|
90
|
+
throw new Error("invalid polygon, fewer than 4 points");
|
|
91
|
+
}
|
|
92
|
+
if (!equals(newPoints[0], newPoints[newPoints.length - 1])) {
|
|
93
|
+
throw new Error("invalid polygon, first and last points not equal");
|
|
94
|
+
}
|
|
93
95
|
}
|
|
94
|
-
if (isPointOnLineSegment(
|
|
95
|
-
newPoints[newPointsLength - 3],
|
|
96
|
-
newPoints[newPointsLength - 1],
|
|
97
|
-
newPoints[newPointsLength - 2]
|
|
98
|
-
))
|
|
99
|
-
newPoints.splice(newPoints.length - 2, 1);
|
|
100
96
|
return newPoints;
|
|
101
97
|
}
|
|
102
98
|
function equals(pt1, pt2) {
|
|
103
99
|
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
104
100
|
}
|
|
105
|
-
|
|
106
|
-
var x = point[0], y = point[1];
|
|
107
|
-
var startX = start[0], startY = start[1];
|
|
108
|
-
var endX = end[0], endY = end[1];
|
|
109
|
-
var dxc = x - startX;
|
|
110
|
-
var dyc = y - startY;
|
|
111
|
-
var dxl = endX - startX;
|
|
112
|
-
var dyl = endY - startY;
|
|
113
|
-
var cross = dxc * dyl - dyc * dxl;
|
|
114
|
-
if (cross !== 0)
|
|
115
|
-
return false;
|
|
116
|
-
else if (Math.abs(dxl) >= Math.abs(dyl))
|
|
117
|
-
return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
|
|
118
|
-
else
|
|
119
|
-
return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
|
|
120
|
-
}
|
|
121
|
-
var turf_clean_coords_default = cleanCoords;
|
|
101
|
+
var index_default = cleanCoords;
|
|
122
102
|
|
|
123
103
|
|
|
124
104
|
|
|
125
|
-
exports.cleanCoords = cleanCoords; exports.default =
|
|
105
|
+
exports.cleanCoords = cleanCoords; exports.default = index_default;
|
|
126
106
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-clean-coords/dist/cjs/index.cjs","../../index.ts"],"names":[],"mappings":"AAAA;ACCA,wCAAwB;AACxB,4CAAmC;AACnC,iEAAmC;AACnC;AAsBA,SAAS,WAAA,CACP,OAAA,EACA,QAAA,EAEI,CAAC,CAAA,EACL;AAEA,EAAA,IAAI,OAAA,EAAS,OAAO,QAAA,IAAY,SAAA,EAAW,OAAA,CAAQ,OAAA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C,CAAA;AAAA,IACxD;AACA,IAAA,GAAA,CAAI,CAAC,MAAA,CAAO,SAAA,CAAU,CAAC,CAAA,EAAG,SAAA,CAAU,SAAA,CAAU,OAAA,EAAS,CAAC,CAAC,CAAA,EAAG;AAC1D,MAAA,MAAM,IAAI,KAAA,CAAM,kDAAkD,CAAA;AAAA,IACpE;AAAA,EACF;AAEA,EAAA,OAAO,SAAA;AACT;AAUA,SAAS,MAAA,CAAO,GAAA,EAAe,GAAA,EAAe;AAC5C,EAAA,OAAO,GAAA,CAAI,CAAC,EAAA,IAAM,GAAA,CAAI,CAAC,EAAA,GAAK,GAAA,CAAI,CAAC,EAAA,IAAM,GAAA,CAAI,CAAC,CAAA;AAC9C;AAGA,IAAO,cAAA,EAAQ,WAAA;ADnFf;AACE;AACA;AACF,mEAAC","file":"/home/runner/work/turf/turf/packages/turf-clean-coords/dist/cjs/index.cjs","sourcesContent":[null,"import { Position } from \"geojson\";\nimport { feature } from \"@turf/helpers\";\nimport { getCoords, getType } from \"@turf/invariant\";\nimport { booleanPointOnLine } from \"@turf/boolean-point-on-line\";\nimport { lineString } from \"@turf/helpers\";\n\n// To-Do => Improve Typescript GeoJSON handling\n\n/**\n * Removes redundant coordinates from any GeoJSON Geometry.\n *\n * @function\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 const points = getCoords(line);\n // handle \"clean\" segment\n if (points.length === 2 && !equals(points[0], points[1])) return points;\n\n const newPoints = [];\n\n // Segments based approach. With initial segment a-b, keep comparing to a\n // longer segment a-c and as long as b is still on a-c, b is a redundant\n // point.\n let a = 0,\n b = 1,\n c = 2;\n\n // Guaranteed we'll use the first point.\n newPoints.push(points[a]);\n // While there is still room to extend the segment ...\n while (c < points.length) {\n if (booleanPointOnLine(points[b], lineString([points[a], points[c]]))) {\n // b is on a-c, so we can discard point b, and extend a-b to be the same\n // as a-c as the basis for comparison during the next iteration.\n b = c;\n } else {\n // b is NOT on a-c, suggesting a-c is not an extension of a-b. Commit a-b\n // as a necessary segment.\n newPoints.push(points[b]);\n\n // Make our a-b for the next iteration start from the end of the segment\n // that was just locked in i.e. next a-b should be the current b-(b+1).\n a = b;\n b++;\n c = b;\n }\n // Plan to look at the next point during the next iteration.\n c++;\n }\n // No remaining points, so commit the current a-b segment.\n newPoints.push(points[b]);\n\n if (type === \"Polygon\" || type === \"MultiPolygon\") {\n // For polygons need to make sure the start / end point wasn't one of the\n // points that needed to be cleaned.\n // https://github.com/Turfjs/turf/issues/2406\n // For points [a, b, c, ..., z, a]\n // if a is on line b-z, it too can be removed. New array becomes\n // [b, c, ..., z, b]\n if (\n booleanPointOnLine(\n newPoints[0],\n lineString([newPoints[1], newPoints[newPoints.length - 2]])\n )\n ) {\n newPoints.shift(); // Discard starting point.\n newPoints.pop(); // Discard closing point.\n newPoints.push(newPoints[0]); // Duplicate the new closing point to end of array.\n }\n\n // (Multi)Polygons must have at least 4 points and be closed.\n if (newPoints.length < 4) {\n throw new Error(\"invalid polygon, fewer than 4 points\");\n }\n if (!equals(newPoints[0], newPoints[newPoints.length - 1])) {\n throw new Error(\"invalid polygon, first and last points not equal\");\n }\n }\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\nexport { cleanCoords };\nexport default cleanCoords;\n"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Removes redundant coordinates from any GeoJSON Geometry.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
4
|
+
* @function
|
|
5
5
|
* @param {Geometry|Feature} geojson Feature or Geometry
|
|
6
6
|
* @param {Object} [options={}] Optional parameters
|
|
7
7
|
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Removes redundant coordinates from any GeoJSON Geometry.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
4
|
+
* @function
|
|
5
5
|
* @param {Geometry|Feature} geojson Feature or Geometry
|
|
6
6
|
* @param {Object} [options={}] Optional parameters
|
|
7
7
|
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated
|
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// index.ts
|
|
2
2
|
import { feature } from "@turf/helpers";
|
|
3
3
|
import { getCoords, getType } from "@turf/invariant";
|
|
4
|
+
import { booleanPointOnLine } from "@turf/boolean-point-on-line";
|
|
5
|
+
import { lineString } from "@turf/helpers";
|
|
4
6
|
function cleanCoords(geojson, options = {}) {
|
|
5
7
|
var mutate = typeof options === "object" ? options.mutate : options;
|
|
6
|
-
if (!geojson)
|
|
7
|
-
throw new Error("geojson is required");
|
|
8
|
+
if (!geojson) throw new Error("geojson is required");
|
|
8
9
|
var type = getType(geojson);
|
|
9
10
|
var newCoords = [];
|
|
10
11
|
switch (type) {
|
|
@@ -59,68 +60,47 @@ function cleanCoords(geojson, options = {}) {
|
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
function cleanLine(line, type) {
|
|
62
|
-
|
|
63
|
-
if (points.length === 2 && !equals(points[0], points[1]))
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
newPointsLength = newPoints.length;
|
|
76
|
-
if (newPointsLength > 2) {
|
|
77
|
-
if (isPointOnLineSegment(
|
|
78
|
-
newPoints[newPointsLength - 3],
|
|
79
|
-
newPoints[newPointsLength - 1],
|
|
80
|
-
newPoints[newPointsLength - 2]
|
|
81
|
-
))
|
|
82
|
-
newPoints.splice(newPoints.length - 2, 1);
|
|
83
|
-
}
|
|
63
|
+
const points = getCoords(line);
|
|
64
|
+
if (points.length === 2 && !equals(points[0], points[1])) return points;
|
|
65
|
+
const newPoints = [];
|
|
66
|
+
let a = 0, b = 1, c = 2;
|
|
67
|
+
newPoints.push(points[a]);
|
|
68
|
+
while (c < points.length) {
|
|
69
|
+
if (booleanPointOnLine(points[b], lineString([points[a], points[c]]))) {
|
|
70
|
+
b = c;
|
|
71
|
+
} else {
|
|
72
|
+
newPoints.push(points[b]);
|
|
73
|
+
a = b;
|
|
74
|
+
b++;
|
|
75
|
+
c = b;
|
|
84
76
|
}
|
|
77
|
+
c++;
|
|
85
78
|
}
|
|
86
|
-
newPoints.push(points[
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
79
|
+
newPoints.push(points[b]);
|
|
80
|
+
if (type === "Polygon" || type === "MultiPolygon") {
|
|
81
|
+
if (booleanPointOnLine(
|
|
82
|
+
newPoints[0],
|
|
83
|
+
lineString([newPoints[1], newPoints[newPoints.length - 2]])
|
|
84
|
+
)) {
|
|
85
|
+
newPoints.shift();
|
|
86
|
+
newPoints.pop();
|
|
87
|
+
newPoints.push(newPoints[0]);
|
|
88
|
+
}
|
|
89
|
+
if (newPoints.length < 4) {
|
|
90
|
+
throw new Error("invalid polygon, fewer than 4 points");
|
|
91
|
+
}
|
|
92
|
+
if (!equals(newPoints[0], newPoints[newPoints.length - 1])) {
|
|
93
|
+
throw new Error("invalid polygon, first and last points not equal");
|
|
94
|
+
}
|
|
93
95
|
}
|
|
94
|
-
if (isPointOnLineSegment(
|
|
95
|
-
newPoints[newPointsLength - 3],
|
|
96
|
-
newPoints[newPointsLength - 1],
|
|
97
|
-
newPoints[newPointsLength - 2]
|
|
98
|
-
))
|
|
99
|
-
newPoints.splice(newPoints.length - 2, 1);
|
|
100
96
|
return newPoints;
|
|
101
97
|
}
|
|
102
98
|
function equals(pt1, pt2) {
|
|
103
99
|
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
104
100
|
}
|
|
105
|
-
|
|
106
|
-
var x = point[0], y = point[1];
|
|
107
|
-
var startX = start[0], startY = start[1];
|
|
108
|
-
var endX = end[0], endY = end[1];
|
|
109
|
-
var dxc = x - startX;
|
|
110
|
-
var dyc = y - startY;
|
|
111
|
-
var dxl = endX - startX;
|
|
112
|
-
var dyl = endY - startY;
|
|
113
|
-
var cross = dxc * dyl - dyc * dxl;
|
|
114
|
-
if (cross !== 0)
|
|
115
|
-
return false;
|
|
116
|
-
else if (Math.abs(dxl) >= Math.abs(dyl))
|
|
117
|
-
return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
|
|
118
|
-
else
|
|
119
|
-
return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
|
|
120
|
-
}
|
|
121
|
-
var turf_clean_coords_default = cleanCoords;
|
|
101
|
+
var index_default = cleanCoords;
|
|
122
102
|
export {
|
|
123
103
|
cleanCoords,
|
|
124
|
-
|
|
104
|
+
index_default as default
|
|
125
105
|
};
|
|
126
106
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +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 * @
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Position } from \"geojson\";\nimport { feature } from \"@turf/helpers\";\nimport { getCoords, getType } from \"@turf/invariant\";\nimport { booleanPointOnLine } from \"@turf/boolean-point-on-line\";\nimport { lineString } from \"@turf/helpers\";\n\n// To-Do => Improve Typescript GeoJSON handling\n\n/**\n * Removes redundant coordinates from any GeoJSON Geometry.\n *\n * @function\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 const points = getCoords(line);\n // handle \"clean\" segment\n if (points.length === 2 && !equals(points[0], points[1])) return points;\n\n const newPoints = [];\n\n // Segments based approach. With initial segment a-b, keep comparing to a\n // longer segment a-c and as long as b is still on a-c, b is a redundant\n // point.\n let a = 0,\n b = 1,\n c = 2;\n\n // Guaranteed we'll use the first point.\n newPoints.push(points[a]);\n // While there is still room to extend the segment ...\n while (c < points.length) {\n if (booleanPointOnLine(points[b], lineString([points[a], points[c]]))) {\n // b is on a-c, so we can discard point b, and extend a-b to be the same\n // as a-c as the basis for comparison during the next iteration.\n b = c;\n } else {\n // b is NOT on a-c, suggesting a-c is not an extension of a-b. Commit a-b\n // as a necessary segment.\n newPoints.push(points[b]);\n\n // Make our a-b for the next iteration start from the end of the segment\n // that was just locked in i.e. next a-b should be the current b-(b+1).\n a = b;\n b++;\n c = b;\n }\n // Plan to look at the next point during the next iteration.\n c++;\n }\n // No remaining points, so commit the current a-b segment.\n newPoints.push(points[b]);\n\n if (type === \"Polygon\" || type === \"MultiPolygon\") {\n // For polygons need to make sure the start / end point wasn't one of the\n // points that needed to be cleaned.\n // https://github.com/Turfjs/turf/issues/2406\n // For points [a, b, c, ..., z, a]\n // if a is on line b-z, it too can be removed. New array becomes\n // [b, c, ..., z, b]\n if (\n booleanPointOnLine(\n newPoints[0],\n lineString([newPoints[1], newPoints[newPoints.length - 2]])\n )\n ) {\n newPoints.shift(); // Discard starting point.\n newPoints.pop(); // Discard closing point.\n newPoints.push(newPoints[0]); // Duplicate the new closing point to end of array.\n }\n\n // (Multi)Polygons must have at least 4 points and be closed.\n if (newPoints.length < 4) {\n throw new Error(\"invalid polygon, fewer than 4 points\");\n }\n if (!equals(newPoints[0], newPoints[newPoints.length - 1])) {\n throw new Error(\"invalid polygon, first and last points not equal\");\n }\n }\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\nexport { cleanCoords };\nexport default cleanCoords;\n"],"mappings":";AACA,SAAS,eAAe;AACxB,SAAS,WAAW,eAAe;AACnC,SAAS,0BAA0B;AACnC,SAAS,kBAAkB;AAsB3B,SAAS,YACP,SACA,UAEI,CAAC,GACL;AAEA,MAAI,SAAS,OAAO,YAAY,WAAW,QAAQ,SAAS;AAC5D,MAAI,CAAC,QAAS,OAAM,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,U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IAC5D,GACA;AACA,gBAAU,MAAM;AAChB,gBAAU,IAAI;AACd,gBAAU,KAAK,UAAU,CAAC,CAAC;AAAA,IAC7B;AAGA,QAAI,UAAU,SAAS,GAAG;AACxB,YAAM,IAAI,MAAM,sCAAsC;AAAA,IACxD;AACA,QAAI,CAAC,OAAO,UAAU,CAAC,GAAG,UAAU,UAAU,SAAS,CAAC,CAAC,GAAG;AAC1D,YAAM,IAAI,MAAM,kDAAkD;AAAA,IACpE;AAAA,EACF;AAEA,SAAO;AACT;AAUA,SAAS,OAAO,KAAe,KAAe;AAC5C,SAAO,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,IAAI,CAAC;AAC9C;AAGA,IAAO,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/clean-coords",
|
|
3
|
-
"version": "7.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "7.3.0",
|
|
4
|
+
"description": "Removes redundant coordinates from a GeoJSON Geometry.",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
7
|
-
"Stefano Borghi <@stebogit>"
|
|
7
|
+
"Stefano Borghi <@stebogit>",
|
|
8
|
+
"James Beard <@smallsaucepan>"
|
|
8
9
|
],
|
|
9
10
|
"license": "MIT",
|
|
10
11
|
"bugs": {
|
|
@@ -54,23 +55,25 @@
|
|
|
54
55
|
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
57
|
-
"@turf/truncate": "
|
|
58
|
+
"@turf/truncate": "7.3.0",
|
|
58
59
|
"@types/benchmark": "^2.1.5",
|
|
59
|
-
"@types/tape": "^
|
|
60
|
+
"@types/tape": "^5.8.1",
|
|
60
61
|
"benchmark": "^2.1.4",
|
|
62
|
+
"geojson-equality-ts": "^1.0.2",
|
|
61
63
|
"load-json-file": "^7.0.1",
|
|
62
64
|
"npm-run-all": "^4.1.5",
|
|
63
|
-
"tape": "^5.
|
|
64
|
-
"tsup": "^8.0
|
|
65
|
-
"tsx": "^4.
|
|
66
|
-
"typescript": "^5.
|
|
67
|
-
"write-json-file": "^
|
|
65
|
+
"tape": "^5.9.0",
|
|
66
|
+
"tsup": "^8.4.0",
|
|
67
|
+
"tsx": "^4.19.4",
|
|
68
|
+
"typescript": "^5.8.3",
|
|
69
|
+
"write-json-file": "^6.0.0"
|
|
68
70
|
},
|
|
69
71
|
"dependencies": {
|
|
70
|
-
"@turf/
|
|
71
|
-
"@turf/
|
|
72
|
+
"@turf/boolean-point-on-line": "7.3.0",
|
|
73
|
+
"@turf/helpers": "7.3.0",
|
|
74
|
+
"@turf/invariant": "7.3.0",
|
|
72
75
|
"@types/geojson": "^7946.0.10",
|
|
73
|
-
"tslib": "^2.
|
|
76
|
+
"tslib": "^2.8.1"
|
|
74
77
|
},
|
|
75
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "9f58a103e8f9a587ab640307ed03ba5233913ddd"
|
|
76
79
|
}
|