rust-geo-svg
Functionality to convert between SVG and geo-types.
SVG to Geometry
This package provides a functions to read a string containing an SVG element or d
string and parse it to a geometry.
svg_to_geometry(svg: &str)
Note this function does not parse a full SVG string (e.g., <svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0L10 0L10 10L0 10Z"/></svg>
), it only parses the individual shape elements (e.g., <path d="M0 0L10 0L10 10L0 10Z"/>
). The following SVG elements are supported and produce the specified Geometry types:
- <path> → GeometryCollection
- <polygon> → GeometryCollection with a single Polygon
- <polyline> → GeometryCollection with a single LineString
- <rect> → GeometryCollection with a single Polygon
- <line> → GeometryCollection with a single Line
Examples
use ;
use svg_to_geometry;
let poly: = polygon!
.into;
let svg_string =
String from;
let parsed_svg = svg_to_geometry;
assert_eq!;
// Unwrap the GeometryCollection result
let geom = parsed_svg.ok.unwrap;
assert_eq!;
// Read the geometry as a Polygon
let pl = geom.0.clone.into_polygon;
assert_eq!;
assert_eq!;
use ;
use svg_to_geometry;
et poly: = polygon!
.into;
let svg_string = String from;
let parsed_svg = svg_to_geometry;
assert_eq!;
// Unwrap the GeometryCollection result
let geom = parsed_svg.ok.unwrap;
assert_eq!;
// Read the geometry as a Polygon
let pl = geom.0.clone.into_polygon;
assert_eq!;
assert_eq!;
svg_d_path_to_geometry(svg: &str)
A <path>
element d
string can be parsed directly into a Geometry by the svg_d_path_to_geometry(svg: &str)
function. The output will always be a GeometryCollection.
Examples
use svg_d_path_to_geometry;
use polygon;
let poly = polygon!;
let svg_string = String from;
let parsed_svg = svg_d_path_to_geometry;
assert_eq!;
// Unwrap the GeometryCollection result
let geom = parsed_svg.ok.unwrap;
assert_eq!;
// Read the geometry as a Polygon
let pl = geom.0.clone.into_polygon;
assert_eq!;
assert_eq!;
Error handling
Both function return a Result which will either contain the parsed Geometry or an Error of the SvgError
Enum. An error may result from passing an unsupported SVG element type, from an improperly formed SVG element, or from an inability to parse a float
from the supplied string.
Geometry to SVG
This package provides two traits for converting a Geometry to SVG. Note that the parsing of curves in <path>
d
-strings is simplistic. It plots 100 points along the curve.
TODO update this functionality to use a recursive function instead to create points until they are collinear (enough).
ToSvg
Using to_svg()
from any Geometry type with produce an SVG element of the simplest type possible:
- Polygon → <path>
- LineString → <polyline>
- Line → <line>
- Triangle → <polygon> with three points
- Rect → <rect> with
x
,y
,width
, andheight
Complex Geometry types will return multiple SVG elements separated by newline
s:
- GeometryCollection →
newline
separated SVG elements corresponding to the individual Geometries it contains - MultiPolygon →
newline
separated elements - MultiLineString →
newline
separated elements
Example
use ;
use ToSvg;
let poly1 = polygon!;
let poly2 = polygon!;
let mp = MultiPolygon;
let wkt_out = mp.to_svg;
let expected = String from;
assert_eq!;
ToSvgString
Using to_svg_string()
from any Geometry type will produce an SVG d
string for all the points of that geometry, which can be used in an SVG path element
Examples
use ;
use ToSvgString;
let poly1 = polygon!;
let poly2 = polygon!;
let mp = MultiPolygon;
let wkt_out = mp.to_svg_string;
let expected = String from;
assert_eq!;
Similar projects
For a similar project that provides higher level functionality to build SVG's from Rust geo-types, see https://github.com/lelongg/geo-svg