26 releases

0.11.1 Oct 10, 2024
0.11.0 Jul 25, 2024
0.10.3 Jun 22, 2022
0.10.0 Feb 25, 2022
0.0.3 Feb 1, 2015

#67 in Parser implementations

Download history 7345/week @ 2024-07-30 8656/week @ 2024-08-06 8321/week @ 2024-08-13 5742/week @ 2024-08-20 5134/week @ 2024-08-27 7914/week @ 2024-09-03 7022/week @ 2024-09-10 6399/week @ 2024-09-17 7996/week @ 2024-09-24 9717/week @ 2024-10-01 9242/week @ 2024-10-08 12256/week @ 2024-10-15 12163/week @ 2024-10-22 13152/week @ 2024-10-29 16327/week @ 2024-11-05 16909/week @ 2024-11-12

60,575 downloads per month
Used in 30 crates (21 directly)

MIT/Apache

185KB
3.5K SLoC

wkt

Rust read/write support for well-known text (WKT).

Crate API Documentation

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.


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 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 = 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!

You can use Wkt::from_str to parse a WKT string into this crate's intermediate geometry structure. You can use that directly, or if have your own geometry types that you'd prefer to use, utilize that [Wkt] struct to implement the ToWkt or TryFromWkt traits for your own types.

In doing so, you'll likely want to match on one of the WKT types (Point, Linestring, etc.) stored in its item field

use std::str::FromStr;
use wkt::Wkt;

let wktls: Wkt<f64> = Wkt::from_str("LINESTRING(10 20, 20 30)").unwrap();
let ls = match wktls {
    Wkt::LineString(line_string) => {
        // you now have access to the `wkt::types::LineString`.
        assert_eq!(line_string.0[0].x, 10.0);
    }
    _ => unreachable!(),
};

Dependencies

~1–1.6MB
~34K SLoC