pub trait ToWkt<T> where
T: WktNum + Display, {
fn to_wkt(&self) -> Wkt<T>;
fn wkt_string(&self) -> String { ... }
fn write_wkt(&self, writer: impl Write) -> Result<()> { ... }
}
Expand description
A trait for converting values to WKT
Required Methods
Provided Methods
fn wkt_string(&self) -> String
fn wkt_string(&self) -> String
Serialize as a WKT string
// This example requires the geo-types feature (on by default).
use wkt::ToWkt;
let point: geo_types::Point<f64> = geo_types::point!(x: 1.2, y: 3.4);
assert_eq!("POINT(1.2 3.4)", &point.wkt_string());
Write a WKT string to a File
, or anything else that implements Write
.
// This example requires the geo-types feature (on by default).
use wkt::ToWkt;
use std::fs::File;
let point: geo_types::Point<f64> = geo_types::point!(x: 1.2, y: 3.4);
// use a vec as a fake "file" for the purpose of example, but you could equally replace the
// following with:
// let mut file = File::create("myfile.wkt").unwrap();
let mut file = vec![] ;
point.write_wkt(&mut file).unwrap();
let wkt_string = String::from_utf8(file).unwrap();
assert_eq!(wkt_string, "POINT(1.2 3.4)");
Implementations on Foreign Types
sourceimpl<T> ToWkt<T> for Geometry<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for Geometry<T> where
T: CoordNum + Display,
Examples
use geo_types::{point, Geometry};
use wkt::ToWkt;
let geometry: Geometry<f64> = Geometry::Point(point!(x: 1., y: 2.));
assert_eq!(geometry.wkt_string(), "POINT(1 2)");
sourceimpl<T> ToWkt<T> for Point<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for Point<T> where
T: CoordNum + Display,
Examples
use geo_types::{point, Point};
use wkt::ToWkt;
let point: Point<f64> = point!(x: 1., y: 2.);
assert_eq!(point.wkt_string(), "POINT(1 2)");
sourceimpl<T> ToWkt<T> for Line<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for Line<T> where
T: CoordNum + Display,
Examples
use geo_types::{coord, Line};
use wkt::ToWkt;
let line = Line::<f64>::new(coord!(x: 1., y: 2.), coord!(x: 3., y: 4.));
assert_eq!(line.wkt_string(), "LINESTRING(1 2,3 4)");
sourceimpl<T> ToWkt<T> for LineString<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for LineString<T> where
T: CoordNum + Display,
Examples
use geo_types::{line_string, LineString};
use wkt::ToWkt;
let line_string: LineString<f64> = line_string![(x: 1., y: 2.), (x: 3., y: 4.), (x: 5., y: 6.)];
assert_eq!(line_string.wkt_string(), "LINESTRING(1 2,3 4,5 6)");
sourceimpl<T> ToWkt<T> for Polygon<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for Polygon<T> where
T: CoordNum + Display,
Examples
use geo_types::{polygon, Polygon};
use wkt::ToWkt;
let polygon: Polygon<f64> = polygon![(x: 0., y: 0.), (x: 4., y: 0.), (x: 2., y: 4.), (x: 0., y: 0.)];
assert_eq!(polygon.wkt_string(), "POLYGON((0 0,4 0,2 4,0 0))");
sourceimpl<T> ToWkt<T> for MultiPoint<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for MultiPoint<T> where
T: CoordNum + Display,
Examples
use geo_types::{point, MultiPoint};
use wkt::ToWkt;
let multi_point: MultiPoint<f64> = MultiPoint::new(vec![point!(x: 0., y: 0.), point!(x: 4., y: 0.), point!(x: 2., y: 4.)]);
assert_eq!(multi_point.wkt_string(), "MULTIPOINT((0 0),(4 0),(2 4))");
sourceimpl<T> ToWkt<T> for MultiLineString<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for MultiLineString<T> where
T: CoordNum + Display,
Examples
use geo_types::{line_string, LineString, MultiLineString};
use wkt::ToWkt;
let line_string_1: LineString<f64> = line_string![(x: 1., y: 2.), (x: 3., y: 4.), (x: 5., y: 6.)];
let line_string_2: LineString<f64> = line_string![(x: 7., y: 8.), (x: 9., y: 0.)];
let multi_line_string: MultiLineString<f64> = MultiLineString::new(vec![line_string_1, line_string_2]);
assert_eq!(multi_line_string.wkt_string(), "MULTILINESTRING((1 2,3 4,5 6),(7 8,9 0))");
sourceimpl<T> ToWkt<T> for MultiPolygon<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for MultiPolygon<T> where
T: CoordNum + Display,
Examples
use geo_types::{polygon, Polygon, MultiPolygon};
use wkt::ToWkt;
// triangle
let polygon_1: Polygon<f64> = polygon![(x: 0., y: 0.), (x: 4., y: 0.), (x: 2., y: 4.), (x: 0., y: 0.)];
// square
let polygon_2: Polygon<f64> = polygon![(x: 4., y: 4.), (x: 8., y: 4.), (x: 8., y: 8.), (x: 4., y: 8.), (x: 4., y: 4.)];
let multi_polygon: MultiPolygon<f64> = MultiPolygon::new(vec![polygon_1, polygon_2]);
assert_eq!(multi_polygon.wkt_string(), "MULTIPOLYGON(((0 0,4 0,2 4,0 0)),((4 4,8 4,8 8,4 8,4 4)))");
sourceimpl<T> ToWkt<T> for GeometryCollection<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for GeometryCollection<T> where
T: CoordNum + Display,
Examples
use geo_types::{line_string, LineString, polygon, Polygon, GeometryCollection};
use wkt::ToWkt;
let polygon: Polygon<f64> = polygon![(x: 0., y: 0.), (x: 4., y: 0.), (x: 2., y: 4.), (x: 0., y: 0.)];
let line_string: LineString<f64> = line_string![(x: 1., y: 2.), (x: 3., y: 4.), (x: 5., y: 6.)];
let geometry_collection: GeometryCollection<f64> = GeometryCollection::new_from(vec![polygon.into(), line_string.into()]);
assert_eq!(geometry_collection.wkt_string(), "GEOMETRYCOLLECTION(POLYGON((0 0,4 0,2 4,0 0)),LINESTRING(1 2,3 4,5 6))");
sourceimpl<T> ToWkt<T> for Rect<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for Rect<T> where
T: CoordNum + Display,
Examples
use geo_types::{coord, Rect};
use wkt::ToWkt;
let rect: Rect<f64> = Rect::new(coord!(x: 4., y: 4.), coord!(x: 8., y: 8.));
assert_eq!(rect.wkt_string(), "POLYGON((4 4,4 8,8 8,8 4,4 4))");
sourceimpl<T> ToWkt<T> for Triangle<T> where
T: CoordNum + Display,
impl<T> ToWkt<T> for Triangle<T> where
T: CoordNum + Display,
Examples
use geo_types::{coord, Triangle};
use wkt::ToWkt;
let triangle: Triangle<f64> = Triangle::new(coord!(x: 0., y: 0.), coord!(x: 4., y: 0.), coord!(x: 2., y: 4.));
assert_eq!(triangle.wkt_string(), "POLYGON((0 0,4 0,2 4,0 0))");