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

wip: proj transform #22

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
3 changes: 3 additions & 0 deletions geozero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ with-postgis-sqlx = ["with-wkb", "sqlx/postgres"]
with-postgis-postgres = ["with-wkb", "postgres-types", "bytes"]
with-mvt = ["prost"]
with-tessellator = ["lyon"]
with-proj = ["proj", "delegate"]

[dependencies]
thiserror = "1.0"
Expand All @@ -40,6 +41,8 @@ postgres-types = { version = "0.2", optional = true }
bytes = { version = "1.1", optional = true }
prost = { version = "0.9.0", optional = true }
wkt = { version = "0.10.0", optional = true }
proj = { version = "0.25.2", optional = true }
delegate = { version = "0.6.2", optional = true }

[dev-dependencies]
seek_bufread = "1.2"
Expand Down
4 changes: 4 additions & 0 deletions geozero/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod error;
mod feature_processor;
mod geometry_processor;
mod multiplex;
mod proj;
mod property_processor;

pub use api::*;
Expand Down Expand Up @@ -72,6 +73,9 @@ pub mod svg;
#[cfg(feature = "with-svg")]
pub use crate::svg::conversion::*;

#[cfg(feature = "with-proj")]
pub use crate::proj::ToProjected;

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

Expand Down
139 changes: 139 additions & 0 deletions geozero/src/proj.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use crate::error::Result;
use crate::{
ColumnValue, CoordDimensions, FeatureProcessor, GeomProcessor, GeozeroDatasource,
PropertyProcessor,
};

use delegate::delegate;
use proj::Proj;

use std::rc::Rc;

/// ```
/// use proj::Proj;
/// use geozero::{ToProjected, ProcessToJson, wkt::WktStr};
///
/// // Convert from [NAD 83 US Survey Feet](https://epsg.io/2230) to [NAD 83 Meters](https://epsg.io/26946) Using EPSG Codes
/// let from = "EPSG:2230";
/// let to = "EPSG:26946";
/// let proj = Proj::new_known_crs(from, to, None).unwrap();
///
/// let wkt = WktStr("POINT (4760096.421921 3744293.729449)");
/// let expected = r#"{"type": "Point", "coordinates": [1450880.2910605022,1141263.0111604782]}"#;
/// assert_eq!(expected, &wkt.to_projected(proj).to_json().unwrap());
/// ```
pub trait ToProjected: GeozeroDatasource + Sized {
fn to_projected(self, proj: Proj) -> ProjWrappedDataSource<Self>;
}

impl<Input: GeozeroDatasource> ToProjected for Input {
fn to_projected(self, proj: Proj) -> ProjWrappedDataSource<Input> {
ProjWrappedDataSource {
proj: Rc::new(proj),
input: self,
}
}
}

pub struct ProjWrappedDataSource<Input: GeozeroDatasource> {
proj: Rc<Proj>,
input: Input,
}

struct ProjPreprocessor<'a> {
proj: Rc<Proj>,
output: &'a mut dyn FeatureProcessor,
}

impl<Input: GeozeroDatasource> GeozeroDatasource for ProjWrappedDataSource<Input> {
fn process<P: FeatureProcessor>(&mut self, processor: &mut P) -> Result<()> {
let mut wrapped = ProjPreprocessor {
proj: self.proj.clone(),
output: processor,
};
self.input.process(&mut wrapped)
}
}

impl FeatureProcessor for ProjPreprocessor<'_> {
delegate! {
to self.output {
fn dataset_begin(&mut self, name: Option<&str>) -> Result<()>;
fn dataset_end(&mut self) -> Result<()>;
fn feature_begin(&mut self, idx: u64) -> Result<()>;
fn feature_end(&mut self, idx: u64) -> Result<()>;
fn properties_begin(&mut self) -> Result<()>;
fn properties_end(&mut self) -> Result<()>;
fn geometry_begin(&mut self) -> Result<()>;
fn geometry_end(&mut self) -> Result<()>;
}
}
}

