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

@turf/simplify

Package Overview
Dependencies
Maintainers
4
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 4.6.0 to 4.7.0

37

index.d.ts
/// <reference types="geojson" />
type LineString = GeoJSON.Feature<GeoJSON.LineString>;
type Polygon = GeoJSON.Feature<GeoJSON.Polygon>;
type MultiLineString = GeoJSON.Feature<GeoJSON.MultiLineString>;
type MultiPolygon = GeoJSON.Feature<GeoJSON.MultiPolygon>;
type LineStrings = GeoJSON.FeatureCollection<GeoJSON.LineString>;
type Polygons = GeoJSON.FeatureCollection<GeoJSON.Polygon>;
type MultiLineStrings = GeoJSON.FeatureCollection<GeoJSON.MultiLineString>;
type MultiPolygons = GeoJSON.FeatureCollection<GeoJSON.MultiPolygon>;
type Feature = GeoJSON.Feature<any>;
type Features = GeoJSON.FeatureCollection<any>;
type GeometryCollection = GeoJSON.GeometryCollection;
type Geoms = GeoJSON.Feature<any> | GeoJSON.FeatureCollection<any> | GeoJSON.GeometryObject | GeoJSON.GeometryCollection;
interface SimplifyStatic {
/**
* http://turfjs.org/docs/#simplify
*/
/**
* http://turfjs.org/docs/#simplify
*/
declare function simplify<T extends Geoms>(
geojson: T,
tolerance?: number,
highQuality?: boolean,
mutate?: boolean): T;
(feature: LineString, tolerance?: number, highQuality?: boolean): LineString;
(feature: LineStrings, tolerance?: number, highQuality?: boolean): LineStrings;
(feature: Polygon, tolerance?: number, highQuality?: boolean): Polygon;
(feature: Polygons, tolerance?: number, highQuality?: boolean): Polygons;
(feature: MultiLineString, tolerance?: number, highQuality?: boolean): MultiLineString;
(feature: MultiLineStrings, tolerance?: number, highQuality?: boolean): MultiLineStrings;
(feature: MultiPolygon, tolerance?: number, highQuality?: boolean): MultiPolygon;
(feature: MultiPolygons, tolerance?: number, highQuality?: boolean): MultiPolygons;
(feature: GeometryCollection, tolerance?: number, highQuality?: boolean): GeometryCollection;
(feature: Feature, tolerance?: number, highQuality?: boolean): Feature;
(feature: Features, tolerance?: number, highQuality?: boolean): Features;
}
declare const simplify: SimplifyStatic;
declare namespace simplify { }
export = simplify;

@@ -1,17 +0,18 @@

