#svg #polygon #graphics #geo

geo-svg

Convert geo types to SVG strings for visualization

15 releases

new 0.7.0 Nov 27, 2024
0.6.3 Feb 28, 2024
0.6.1 Oct 31, 2023
0.5.0 Apr 6, 2021
0.2.2 Jul 8, 2019

#126 in Debugging

Download history 4/week @ 2024-08-07 27/week @ 2024-08-14 17/week @ 2024-08-21 9/week @ 2024-08-28 3/week @ 2024-09-04 13/week @ 2024-09-11 24/week @ 2024-09-18 61/week @ 2024-09-25 15/week @ 2024-10-02 27/week @ 2024-10-09 17/week @ 2024-10-16 32/week @ 2024-10-23 21/week @ 2024-10-30 47/week @ 2024-11-06 17/week @ 2024-11-13 20/week @ 2024-11-20

106 downloads per month
Used in 3 crates

ISC license

40KB
709 lines

geo-svg

This crate is a lib to generate SVG strings from geo-types.

crate.io docs.rs

Below is an example of a geometry collection rendered to SVG.

example

Features

  • GeometryCollection and all variants of Geometry are supported
  • the viewport size is automatically computed to contain all shapes
  • style and formatting options are available

Example

The following will show how to convert a line to a SVG string. The to_svg method is provided by the ToSvg trait which is implemented for all geo-types.

use geo_types::{Coordinate, Line, Point};
use geo_svg::{Color, ToSvg};
let point = Point::new(10.0, 28.1);
let line = Line::new(
    Coordinate { x: 114.19, y: 22.26 },
    Coordinate { x: 15.93, y: -15.76 },
);

let svg = point
    .to_svg()
    .with_radius(2.0)
    .and(line.to_svg().with_stroke_width(2.5))
    .with_fill_color(Color::Named("red"))
    .with_stroke_color(Color::Rgb(200, 0, 100))
    .with_fill_opacity(0.7);

println!("{}", svg);

Result

<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid meet" viewBox="7 -18.26 109.69 49.36"><circle cx="10" cy="28.1" r="2" fill="red" fill-opacity="0.7" stroke="rgb(200,0,100)"/><path d="M 114.19 22.26 L 15.93 -15.76" fill="red" fill-opacity="0.7" stroke="rgb(200,0,100)" stroke-width="2.5"/></svg>

Dependencies

~5MB
~96K SLoC