Visvalingam line simplification with topology-preserving shared arcs
README.md

visvalingam #

Visvalingam-Whyatt line simplification, and a topology-preserving layer that keeps a polygon set from tearing apart under it.

Map data is often far more detailed than the map that draws it. A boundary digitised for a street map carries vertices that a world map cannot show, and every one of them costs bytes to ship. Simplifying each polygon of a set on its own opens gaps, though: two polygons share a boundary, and dropping a different vertex on each side pulls them apart.

This library cuts the rings into arcs first, each arc being a maximal run of vertices that belongs to the same set of rings. Each arc is simplified once and handed back to every ring that uses it, so neighbours share the same points at every cutoff and stay joined.

Visvalingam.thresholds scores each point of a line with the effective area at which it would be dropped, instead of returning a shorter line. A point's effective area is the area of the triangle it forms with its two current neighbours. The algorithm repeatedly removes the point with the smallest area and recomputes its neighbours' areas, and it records for each point the area at which it went, never less than the one recorded before it, so cutting at a single threshold keeps exactly the points scored above it. The scores are twice the triangle's area, which orders the points identically and saves a division, and the two endpoints score infinity. One threshold can then be chosen across a whole collection, which spends the detail where the shape is instead of spreading it evenly over every polygon. On a coastline the small wiggles go before the headlands.

Positions are float arrays whose first two elements are read, the shape of a GeoJSON position (RFC 7946 Section 3.1.1). Nothing here is geographic: the arithmetic is planar, in the caller's units. The library needs OCaml 4.14 or later and has no dependencies.

Installation #

$ opam install visvalingam

Usage #

Two squares sharing a wiggly border, digitised once so the shared vertices are the same coordinates in both rings:

# let inner = [ [|1.02; 0.2|]; [|0.98; 0.4|]; [|1.03; 0.6|]; [|0.99; 0.8|] ]
val inner : float array list =
  [[|1.02; 0.2|]; [|0.98; 0.4|]; [|1.03; 0.6|]; [|0.99; 0.8|]]
# let border = [ [|1.; 0.|] ] @ inner @ [ [|1.; 1.|] ]
val border : float array list =
  [[|1.; 0.|]; [|1.02; 0.2|]; [|0.98; 0.4|]; [|1.03; 0.6|]; [|0.99; 0.8|];
   [|1.; 1.|]]
# let left = Array.of_list ([ [|0.; 0.|] ] @ border @ [ [|0.; 1.|]; [|0.; 0.|] ])
val left : float array array =
  [|[|0.; 0.|]; [|1.; 0.|]; [|1.02; 0.2|]; [|0.98; 0.4|]; [|1.03; 0.6|];
    [|0.99; 0.8|]; [|1.; 1.|]; [|0.; 1.|]; [|0.; 0.|]|]
# let right =
    Array.of_list
      ([ [|1.; 0.|]; [|2.; 0.|]; [|2.; 1.|]; [|1.; 1.|] ]
       @ List.rev inner @ [ [|1.; 0.|] ])
val right : float array array =
  [|[|1.; 0.|]; [|2.; 0.|]; [|2.; 1.|]; [|1.; 1.|]; [|0.99; 0.8|];
    [|1.03; 0.6|]; [|0.98; 0.4|]; [|1.02; 0.2|]; [|1.; 0.|]|]

Decompose once, then cut:

# let t = Visvalingam.Topology.v [ left; right ]
val t : Visvalingam.Topology.t = <abstr>
# Visvalingam.Topology.(vertices t, arcs t)
- : int * int = (18, 3)
# let cut = Visvalingam.Topology.cutoff t ~ratio:0.6
val cut : float = 0.0300000000000000266
# let out = Visvalingam.Topology.rings t ~cut
val out : float array array list =
  [[|[|1.; 0.|]; [|1.03; 0.6|]; [|1.; 1.|]; [|0.; 1.|]; [|0.; 0.|];
     [|1.; 0.|]|];
   [|[|1.; 0.|]; [|2.; 0.|]; [|2.; 1.|]; [|1.; 1.|]; [|1.03; 0.6|];
     [|1.; 0.|]|]]

cutoff ~ratio:0.6 asks for the one threshold that leaves about 60% of the 18 counted vertices standing, a budget of 10.8. Arc endpoints, where rings meet, are never dropped and are spent first; the rest of the budget goes to the interior points with the highest scores. The threshold 0.03 is the score of (1.03, 0.6), the last border vertex kept: once its neighbours on the border are gone, it forms with (1, 0) and (1, 1) a triangle of area ½ × 0.03 × 1 = 0.015, and its score is twice that.

Both rings dropped the same three border vertices, so the border still matches vertex for vertex, as it would at any other cutoff:

# let on_border r =
    List.filter
      (fun (x, y) -> List.exists (fun p -> p.(0) = x && p.(1) = y) border)
      (Array.to_list (Array.map (fun p -> (p.(0), p.(1))) r))
val on_border : float array array -> (float * float) list = <fun>
# match out with
  | [ l; r ] -> on_border l = List.rev (on_border r)
  | _ -> false
- : bool = true

Rounding is a separate, final step. It shortens the coordinates that are left, and on a coordinate list those digits are most of the bytes:

# Visvalingam.round ~precision:1 [| [| 1.234; 5.678 |] |]
- : float array array = [|[|1.2; 5.7|]|]

Notes #

Vertices are matched on their exact coordinates. A set whose shared boundaries were digitised twice has no arcs to find, so every ring is simplified alone and the boundaries tear. Check the source before relying on the topology. Rounding does not preserve topology either, so round on the way out and never before Visvalingam.Topology.v.

min_ring is the number of positions below which a ring may not collapse (defaults to 4). A ring that falls below it gets points back on its own arcs and the set is rebuilt. Adding points never breaks a join, so a neighbour that shares the arc keeps the extra detail too.

Interop #

test/interop/mapshaper replays traces from mapshaper 0.6.44, the reference implementation. The thresholds here are effective areas and mapshaper's are a distance interval, so the scales differ and only the removal order can be compared. The test asserts that some cut of this library reproduces every simplification mapshaper produced, exactly. Regenerate the traces with REGEN=1 dune build @traces; the tests themselves do not need Node.js.

  • mapshaper is the JavaScript tool this library follows, and the reference for what topology-preserving simplification should produce.
  • topojson is a codec for the TopoJSON format, which stores geometry as shared arcs. This library computes and simplifies the arcs, and that one serialises them.
  • Visvalingam, M. and Whyatt, J.D. (1993), "Line generalisation by repeated elimination of points", The Cartographic Journal 30(1), 46-51, describes the algorithm. Douglas and Peucker's algorithm (1973) is the usual alternative, and drops headlands before wiggles.

License #

ISC. See LICENSE.md.