kml

crates.io Build status Documentation

Rust support for reading and writing KML with a focus on conversion to geo-types primitives.

Examples

Reading

```rust use std::path::Path; use kml::{Kml, KmlReader};

let kml_str = r#" 1 -1,2,0 -1.5,3,0 -1.5,2,0 -1,2,0 "#;

// Parse from a string let kml: Kml = kml_str.parse().unwrap();

// Read from a file path let kmlpath = Path::new(env!("CARGOMANIFESTDIR")) .join("tests") .join("fixtures") .join("polygon.kml"); let mut kmlreader = KmlReader::<_, f64>::fromfile(kmlpath).unwrap(); let kmldata = kmlreader.parse().unwrap();

// Read KMZ files with the zip feature or default features enabled let kmzpath = Path::new(env!("CARGOMANIFESTDIR")) .join("tests") .join("fixtures") .join("polygon.kmz"); let mut kmzreader = KmlReader::<_, f64>::fromkmzfile(kmzpath).unwrap(); let kmzdata = kmz_reader.parse().unwrap(); ```

Writing

```rust use std::str; use quick_xml; use kml::{Kml, KmlWriter, types::{AltitudeMode, Coord, Point}};

let kml = Kml::Point(Point { coord: Coord { x: 1., y: 1., z: Some(1.), }, ..Default::default() });

let mut buf = Vec::new(); let mut writer = KmlWriter::new(quick_xml::Writer::new(&mut buf)); writer.write(&kml).unwrap(); ```

Conversion

```rust use geotypes::{self, GeometryCollection}; use kml::{quickcollection, Kml, types::Point};

let kmlpoint = Point::new(1., 1., None); // Convert into geotypes primitives let geopoint = geotypes::Point::from(kmlpoint); // Convert back into kml::types structs let kmlpoint = Point::from(geo_point);

let kmlfolderstr = r#" 1,1,1 relativeToGround 1,1 2,1 3,1 relativeToGround "#; let kmlfolder: Kml = kmlfolder_str.parse().unwrap();

// Use the quickcollection helper to convert Kml to a geotypes::GeometryCollection let geomcoll: GeometryCollection = quickcollection(kml_folder).unwrap(); ```