39 releases (18 stable)

2.0.10 Mar 12, 2024
2.0.8 Dec 21, 2023
2.0.7 Nov 16, 2023
2.0.1 Jul 26, 2023
0.1.2 Dec 31, 2022

#5 in Geospatial

Download history 146/week @ 2024-02-18 165/week @ 2024-02-25 12/week @ 2024-03-03 169/week @ 2024-03-10 16/week @ 2024-03-17 2/week @ 2024-03-24 36/week @ 2024-03-31 1/week @ 2024-04-07

66 downloads per month
Used in 2 crates

MIT license

530KB
15K SLoC

Rust D3 Geo

Rust 2021 Edition.

crates.io Documentation Download

This is a port d3-geo into RUST. It is part of a family of ported d3-modules

This library allows the development of custom maps. It provides a comprehensive set of projections along with the means to scale rotate and translate the final image. The projector processes polygons, lines and points in the form of GeoJSON objects. Additionally this library can be used to calculate lengths, areas and the centroid of such objects

CHANGELOG.md contains a summary of breaking changes between v1.x and 2.0.0.

When to use the rust version of the library

The limits of the javascript library become obvious when developing interactive applications that process large datasets. For example the examples/globe applications operate on a highly detailed ( 1:50M resolution ) map of the earth. On a desktop machine this is beyond the javascript version.

Available Projections
Albers ConicEqualArea Gnomic
AlbersUsa Equidistant Orthographic
AzimuthalEqualArea Equirectangular Mercator
AzimuthalEquiDistant EqualArea MercatorTransverse
Conformal EqualEarth Stereographic

Examples

These Examples are provided to help developers convert their existing javascript to rust. They can be found in the github repository associated with this crate.

Description
examples/globe/rotating

This is a port into rust of this d3-geo example

www.d3indepth.com/geographic/

The javascript version compromises by using a low resolution map. Here no such compromise is required.

This globe is rendered to a HTML CANVAS element.

For performance reasons this example is best viewed by running "cargo build" and then "cargo serve" which compiles the rust code using the --release flag.

(Scale 1:50M)

examples/globe/svg

The globe is rendered as a SVG image.

SVG are useful when the semantic meaning of the data needs to be preserved. The example shows how to load/parse/display the globe as individual SVG PATH elements.

It also includes code samples that generates SVG graticules.

(Scale 1:50M)

examples/globe/drag_and_zoom

This globe is rendered to a HTML CANVAS element

It deliberately mixes typescript methods with rust. The typescript is responsible for handling the mouse events and manipulating the quaternion used to calculate the appropriate change in rotation. In a typescript render loop calls to a rust function render the globe.

This example is currently undergoing rapid development.

(Scale 1:50M)

examples/projections

All available projections are rendered to a HTML CANVAS element

As a confidence building exercise, this demo shows a side by side comparison of the all the projections rendered by in both javascript and rust.

(Scale 1:50M)

examples/globe/albers_usa

This show all the counties in the USA.

AlbersUSA is unlike the other projections. Alaska and Hawaii are rendered as insets. As can be seen in the code a Multi-drain must be used to gather the three projections.

(Scale of 1:10M)

examples/ring

SVG example

Sample code in both RUST and javascript that renders a complex multi-polygon. ( Orthographic and Stereographic )


An outline of the common steps found in all the examples

  1. For a given projection, use its default projection builder, make changes to the scale, translation .. etc, then call build() to construct a projector.

    let projector = Stereographic::<f64>::builder()
      .scale_set(100_f64)
      .translate_set(&Coord {
         x: 300_f64,
         y: 300_f64,
      })
      .clip_angle(90_f64)
      .precision_set(&10_f64)
      .build();
    
  2. Construct a PathBuilder

    A Path is a collection of nodes where each step on the path transforms the geometry object in some manner.

    An object is then streamed along the path.

    Here is an overview of the key nodes.

    Clipping: Two strategies are used "Antimeridian" and "ClipAngle" [See clip_angle_set() and clip_angle_reset()]

    Resampling: Dense geometry can be reduced by declaring a separation distance under which points, used to describe polygons and lines, are considered indistinguishable [See precision_set()]

    Bounding: A projection space box can be set, and only geometry within this extent will be displayed. Polygons partially inside the box are restructured to conform to the edges of the box. [See clip_extent_set() clip_extent_clear()]

    Endpoints are special path nodes which hold the result of a calculation. A variety of endpoint are available Area, Centroid, Length which can be use to compute properties about polygons or lines. These examples only show endpoints that render to a HTML canvas element or a SVG path element.

    When rendering to a HTML canvas the endpoint holds Path2D "rendering context"

     // Construct a PathBuilder linked to Path2d
     // rendering context.
     let path2d = Path2d::new()?;
     let endpoint = PathBuilder::new(path2d);
     let pb = PathBuilder::new(endpoint);
     let path = pb.build();
    

    When generating a SVG image, the endpoint holds a string value from which a PATH element can be constructed.

      let pb = PathBuilder::pathstring();
      let path = pb.build();
    
  3. Please see the different examples, but the common next step is to construct a PathBuilder object and then to stream a geometry object into it :-

       // 'countries' is a geometry extracted from
       // a world map json file.
       path.stream(&countries)
    

