[][src]Function geo_svg_io::geo_svg_reader::svg_to_geometry_collection

pub fn svg_to_geometry_collection(
    svg: &str
) -> Result<GeometryCollection<f64>, SvgError>

Returns a GeometryCollection parsed from the submitted SVG element

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

Note also that the current parsing of curves in a <path>is rather simple right now, it just finds 100 points along the curve.

Examples

Parsing a <path> element:

use geo_types::{ Polygon, polygon };
use geo_svg_io::geo_svg_reader::svg_to_geometry_collection;

let poly: Polygon<f64> = polygon!(
        exterior: [
            (x: 0.0_f64, y: 0.0),
            (x: 0.0, y: 60.0),
            (x: 60.0, y: 60.0),
            (x: 60.0, y: 0.0),
            (x: 0.0, y: 0.0),],
        interiors:[[
            (x: 10.0, y: 10.0),
            (x: 40.0, y: 1.0),
            (x: 40.0, y: 40.0),
            (x: 10.50, y: 40.0),
            (x: 10.0, y: 10.0),]
            ]
        )
        .into();
let svg_string =
            String::from(r#"<path d="M0 0L0 60L60 60L60 0L0 0M10 10L40 1L40 40L10.5 40L10 10"/>"#);

let parsed_svg = svg_to_geometry_collection(&svg_string);
assert_eq!(parsed_svg.is_ok(), true);

// Unwrap the GeometryCollection result
let geom = parsed_svg.ok().unwrap();
assert_eq!(1, geom.0.len());

// Read the geometry as a Polygon
let pl = geom.0[0].clone().into_polygon();
assert_eq!(true, pl.is_some());
assert_eq!(poly, pl.unwrap());

Parsing a <polygon> element:

use geo_types::{ Polygon, polygon };
use geo_svg_io::geo_svg_reader::svg_to_geometry_collection;

let poly: Polygon<f64> = polygon!(
        exterior: [
            (x: 0.0_f64, y: 0.0),
            (x: 0.0, y: 60.0),
            (x: 60.0, y: 60.0),
            (x: 60.0, y: 0.0),
            (x: 0.0, y: 0.0),],
        interiors:[]
        )
        .into();

let svg_string = String::from(r#"<polygon points="0, 0 60, 0 60, 60 0, 60 0, 0"/>"#);

let parsed_svg = svg_to_geometry_collection(&svg_string);
assert_eq!(parsed_svg.is_ok(), true);

// Unwrap the GeometryCollection result
let geom = parsed_svg.ok().unwrap();
assert_eq!(1, geom.0.len());

// Read the geometry as a Polygon
let pl = geom.0[0].clone().into_polygon();
assert_eq!(true, pl.is_some());
assert_eq!(poly, pl.unwrap());