29 releases
| 0.14.0 | May 14, 2025 |
|---|---|
| 0.12.0 | Nov 27, 2024 |
| 0.11.0 | Jul 25, 2024 |
| 0.10.3 | Jun 22, 2022 |
| 0.0.3 | Feb 1, 2015 |
#6 in Geospatial
232,675 downloads per month
Used in 122 crates
(58 directly)
235KB
6K
SLoC
wkt
Rust read/write support for well-known text (WKT).
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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.
lib.rs:
The wkt crate provides conversions to and from the WKT (Well Known Text)
geometry format.
Conversions are available via the TryFromWkt and ToWkt traits, with implementations for
geo_types and geo_traits primitives enabled by default.
For advanced usage, see the types module for a list of internally used types.
This crate has optional serde integration for deserializing fields containing WKT. See
deserialize for an example.
Examples
Read geo_types from a WKT string
// This example requires the geo-types feature (on by default).
use wkt::TryFromWkt;
use geo_types::Point;
let point: Point<f64> = Point::try_from_wkt_str("POINT(10 20)").unwrap();
assert_eq!(point.y(), 20.0);
Write geo_types to a WKT string
// This example requires the geo-types feature (on by default).
use wkt::ToWkt;
use geo_types::Point;
let point: Point<f64> = Point::new(1.0, 2.0);
assert_eq!(point.wkt_string(), "POINT(1 2)");
Read or write your own geometry types
Not using geo-types for your geometries? No problem!
As of wkt version 0.12, this crate provides read and write integration with geo_traits,
a collection of geometry access traits, to provide zero-copy integration with geometry
representations other than geo-types.
This integration allows you to transparently read data from this crate's intermediate geometry structure, and it allows you to write WKT strings directly from your geometry without any intermediate representation.
Reading
You can use Wkt::from_str to parse a WKT string into this crate's intermediate geometry
structure. Wkt (and all structs defined in [types]) implement traits from [geo_traits]. You
can write functions in terms of those traits and you'll be able to work with the parsed WKT
without any further overhead.
use std::str::FromStr;
use wkt::Wkt;
use geo_traits::{GeometryTrait, GeometryType};
fn is_line_string(geom: &impl GeometryTrait<T = f64>) {
assert!(matches!(geom.as_type(), GeometryType::LineString(_)))
}
let wktls: Wkt<f64> = Wkt::from_str("LINESTRING(10 20, 20 30)").unwrap();
is_line_string(&wktls);
Working with the trait definition is preferable to working with wkt::Wkt directly, as the
geometry trait will work with many different geometry representations; not just the one from
this crate.
Writing
Consult the functions provided in to_wkt. Those functions will write any geo_traits object to WKT without any intermediate overhead.
Implement geo_traits on your own geometry representation and those functions will work out
of the box on your data.
Dependencies
~1.1–1.7MB
~37K SLoC