Skip to content

Commit

Permalink
Avoid copying string for WKT parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Jul 17, 2024
1 parent ab5486a commit 1ad7c69
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions geozero/src/wkt/wkt_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::{GeozeroError, Result};
use crate::{FeatureProcessor, GeomProcessor, GeozeroDatasource, GeozeroGeometry};

use std::io::Read;
use std::str::FromStr;
use wkt::types::{Coord, LineString, Polygon};
use wkt::Geometry;

Expand All @@ -11,7 +12,10 @@ pub struct Wkt<B: AsRef<[u8]>>(pub B);

impl<B: AsRef<[u8]>> GeozeroGeometry for Wkt<B> {
fn process_geom<P: GeomProcessor>(&self, processor: &mut P) -> Result<()> {
read_wkt(&mut self.0.as_ref(), processor)
let wkt_str = std::str::from_utf8(self.0.as_ref())
.map_err(|e| GeozeroError::Geometry(e.to_string()))?;
let wkt = wkt::Wkt::from_str(wkt_str).map_err(|e| GeozeroError::Geometry(e.to_string()))?;
process_wkt_geom(&wkt.item, processor)
}
}

Expand All @@ -24,7 +28,9 @@ pub struct WktString(pub String);
impl GeozeroGeometry for WktString {
fn process_geom<P: GeomProcessor>(&self, processor: &mut P) -> Result<()> {
#[allow(deprecated)]
read_wkt(&mut self.0.as_bytes(), processor)
let wkt = wkt::Wkt::from_str(self.0.as_str())
.map_err(|e| GeozeroError::Geometry(e.to_string()))?;
process_wkt_geom(&wkt.item, processor)
}
}

Expand All @@ -36,15 +42,17 @@ pub struct WktStr<'a>(pub &'a str);
impl GeozeroGeometry for WktStr<'_> {
fn process_geom<P: GeomProcessor>(&self, processor: &mut P) -> Result<()> {
#[allow(deprecated)]
read_wkt(&mut self.0.as_bytes(), processor)
let wkt = wkt::Wkt::from_str(self.0).map_err(|e| GeozeroError::Geometry(e.to_string()))?;
process_wkt_geom(&wkt.item, processor)
}
}

#[allow(deprecated)]
impl GeozeroDatasource for WktStr<'_> {
fn process<P: FeatureProcessor>(&mut self, processor: &mut P) -> Result<()> {
#[allow(deprecated)]
read_wkt(&mut self.0.as_bytes(), processor)
let wkt = wkt::Wkt::from_str(self.0).map_err(|e| GeozeroError::Geometry(e.to_string()))?;
process_wkt_geom(&wkt.item, processor)
}
}

Expand Down Expand Up @@ -72,7 +80,6 @@ impl<R: Read> GeozeroDatasource for WktReader<R> {

/// Read and process WKT geometry.
pub fn read_wkt<R: Read, P: GeomProcessor>(reader: &mut R, processor: &mut P) -> Result<()> {
use std::str::FromStr;
// PERF: it would be good to avoid copying data into this string when we already
// have a string as input. Maybe the wkt crate needs a from_reader implementation.
let mut wkt_string = String::new();
Expand Down

0 comments on commit 1ad7c69

Please sign in to comment.