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

Update for latest wkt #221

Merged
merged 6 commits into from
Aug 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ serde_json = "1.0.104"
sqlx = { version = "0.7", default-features = false }
thiserror = "1.0"
tokio = { version = "1.30.0", default-features = false }
wkt = "0.10.3"
wkt = "0.11"

[patch.crates-io]
geozero = { path = "./geozero" }
Expand Down
14 changes: 6 additions & 8 deletions geozero/src/csv/csv_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,12 @@ pub fn process_csv_geom(
processor.geometrycollection_begin(1, 0)?;
}

crate::wkt::wkt_reader::process_wkt_geom_n(&wkt.item, record_idx, processor).map_err(
|e| {
// +2 to start at line 1 and to account for the header row
let line = record_idx + 2;
log::warn!("line {line}: invalid WKT: '{geometry_field}', record: {record:?}");
e
},
)?;
crate::wkt::wkt_reader::process_wkt_geom_n(&wkt, record_idx, processor).map_err(|e| {
// +2 to start at line 1 and to account for the header row
let line = record_idx + 2;
log::warn!("line {line}: invalid WKT: '{geometry_field}', record: {record:?}");
e
})?;
}

if !collection_started {
Expand Down
29 changes: 14 additions & 15 deletions geozero/src/wkt/wkt_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{FeatureProcessor, GeomProcessor, GeozeroDatasource, GeozeroGeometry}
use std::io::Read;
use std::str::FromStr;
use wkt::types::{Coord, LineString, Polygon};
use wkt::Geometry;

/// A wrapper around a WKT String or String slice.
#[derive(Debug)]
Expand All @@ -15,7 +14,7 @@ impl<B: AsRef<[u8]>> GeozeroGeometry for Wkt<B> {
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)
process_wkt_geom(&wkt, processor)
}
}

Expand All @@ -30,7 +29,7 @@ impl GeozeroGeometry for WktString {
#[allow(deprecated)]
let wkt = wkt::Wkt::from_str(self.0.as_str())
.map_err(|e| GeozeroError::Geometry(e.to_string()))?;
process_wkt_geom(&wkt.item, processor)
process_wkt_geom(&wkt, processor)
}
}

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

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

Expand Down Expand Up @@ -85,22 +84,22 @@ pub fn read_wkt<R: Read, P: GeomProcessor>(reader: &mut R, processor: &mut P) ->
let mut wkt_string = String::new();
reader.read_to_string(&mut wkt_string)?;
let wkt = wkt::Wkt::from_str(&wkt_string).map_err(|e| GeozeroError::Geometry(e.to_string()))?;
process_wkt_geom(&wkt.item, processor)
process_wkt_geom(&wkt, processor)
}

/// Process WKT geometry
fn process_wkt_geom<P: GeomProcessor>(geometry: &Geometry<f64>, processor: &mut P) -> Result<()> {
fn process_wkt_geom<P: GeomProcessor>(geometry: &wkt::Wkt<f64>, processor: &mut P) -> Result<()> {
Copy link
Member

Choose a reason for hiding this comment

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

can we simplify all these with use wkt::Wkt ?

Copy link
Member

Choose a reason for hiding this comment

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

there are already other entities in the local module scope called Wkt. You could use an alias, but it's unlikely to be any more concise than wkt::Wkt.

process_wkt_geom_n(geometry, 0, processor)
}

pub(crate) fn process_wkt_geom_n<P: GeomProcessor>(
geometry: &Geometry<f64>,
geometry: &wkt::Wkt<f64>,
idx: usize,
processor: &mut P,
) -> Result<()> {
let multi_dim = processor.multi_dim();
match geometry {
Geometry::Point(g) => {
wkt::Wkt::Point(g) => {
if let Some(ref coord) = g.0 {
processor.point_begin(idx)?;
process_coord(coord, multi_dim, 0, processor)?;
Expand All @@ -109,7 +108,7 @@ pub(crate) fn process_wkt_geom_n<P: GeomProcessor>(
processor.empty_point(idx)
}
}
Geometry::MultiPoint(g) => {
wkt::Wkt::MultiPoint(g) => {
nyurik marked this conversation as resolved.
Show resolved Hide resolved
processor.multipoint_begin(g.0.len(), idx)?;
let multi_dim1 = processor.multi_dim();
for (idxc, point) in g.0.iter().enumerate() {
Expand All @@ -123,23 +122,23 @@ pub(crate) fn process_wkt_geom_n<P: GeomProcessor>(
}
processor.multipoint_end(idx)
}
Geometry::LineString(g) => process_linestring(g, true, idx, processor),
Geometry::MultiLineString(g) => {
wkt::Wkt::LineString(g) => process_linestring(g, true, idx, processor),
wkt::Wkt::MultiLineString(g) => {
processor.multilinestring_begin(g.0.len(), idx)?;
for (idxc, linestring) in g.0.iter().enumerate() {
process_linestring(linestring, false, idxc, processor)?;
}
processor.multilinestring_end(idx)
}
Geometry::Polygon(g) => process_polygon(g, true, idx, processor),
Geometry::MultiPolygon(g) => {
wkt::Wkt::Polygon(g) => process_polygon(g, true, idx, processor),
wkt::Wkt::MultiPolygon(g) => {
processor.multipolygon_begin(g.0.len(), idx)?;
for (idx2, polygon) in g.0.iter().enumerate() {
process_polygon(polygon, false, idx2, processor)?;
}
processor.multipolygon_end(idx)
}
Geometry::GeometryCollection(g) => {
wkt::Wkt::GeometryCollection(g) => {
processor.geometrycollection_begin(g.0.len(), idx)?;
for (idx2, geometry) in g.0.iter().enumerate() {
process_wkt_geom_n(geometry, idx2, processor)?;
Expand Down
Loading