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

@turf/clean-coords

Package Overview
Dependencies
Maintainers
4
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/clean-coords - npm Package Compare versions

Comparing version 5.1.5 to 6.0.0

index.ts

135

index.js

@@ -1,4 +0,6 @@

import { feature } from '@turf/helpers';
import { getCoords, getType } from '@turf/invariant';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var helpers_1 = require("@turf/helpers");
var invariant_1 = require("@turf/invariant");
// To-Do => Improve Typescript GeoJSON handling
/**

@@ -23,45 +25,44 @@ * Removes redundant coordinates from any GeoJSON Geometry.

function cleanCoords(geojson, options) {
if (options === void 0) { options = {}; }
// Backwards compatible with v4.0
var mutate = (typeof options === 'object') ? options.mutate : options;
if (!geojson) throw new Error('geojson is required');
var type = getType(geojson);
if (!geojson)
throw new Error('geojson is required');
var type = invariant_1.getType(geojson);
// Store new "clean" points in this Array
var newCoords = [];
switch (type) {
case 'LineString':
newCoords = cleanLine(geojson);
break;
case 'MultiLineString':
case 'Polygon':
getCoords(geojson).forEach(function (line) {
newCoords.push(cleanLine(line));
});
break;
case 'MultiPolygon':
getCoords(geojson).forEach(function (polygons) {
var polyPoints = [];
polygons.forEach(function (ring) {
polyPoints.push(cleanLine(ring));
case 'LineString':
newCoords = cleanLine(geojson);
break;
case 'MultiLineString':
case 'Polygon':
invariant_1.getCoords(geojson).forEach(function (line) {
newCoords.push(cleanLine(line));
});
newCoords.push(polyPoints);
});
break;
case 'Point':
return geojson;
case 'MultiPoint':
var existing = {};
getCoords(geojson).forEach(function (coord) {
var key = coord.join('-');
if (!existing.hasOwnProperty(key)) {
newCoords.push(coord);
existing[key] = true;
}
});
break;
default:
throw new Error(type + ' geometry not supported');
break;
case 'MultiPolygon':
invariant_1.getCoords(geojson).forEach(function (polygons) {
var polyPoints = [];
polygons.forEach(function (ring) {
polyPoints.push(cleanLine(ring));
});
newCoords.push(polyPoints);
});
break;
case 'Point':
return geojson;
case 'MultiPoint':
var existing = {};
invariant_1.getCoords(geojson).forEach(function (coord) {
var key = coord.join('-');
if (!existing.hasOwnProperty(key)) {
newCoords.push(coord);
existing[key] = true;
}
});
break;
default:
throw new Error(type + ' geometry not supported');
}
// Support input mutation

@@ -73,4 +74,5 @@ if (geojson.coordinates) {

}
return {type: type, coordinates: newCoords};
} else {
return { type: type, coordinates: newCoords };
}
else {
if (mutate === true) {

@@ -80,6 +82,5 @@ geojson.geometry.coordinates = newCoords;

}
return feature({type: type, coordinates: newCoords}, geojson.properties, geojson.bbox, geojson.id);
return helpers_1.feature({ type: type, coordinates: newCoords }, geojson.properties, { bbox: geojson.bbox, id: geojson.id });
}
}
/**

@@ -93,24 +94,31 @@ * Clean Coords

function cleanLine(line) {
var points = getCoords(line);
var points = invariant_1.getCoords(line);
// handle "clean" segment
if (points.length === 2 && !equals(points[0], points[1])) return points;
var prevPoint, point, nextPoint;
if (points.length === 2 && !equals(points[0], points[1]))
return points;
var newPoints = [];
var secondToLast = points.length - 1;
var newPointsLength = newPoints.length;
newPoints.push(points[0]);
for (var i = 1; i < secondToLast; i++) {
prevPoint = points[i - 1];
point = points[i];
nextPoint = points[i + 1];
if (!isPointOnLineSegment(prevPoint, nextPoint, point)) {
newPoints.push(point);
var prevAddedPoint = newPoints[newPoints.length - 1];
if ((points[i][0] === prevAddedPoint[0]) && (points[i][1] === prevAddedPoint[1]))
continue;
else {
newPoints.push(points[i]);
newPointsLength = newPoints.length;
if (newPointsLength > 2) {
if (isPointOnLineSegment(newPoints[newPointsLength - 3], newPoints[newPointsLength - 1], newPoints[newPointsLength - 2]))
newPoints.splice(newPoints.length - 2, 1);
}
}
}
newPoints.push(nextPoint);
newPoints.push(points[points.length - 1]);
newPointsLength = newPoints.length;
if (equals(points[0], points[points.length - 1]) && newPointsLength < 4)
throw new Error('invalid polygon');
if (isPointOnLineSegment(newPoints[newPointsLength - 3], newPoints[newPointsLength - 1], newPoints[newPointsLength - 2]))
newPoints.splice(newPoints.length - 2, 1);
return newPoints;
}
/**

@@ -127,3 +135,2 @@ * Compares two points and returns if they are equals

}
/**

@@ -143,3 +150,2 @@ * Returns if `point` is on the segment between `start` and `end`.

var endX = end[0], endY = end[1];
var dxc = x - startX;

@@ -150,8 +156,9 @@ var dyc = y - startY;

var cross = dxc * dyl - dyc * dxl;
if (cross !== 0) return false;
else if (Math.abs(dxl) >= Math.abs(dyl)) return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
else return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
if (cross !== 0)
return false;
else if (Math.abs(dxl) >= Math.abs(dyl))
return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
else
return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
}
export default cleanCoords;
exports.default = cleanCoords;

@@ -0,19 +1,15 @@

{
"name": "@turf/clean-coords",
"version": "5.1.5",
"version": "6.0.0",
"description": "turf clean-coords module",
"main": "main.js",
"module": "main.es.js",
"types": "index.d.ts",
"main": "index",
"files": [
"index.js",
"index.d.ts",
"main.js",
"main.es.js"
"index.ts"
],
"scripts": {
"pretest": "rollup -c ../../rollup.config.js",
"test": "node -r @std/esm test.js",
"posttest": "node -r @std/esm ../../scripts/validate-es5-dependencies.js",
"bench": "node -r @std/esm bench.js",
"pretest": "tsc",
"test": "node test.js",
"bench": "node bench.js",
"docs": "node ../../scripts/generate-readmes"

@@ -40,7 +36,6 @@ },

"devDependencies": {
"@std/esm": "*",
"@turf/truncate": "^5.1.5",
"@turf/truncate": "*",
"benchmark": "*",
"load-json-file": "*",
"rollup": "*",
"typescript": "*",
"tape": "*",

@@ -50,9 +45,5 @@ "write-json-file": "*"

"dependencies": {
"@turf/helpers": "^5.1.5",
"@turf/invariant": "^5.1.5"
},
"@std/esm": {
"esm": "js",
"cjs": true
"@turf/helpers": "6.x",
"@turf/invariant": "6.x"
}
}
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