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

Adds Ewkt dialect #155

Merged
merged 15 commits into from
Aug 12, 2023
4 changes: 2 additions & 2 deletions geozero-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use geozero::csv::{CsvReader, CsvWriter};
use geozero::error::Result;
use geozero::geojson::{GeoJsonReader, GeoJsonWriter};
use geozero::svg::SvgWriter;
use geozero::wkt::{WktReader, WktWriter};
use geozero::wkt::{WktDialect, WktReader, WktWriter};
use geozero::{FeatureProcessor, GeozeroDatasource};
use std::ffi::OsStr;
use std::fs::File;
Expand Down Expand Up @@ -87,7 +87,7 @@ fn process(args: Cli) -> Result<()> {
let mut fout = BufWriter::new(File::create(&args.dest)?);
match args.dest.extension().and_then(OsStr::to_str) {
Some("csv") => transform(args, &mut CsvWriter::new(&mut fout)),
Some("wkt") => transform(args, &mut WktWriter::new(&mut fout)),
Some("wkt") => transform(args, &mut WktWriter::new(&mut fout, WktDialect::Wkt)),
Some("json") | Some("geojson") => transform(args, &mut GeoJsonWriter::new(&mut fout)),
Some("fgb") => {
let mut fgb = FgbWriter::create("fgb", GeometryType::Unknown)?;
Expand Down
3 changes: 2 additions & 1 deletion geozero/src/csv/csv_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ impl<W: Write> GeomProcessor for CsvWriter<'_, W> {
// incorporated in the output of the CSV writer. Is there a better way?
mod buffering_wkt_writer {
use crate::error::Result;
use crate::wkt::WktDialect;
use crate::{wkt::WktWriter, CoordDimensions, GeomProcessor};

#[derive(Default)]
Expand All @@ -271,7 +272,7 @@ mod buffering_wkt_writer {
}

fn wkt_writer(&mut self) -> WktWriter<'_, Vec<u8>> {
WktWriter::new(&mut self.buffer)
WktWriter::new(&mut self.buffer, WktDialect::Wkt)
}
}

Expand Down
15 changes: 3 additions & 12 deletions geozero/src/gdal/gdal_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn process_polygon<P: GeomProcessor>(
#[cfg(feature = "with-wkt")]
mod test {
use super::*;
use crate::wkt::WktWriter;
use crate::wkt::{WktDialect, WktWriter};
use crate::{CoordDimensions, ToWkt};

#[test]
Expand All @@ -160,7 +160,7 @@ mod test {
let geo = Geometry::from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&geo, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&geo, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand All @@ -183,16 +183,7 @@ mod test {
fn line_3d() {
let wkt = "LINESTRING(1 1 10,2 2 20)";
let geo = Geometry::from_wkt(wkt).unwrap();
assert_eq!(
geo.to_wkt_ndim(CoordDimensions {
z: true,
m: false,
t: false,
tm: false
})
.unwrap(),
wkt
);
assert_eq!(geo.to_ewkt(None).unwrap(), wkt);
Oreilles marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions geozero/src/geo_types/geo_types_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn process_polygon<P: GeomProcessor>(
#[cfg(feature = "with-wkt")]
mod test {
use super::*;
use crate::wkt::WktWriter;
use crate::wkt::{WktDialect, WktWriter};
use crate::ToWkt;
use std::convert::TryFrom;
use std::str::FromStr;
Expand All @@ -127,7 +127,7 @@ mod test {
let geo = Geometry::try_from(wkt::Wkt::from_str(wkt).unwrap()).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&geo, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&geo, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand Down
24 changes: 15 additions & 9 deletions geozero/src/geojson/geojson_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ fn process_polygon<P: GeomProcessor>(
mod test {
use super::*;
use crate::geojson::GeoJsonWriter;
use crate::wkt::WktWriter;
use crate::wkt::{WktDialect, WktWriter};
use crate::{ProcessToSvg, ToJson, ToWkt};
use std::fs::File;

Expand All @@ -293,9 +293,11 @@ mod test {
[1875038.447610231,-3269648.6879248763],[1874359.641504197,-3270196.812984864],[1874141.0428635243,-3270953.7840121365],[1874440.1778162003,-3271619.4315206874],[1876396.0598222911,-3274138.747656357],[1876442.0805243007,-3275052.60551469],[1874739.312657555,-3275457.333765534]
]}"#;
let mut wkt_data: Vec<u8> = Vec::new();
assert!(
read_geojson_geom(&mut geojson.as_bytes(), &mut WktWriter::new(&mut wkt_data)).is_ok()
);
assert!(read_geojson_geom(
&mut geojson.as_bytes(),
&mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)
)
.is_ok());
let wkt = std::str::from_utf8(&wkt_data).unwrap();
assert_eq!(wkt, "LINESTRING(1875038.447610231 -3269648.6879248763,1874359.641504197 -3270196.812984864,1874141.0428635243 -3270953.7840121365,1874440.1778162003 -3271619.4315206874,1876396.0598222911 -3274138.747656357,1876442.0805243007 -3275052.60551469,1874739.312657555 -3275457.333765534)"
);
Expand All @@ -306,15 +308,15 @@ mod test {
fn geometries3d() -> Result<()> {
let geojson = r#"{"type": "LineString", "coordinates": [[1,1,10],[2,2,20]]}"#;
let mut wkt_data: Vec<u8> = Vec::new();
let mut out = WktWriter::new(&mut wkt_data);
let mut out = WktWriter::new(&mut wkt_data, WktDialect::Wkt);
out.dims.z = true;
assert!(read_geojson_geom(&mut geojson.as_bytes(), &mut out).is_ok());
let wkt = std::str::from_utf8(&wkt_data).unwrap();
assert_eq!(wkt, "LINESTRING(1 1 10,2 2 20)");

let geojson = r#"{"type": "LineString", "coordinates": [[1,1],[2,2]]}"#;
let mut wkt_data: Vec<u8> = Vec::new();
let mut out = WktWriter::new(&mut wkt_data);
let mut out = WktWriter::new(&mut wkt_data, WktDialect::Wkt);
out.dims.z = true;
assert!(read_geojson_geom(&mut geojson.as_bytes(), &mut out).is_ok());
let wkt = std::str::from_utf8(&wkt_data).unwrap();
Expand Down Expand Up @@ -345,7 +347,11 @@ mod test {
}]
}"#;
let mut wkt_data: Vec<u8> = Vec::new();
assert!(read_geojson_fc(geojson.as_bytes(), &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(read_geojson_fc(
geojson.as_bytes(),
&mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)
)
.is_ok());
let wkt = std::str::from_utf8(&wkt_data).unwrap();
assert_eq!(wkt, "MULTIPOLYGON(((173.020375 -40.919052,173.247234 -41.331999,173.958405 -40.926701,174.247587 -41.349155,174.248517 -41.770008,173.876447 -42.233184,173.22274 -42.970038,172.711246 -43.372288,173.080113 -43.853344,172.308584 -43.865694,171.452925 -44.242519,171.185138 -44.897104,170.616697 -45.908929,169.831422 -46.355775,169.332331 -46.641235,168.411354 -46.619945,167.763745 -46.290197,166.676886 -46.219917,166.509144 -45.852705,167.046424 -45.110941,168.303763 -44.123973,168.949409 -43.935819,169.667815 -43.555326,170.52492 -43.031688,171.12509 -42.512754,171.569714 -41.767424,171.948709 -41.514417,172.097227 -40.956104,172.79858 -40.493962,173.020375 -40.919052)),((174.612009 -36.156397,175.336616 -37.209098,175.357596 -36.526194,175.808887 -36.798942,175.95849 -37.555382,176.763195 -37.881253,177.438813 -37.961248,178.010354 -37.579825,178.517094 -37.695373,178.274731 -38.582813,177.97046 -39.166343,177.206993 -39.145776,176.939981 -39.449736,177.032946 -39.879943,176.885824 -40.065978,176.508017 -40.604808,176.01244 -41.289624,175.239567 -41.688308,175.067898 -41.425895,174.650973 -41.281821,175.22763 -40.459236,174.900157 -39.908933,173.824047 -39.508854,173.852262 -39.146602,174.574802 -38.797683,174.743474 -38.027808,174.697017 -37.381129,174.292028 -36.711092,174.319004 -36.534824,173.840997 -36.121981,173.054171 -35.237125,172.636005 -34.529107,173.007042 -34.450662,173.551298 -35.006183,174.32939 -35.265496,174.612009 -36.156397)))");
Ok(())
Expand Down Expand Up @@ -385,7 +391,7 @@ mod test {
fn from_file() -> Result<()> {
let f = File::open("tests/data/places.json")?;
let mut wkt_data: Vec<u8> = Vec::new();
assert!(read_geojson(f, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(read_geojson(f, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());
let wkt = std::str::from_utf8(&wkt_data).unwrap();
assert_eq!(
&wkt[0..100],
Expand All @@ -402,7 +408,7 @@ mod test {
fn from_file_fc() -> Result<()> {
let f = File::open("tests/data/places.json")?;
let mut wkt_data: Vec<u8> = Vec::new();
assert!(read_geojson_fc(f, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(read_geojson_fc(f, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());
let wkt = std::str::from_utf8(&wkt_data).unwrap();
assert_eq!(
&wkt[0..100],
Expand Down
20 changes: 10 additions & 10 deletions geozero/src/geos/geos_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ fn process_polygon<'a, P: GeomProcessor, G: Geom<'a>>(
#[cfg(feature = "with-wkt")]
mod test {
use super::*;
use crate::wkt::WktWriter;
use crate::wkt::{WktDialect, WktWriter};

#[test]
fn point_geom() {
let wkt = "POINT(1 1)";
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand All @@ -193,7 +193,7 @@ mod test {
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand All @@ -204,7 +204,7 @@ mod test {
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand All @@ -215,7 +215,7 @@ mod test {
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
let mut writer = WktWriter::new(&mut wkt_data);
let mut writer = WktWriter::new(&mut wkt_data, WktDialect::Wkt);
writer.dims.z = true;
assert!(process_geom(&ggeom, &mut writer).is_ok());

Expand All @@ -228,7 +228,7 @@ mod test {
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(
std::str::from_utf8(&wkt_data).unwrap(),
Expand All @@ -242,7 +242,7 @@ mod test {
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand All @@ -253,7 +253,7 @@ mod test {
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand All @@ -264,7 +264,7 @@ mod test {
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand All @@ -275,7 +275,7 @@ mod test {
let ggeom = GGeometry::new_from_wkt(wkt).unwrap();

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_geom(&ggeom, &mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)).is_ok());

assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), wkt);
}
Expand Down
37 changes: 27 additions & 10 deletions geozero/src/wkb/wkb_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ impl GeozeroGeometry for GpkgWkb {
/// Process WKB geometry.
pub fn process_wkb_geom<R: Read, P: GeomProcessor>(raw: &mut R, processor: &mut P) -> Result<()> {
let info = read_wkb_header(raw)?;
processor.srid(info.srid)?;
process_wkb_geom_n(raw, &info, read_wkb_header, 0, processor)
}

/// Process EWKB geometry.
pub fn process_ewkb_geom<R: Read, P: GeomProcessor>(raw: &mut R, processor: &mut P) -> Result<()> {
let info = read_ewkb_header(raw)?;
processor.srid(info.srid)?;
process_wkb_geom_n(raw, &info, read_ewkb_header, 0, processor)
}

/// Process GPKG geometry.
pub fn process_gpkg_geom<R: Read, P: GeomProcessor>(raw: &mut R, processor: &mut P) -> Result<()> {
let info = read_gpkg_header(raw)?;
processor.srid(info.srid)?;
process_wkb_geom_n(raw, &info, read_wkb_header, 0, processor)
}

Expand Down Expand Up @@ -440,7 +443,7 @@ fn process_curvepolygon<R: Read, P: GeomProcessor>(
#[cfg(feature = "with-wkt")]
mod test {
use super::*;
use crate::wkt::WktWriter;
use crate::wkt::{WktDialect, WktWriter};
use crate::ToWkt;

#[test]
Expand All @@ -458,14 +461,16 @@ mod test {

// Process xy only
let mut wkt_data: Vec<u8> = Vec::new();
assert!(
process_ewkb_geom(&mut ewkb.as_slice(), &mut WktWriter::new(&mut wkt_data)).is_ok()
);
assert!(process_ewkb_geom(
&mut ewkb.as_slice(),
&mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)
)
.is_ok());
assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), "POINT(10 -20)");

// Process all dimensions
let mut wkt_data: Vec<u8> = Vec::new();
let mut writer = WktWriter::new(&mut wkt_data);
let mut writer = WktWriter::new(&mut wkt_data, WktDialect::Wkt);
writer.dims.z = true;
writer.dims.m = true;
assert!(process_ewkb_geom(&mut ewkb.as_slice(), &mut writer).is_ok());
Expand All @@ -484,7 +489,7 @@ mod test {
assert!(info.has_z);

let mut wkt_data: Vec<u8> = Vec::new();
let mut writer = WktWriter::new(&mut wkt_data);
let mut writer = WktWriter::new(&mut wkt_data, WktDialect::Wkt);
writer.dims.z = true;
assert!(process_ewkb_geom(&mut ewkb.as_slice(), &mut writer).is_ok());
assert_eq!(
Expand Down Expand Up @@ -595,7 +600,7 @@ mod test {
fn ewkb_to_wkt(ewkb_str: &str, with_z: bool) -> String {
let ewkb = hex::decode(ewkb_str).unwrap();
let mut wkt_data: Vec<u8> = Vec::new();
let mut writer = WktWriter::new(&mut wkt_data);
let mut writer = WktWriter::new(&mut wkt_data, WktDialect::Wkt);
writer.dims.z = with_z;
assert_eq!(
process_ewkb_geom(&mut ewkb.as_slice(), &mut writer).map_err(|e| e.to_string()),
Expand All @@ -615,7 +620,11 @@ mod test {
assert_eq!(info.srid, Some(4326));

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_gpkg_geom(&mut wkb.as_slice(), &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_gpkg_geom(
&mut wkb.as_slice(),
&mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)
)
.is_ok());
assert_eq!(std::str::from_utf8(&wkt_data).unwrap(), "POINT(1.1 1.1)");

// mln3dzm
Expand All @@ -627,7 +636,11 @@ mod test {
assert_eq!(info.envelope, vec![10.0, 20.0, 10.0, 20.0]);

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_gpkg_geom(&mut wkb.as_slice(), &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_gpkg_geom(
&mut wkb.as_slice(),
&mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)
)
.is_ok());
assert_eq!(
std::str::from_utf8(&wkt_data).unwrap(),
"MULTILINESTRING((20 10,10 20))"
Expand All @@ -640,7 +653,11 @@ mod test {
assert_eq!(info.envelope, vec![1.0, 22.0, 3.0, 22.0]);

let mut wkt_data: Vec<u8> = Vec::new();
assert!(process_gpkg_geom(&mut wkb.as_slice(), &mut WktWriter::new(&mut wkt_data)).is_ok());
assert!(process_gpkg_geom(
&mut wkb.as_slice(),
&mut WktWriter::new(&mut wkt_data, WktDialect::Wkt)
)
.is_ok());
assert_eq!(
std::str::from_utf8(&wkt_data).unwrap(),
"GEOMETRYCOLLECTION(POINT(1 3),POLYGON((21 21,22 21,21 22,21 21)))"
Expand Down
Loading
Loading