turf-simplify
Advanced tools
Comparing version 1.0.3 to 3.0.0-canary.2f5f7167
30
bench.js
@@ -6,3 +6,8 @@ var simplify = require('./'); | ||
var line = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/linestring.geojson')); | ||
var multiline = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/multilinestring.geojson')); | ||
var poly = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/polygon.geojson')); | ||
var multipoly = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/multipolygon.geojson')); | ||
var simple = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/simple.geojson')); | ||
var featurecollection = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/featurecollection.geojson')); | ||
var geometrycollection = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/geometrycollection.geojson')); | ||
var argentina = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/argentina.geojson')); | ||
@@ -12,9 +17,24 @@ var suite = new Benchmark.Suite('turf-simplify'); | ||
.add('turf-simplify#LineString',function () { | ||
simplify(line, .1, 0); | ||
simplify(line, 0.1, 0); | ||
}) | ||
.add('turf-simplify#MultiLineString',function () { | ||
simplify(multiline, 0.01, 0); | ||
}) | ||
.add('turf-simplify#Polygon',function () { | ||
simplify(poly, .01, 0); | ||
simplify(poly, 0.01, 0); | ||
}) | ||
.add('turf-simplify#MultiPolygon',function () { | ||
simplify(multipoly, 0.01, 0); | ||
}) | ||
.add('turf-simplify#SimplePolygon',function () { | ||
simplify(simple, 0.01, 0); | ||
}) | ||
.add('turf-simplify#FeatureCollection',function () { | ||
simplify(featurecollection, 0.01, 0); | ||
}) | ||
.add('turf-simplify#GeometryCollection',function () { | ||
simplify(geometrycollection, 0.01, 0); | ||
}) | ||
.add('turf-simplify#Argentina',function () { | ||
simplify(argentina, .05, 0); | ||
simplify(argentina, 0.05, 0); | ||
}) | ||
@@ -25,4 +45,4 @@ .on('cycle', function (event) { | ||
.on('complete', function () { | ||
}) | ||
.run(); | ||
.run(); |
167
index.js
var simplify = require('simplify-js'); | ||
// supported GeoJSON geometries, used to check whether to wrap in simpleFeature() | ||
var supportedTypes = ['LineString', 'MultiLineString', 'Polygon', 'MultiPolygon']; | ||
/** | ||
* Takes a {@link LineString} or {@link Polygon} feature and returns a simplified version. Internally uses [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification. | ||
* 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. | ||
* | ||
* @module turf/simplify | ||
* @name simplify | ||
* @category transformation | ||
* @param {Feature} feature a {@link LineString} or {@link Polygon} feature to be simplified | ||
* @param {number} tolerance simplification tolerance | ||
* @param {boolean} highQuality whether or not to spend more time to create | ||
* @param {Feature<(LineString|Polygon|MultiLineString|MultiPolygon)>|FeatureCollection|GeometryCollection} feature feature to be simplified | ||
* @param {Number} tolerance simplification tolerance | ||
* @param {Boolean} highQuality whether or not to spend more time to create | ||
* a higher-quality simplification with a different algorithm | ||
* @return {Feature} a simplified feature | ||
* @return {Feature<(LineString|Polygon|MultiLineString|MultiPolygon)>|FeatureCollection|GeometryCollection} a simplified feature | ||
* @example | ||
@@ -53,40 +56,128 @@ * var feature = { | ||
*/ | ||
module.exports = function(feature, tolerance, highQuality){ | ||
if(feature.geometry.type === 'LineString') { | ||
var line = { | ||
type: 'LineString', | ||
coordinates: [] | ||
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); | ||
// 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; | ||
} | ||
}; | ||
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; | ||
} | ||
} | ||
/* | ||
* 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; | ||
} | ||
} | ||
function simpleFeature(geom, properties) { | ||
return { | ||
type: 'Feature', | ||
geometry: geom, | ||
properties: properties | ||
}; | ||
var pts = feature.geometry.coordinates.map(function(coord) { | ||
return {x: coord[0], y: coord[1]}; | ||
}); | ||
line.coordinates = simplify(pts, tolerance, highQuality).map(function(coords){ | ||
return [coords.x, coords.y]; | ||
}); | ||
return simpleFeature(line, feature.properties); | ||
} else if(feature.geometry.type === 'Polygon') { | ||
var poly = { | ||
type: 'Polygon', | ||
coordinates: [] | ||
}; | ||
feature.geometry.coordinates.forEach(function(ring){ | ||
var pts = ring.map(function(coord) { | ||
} | ||
function simplifyLine(coordinates, tolerance, highQuality) { | ||
return simplify(coordinates.map(function (coord) { | ||
return {x: coord[0], y: coord[1]}; | ||
}); | ||
var simpleRing = simplify(pts, tolerance, highQuality).map(function(coords){ | ||
}), tolerance, highQuality).map(function (coords) { | ||
return [coords.x, coords.y]; | ||
}); | ||
poly.coordinates.push(simpleRing); | ||
}); | ||
return simpleFeature(poly, feature.properties) | ||
} | ||
} | ||
function simpleFeature (geom, properties) { | ||
return { | ||
type: 'Feature', | ||
geometry: geom, | ||
properties: properties | ||
}; | ||
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; | ||
}); | ||
} |
{ | ||
"name": "turf-simplify", | ||
"version": "1.0.3", | ||
"version": "3.0.0-canary.2f5f7167", | ||
"description": "simplify geographic shapes", | ||
@@ -10,4 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "tape test/*.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
"test": "tape test/*.js" | ||
}, | ||
@@ -31,5 +30,3 @@ "repository": { | ||
"benchmark": "^1.0.0", | ||
"tape": "~3.5.0", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3" | ||
"tape": "~3.5.0" | ||
}, | ||
@@ -36,0 +33,0 @@ "dependencies": { |
@@ -10,5 +10,3 @@ # turf-simplify | ||
Simplifies a Feature containing a LineString or | ||
Polygon geometry. Internally uses [simplify-js](http://mourner.github.io/simplify-js/) | ||
to perform simplification. | ||
Takes a LineString or Polygon and returns a simplified version. Internally uses [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification. | ||
@@ -18,7 +16,7 @@ | ||
| parameter | type | description | | ||
| ------------- | ------- | -------------------------------------------------------------------------------------------------------- | | ||
| `feature` | Feature | - a LineString or Polygon feature to be simplified | | ||
| `tolerance` | number | - simplification tolerance | | ||
| `highQuality` | boolean | - whether or not to spend more time to create a higher-quality simplification with a different algorithm | | ||
| parameter | type | description | | ||
| ------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------ | | ||
| `feature` | Feature\.\<LineString|Polygon\> | feature to be simplified | | ||
| `tolerance` | Number | simplification tolerance | | ||
| `highQuality` | Boolean | whether or not to spend more time to create a higher-quality simplification with a different algorithm | | ||
@@ -29,25 +27,34 @@ | ||
```js | ||
var feature = turf.polygon([[ | ||
[-70.603637, -33.399918], | ||
[-70.614624, -33.395332], | ||
[-70.639343, -33.392466], | ||
[-70.659942, -33.394759], | ||
[-70.683975, -33.404504], | ||
[-70.697021, -33.419406], | ||
[-70.701141, -33.434306], | ||
[-70.700454, -33.446339], | ||
[-70.694274, -33.458369], | ||
[-70.682601, -33.465816], | ||
[-70.668869, -33.472117], | ||
[-70.646209, -33.473835], | ||
[-70.624923, -33.472117], | ||
[-70.609817, -33.468107], | ||
[-70.595397, -33.458369], | ||
[-70.587158, -33.442901], | ||
[-70.587158, -33.426283], | ||
[-70.590591, -33.414248], | ||
[-70.594711, -33.406224], | ||
[-70.603637, -33.399918] | ||
]]); | ||
var feature = { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [[ | ||
[-70.603637, -33.399918], | ||
[-70.614624, -33.395332], | ||
[-70.639343, -33.392466], | ||
[-70.659942, -33.394759], | ||
[-70.683975, -33.404504], | ||
[-70.697021, -33.419406], | ||
[-70.701141, -33.434306], | ||
[-70.700454, -33.446339], | ||
[-70.694274, -33.458369], | ||
[-70.682601, -33.465816], | ||
[-70.668869, -33.472117], | ||
[-70.646209, -33.473835], | ||
[-70.624923, -33.472117], | ||
[-70.609817, -33.468107], | ||
[-70.595397, -33.458369], | ||
[-70.587158, -33.442901], | ||
[-70.587158, -33.426283], | ||
[-70.590591, -33.414248], | ||
[-70.594711, -33.406224], | ||
[-70.603637, -33.399918] | ||
]] | ||
} | ||
}; | ||
var tolerance = 0.01; | ||
var simplified = turf.simplify( | ||
@@ -61,2 +68,5 @@ feature, tolerance, false); | ||
**Returns** `Feature.<LineString|Polygon>`, a simplified feature | ||
## Installation | ||
@@ -76,1 +86,2 @@ | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
2
215
0
83
1
11027
5
1
1