27 releases

0.12.0 Nov 27, 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

#66 in Parser implementations

Download history 4960/week @ 2024-08-25 7433/week @ 2024-09-01 7013/week @ 2024-09-08 6648/week @ 2024-09-15 7480/week @ 2024-09-22 9404/week @ 2024-09-29 9081/week @ 2024-10-06 11295/week @ 2024-10-13 12739/week @ 2024-10-20 13006/week @ 2024-10-27 13820/week @ 2024-11-03 21005/week @ 2024-11-10 15315/week @ 2024-11-17 14438/week @ 2024-11-24 15149/week @ 2024-12-01 16589/week @ 2024-12-08

62,360 downloads per month
Used in 32 crates (23 directly)

MIT/Apache

190KB
4.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 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 = 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.6MB
~35K SLoC