Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement geo-traits for writing to WKT & perf improvement #124

Merged
merged 22 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(&g, &mut String::new()).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(&g, &mut String::new()).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 core::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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: std::fmt here, but core::fmt at the top — settle on one.

Copy link
Member Author

@kylebarron kylebarron Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, oops. I don't actually know the difference between std and core. I changed to std everywhere. I hope that's ok

Copy link
Member

@michaelkirk michaelkirk Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core is a subset of std.

std re-exports all of core and then has some other fancier functionality. So referencing things from core or std is equivalent, it just makes me twitch a little to see both used interchangeably in such close proximity. 🤪

The distinction is typically only relevant if you are trying to target a no-std build for very lightweight platforms (like an embedded system) which might not have support for the std lib.

}
}
}
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(self, f)?)
}
}

Expand Down
Loading
Loading