@watercolorizer/visvalingam
v2.1.0
Published
Visvalingam’s line simplification algorithm
Maintainers
Readme
@watercolorizer/visvalingam
Visvalingam–Whyatt algorithm for line simplification.
Installation
For node, install with npm:
npm i @watercolorizer/visvalingamIn browsers, import it from an ESM CDN:
<script type="module">
import { simplify } from 'https://unpkg.com/@watercolorizer/visvalingam/dist/index.js'
</script>Getting Started
import { simplify } from '@watercolorizer/visvalingam';
type Vertex = { x: number; y: number };
const points: Vertex[] = [
/* ...your points here */
];
const areaOfTriangle = ([v0, v1, v2]: [Vertex, Vertex, Vertex]) =>
Math.abs((v0.x - v2.x) * (v1.y - v0.y) - (v0.x - v1.x) * (v2.y - v0.y));
// Simplify polyline in points to 10, removing the triangles that have the least area.
const sPoints = simplify(areaOfTriangle, points, 10);This implementation is not opinionated about any particular vertex format, as long as associated the weightFn is
compatible.
type LngLat = [lng: number, lat: number, elevation?: number];
const areaOf = (triangle: [LngLat, LngLat, LngLat]) =>
spherical.computeArea(triangle);
// Simplify out half the points of a polyline
const simplifiedPoints = simplify(
areaOf,
geodata.lineString,
geodata.lineString / 2,
);More Info
- Line Simplification by Mike Bostock.
- Intersection Avoidance by Jason Davies.
