Skip to content

Commit

Permalink
Merge pull request #124 from georust/kyle/geo-traits-writer
Browse files Browse the repository at this point in the history
Implement geo-traits for writing to WKT & perf improvement
  • Loading branch information
kylebarron authored Nov 26, 2024
2 parents cdbff53 + 6ab501c commit 458a73f
Show file tree
Hide file tree
Showing 13 changed files with 558 additions and 193 deletions.
28 changes: 27 additions & 1 deletion benches/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,31 @@ fn geo_write_wkt(c: &mut criterion::Criterion) {
});
}

criterion_group!(benches, wkt_to_string, geo_to_wkt_string, geo_write_wkt);
fn geo_write_wkt_as_trait(c: &mut criterion::Criterion) {
c.bench_function("geo: write small wkt using trait", |bencher| {
let s = include_str!("./small.wkt");
let w = wkt::Wkt::<f64>::from_str(s).unwrap();
let g = geo_types::Geometry::try_from(w).unwrap();
bencher.iter(|| {
wkt::to_wkt::write_geometry(&mut String::new(), &g).unwrap();
});
});

c.bench_function("geo: write big wkt using trait", |bencher| {
let s = include_str!("./big.wkt");
let w = wkt::Wkt::<f64>::from_str(s).unwrap();
let g = geo_types::Geometry::try_from(w).unwrap();
bencher.iter(|| {
wkt::to_wkt::write_geometry(&mut String::new(), &g).unwrap();
});
});
}

criterion_group!(
benches,
wkt_to_string,
geo_to_wkt_string,
geo_write_wkt,
geo_write_wkt_as_trait
);
criterion_main!(benches);
24 changes: 24 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::fmt;

use thiserror::Error;

/// Generic errors for WKT writing and reading
#[derive(Error, Debug)]
pub enum Error {
#[error("Only 2D input is supported when writing Rect to WKT.")]
RectUnsupportedDimension,
#[error("Only defined dimensions and undefined dimensions of 2, 3, or 4 are supported.")]
UnknownDimension,
/// Wrapper around `[std::fmt::Error]`
#[error(transparent)]
FmtError(#[from] std::fmt::Error),
}

impl From<Error> for fmt::Error {
fn from(value: Error) -> Self {
match value {
Error::FmtError(err) => err,
_ => std::fmt::Error,
}
}
}
17 changes: 5 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ use geo_traits::{
};
use num_traits::{Float, Num, NumCast};

use crate::to_wkt::write_geometry;
use crate::tokenizer::{PeekableTokens, Token, Tokens};
use crate::types::{
Dimension, GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point,
Polygon,
};

mod to_wkt;
pub mod to_wkt;
mod tokenizer;

/// Error variant for this crate
pub mod error;
/// `WKT` primitive types and collections
pub mod types;

Expand All @@ -105,8 +108,6 @@ pub use infer_type::infer_type;
#[cfg(feature = "geo-types")]
extern crate geo_types;

extern crate thiserror;

pub use crate::to_wkt::ToWkt;

#[cfg(feature = "geo-types")]
Expand Down Expand Up @@ -359,15 +360,7 @@ where
T: WktNum + fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
Wkt::Point(point) => point.fmt(f),
Wkt::LineString(linestring) => linestring.fmt(f),
Wkt::Polygon(polygon) => polygon.fmt(f),
Wkt::MultiPoint(multipoint) => multipoint.fmt(f),
Wkt::MultiLineString(multilinstring) => multilinstring.fmt(f),
Wkt::MultiPolygon(multipolygon) => multipolygon.fmt(f),
Wkt::GeometryCollection(geometrycollection) => geometrycollection.fmt(f),
}
Ok(write_geometry(f, self)?)
}
}

Expand Down
Loading

0 comments on commit 458a73f

Please sign in to comment.