Skip to content

simplify

simplifyGeometry

Signature

js
simplifyGeometry (geometry, options)

Description

Simplifies all coordinate sequences of a GeoJSON geometry using the Visvalingam-Whyatt algorithm. Supports LineString, Polygon, MultiLineString, MultiPolygon, and GeometryCollection. Point and MultiPoint geometries are returned unchanged. The operation is performed in place — the original object is mutated and returned.

Parameters

NameTypeRequiredDescription
geometryobjectyesA GeoJSON geometry object
options.tolerancenumbernoMinimum triangle area threshold below which a point is removed (default: 0)
options.getWeightfunctionnoA function (coord, index) => number that returns a weight multiplied by the triangle area of each point. Defaults to () => 1 (no weighting)

Returns

TypeDescription
objectThe mutated geometry

Throws

Throws if geometry is not a valid GeoJSON geometry.

Examples

js
// LineString
simplifyGeometry(
  { type: 'LineString', coordinates: [[0,0],[1,0.01],[2,0.02],[3,0.01],[4,0]] },
  { tolerance: 1e-4 }
)
// { type: 'LineString', coordinates: [[0,0],[4,0]] }
js
// Polygon — each ring is simplified independently
simplifyGeometry(
  { type: 'Polygon', coordinates: [[[0,0],[1,0.01],[2,0],[3,0.01],[4,0],[2,-1],[0,0]]] },
  { tolerance: 1e-4 }
)
// { type: 'Polygon', coordinates: [[[0,0],[4,0],[2,-1],[0,0]]] }
js
// GeometryCollection — recurses into each geometry
simplifyGeometry(
  {
    type: 'GeometryCollection',
    geometries: [
      { type: 'LineString', coordinates: [[0,0],[1,0.01],[2,0]] },
      { type: 'LineString', coordinates: [[3,0],[4,0.01],[5,0]] }
    ]
  },
  { tolerance: 1e-4 }
)
// {
//   type: 'GeometryCollection',
//   geometries: [
//     { type: 'LineString', coordinates: [[0,0],[2,0]] },
//     { type: 'LineString', coordinates: [[3,0],[5,0]] }
//   ]
// }
js
// Point — returned unchanged
simplifyGeometry({ type: 'Point', coordinates: [2.3522, 48.8566] }, { tolerance: 1e-4 })
// { type: 'Point', coordinates: [2.3522, 48.8566] }
js
// Custom weight — preserve points of interest regardless of their area
simplifyGeometry(
  { type: 'LineString', coordinates: [[0,0],[1,0.01],[2,0.01],[3,0.01],[4,0]] },
  {
    tolerance: 1e-4,
    getWeight: (coord) => coord[0] === 2 ? 1e10 : 1
  }
)
// { type: 'LineString', coordinates: [[0,0],[2,0.01],[4,0]] }
js
// Mutates in place
const geometry = { type: 'LineString', coordinates: [[0,0],[1,0.01],[2,0]] }
const result = simplifyGeometry(geometry, { tolerance: 1e-4 })
result === geometry // true

simplifyGeoJson

Signature

js
simplifyGeoJson (geoJson, options)

Description

Simplifies all coordinate sequences of any GeoJSON object using the Visvalingam-Whyatt algorithm. Accepts a plain geometry, a Feature, or a FeatureCollection. Features with a null geometry are skipped silently. The operation is performed in place — the original object is mutated and returned.

Parameters

NameTypeRequiredDescription
geoJsonobjectyesAny valid GeoJSON object (geometry, Feature, or FeatureCollection)
options.tolerancenumbernoMinimum triangle area threshold below which a point is removed (default: 0)
options.getWeightfunctionnoA function (coord, index) => number that returns a weight multiplied by the triangle area of each point. Defaults to () => 1 (no weighting)

Returns

TypeDescription
objectThe mutated GeoJSON object

Throws

Throws if geoJson is not a valid GeoJSON object.

Examples

js
// Plain geometry
simplifyGeoJson(
  { type: 'LineString', coordinates: [[0,0],[1,0.01],[2,0.02],[3,0.01],[4,0]] },
  { tolerance: 1e-4 }
)
// { type: 'LineString', coordinates: [[0,0],[4,0]] }
js
// Feature
simplifyGeoJson(
  {
    type: 'Feature',
    properties: { name: 'Route' },
    geometry: { type: 'LineString', coordinates: [[0,0],[1,0.01],[2,0.02],[3,0.01],[4,0]] }
  },
  { tolerance: 1e-4 }
)
// {
//   type: 'Feature',
//   properties: { name: 'Route' },
//   geometry: { type: 'LineString', coordinates: [[0,0],[4,0]] }
// }
js
// Feature with null geometry — skipped silently
simplifyGeoJson(
  { type: 'Feature', geometry: null, properties: {} },
  { tolerance: 1e-4 }
)
// { type: 'Feature', geometry: null, properties: {} }
js
// FeatureCollection
simplifyGeoJson(
  {
    type: 'FeatureCollection',
    features: [
      { type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [[0,0],[1,0.01],[2,0]] } },
      { type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [[3,0],[4,0.01],[5,0]] } }
    ]
  },
  { tolerance: 1e-4 }
)
// {
//   type: 'FeatureCollection',
//   features: [
//     { type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [[0,0],[2,0]] } },
//     { type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [[3,0],[5,0]] } }
//   ]
// }
js
// Mutates in place
const geoJson = {
  type: 'Feature',
  geometry: { type: 'LineString', coordinates: [[0,0],[1,0.01],[2,0]] },
  properties: {}
}
const result = simplifyGeoJson(geoJson, { tolerance: 1e-4 })
result === geoJson // true