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

GeoArrow WKB reader #39

Merged
merged 5 commits into from
Jul 20, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
**/target
**/Cargo.lock
2 changes: 2 additions & 0 deletions geozero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ keywords = ["geo", "geojson", "gdal", "geos", "postgis"]

[features]
default = ["with-svg", "with-wkt", "with-geo", "with-geojson"]
with-arrow = ["arrow2"]
with-csv = ["csv", "with-wkt"]
with-svg = []
with-wkt = ["wkt"]
Expand Down Expand Up @@ -43,6 +44,7 @@ postgres-types = { version = "0.2", optional = true }
bytes = { version = "1.0", optional = true }
prost = { version = "0.10.0", optional = true }
wkt = { version = "0.10.0", optional = true }
arrow2 = { version = "0.12", optional = true, features = ["io_ipc"]}

[dev-dependencies]
seek_bufread = "1.2"
Expand Down
64 changes: 64 additions & 0 deletions geozero/src/arrow/geoarrow_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::error::Result;
use crate::wkb::wkb_reader::{process_wkb_geom_n, read_wkb_header};
use crate::{GeomProcessor, GeozeroGeometry};
use arrow2::array::{BinaryArray, Offset};

impl GeozeroGeometry for BinaryArray<i32> {
fn process_geom<P: GeomProcessor>(&self, processor: &mut P) -> Result<()> {
process_geoarrow_wkb_geom(self, processor)
}
}

impl GeozeroGeometry for BinaryArray<i64> {
fn process_geom<P: GeomProcessor>(&self, processor: &mut P) -> Result<()> {
process_geoarrow_wkb_geom(self, processor)
}
}

pub fn process_geoarrow_wkb_geom<T: Offset>(
array: &BinaryArray<T>,
processor: &mut impl GeomProcessor,
) -> Result<()> {
let array_len = array.len();
processor.geometrycollection_begin(array_len, 0)?;

for i in 0..array_len {
let raw = &mut array.value(i);
let info = read_wkb_header(raw)?;
process_wkb_geom_n(raw, &info, read_wkb_header, i, processor)?;
}

processor.geometrycollection_end(array_len - 1)?;

Ok(())
}

#[cfg(test)]
mod test {
use super::*;
use crate::wkt::conversion::ToWkt;
use arrow2::io::ipc::read;
use std::fs::File;

#[test]
fn multipoly_file() -> arrow2::error::Result<()> {
let mut file = File::open("tests/data/countries.arrow")?;
let metadata = read::read_file_metadata(&mut file)?;
let mut reader = read::FileReader::new(file, metadata, None);

let columns = reader.next().unwrap()?;

let array = &columns.arrays()[2];
let wkbarr = array.as_any().downcast_ref::<BinaryArray<i32>>().unwrap();
let wkt = wkbarr.to_wkt().unwrap();
assert_eq!(
&wkt[0..100],
"GEOMETRYCOLLECTION(MULTIPOLYGON(((-59.572095 -80.040179,-59.865849 -80.549657,-60.159656 -81.000327,"
);
assert_eq!(
&wkt[wkt.len()-100..],
"-51.5,-58.55 -51.1,-57.75 -51.55,-58.05 -51.9,-59.4 -52.2,-59.85 -51.85,-60.7 -52.3,-61.2 -51.85))))"
);
Ok(())
}
}
5 changes: 5 additions & 0 deletions geozero/src/arrow/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! GeoArrow conversions.
//!
pub(crate) mod geoarrow_reader;

pub use geoarrow_reader::*;
3 changes: 3 additions & 0 deletions geozero/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub use geometry_processor::*;
pub use multiplex::*;
pub use property_processor::*;

#[cfg(feature = "with-arrow")]
pub mod arrow;

#[cfg(feature = "with-csv")]
pub mod csv;
#[cfg(feature = "with-csv")]
Expand Down
8 changes: 4 additions & 4 deletions geozero/src/wkb/wkb_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl GeozeroGeometry for Ewkb {
}
}

/// GepPackage WKB reader.
/// GeoPackage WKB reader.
pub struct GpkgWkb(pub Vec<u8>);

impl GeozeroGeometry for GpkgWkb {
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn process_wkb_type_geom<R: Read, P: GeomProcessor>(
}

#[derive(Debug)]
struct WkbInfo {
pub(crate) struct WkbInfo {
endian: scroll::Endian,
base_type: WKBGeometryType,
has_z: bool,
Expand All @@ -75,7 +75,7 @@ struct WkbInfo {
}

/// OGC WKB header.
fn read_wkb_header<R: Read>(raw: &mut R) -> Result<WkbInfo> {
pub(crate) fn read_wkb_header<R: Read>(raw: &mut R) -> Result<WkbInfo> {
let byte_order = raw.ioread::<u8>()?;
let endian = if byte_order == WKBByteOrder::XDR as u8 {
scroll::BE
Expand Down Expand Up @@ -177,7 +177,7 @@ fn read_gpkg_header<R: Read>(raw: &mut R) -> Result<WkbInfo> {

// TODO: Spatialite https://www.gaia-gis.it/gaia-sins/BLOB-Geometry.html

fn process_wkb_geom_n<R: Read, P: GeomProcessor>(
pub(crate) fn process_wkb_geom_n<R: Read, P: GeomProcessor>(
raw: &mut R,
info: &WkbInfo,
read_header: fn(&mut R) -> Result<WkbInfo>,
Expand Down
Binary file added geozero/tests/data/countries.arrow
Binary file not shown.