var simplify = require('simplify-js');
var simplifyJS = require('simplify-js');
var cleanCoords = require('@turf/clean-coords');
var geomEach = require('@turf/meta').geomEach;
var clone = require('@turf/clone');
// supported GeoJSON geometries, used to check whether to wrap in simpleFeature()
var supportedTypes = ['LineString', 'MultiLineString', 'Polygon', 'MultiPolygon'];
/**
* Takes a {@link LineString} or {@link Polygon} and returns a simplified version. Internally uses [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification.
* Takes a {@link GeoJSON} object and returns a simplified version. Internally uses
* [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification.
*
* @name simplify
* @param {Feature<(LineString|Polygon|MultiLineString|MultiPolygon)>|FeatureCollection|GeometryCollection} feature feature to be simplified
* @param {GeoJSON} geojson object to be simplified
* @param {number} [tolerance=1] simplification tolerance
* @param {boolean} [highQuality=false] whether or not to spend more time to create
* a higher-quality simplification with a different algorithm
* @returns {Feature<(LineString|Polygon|MultiLineString|MultiPolygon)>|FeatureCollection|GeometryCollection} a simplified feature
* @param {boolean} [highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
* @param {boolean} [mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
* @returns {GeoJSON} a simplified GeoJSON
* @example
* var feature = turf.polygon([[
* var geojson = turf.polygon([[
* [-70.603637, -33.399918],

@@ -40,102 +41,71 @@ * [-70.614624, -33.395332],

*
* var simplified = turf.simplify(feature, tolerance, false);
* var simplified = turf.simplify(geojson, tolerance, false);
*
* //addToMap
* var addToMap = [feature, simplified]
* var addToMap = [geojson, simplified]
*/
module.exports = function (feature, tolerance, highQuality) {
if (feature.type === 'Feature') {
return simpleFeature(
simplifyHelper(feature, tolerance, highQuality),
feature.properties);
} else if (feature.type === 'FeatureCollection') {
return {
type: 'FeatureCollection',
features: feature.features.map(function (f) {
var simplified = simplifyHelper(f, tolerance, highQuality);
module.exports = function (geojson, tolerance, highQuality, mutate) {
if (!geojson) throw new Error('geojson is required');
if (tolerance && tolerance < 0) throw new Error('invalid tolerance');
// we create simpleFeature here because it doesn't apply to GeometryCollection
// so we can't create it at simplifyHelper()
if (supportedTypes.indexOf(simplified.type) > -1) {
return simpleFeature(simplified, f.properties);
} else {
return simplified;
}
})
};
} else if (feature.type === 'GeometryCollection') {
return {
type: 'GeometryCollection',
geometries: feature.geometries.map(function (g) {
if (supportedTypes.indexOf(g.type) > -1) {
return simplifyHelper({
type: 'Feature',
geometry: g
}, tolerance, highQuality);
}
return g;
})
};
} else {
return feature;
}
// Clone geojson to avoid side effects
if (mutate !== true) geojson = clone(geojson);
geomEach(geojson, function (geom) {
simplify(geom, tolerance, highQuality);
});
return geojson;
};
/**
* Simplifies a feature's coordinates
*
* @private
* @param {Geometry} geometry to be simplified
* @param {number} [tolerance=1] simplification tolerance
* @param {boolean} [highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
* @returns {Geometry} output
*/
function simplify(geometry, tolerance, highQuality) {
var type = geometry.type;
function simplifyHelper(feature, tolerance, highQuality) {
if (feature.geometry.type === 'LineString') {
return {
type: 'LineString',
coordinates: simplifyLine(feature.geometry.coordinates, tolerance, highQuality)
};
} else if (feature.geometry.type === 'MultiLineString') {
return {
type: 'MultiLineString',
coordinates: feature.geometry.coordinates.map(function (lines) {
return simplifyLine(lines, tolerance, highQuality);
})
};
} else if (feature.geometry.type === 'Polygon') {
return {
type: 'Polygon',
coordinates: simplifyPolygon(feature.geometry.coordinates, tolerance, highQuality)
};
} else if (feature.geometry.type === 'MultiPolygon') {
return {
type: 'MultiPolygon',
coordinates: feature.geometry.coordinates.map(function (rings) {
return simplifyPolygon(rings, tolerance, highQuality);
})
};
} else {
// unsupported geometry type supplied
return feature;
}
}
// "unsimplyfiable" geometry types
if (type === 'Point' || type === 'MultiPoint') return geometry;
/*
* returns true if ring's 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
} else if (ring.length === 3 &&
((ring[2][0] === ring[0][0]) && (ring[2][1] === ring[0][1]))) {
return false;
} else {
return 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;
}
function simpleFeature(geom, properties) {
return {
type: 'Feature',
geometry: geom,
properties: properties
};
}
/**
* Simplifies the coordinates of a LineString with simplify-js
*
* @private
* @param {Array<number>} coordinates to be processed
* @param {number} tolerance simplification tolerance
* @param {boolean} highQuality whether or not to spend more time to create a higher-quality
* @returns {Array<Array<number>>} simplified coords
*/
function simplifyLine(coordinates, tolerance, highQuality) {
return simplify(coordinates.map(function (coord) {
return simplifyJS(coordinates.map(function (coord) {
return {x: coord[0], y: coord[1], z: coord[2]};

@@ -147,2 +117,12 @@ }), tolerance, highQuality).map(function (coords) {

/**
* Simplifies the coordinates of a Polygon with simplify-js
*
* @private
* @param {Array<number>} coordinates to be processed
* @param {number} tolerance simplification tolerance
* @param {boolean} highQuality whether or not to spend more time to create a higher-quality
* @returns {Array<Array<Array<number>>>} simplified coords
*/
function simplifyPolygon(coordinates, tolerance, highQuality) {

@@ -154,5 +134,5 @@ return coordinates.map(function (ring) {

if (pts.length < 4) {
throw new Error('Invalid polygon');
throw new Error('invalid polygon');
}
var simpleRing = simplify(pts, tolerance, highQuality).map(function (coords) {
var simpleRing = simplifyJS(pts, tolerance, highQuality).map(function (coords) {
return [coords.x, coords.y];

@@ -163,3 +143,3 @@ });

tolerance -= tolerance * 0.01;
simpleRing = simplify(pts, tolerance, highQuality).map(function (coords) {
simpleRing = simplifyJS(pts, tolerance, highQuality).map(function (coords) {
return [coords.x, coords.y];

@@ -170,3 +150,3 @@ });

(simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0]) ||
(simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1])) {
(simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1])) {
simpleRing.push(simpleRing[0]);

@@ -177,1 +157,15 @@ }

}
/**
* Returns true if ring has at least 3 coordinates and its first coordinate is the same as its last
*
* @private
* @param {Array<number>} ring coordinates to be checked
* @returns {boolean} true if valid
*/
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])));
}
{
"name": "@turf/simplify",
"version": "4.6.0",
"version": "4.7.0",
"description": "turf simplify module",

@@ -25,2 +25,5 @@ "main": "index.js",

"author": "Turf Authors",
"contributors": [
"Stefano Borghi <@stebogit>"
],
"license": "MIT",

@@ -32,8 +35,15 @@ "bugs": {

"devDependencies": {
"@turf/truncate": "4.7.0",
"benchmark": "^2.1.4",
"tape": "^4.6.3"
"load-json-file": "^2.0.0",
"tape": "^4.6.3",
"write-json-file": "^2.0.0"
},
"dependencies": {
"@turf/clean-coords": "4.7.0",
"@turf/clone": "4.7.0",
"@turf/helpers": "4.7.0",
"@turf/meta": "4.7.0",
"simplify-js": "^1.2.1"
}
}

@@ -5,10 +5,11 @@ # @turf/simplify

Takes a [LineString](http://geojson.org/geojson-spec.html#linestring) or [Polygon](http://geojson.org/geojson-spec.html#polygon) and returns a simplified version. Internally uses [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification.
Takes a [GeoJSON](http://geojson.org/geojson-spec.html#geojson-objects) object and returns a simplified version. Internally uses
[simplify-js](http://mourner.github.io/simplify-js/) to perform simplification.
**Parameters**
- `feature` **([Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;([LineString](http://geojson.org/geojson-spec.html#linestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))> | [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [GeometryCollection](http://geojson.org/geojson-spec.html#geometrycollection))** feature to be simplified
- `geojson` **[GeoJSON](http://geojson.org/geojson-spec.html#geojson-objects)** object to be simplified
- `tolerance` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** simplification tolerance (optional, default `1`)
- `highQuality` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** whether or not to spend more time to create
a higher-quality simplification with a different algorithm (optional, default `false`)
- `highQuality` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** whether or not to spend more time to create a higher-quality simplification with a different algorithm (optional, default `false`)
- `mutate` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`)

@@ -18,3 +19,3 @@ **Examples**

```javascript
var feature = turf.polygon([[
var geojson = turf.polygon([[
[-70.603637, -33.399918],

@@ -43,9 +44,9 @@ [-70.614624, -33.395332],

var simplified = turf.simplify(feature, tolerance, false);
var simplified = turf.simplify(geojson, tolerance, false);
//addToMap
var addToMap = [feature, simplified]
var addToMap = [geojson, simplified]
```
Returns **([Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;([LineString](http://geojson.org/geojson-spec.html#linestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))> | [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [GeometryCollection](http://geojson.org/geojson-spec.html#geometrycollection))** a simplified feature
Returns **[GeoJSON](http://geojson.org/geojson-spec.html#geojson-objects)** a simplified GeoJSON

@@ -52,0 +53,0 @@ <!-- This file is automatically generated. Please don't edit it directly:

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