Running the examples


Requirements:

To view an example application either create a development build, or construct a static-web site as follows

Start And Run A Development Build

git clone https://github.com/martinfrances107/rust_d3_geo.git
cd rust_d3_geo/examples/ring/
npm install
npm run start

The last command "npm run start" will automatically open your default browser at http:://localhost::8080

Performance: Building A Static Site

Much better performance can be achieved by building a static web site and viewing that directly. At the expense of longer build times the RUST portions of the code a build using the "--release" tags

  git clone https://github.com/martinfrances107/rust_d3_geo.git
  cd rust_d3_geo/examples/ring
  npm install
  npm run build
  npm run serve

Benchmarking

There are two distinct compute environments

  1. The browser: Is a highly constrained environment. Here are two equivalent benchmarks, one for rust rust_d3_geo_voronoi and one for javascript d3-geo-voronoi. Rust runs as a service worker with no direct control the DOM. Passing objects as JsValue between threads has negative performance impacts despite this benchmark runs twice as fast.

  2. Node like environments: The github repository associated with crate has two "profile targets" and two "benches" which can be used to to spot bottlenecks in this environment. The benches are Criterion.rs based micro benchmarks.

Flamegraph

A profile_target is binary that outputs a HTML page containing a SVG image showing the globe with graticule markings.

A flame-graph can be created by entering a particular profile targets directory and running :-

cd profile_target/albers_usa
sudo CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph

The complexity of rendering 240 countries/polygons provides a good view in memory allocation issues.

Coding Standard

  • Idiomatic RUST, as defined by cargo clippy where possible.

  • No booleans as arguments to functions/methods, use two state enums instead.

    See "Reflect" as an example.

    pub trait ReflectSet {
        /// f64 or f32.
        type T;
       /// Set the projection builder to invert the x-coordinate.
        fn reflect_x_set(&mut self, reflect: Reflect) -> &mut Self;
       /// Set the projection builder to invert the y-coordinate.
        fn reflect_y_set(&mut self, reflect: Reflect) -> &mut Self;
    }
    

    This allows for a clearer statement of intent :-

    builder.reflect_y_set(Reflect::Flipped);
    
  • "Type-Driven API Design" is the preferred way of constructing state machines.

    In the example below, when assembling a stream pipeline, connect() can only be called when the state is "Unconnected". The output type's STATE is "Connected<SINK>".

    impl StreamTransformRadians<Unconnected> {
      #[inline]
      /// Connect this node to the next element in the pipeline.
      pub const fn connect<EP, SINK, T>(self, sink: SINK) -> StreamTransformRadians<Connected<SINK>>
      where
        SINK: Clone,
      {
        StreamTransformRadians(Connected { sink })
      }
    }
    

    The "Stream" trait is only implemented when the STATE is "Connected<SINK>". By design, all code is prevented from calling line_start() or point() unless the object has been connected to another pipeline stage.


Future 2.0 upgrades

"Semantic Versioning" guidelines :-

  • Increment the major number when a breaking change occurs.
  • Increment the minor number when a new feature is added, @deprecated notes added to outdated functions,
  • Increment the patch number for tightly focused security fixes.

Future Work.

  • rayon is rust's crate for multithread support.

  • I have made extensive use of iterators when porting the code and rayon support the easy conversion of single threaded iterators to multithread iterators.

  • The Hashmaps - appear slow. Maybe I can get performance improvements by replacing them with B-tree collections?

  • Should I use an arena for memory management?

Architecture discussion

There is an aspect of the design that needs review. It related to the best way to implement a doubly-linked list which has cross links between nodes.

The clipping algorithm in clip/rejoin/mod.rs needs to be refactored. see The intersection Problem. Test coverage in that area is high so the algorithms is working but the data structures make extensive use of vectors ( heap objects ) containing references to other heap objects Vec<Options<Rc<RefCell<_Intersection_>>>> which is not performant.

A full discussion can be found here

Unimplemented sections of the library

Support for a custom projection is not yet supported.

For an example of this see the test labelled "projection.fitExtent(…) custom projection"

Dependencies

~16MB
~276K SLoC