kml/lib.rs
1//! # kml
2//!
3//! Rust support for reading and writing KML with a focus on conversion to [`geo-types`](https://github.com/georust/geo)
4//! primitives.
5//!
6//! ## Examples
7//!
8//! ### Reading
9//!
10//! ```
11//! use std::path::Path;
12//! use kml::{Kml, KmlReader};
13//!
14//! let kml_str = r#"
15//! <Polygon>
16//! <outerBoundaryIs>
17//! <LinearRing>
18//! <tessellate>1</tessellate>
19//! <coordinates>
20//! -1,2,0
21//! -1.5,3,0
22//! -1.5,2,0
23//! -1,2,0
24//! </coordinates>
25//! </LinearRing>
26//! </outerBoundaryIs>
27//! </Polygon>
28//! "#;
29//!
30//! // Parse from a string
31//! let kml: Kml = kml_str.parse().unwrap();
32//!
33//! // Read from a file path
34//! let kml_path = Path::new(env!("CARGO_MANIFEST_DIR"))
35//! .join("tests")
36//! .join("fixtures")
37//! .join("polygon.kml");
38//! let mut kml_reader = KmlReader::<_, f64>::from_path(kml_path).unwrap();
39//! let kml_data = kml_reader.read().unwrap();
40//!
41//! // Read KMZ files with the `zip` feature or default features enabled
42//! # #[cfg(feature = "zip")] {
43//! let kmz_path = Path::new(env!("CARGO_MANIFEST_DIR"))
44//! .join("tests")
45//! .join("fixtures")
46//! .join("polygon.kmz");
47//! let mut kmz_reader = KmlReader::<_, f64>::from_kmz_path(kmz_path).unwrap();
48//! let kmz_data = kmz_reader.read().unwrap();
49//! # }
50//! ```
51//!
52//! ### Writing
53//!
54//! ```
55//! use std::str;
56//! use kml::{Kml, KmlWriter, types::Point};
57//!
58//! let kml = Kml::Point(Point::new(1., 1., None));
59//!
60//! let mut buf = Vec::new();
61//! let mut writer = KmlWriter::from_writer(&mut buf);
62//! writer.write(&kml).unwrap();
63//! ```
64//!
65//! ### Conversion
66//!
67//! ```
68//! # #[cfg(feature = "geo-types")] {
69//! use geo_types::{self, GeometryCollection};
70//! use kml::{Kml, types::Point};
71//!
72//! let kml_point = Point::new(1., 1., None);
73//! // Convert into geo_types primitives
74//! let geo_point = geo_types::Point::from(kml_point);
75//! // Convert back into kml::types structs
76//! let kml_point = Point::from(geo_point);
77//!
78//! let kml_folder_str = r#"
79//! <Folder>
80//! <Point>
81//! <coordinates>1,1,1</coordinates>
82//! <altitudeMode>relativeToGround</altitudeMode>
83//! </Point>
84//! <LineString>
85//! <coordinates>1,1 2,1 3,1</coordinates>
86//! <altitudeMode>relativeToGround</altitudeMode>
87//! </LineString>
88//! </Folder>"#;
89//! let kml_folder: Kml<f64> = kml_folder_str.parse().unwrap();
90//!
91//! let geom_coll: GeometryCollection<f64> = kml_folder.try_into().unwrap();
92//! # }
93//! ```
94
95#![cfg_attr(docsrs, feature(doc_cfg))]
96
97pub mod types;
98
99pub use crate::types::{Kml, KmlDocument, KmlVersion};
100
101mod errors;
102pub use crate::errors::Error;
103
104pub mod reader;
105pub use crate::reader::KmlReader;
106
107pub mod writer;
108pub use crate::writer::KmlWriter;
109
110#[cfg(feature = "geo-types")]
111pub mod conversion;
112
113#[cfg(feature = "geo-types")]
114#[allow(deprecated)]
115pub use conversion::quick_collection;
116
117#[cfg(feature = "zip")]
118mod kmz_reader;
119
120#[allow(unused_imports)]
121#[cfg(feature = "zip")]
122pub use kmz_reader::*;