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

Pass formatter parameters to WKT writer #200

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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: 20 additions & 8 deletions geozero/src/wkt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,32 @@ pub(crate) mod conversion {
dialect: WktDialect,
dims: CoordDimensions,
srid: Option<i32>,
precision: Option<usize>,
) -> Result<String>;
}

impl<T: GeozeroGeometry> ToWkt for T {
fn to_wkt(&self) -> Result<String> {
self.to_wkt_with_opts(WktDialect::Wkt, CoordDimensions::default(), None)
self.to_wkt_with_opts(WktDialect::Wkt, CoordDimensions::default(), None, None)
}

fn to_ewkt(&self, srid: Option<i32>) -> Result<String> {
self.to_wkt_with_opts(WktDialect::Ewkt, CoordDimensions::xyzm(), srid)
self.to_wkt_with_opts(WktDialect::Ewkt, CoordDimensions::xyzm(), srid, None)
}

fn to_wkt_ndim(&self, dims: CoordDimensions) -> Result<String> {
self.to_wkt_with_opts(WktDialect::Wkt, dims, None)
self.to_wkt_with_opts(WktDialect::Wkt, dims, None, None)
}

fn to_wkt_with_opts(
&self,
dialect: WktDialect,
dims: CoordDimensions,
srid: Option<i32>,
precision: Option<usize>,
) -> Result<String> {
let mut out: Vec<u8> = Vec::new();
let mut writer = WktWriter::with_opts(&mut out, dialect, dims, srid);
let mut writer = WktWriter::with_opts(&mut out, dialect, dims, srid, precision);
self.process_geom(&mut writer)?;
String::from_utf8(out).map_err(|_| {
crate::error::GeozeroError::Geometry("Invalid UTF-8 encoding".to_string())
Expand Down Expand Up @@ -82,8 +84,13 @@ mod wkb {
impl FromWkb for Ewkt<String> {
fn from_wkb<R: Read>(rdr: &mut R, dialect: WkbDialect) -> Result<Self> {
let mut out: Vec<u8> = Vec::new();
let mut writer =
WktWriter::with_opts(&mut out, WktDialect::Ewkt, CoordDimensions::xyzm(), None);
let mut writer = WktWriter::with_opts(
&mut out,
WktDialect::Ewkt,
CoordDimensions::xyzm(),
None,
None,
);
crate::wkb::process_wkb_type_geom(rdr, &mut writer, dialect)?;
let wkt = String::from_utf8(out).map_err(|_| {
crate::error::GeozeroError::Geometry("Invalid UTF-8 encoding".to_string())
Expand All @@ -110,8 +117,13 @@ mod wkb {
impl FromWkb for EwktString {
fn from_wkb<R: Read>(rdr: &mut R, dialect: WkbDialect) -> Result<Self> {
let mut out: Vec<u8> = Vec::new();
let mut writer =
WktWriter::with_opts(&mut out, WktDialect::Ewkt, CoordDimensions::xyzm(), None);
let mut writer = WktWriter::with_opts(
&mut out,
WktDialect::Ewkt,
CoordDimensions::xyzm(),
None,
None,
);
crate::wkb::process_wkb_type_geom(rdr, &mut writer, dialect)?;
let wkt = String::from_utf8(out).map_err(|_| {
crate::error::GeozeroError::Geometry("Invalid UTF-8 encoding".to_string())
Expand Down
15 changes: 12 additions & 3 deletions geozero/src/wkt/wkt_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,33 @@ pub struct WktWriter<W: Write> {
first_header: bool,
/// Stack of in-progress geometry sizes
geometry_sizes: Vec<usize>,
precision: Option<usize>,
pub(crate) out: W,
}

impl<W: Write> WktWriter<W> {
pub fn new(out: W) -> Self {
Self::with_opts(out, WktDialect::Wkt, CoordDimensions::default(), None)
Self::with_opts(out, WktDialect::Wkt, CoordDimensions::default(), None, None)
}

pub fn with_dims(out: W, dims: CoordDimensions) -> Self {
Self::with_opts(out, WktDialect::Wkt, dims, None)
Self::with_opts(out, WktDialect::Wkt, dims, None, None)
}

pub fn with_opts(
out: W,
dialect: WktDialect,
dims: CoordDimensions,
srid: Option<i32>,
precision: Option<usize>,
) -> Self {
Self {
dims,
srid,
dialect,
first_header: true,
geometry_sizes: vec![],
precision,
out,
}
}
Expand Down Expand Up @@ -101,7 +104,13 @@ impl<W: Write> GeomProcessor for WktWriter<W> {
if f64::is_nan(x) && f64::is_nan(y) {
self.out.write_all(b"EMPTY")?;
} else {
self.out.write_all(format!("{x} {y}").as_bytes())?;
let coord_string = if let Some(precision) = self.precision {
format!("{x:.precision$} {y:.precision$}")
} else {
format!("{x} {y}")
};

self.out.write_all(coord_string.as_bytes())?;
}
Ok(())
}
Expand Down
Loading