Expand description
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.
Re-exports§
pub use crate::to_wkt::ToWkt;pub use deserialize::deserialize_wkt;serdepub use deserialize::geo_types::deserialize_geometry;Deprecated serdeandgeo-typespub use deserialize::geo_types::deserialize_point;serdeandgeo-types
Modules§
- conversion
Deprecated geo-types - deserialize
serde - This module deserialises to WKT using
serde. - error
- Error variant for this crate
- geo_
types_ from_ wkt geo-types - This module provides conversions between WKT primitives and
geo_typesprimitives. - to_wkt
- Serialize geometries to WKT strings.
- types
WKTprimitive types and collections
Macros§
Enums§
Traits§
- TryFrom
Wkt - Create geometries from WKT.
- WktFloat
- WktNum
Functions§
- infer_
type - Infer the geometry type and dimension from an input WKT string slice.