-
Notifications
You must be signed in to change notification settings - Fork 40
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
michaelkirk
wants to merge
1
commit into
georust:main
Choose a base branch
from
michaelkirk:mkirk/transform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
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<()>; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.