Simplify polyline or polygon vertices in JS
npm install poly-simplify





[{x:0, y:10}, {x:1, y:20}]
point attributes – 0 10 1 20
[[0 10], [1 20]]
[{"x":0,"y":10},{"x":1,"y":20}]
M0 0 100 200z
`
$3
`
`
$3
Install
`
npm install poly-simplify
`
Usage
`
import polySimplify from 'poly-simplify';
const simplified = polySimplify('M0 0 h10 h10 v10 v10');
console.log(simplified);
`
2. Options
| param | default | type | effect |
| -- | -- | -- | -- |
| quality | 0.5 | number/string | simplification tolerance: accepts numeric value from 0-1 for relative quality, absolute pixel thresholds adding px unit to string 1px or maximum vertices specified via v unit like so 150v |
| RDP | true | Boolean | Applies Ramer-Douglas-Peucker simplification |
| VW | true | Boolean | Visvalingam-Whyatt simplification |
| RD | true | Boolean | Radial Distance simplification |
| output | points | string | output result: 1. "points"= point object array 2. "path"= SVG path data string 3. "pathdata" = pathdata array 4. "json"= JSON string 5. "pointString" = flat point string (as used for polygon points attribute) |
| skipPoints | false | Boolean | simplify by reducing number of vertices to a max limit |
| maxPoints | 0 | Number | Max points limit for useMax option |
| mercator | false | Boolean | Applies Mercator projection – for geodata polygons (or funny distortion effects) |
| unite | false | Boolean | Unites self intersecting polygons |
| scale | 1 | Number | scale polygon |
| scaleToWidth, scaleToHeight | 0 | Number | fit polygon into specified max dimensions |
| alignToZero | false | Boolean | align weird starting coordinates to x/y=0 |
| translateX, translateY | 0 | Number | set manual x/y offsets to realign polygon |
| decimals | -1 | Number | round coordinates to decimals. -1 won't apply rounding. Too small floating point accuracy is auto-adjusted to avoid distortions |
| toRelative | false | Boolean | SVG converts commands to relative – more compact |
| toShorthands | false | Boolean | SVG applies shorthand commands where possible h, v – more compact |
| minifyString | false | Boolean | SVG applies string based minification e.g omitting leading zeroes or concatenating subsequent floats .123.456 – more compact |
| meta | false | Boolean | return additional data like: vertices counts (original and simplified) – this option is mostly for debugging e.g for integrety checks |
3. Quality control
$3
By default poly-simplify expects a value between 0–1.
This concept tries to achieve a relative quality similar to raster image compression settings such as Jpg quality levels:
* 1 - we only apply "lossless" simplification by removing only colinear points. More aggressive simplification algorithms like Radial-distance, Ramer-Douglas-Peucker or Visvalingam-Whyatt are disabled
* 0.5 – 1 – tries to find a ballance between visual fidelity and point reduction
* <0.5 – adjusts thresholds for more aggressive simplification
This approach will auto-detect reasonable thresholds by approximating the total dimensions of a polygon. So geoData polys that may be smaller than 1px/unit can be simplified without the need to know a suitable tolerance7threshold that might be 0.00001px
$3
You can also enter an absolute threshold via string input:
1px – sets the simplification threshold to absolute 1 units – bear in mind this may lead to heavy distortions if your polygon is way smaller than the threshold value
150v – the v`(vertices) unit will add a maximum point limit to the simplification. In this example: reduce to a maximum of 150 points/vertices.