Awesome
TopoJSON Simplify
Topology-preserving simplification and filtering for TopoJSON. Smaller files, faster rendering!
For an introduction to line simplification:
Installing
If you use NPM, npm install topojson-simplify
. Otherwise, download the latest release. You can also load directly from unpkg. AMD, CommonJS, and vanilla environments are supported. In vanilla, a topojson
global is exported:
<script src="https://unpkg.com/topojson-client@3"></script>
<script src="https://unpkg.com/topojson-simplify@3"></script>
<script>
topology = topojson.presimplify(topology);
</script>
Try topojson-simplify in your browser.
API Reference
<a name="presimplify" href="#presimplify">#</a> topojson.<b>presimplify</b>(<i>topology</i>[, <i>weight</i>]) <>
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. 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 to remove coordinates below a desired weight threshold.
<a name="simplify" href="#simplify">#</a> topojson.<b>simplify</b>(<i>topology</i>[, <i>minWeight</i>]) <>
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. This method has no effect on Point and MultiPoint geometries.
See presimplify to assign z-value for each coordinate. See also toposimplify.
<a name="quantile" href="#quantile">#</a> topojson.<b>quantile</b>(<i>topology</i>, <i>p</i>) <>
Returns the p-quantile of the weighted points in the given presimplified topology, where p is a number in the range [0, 1]. The quantile value is then typically passed as the minWeight to 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, which is the default for the R programming language and Excel.
Filtering
<a name="filter" href="#filter">#</a> topojson.<b>filter</b>(<i>topology</i>[, <i>filter</i>]) <>
Returns a shallow copy of the specified topology, removing any rings that fail the specified filter function. See filterAttached and 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.
<a name="filterAttached" href="#filterAttached">#</a> topojson.<b>filterAttached</b>(<i>topology</i>) <>
Returns a ring filter function that returns true if the specified ring shares an arc with any other object in the topology.
<a name="filterAttachedWeight" href="#filterAttachedWeight">#</a> topojson.<b>filterAttachedWeight</b>(<i>topology</i>[, <i>minWeight</i>[, <i>weight</i>]]) <>
Returns a ring filter function 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.
<a name="filterWeight" href="#filterWeight">#</a> topojson.<b>filterWeight</b>(<i>topology</i>[, <i>minWeight</i>[, <i>weight</i>]]) <>
Returns a ring filter function 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. If weight is not specified, it defaults to planarRingArea.
<a name="_filter" href="#_filter">#</a> <i>filter</i>(<i>ring</i>, <i>interior</i>)
To filter a topology, you supply a ring filter function to 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
<a name="planarRingArea" href="#planarRingArea">#</a> topojson.<b>planarRingArea</b>(<i>ring</i>) <>
Returns the planar area 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.
<a name="planarTriangleArea" href="#planarTriangleArea">#</a> topojson.<b>planarTriangleArea</b>(<i>triangle</i>) <>
Returns the planar area 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.
<a name="sphericalRingArea" href="#sphericalRingArea">#</a> topojson.<b>sphericalRingArea</b>(<i>ring</i>, <i>interior</i>) <>
Returns the spherical area 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’s winding order convention to determine which side of the polygon is the inside: polygons smaller than a hemisphere must be clockwise, while polygons larger than a hemisphere must be anticlockwise. If interior is true, the opposite winding order is used. This winding order convention is also used by ESRI shapefiles; however, it is the opposite convention of GeoJSON’s RFC 7946.
<a name="sphericalTriangleArea" href="#sphericalTriangleArea">#</a> topojson.<b>sphericalTriangleArea</b>(<i>triangle</i>) <>
Returns the spherical excess 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.
Command Line Reference
toposimplify
<a name="toposimplify" href="#toposimplify">#</a> <b>toposimplify</b> [<i>options…</i>] [<i>file</i>] <>
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, simplify and filter.
<a name="toposimplify_help" href="#toposimplify_help">#</a> toposimplify <b>-h</b> <br><a href="#toposimplify_help">#</a> toposimplify <b>--help</b>
Output usage information.
<a name="toposimplify_version" href="#toposimplify_version">#</a> toposimplify <b>-V</b> <br><a href="#toposimplify_version">#</a> toposimplify <b>--version</b>
Output the version number.
<a name="toposimplify_out" href="#toposimplify_out">#</a> toposimplify <b>-o</b> <i>file</i> <br><a href="#toposimplify_out">#</a> toposimplify <b>--out</b> <i>file</i>
Specify the output TopoJSON file name. Defaults to “-” for stdout.
<a name="toposimplify_planar_area" href="#toposimplify_planar_area">#</a> toposimplify <b>-p</b> <i>value</i> <br><a href="#toposimplify_planar_area">#</a> toposimplify <b>--planar-area</b> <i>value</i>
Specify simplification threshold value as the minimum planar triangle area, typically in square pixels.
<a name="toposimplify_planar_quantile" href="#toposimplify_planar_quantile">#</a> toposimplify <b>-P</b> <i>value</i> <br><a href="#toposimplify_planar_quantile">#</a> toposimplify <b>--planar-quantile</b> <i>value</i>
Specify simplification threshold value as the minimum quantile of planar triangle areas. The value should be in the range [0, 1].
<a name="toposimplify_spherical_area" href="#toposimplify_spherical_area">#</a> toposimplify <b>-s</b> <i>value</i> <br><a href="#toposimplify_spherical_area">#</a> toposimplify <b>--spherical-area</b> <i>value</i>
Specify simplification threshold value as the minimum spherical triangle area (spherical excess), in steradians.
<a name="toposimplify_spherical_quantile" href="#toposimplify_spherical_quantile">#</a> toposimplify <b>-S</b> <i>value</i> <br><a href="#toposimplify_spherical_quantile">#</a> toposimplify <b>--spherical-quantile</b> <i>value</i>
Specify simplification threshold value as the minimum quantile of spherical triangle areas (spherical excess). The value should be in the range [0, 1].
<a name="toposimplify_filter_detached" href="#toposimplify_filter_detached">#</a> toposimplify <b>-f</b> <br><a href="#toposimplify_filter_detached">#</a> toposimplify <b>--filter-detached</b>
Remove detached rings that are smaller than the simplification threshold after simplifying. See filterAttached.
<a name="toposimplify_filter_all" href="#toposimplify_filter_all">#</a> toposimplify <b>-F</b> <br><a href="#toposimplify_filter_all">#</a> toposimplify <b>--filter-all</b>
Remove any rings that are smaller than the simplification threshold after simplifying. See filterWeight.