impl PropertyProcessor for ProjPreprocessor<'_> {
delegate! {
to self.output {
fn property(&mut self, idx: usize, name: &str, value: &ColumnValue) -> Result<bool>;
}
}
}

impl GeomProcessor for ProjPreprocessor<'_> {
fn coordinate(
&mut self,
x: f64,
y: f64,
z: Option<f64>,
m: Option<f64>,
t: Option<f64>,
tm: Option<u64>,
idx: usize,
) -> Result<()> {
let (x, y) = self.proj.convert((x, y)).unwrap();
self.output.coordinate(x, y, z, m, t, tm, idx)
}

fn xy(&mut self, x: f64, y: f64, idx: usize) -> Result<()> {
let (x, y) = self.proj.convert((x, y)).unwrap();
self.output.xy(x, y, idx)
}
Copy link
Member Author

@michaelkirk michaelkirk Apr 13, 2022

Choose a reason for hiding this comment

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

This is the crux - we transform the points before passing them in to the "next" processor in the pipeline.

We only "need" these two methods coordinate / xy, but we have to explicitly delegate our other impls to the "output" processor, otherwise we'd inherit the "no-op" implementations for the GeomProcessor trait. (which is really painful)

Effectively, I'm going for some kind of interceptor pattern here.

As well as being very verbose, it also seems like it'd be easy to break if we added a new method to GeomProcessor/FeatureProcessor/PropertyProcessor and failed to update these long list of delegations below.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I'm currently short on time and leaving for Easter holidays soon. My only input would be that maybe a delegation pattern like https://github.com/flatgeobuf/flatgeobuf/blob/845111d2ba6765c8f43379877eb1f3440b78d4b6/src/rust/src/file_writer.rs#L302 could work here as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

maybe a delegation pattern

If I understand correctly, that's exactly what I'm doing - I did use the delegate crate macro to make it a little more concise, but it seems like a lot of boilerplate.

I'm searching for, but have so far failed to find, a better way to do this.


delegate! {
to self.output {
fn dimensions(&self) -> CoordDimensions;
fn multi_dim(&self) -> bool;
fn srid(&mut self, srid: Option<i32>) -> Result<()>;
fn empty_point(&mut self, idx: usize) -> Result<()>;
fn point_begin(&mut self, idx: usize) -> Result<()>;
fn point_end(&mut self, idx: usize) -> Result<()>;
fn multipoint_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn multipoint_end(&mut self, idx: usize) -> Result<()>;
fn linestring_begin(&mut self, tagged: bool, size: usize, idx: usize) -> Result<()>;
fn linestring_end(&mut self, tagged: bool, idx: usize) -> Result<()>;
fn multilinestring_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn multilinestring_end(&mut self, idx: usize) -> Result<()>;
fn polygon_begin(&mut self, tagged: bool, size: usize, idx: usize) -> Result<()>;
fn polygon_end(&mut self, tagged: bool, idx: usize) -> Result<()>;
fn multipolygon_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn multipolygon_end(&mut self, idx: usize) -> Result<()>;
fn geometrycollection_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn geometrycollection_end(&mut self, idx: usize) -> Result<()>;
fn circularstring_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn circularstring_end(&mut self, idx: usize) -> Result<()>;
fn compoundcurve_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn compoundcurve_end(&mut self, idx: usize) -> Result<()>;
fn curvepolygon_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn curvepolygon_end(&mut self, idx: usize) -> Result<()>;
fn multicurve_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn multicurve_end(&mut self, idx: usize) -> Result<()>;
fn multisurface_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn multisurface_end(&mut self, idx: usize) -> Result<()>;
fn triangle_begin(&mut self, tagged: bool, size: usize, idx: usize) -> Result<()>;
fn triangle_end(&mut self, tagged: bool, idx: usize) -> Result<()>;
fn polyhedralsurface_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn polyhedralsurface_end(&mut self, idx: usize) -> Result<()>;
fn tin_begin(&mut self, size: usize, idx: usize) -> Result<()>;
fn tin_end(&mut self, idx: usize) -> Result<()>;
}
}
}