# TopoJSON Simplify Topology-preserving simplification and filtering for TopoJSON. Smaller files, faster rendering! For an introduction to line simplification: * https://bost.ocks.org/mike/simplify/ * https://www.jasondavies.com/simplify/ ## Installing If you use NPM, `npm install topojson-simplify`. Otherwise, download the [latest release](https://github.com/topojson/topojson-simplify/releases/latest). You can also load directly from [unpkg](https://unpkg.com). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `topojson` global is exported: ```html ``` [Try topojson-simplify in your browser.](https://tonicdev.com/npm/topojson-simplify) # API Reference # topojson.presimplify(topology[, weight]) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/presimplify.js "Source") Returns a shallow copy of the specified *topology* where each coordinate of each arc is assigned a *z*-value according to the specified *weight* function. If *weight* is not specified, it defaults to [planarTriangleArea](#planarTriangleArea). If the input *topology* is delta-encoded (that is, if a *topology*.transform is present), this transform is removed in the returned output topology. The returned presimplified topology can be passed to [simplify](#simplify) to remove coordinates below a desired weight threshold. # topojson.simplify(topology[, minWeight[, weight]]) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/simplify.js "Source") Returns a shallow copy of the specified *topology* where every arc coordinate whose *z*-value is lower than *minWeight* is removed. Only the *x* and *y* dimensions of the coordinates are preserved in the returned topology. If *minWeight* is not specified, it defaults to [Number.MIN_VALUE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE). This method has no effect on Point and MultiPoint geometries. See [presimplify](#presimplify) to assign *z*-value for each coordinate. See also [toposimplify](#toposimplify). # topojson.quantile(topology, p) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/quantile.js "Source") Returns the *p*-quantile of the weighted points in the given [presimplified](#presimplify) *topology*, where *p* is a number in the range [0, 1]. The quantile value is then typically passed as the *minWeight* to [simplify](#simplify). For example, the median weight can be computed using *p* = 0.5, the first quartile at *p* = 0.25, and the third quartile at *p* = 0.75. This implementation uses the [R-7 method](https://en.wikipedia.org/wiki/Quantile#Quantiles_of_a_population), which is the default for the R programming language and Excel. ### Filtering # topojson.filter(topology[, filter]) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/filter.js "Source") Returns a shallow copy of the specified *topology*, removing any rings that fail the specified [*filter* function](#_filter). See [filterAttached](#filterAttached) and [filterWeight](#filterWeight) for built-in filter implementations. If a resulting Polygon geometry object has no rings, it is replaced with a null geometry; likewise, empty polygons are removed from MultiPolygon geometry objects, and if the resulting MultiPolygon geometry object has no polygons, it is replaced with a null geometry; likewise, any null geometry objects are removed from GeometryCollection objects, and if the resulting GeometryCollection is empty, it is replaced with a null geometry. After any geometry objects are removed from the *topology*, the resulting topology is pruned, removing any unused arcs. As a result, this operation typically changes the arc indexes of the topology. # topojson.filterAttached(topology) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/filterAttached.js "Source") Returns a [ring *filter* function](#_filter) that returns true if the specified *ring* shares an arc with any other object in the *topology*. # topojson.filterAttachedWeight(topology[, minWeight[, weight]]) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/filterAttachedWeight.js "Source") Returns a [ring *filter* function](#_filter) that returns true if the weight of the specified *ring* is greater than or equal to the specified *minWeight* threshold or the specified *ring* shares an arc with any other object in the *topology*. # topojson.filterWeight(topology[, minWeight[, weight]]) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/filterWeight.js "Source") Returns a [ring *filter* function](#_filter) that returns true if the weight of the specified *ring* is greater than or equal to the specified *minWeight* threshold. If *minWeight* is not specified, it defaults to [Number.MIN_VALUE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE). If *weight* is not specified, it defaults to [planarRingArea](#planarRingArea). # filter(ring, interior) To filter a topology, you supply a ring *filter* function to [filter](#filter). The *filter* function is invoked for each ring in the input topology, being passed two arguments: the *ring*, specified as an array of points where each point is a two-element array of numbers, and the *interior* flag. If *interior* is false, the given *ring* is the exterior ring of a polygon; if *interior* is true, the given *ring* is an interior ring (a hole). The *filter* function must then return true if the ring should be preserved, or false if the ring should be removed. ### Geometry # topojson.planarRingArea(ring) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/planar.js#L6 "Source") Returns the [planar area](http://mathworld.wolfram.com/PolygonArea.html) of the specified *ring*, which is an array of points \[\[*x*₀, *y*₀\], \[*x*₁, *y*₁\], …\]. The first point must be equal to the last point. This implementation is agnostic to winding order; the returned value is always non-negative. # topojson.planarTriangleArea(triangle) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/planar.js#L1 "Source") Returns the [planar area](http://mathworld.wolfram.com/TriangleArea.html) of the specified *triangle*, which is an array of three points \[\[*x*₀, *y*₀\], \[*x*₁, *y*₁\], \[*x*₂, *y*₂\]\]. This implementation is agnostic to winding order; the returned value is always non-negative. # topojson.sphericalRingArea(ring, interior) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/spherical.js#L14 "Source") Returns the [spherical area](https://en.wikipedia.org/wiki/Spherical_trigonometry#Area_and_spherical_excess) of the specified *ring*, which is an array of points \[\[*x*₀, *y*₀\], \[*x*₁, *y*₁\], …\] where *x* and *y* represent longitude and latitude in degrees, respectively. The first point must be equal to the last point. This implementation uses [d3-geo](https://github.com/d3/d3-geo)’s [winding order convention](https://bl.ocks.org/mbostock/a7bdfeb041e850799a8d3dce4d8c50c8) to determine which side of the polygon is the inside: polygons smaller than a hemisphere must be clockwise, while polygons [larger than a hemisphere](https://bl.ocks.org/mbostock/6713736) must be anticlockwise. If *interior* is true, the opposite winding order is used. This winding order convention is also used by [ESRI shapefiles](https://github.com/mbostock/shapefile); however, it is the **opposite** convention of GeoJSON’s [RFC 7946](https://tools.ietf.org/html/rfc7946#section-3.1.6). # topojson.sphericalTriangleArea(triangle) [<>](https://github.com/topojson/topojson-simplify/blob/master/src/spherical.js#L43 "Source") Returns the [spherical excess](http://mathworld.wolfram.com/SphericalExcess.html) of the specified *triangle*, which is an array of three points \[\[*x*₀, *y*₀\], \[*x*₁, *y*₁\], \[*x*₂, *y*₂\]\] where *x* and *y* represent longitude and latitude in degrees, respectively. This implementation uses the same winding order convention as [sphericalRingArea](#sphericalRingArea). ## Command Line Reference ### toposimplify # toposimplify [options…] [file] [<>](https://github.com/topojson/topojson-simplify/blob/master/bin/toposimplify "Source") Given an input *topology*, assigns a *z*-value to every arc coordinate according to a configurable *weight* function, and then generates an output *topology* where every arc coordinate whose *z*-value is lower than a configurable minimum weight is removed. Only the *x* and *y* dimensions of the coordinates are preserved in the returned topology. See also [presimplify](#presimplify), [simplify](#simplify) and [filter](#filter). # toposimplify -h
# toposimplify --help Output usage information. # toposimplify -V
# toposimplify --version Output the version number. # toposimplify -o file
# toposimplify --out file Specify the output TopoJSON file name. Defaults to “-” for stdout. # toposimplify -p value
# toposimplify --planar-area value Specify simplification threshold *value* as the minimum planar triangle area, typically in square pixels. # toposimplify -P value
# toposimplify --planar-quantile value Specify simplification threshold *value* as the minimum quantile of planar triangle areas. The *value* should be in the range [0, 1]. # toposimplify -s value
# toposimplify --spherical-area value Specify simplification threshold *value* as the minimum spherical triangle area ([spherical excess](http://mathworld.wolfram.com/SphericalExcess.html)), in [steradians](https://en.wikipedia.org/wiki/Steradian). # toposimplify -S value
# toposimplify --spherical-quantile value Specify simplification threshold *value* as the minimum quantile of spherical triangle areas ([spherical excess](http://mathworld.wolfram.com/SphericalExcess.html)). The *value* should be in the range [0, 1]. # toposimplify -f
# toposimplify --filter-detached Remove detached rings that are smaller than the simplification threshold after simplifying. See [filterAttached](#filterAttached). # toposimplify -F
# toposimplify --filter-all Remove any rings that are smaller than the simplification threshold after simplifying. See [filterWeight](#filterWeight).