Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@turf/simplify

Package Overview
Dependencies
Maintainers
6
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/simplify - npm Package Compare versions

Comparing version 6.2.0-alpha.2 to 6.2.0-alpha.3

dist/es/package.json

276

dist/es/index.js

@@ -17,7 +17,6 @@ import cleanCoords from '@turf/clean-coords';

function getSqDist(p1, p2) {
var dx = p1.x - p2.x,
dy = p1.y - p2.y;
var dx = p1.x - p2.x,
dy = p1.y - p2.y;
return dx * dx + dy * dy;
return dx * dx + dy * dy;
}

@@ -27,26 +26,23 @@

function getSqSegDist(p, p1, p2) {
var x = p1.x,
y = p1.y,
dx = p2.x - x,
dy = p2.y - y;
var x = p1.x,
y = p1.y,
dx = p2.x - x,
dy = p2.y - y;
if (dx !== 0 || dy !== 0) {
var t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
if (dx !== 0 || dy !== 0) {
var t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = p2.x;
y = p2.y;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
if (t > 1) {
x = p2.x;
y = p2.y;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = p.x - x;
dy = p.y - y;
dx = p.x - x;
dy = p.y - y;
return dx * dx + dy * dy;
return dx * dx + dy * dy;
}

@@ -57,39 +53,40 @@ // rest of the code doesn't care about point format

function simplifyRadialDist(points, sqTolerance) {
var prevPoint = points[0],
newPoints = [prevPoint],
point;
var prevPoint = points[0],
newPoints = [prevPoint],
point;
for (var i = 1, len = points.length; i < len; i++) {
point = points[i];
for (var i = 1, len = points.length; i < len; i++) {
point = points[i];
if (getSqDist(point, prevPoint) > sqTolerance) {
newPoints.push(point);
prevPoint = point;
}
if (getSqDist(point, prevPoint) > sqTolerance) {
newPoints.push(point);
prevPoint = point;
}
}
if (prevPoint !== point) newPoints.push(point);
if (prevPoint !== point) newPoints.push(point);
return newPoints;
return newPoints;
}
function simplifyDPStep(points, first, last, sqTolerance, simplified) {
var maxSqDist = sqTolerance,
index;
var maxSqDist = sqTolerance,
index;
for (var i = first + 1; i < last; i++) {
var sqDist = getSqSegDist(points[i], points[first], points[last]);
for (var i = first + 1; i < last; i++) {
var sqDist = getSqSegDist(points[i], points[first], points[last]);
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
}
if (maxSqDist > sqTolerance) {
if (index - first > 1) simplifyDPStep(points, first, index, sqTolerance, simplified);
simplified.push(points[index]);
if (last - index > 1) simplifyDPStep(points, index, last, sqTolerance, simplified);
}
if (maxSqDist > sqTolerance) {
if (index - first > 1)
simplifyDPStep(points, first, index, sqTolerance, simplified);
simplified.push(points[index]);
if (last - index > 1)
simplifyDPStep(points, index, last, sqTolerance, simplified);
}
}

@@ -99,9 +96,9 @@

function simplifyDouglasPeucker(points, sqTolerance) {
var last = points.length - 1;
var last = points.length - 1;
var simplified = [points[0]];
simplifyDPStep(points, 0, last, sqTolerance, simplified);
simplified.push(points[last]);
var simplified = [points[0]];
simplifyDPStep(points, 0, last, sqTolerance, simplified);
simplified.push(points[last]);
return simplified;
return simplified;
}

@@ -111,11 +108,10 @@

function simplify(points, tolerance, highestQuality) {
if (points.length <= 2) return points;
if (points.length <= 2) return points;
var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
points = simplifyDouglasPeucker(points, sqTolerance);
points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
points = simplifyDouglasPeucker(points, sqTolerance);
return points;
return points;
}

@@ -164,19 +160,19 @@

function simplify$1(geojson, options) {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var tolerance = options.tolerance !== undefined ? options.tolerance : 1;
var highQuality = options.highQuality || false;
var mutate = options.mutate || false;
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error("options is invalid");
var tolerance = options.tolerance !== undefined ? options.tolerance : 1;
var highQuality = options.highQuality || false;
var mutate = options.mutate || false;
if (!geojson) throw new Error('geojson is required');
if (tolerance && tolerance < 0) throw new Error('invalid tolerance');
if (!geojson) throw new Error("geojson is required");
if (tolerance && tolerance < 0) throw new Error("invalid tolerance");
// Clone geojson to avoid side effects
if (mutate !== true) geojson = clone(geojson);
// Clone geojson to avoid side effects
if (mutate !== true) geojson = clone(geojson);
geomEach(geojson, function (geom) {
simplifyGeom(geom, tolerance, highQuality);
});
return geojson;
geomEach(geojson, function (geom) {
simplifyGeom(geom, tolerance, highQuality);
});
return geojson;
}

@@ -194,32 +190,39 @@

function simplifyGeom(geometry, tolerance, highQuality) {
var type = geometry.type;
var type = geometry.type;
// "unsimplyfiable" geometry types
if (type === 'Point' || type === 'MultiPoint') return geometry;
// "unsimplyfiable" geometry types
if (type === "Point" || type === "MultiPoint") return geometry;
// Remove any extra coordinates
cleanCoords(geometry, true);
// Remove any extra coordinates
cleanCoords(geometry, true);
var coordinates = geometry.coordinates;
switch (type) {
case 'LineString':
geometry['coordinates'] = simplifyLine(coordinates, tolerance, highQuality);
break;
case 'MultiLineString':
geometry['coordinates'] = coordinates.map(function (lines) {
return simplifyLine(lines, tolerance, highQuality);
});
break;
case 'Polygon':
geometry['coordinates'] = simplifyPolygon(coordinates, tolerance, highQuality);
break;
case 'MultiPolygon':
geometry['coordinates'] = coordinates.map(function (rings) {
return simplifyPolygon(rings, tolerance, highQuality);
});
}
return geometry;
var coordinates = geometry.coordinates;
switch (type) {
case "LineString":
geometry["coordinates"] = simplifyLine(
coordinates,
tolerance,
highQuality
);
break;
case "MultiLineString":
geometry["coordinates"] = coordinates.map(function (lines) {
return simplifyLine(lines, tolerance, highQuality);
});
break;
case "Polygon":
geometry["coordinates"] = simplifyPolygon(
coordinates,
tolerance,
highQuality
);
break;
case "MultiPolygon":
geometry["coordinates"] = coordinates.map(function (rings) {
return simplifyPolygon(rings, tolerance, highQuality);
});
}
return geometry;
}
/**

@@ -235,10 +238,13 @@ * Simplifies the coordinates of a LineString with simplify-js

function simplifyLine(coordinates, tolerance, highQuality) {
return simplify(coordinates.map(function (coord) {
return {x: coord[0], y: coord[1], z: coord[2]};
}), tolerance, highQuality).map(function (coords) {
return (coords.z) ? [coords.x, coords.y, coords.z] : [coords.x, coords.y];
});
return simplify(
coordinates.map(function (coord) {
return { x: coord[0], y: coord[1], z: coord[2] };
}),
tolerance,
highQuality
).map(function (coords) {
return coords.z ? [coords.x, coords.y, coords.z] : [coords.x, coords.y];
});
}
/**

@@ -254,29 +260,33 @@ * Simplifies the coordinates of a Polygon with simplify-js

function simplifyPolygon(coordinates, tolerance, highQuality) {
return coordinates.map(function (ring) {
var pts = ring.map(function (coord) {
return {x: coord[0], y: coord[1]};
});
if (pts.length < 4) {
throw new Error('invalid polygon');
}
var simpleRing = simplify(pts, tolerance, highQuality).map(function (coords) {
return [coords.x, coords.y];
});
//remove 1 percent of tolerance until enough points to make a triangle
while (!checkValidity(simpleRing)) {
tolerance -= tolerance * 0.01;
simpleRing = simplify(pts, tolerance, highQuality).map(function (coords) {
return [coords.x, coords.y];
});
}
if (
(simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0]) ||
(simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1])) {
simpleRing.push(simpleRing[0]);
}
return simpleRing;
return coordinates.map(function (ring) {
var pts = ring.map(function (coord) {
return { x: coord[0], y: coord[1] };
});
if (pts.length < 4) {
throw new Error("invalid polygon");
}
var simpleRing = simplify(pts, tolerance, highQuality).map(function (
coords
) {
return [coords.x, coords.y];
});
//remove 1 percent of tolerance until enough points to make a triangle
while (!checkValidity(simpleRing)) {
tolerance -= tolerance * 0.01;
simpleRing = simplify(pts, tolerance, highQuality).map(function (
coords
) {
return [coords.x, coords.y];
});
}
if (
simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] ||
simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]
) {
simpleRing.push(simpleRing[0]);
}
return simpleRing;
});
}
/**

@@ -290,7 +300,11 @@ * Returns true if ring has at least 3 coordinates and its first coordinate is the same as its last

function checkValidity(ring) {
if (ring.length < 3) return false;
//if the last point is the same as the first, it's not a triangle
return !(ring.length === 3 && ((ring[2][0] === ring[0][0]) && (ring[2][1] === ring[0][1])));
if (ring.length < 3) return false;
//if the last point is the same as the first, it's not a triangle
return !(
ring.length === 3 &&
ring[2][0] === ring[0][0] &&
ring[2][1] === ring[0][1]
);
}
export default simplify$1;

@@ -21,7 +21,6 @@ 'use strict';

function getSqDist(p1, p2) {
var dx = p1.x - p2.x,
dy = p1.y - p2.y;
var dx = p1.x - p2.x,
dy = p1.y - p2.y;
return dx * dx + dy * dy;
return dx * dx + dy * dy;
}

@@ -31,26 +30,23 @@

function getSqSegDist(p, p1, p2) {
var x = p1.x,
y = p1.y,
dx = p2.x - x,
dy = p2.y - y;
var x = p1.x,
y = p1.y,
dx = p2.x - x,
dy = p2.y - y;
if (dx !== 0 || dy !== 0) {
var t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
if (dx !== 0 || dy !== 0) {
var t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = p2.x;
y = p2.y;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
if (t > 1) {
x = p2.x;
y = p2.y;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = p.x - x;
dy = p.y - y;
dx = p.x - x;
dy = p.y - y;
return dx * dx + dy * dy;
return dx * dx + dy * dy;
}

@@ -61,39 +57,40 @@ // rest of the code doesn't care about point format

function simplifyRadialDist(points, sqTolerance) {
var prevPoint = points[0],
newPoints = [prevPoint],
point;
var prevPoint = points[0],
newPoints = [prevPoint],
point;
for (var i = 1, len = points.length; i < len; i++) {
point = points[i];
for (var i = 1, len = points.length; i < len; i++) {
point = points[i];
if (getSqDist(point, prevPoint) > sqTolerance) {
newPoints.push(point);
prevPoint = point;
}
if (getSqDist(point, prevPoint) > sqTolerance) {
newPoints.push(point);
prevPoint = point;
}
}
if (prevPoint !== point) newPoints.push(point);
if (prevPoint !== point) newPoints.push(point);
return newPoints;
return newPoints;
}
function simplifyDPStep(points, first, last, sqTolerance, simplified) {
var maxSqDist = sqTolerance,
index;
var maxSqDist = sqTolerance,
index;
for (var i = first + 1; i < last; i++) {
var sqDist = getSqSegDist(points[i], points[first], points[last]);
for (var i = first + 1; i < last; i++) {
var sqDist = getSqSegDist(points[i], points[first], points[last]);
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
}
if (maxSqDist > sqTolerance) {
if (index - first > 1) simplifyDPStep(points, first, index, sqTolerance, simplified);
simplified.push(points[index]);
if (last - index > 1) simplifyDPStep(points, index, last, sqTolerance, simplified);
}
if (maxSqDist > sqTolerance) {
if (index - first > 1)
simplifyDPStep(points, first, index, sqTolerance, simplified);
simplified.push(points[index]);
if (last - index > 1)
simplifyDPStep(points, index, last, sqTolerance, simplified);
}
}

@@ -103,9 +100,9 @@

function simplifyDouglasPeucker(points, sqTolerance) {
var last = points.length - 1;
var last = points.length - 1;
var simplified = [points[0]];
simplifyDPStep(points, 0, last, sqTolerance, simplified);
simplified.push(points[last]);
var simplified = [points[0]];
simplifyDPStep(points, 0, last, sqTolerance, simplified);
simplified.push(points[last]);
return simplified;
return simplified;
}

@@ -115,11 +112,10 @@

function simplify(points, tolerance, highestQuality) {
if (points.length <= 2) return points;
if (points.length <= 2) return points;
var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
points = simplifyDouglasPeucker(points, sqTolerance);
points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
points = simplifyDouglasPeucker(points, sqTolerance);
return points;
return points;
}

@@ -168,19 +164,19 @@

function simplify$1(geojson, options) {
// Optional parameters
options = options || {};
if (!helpers.isObject(options)) throw new Error('options is invalid');
var tolerance = options.tolerance !== undefined ? options.tolerance : 1;
var highQuality = options.highQuality || false;
var mutate = options.mutate || false;
// Optional parameters
options = options || {};
if (!helpers.isObject(options)) throw new Error("options is invalid");
var tolerance = options.tolerance !== undefined ? options.tolerance : 1;
var highQuality = options.highQuality || false;
var mutate = options.mutate || false;
if (!geojson) throw new Error('geojson is required');
if (tolerance && tolerance < 0) throw new Error('invalid tolerance');
if (!geojson) throw new Error("geojson is required");
if (tolerance && tolerance < 0) throw new Error("invalid tolerance");
// Clone geojson to avoid side effects
if (mutate !== true) geojson = clone(geojson);
// Clone geojson to avoid side effects
if (mutate !== true) geojson = clone(geojson);
meta.geomEach(geojson, function (geom) {
simplifyGeom(geom, tolerance, highQuality);
});
return geojson;
meta.geomEach(geojson, function (geom) {
simplifyGeom(geom, tolerance, highQuality);
});
return geojson;
}

@@ -198,32 +194,39 @@

function simplifyGeom(geometry, tolerance, highQuality) {
var type = geometry.type;
var type = geometry.type;
// "unsimplyfiable" geometry types
if (type === 'Point' || type === 'MultiPoint') return geometry;
// "unsimplyfiable" geometry types
if (type === "Point" || type === "MultiPoint") return geometry;
// Remove any extra coordinates
cleanCoords(geometry, true);
// Remove any extra coordinates
cleanCoords(geometry, true);
var coordinates = geometry.coordinates;
switch (type) {
case 'LineString':
geometry['coordinates'] = simplifyLine(coordinates, tolerance, highQuality);
break;
case 'MultiLineString':
geometry['coordinates'] = coordinates.map(function (lines) {
return simplifyLine(lines, tolerance, highQuality);
});
break;
case 'Polygon':
geometry['coordinates'] = simplifyPolygon(coordinates, tolerance, highQuality);
break;
case 'MultiPolygon':
geometry['coordinates'] = coordinates.map(function (rings) {
return simplifyPolygon(rings, tolerance, highQuality);
});
}
return geometry;
var coordinates = geometry.coordinates;
switch (type) {
case "LineString":
geometry["coordinates"] = simplifyLine(
coordinates,
tolerance,
highQuality
);
break;
case "MultiLineString":
geometry["coordinates"] = coordinates.map(function (lines) {
return simplifyLine(lines, tolerance, highQuality);
});
break;
case "Polygon":
geometry["coordinates"] = simplifyPolygon(
coordinates,
tolerance,
highQuality
);
break;
case "MultiPolygon":
geometry["coordinates"] = coordinates.map(function (rings) {
return simplifyPolygon(rings, tolerance, highQuality);
});
}
return geometry;
}
/**

@@ -239,10 +242,13 @@ * Simplifies the coordinates of a LineString with simplify-js

function simplifyLine(coordinates, tolerance, highQuality) {
return simplify(coordinates.map(function (coord) {
return {x: coord[0], y: coord[1], z: coord[2]};
}), tolerance, highQuality).map(function (coords) {
return (coords.z) ? [coords.x, coords.y, coords.z] : [coords.x, coords.y];
});
return simplify(
coordinates.map(function (coord) {
return { x: coord[0], y: coord[1], z: coord[2] };
}),
tolerance,
highQuality
).map(function (coords) {
return coords.z ? [coords.x, coords.y, coords.z] : [coords.x, coords.y];
});
}
/**

@@ -258,29 +264,33 @@ * Simplifies the coordinates of a Polygon with simplify-js

function simplifyPolygon(coordinates, tolerance, highQuality) {
return coordinates.map(function (ring) {
var pts = ring.map(function (coord) {
return {x: coord[0], y: coord[1]};
});
if (pts.length < 4) {
throw new Error('invalid polygon');
}
var simpleRing = simplify(pts, tolerance, highQuality).map(function (coords) {
return [coords.x, coords.y];
});
//remove 1 percent of tolerance until enough points to make a triangle
while (!checkValidity(simpleRing)) {
tolerance -= tolerance * 0.01;
simpleRing = simplify(pts, tolerance, highQuality).map(function (coords) {
return [coords.x, coords.y];
});
}
if (
(simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0]) ||
(simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1])) {
simpleRing.push(simpleRing[0]);
}
return simpleRing;
return coordinates.map(function (ring) {
var pts = ring.map(function (coord) {
return { x: coord[0], y: coord[1] };
});
if (pts.length < 4) {
throw new Error("invalid polygon");
}
var simpleRing = simplify(pts, tolerance, highQuality).map(function (
coords
) {
return [coords.x, coords.y];
});
//remove 1 percent of tolerance until enough points to make a triangle
while (!checkValidity(simpleRing)) {
tolerance -= tolerance * 0.01;
simpleRing = simplify(pts, tolerance, highQuality).map(function (
coords
) {
return [coords.x, coords.y];
});
}
if (
simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] ||
simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]
) {
simpleRing.push(simpleRing[0]);
}
return simpleRing;
});
}
/**

@@ -294,8 +304,11 @@ * Returns true if ring has at least 3 coordinates and its first coordinate is the same as its last

function checkValidity(ring) {
if (ring.length < 3) return false;
//if the last point is the same as the first, it's not a triangle
return !(ring.length === 3 && ((ring[2][0] === ring[0][0]) && (ring[2][1] === ring[0][1])));
if (ring.length < 3) return false;
//if the last point is the same as the first, it's not a triangle
return !(
ring.length === 3 &&
ring[2][0] === ring[0][0] &&
ring[2][1] === ring[0][1]
);
}
module.exports = simplify$1;
module.exports.default = simplify$1;

@@ -1,2 +0,2 @@

import { AllGeoJSON } from '@turf/helpers'
import { AllGeoJSON } from "@turf/helpers";

@@ -7,8 +7,8 @@ /**

export default function simplify<T extends AllGeoJSON>(
geojson: T,
options?: {
tolerance?: number,
highQuality?: boolean,
mutate?: boolean
}
geojson: T,
options?: {
tolerance?: number;
highQuality?: boolean;
mutate?: boolean;
}
): T;
{
"name": "@turf/simplify",
"version": "6.2.0-alpha.2",
"version": "6.2.0-alpha.3",
"description": "turf simplify module",

@@ -32,2 +32,6 @@ "author": "Turf Authors",

"module": "dist/es/index.js",
"exports": {
"import": "./dist/es/index.js",
"require": "./dist/js/index.js"
},
"types": "index.d.ts",

@@ -40,13 +44,12 @@ "sideEffects": false,

"scripts": {
"bench": "npm-run-all prepare bench:run",
"bench:run": "node bench.js",
"bench": "node -r esm bench.js",
"build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
"docs": "node ../../scripts/generate-readmes",
"posttest": "node -r esm ../../scripts/validate-es5-dependencies.js",
"prepare": "rollup -c ../../rollup.config.js",
"test": "npm-run-all prepare test:*",
"test": "npm-run-all test:*",
"test:tape": "node -r esm test.js",
"test:types": "tsc --noEmit types.ts"
"test:types": "tsc --esModuleInterop --noEmit types.ts"
},
"devDependencies": {
"@turf/truncate": "^6.2.0-alpha.2",
"@turf/truncate": "^6.2.0-alpha.3",
"benchmark": "*",

@@ -60,8 +63,8 @@ "load-json-file": "*",

"dependencies": {
"@turf/clean-coords": "^6.2.0-alpha.2",
"@turf/clone": "^6.2.0-alpha.2",
"@turf/helpers": "^6.2.0-alpha.2",
"@turf/meta": "^6.2.0-alpha.2"
"@turf/clean-coords": "^6.2.0-alpha.3",
"@turf/clone": "^6.2.0-alpha.3",
"@turf/helpers": "^6.2.0-alpha.3",
"@turf/meta": "^6.2.0-alpha.3"
},
"gitHead": "23d5cb91d77e0c1e2e903a2252f525797f1d0d09"
"gitHead": "dce9edfc705352e8cb9e0083c9330ba0e8d77409"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc