#contour #polygon #geojson #isoring #marching-squares

contour

Compute isorings and contour polygons (using marching squares algorithm)

2 unstable releases

0.2.0 Jul 25, 2020
0.1.0 Jan 21, 2019

#3 in Encoding

MIT/Apache

60KB
684 lines

contour-rs

Build Status Travis Build status Appveyor Docs.rs version

Computes isorings and contour polygons by applying marching squares to a rectangular array of numeric values.
Outputs ring coordinates or polygons contours as a Vec of GeoJSON Features.
Note : This is a port of d3-contour.


Usage

Add this to your Cargo.toml:

[dependencies]
contour = "0.1.0"

and this to your crate root:

extern crate contour;

The API exposes:

  • a contour_rings function, which computes isorings coordinates for one threshold value (returns a Vec of rings coordinates).
  • a ContourBuilder struct, which computes isorings coordinates for a Vec of threshold values and transform them in MultiPolygons (returns a Vec of GeoJSON Features).

Example:

let c = ContourBuilder::new(10, 10, false); // x dim., y dim., smoothing
let res = c.contours(&vec![
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 1., 1., 1., 0., 0., 0., 0.,
    0., 0., 0., 1., 1., 1., 0., 0., 0., 0.,
    0., 0., 0., 1., 1., 1., 0., 0., 0., 0.,
    0., 0., 0., 1., 1., 1., 0., 0., 0., 0.,
    0., 0., 0., 1., 1., 1., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
], &[0.5])?; // values, thresholds

Output:

[Feature {
  bbox: None,
  geometry: Some(Geometry {
    bbox: None,
    value: MultiPolygon([[[
      [6., 7.5], [6., 6.5], [6., 5.5], [6., 4.5],
      [6., 3.5], [5.5, 3.], [4.5, 3.], [3.5, 3.],
      [3., 3.5], [3., 4.5], [3., 5.5], [3., 6.5],
      [3., 7.5], [3.5, 8.], [4.5, 8.], [5.5, 8.],
      [6., 7.5]]]]),
    foreign_members: None
    }),
   id: None,
   properties: Some({"value": Number(0.5)}),
   foreign_members: None
   }]

Demo

Demo of this crate compiled to WebAssembly and used from JavaScript : wasm_demo_contour.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.8–1.1MB
~21K SLoC