15 unstable releases (5 breaking)

Uses new Rust 2021

0.5.0 Aug 3, 2022
0.4.3 Apr 5, 2022
0.4.2 Mar 15, 2022
0.4.0 Dec 8, 2021
0.0.5 Dec 30, 2020

#25 in Science

Download history 134/week @ 2022-04-21 86/week @ 2022-04-28 326/week @ 2022-05-05 257/week @ 2022-05-12 123/week @ 2022-05-19 183/week @ 2022-05-26 192/week @ 2022-06-02 98/week @ 2022-06-09 109/week @ 2022-06-16 96/week @ 2022-06-23 59/week @ 2022-06-30 169/week @ 2022-07-07 178/week @ 2022-07-14 159/week @ 2022-07-21 124/week @ 2022-07-28 93/week @ 2022-08-04

619 downloads per month
Used in echomap

MIT/Apache

140KB
3.5K SLoC

kml

crates.io Build status Documentation

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

Examples

Reading

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

let kml_str = r#"
<Polygon>
  <outerBoundaryIs>
    <LinearRing>
    <tessellate>1</tessellate>
    <coordinates>
        -1,2,0
        -1.5,3,0
        -1.5,2,0
        -1,2,0
    </coordinates>
    </LinearRing>
  </outerBoundaryIs>
</Polygon>
"#;

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

// Read from a file path
let kml_path = Path::new(env!("CARGO_MANIFEST_DIR"))
    .join("tests")
    .join("fixtures")
    .join("polygon.kml");
let mut kml_reader = KmlReader::<_, f64>::from_path(kml_path).unwrap();
let kml_data = kml_reader.read().unwrap();

// Read KMZ files with the `zip` feature or default features enabled
let kmz_path = Path::new(env!("CARGO_MANIFEST_DIR"))
    .join("tests")
    .join("fixtures")
    .join("polygon.kmz");
let mut kmz_reader = KmlReader::<_, f64>::from_kmz_path(kmz_path).unwrap();
let kmz_data = kmz_reader.read().unwrap();

Writing

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

let kml = Kml::Point(Point::new(1., 1., None));

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

Conversion

use geo_types::{self, GeometryCollection};
use kml::{quick_collection, Kml, types::Point};

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

let kml_folder_str = r#"
<Folder>
  <Point>
    <coordinates>1,1,1</coordinates>
    <altitudeMode>relativeToGround</altitudeMode>
  </Point>
  <LineString>
    <coordinates>1,1 2,1 3,1</coordinates>
    <altitudeMode>relativeToGround</altitudeMode>
  </LineString>
</Folder>"#;
let kml_folder: Kml<f64> = kml_folder_str.parse().unwrap();

// Use the quick_collection helper to convert Kml to a geo_types::GeometryCollection
let geom_coll: GeometryCollection<f64> = quick_collection(kml_folder).unwrap();

Code of Conduct

All contributors are expected to follow the GeoRust Code of Conduct

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3.5MB
~59K SLoC