|
| 1 | +use arrow_array::Float64Array; |
| 2 | +use geo::LineLocatePoint; |
| 3 | +use geoarrow_array::{GeoArrowArray, GeoArrowArrayAccessor}; |
| 4 | +use geoarrow_schema::error::GeoArrowResult; |
| 5 | + |
| 6 | +use crate::util::downcast::downcast_geoarrow_array_two_args; |
| 7 | +use crate::util::{check_same_len, map_pair_to_f64}; |
| 8 | + |
| 9 | +/// Element-wise: locate each point along its paired line, as the fraction of the |
| 10 | +/// line's length at the nearest point on that line. |
| 11 | +/// |
| 12 | +/// `geo` implements this for a `Line` or `LineString` with a `Point` argument only. |
| 13 | +/// Any other pairing gives a null row, which a caller cannot tell apart from a |
| 14 | +/// null input. |
| 15 | +pub fn line_locate_point( |
| 16 | + lines: &dyn GeoArrowArray, |
| 17 | + points: &dyn GeoArrowArray, |
| 18 | +) -> GeoArrowResult<Float64Array> { |
| 19 | + check_same_len(lines, points)?; |
| 20 | + downcast_geoarrow_array_two_args!(lines, points, line_locate_point_impl) |
| 21 | +} |
| 22 | + |
| 23 | +fn line_locate_point_impl<'a>( |
| 24 | + lines: &'a impl GeoArrowArrayAccessor<'a>, |
| 25 | + points: &'a impl GeoArrowArrayAccessor<'a>, |
| 26 | +) -> GeoArrowResult<Float64Array> { |
| 27 | + map_pair_to_f64(lines, points, |line, point| match (line, point) { |
| 28 | + (geo::Geometry::LineString(line), geo::Geometry::Point(point)) => { |
| 29 | + line.line_locate_point(point) |
| 30 | + } |
| 31 | + (geo::Geometry::Line(line), geo::Geometry::Point(point)) => line.line_locate_point(point), |
| 32 | + _ => None, |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(test)] |
| 37 | +mod test { |
| 38 | + use arrow_array::Array; |
| 39 | + use geo::{Geometry, LineString, point}; |
| 40 | + |
| 41 | + use super::*; |
| 42 | + use crate::test_util::geometry_array; |
| 43 | + |
| 44 | + fn line(coords: Vec<(f64, f64)>) -> Option<Geometry> { |
| 45 | + Some(Geometry::LineString(LineString::from(coords))) |
| 46 | + } |
| 47 | + |
| 48 | + #[test] |
| 49 | + fn locate_midpoint_and_nulls() { |
| 50 | + let lines = geometry_array(vec![ |
| 51 | + line(vec![(0.0, 0.0), (10.0, 0.0)]), |
| 52 | + Some(Geometry::from(point!(x: 0., y: 0.))), |
| 53 | + None, |
| 54 | + line(vec![(1.0, 1.0), (1.0, 1.0)]), |
| 55 | + ]); |
| 56 | + let points = geometry_array(vec![ |
| 57 | + Some(Geometry::from(point!(x: 5., y: 5.))), |
| 58 | + Some(Geometry::from(point!(x: 1., y: 1.))), |
| 59 | + Some(Geometry::from(point!(x: 1., y: 1.))), |
| 60 | + Some(Geometry::from(point!(x: 5., y: 5.))), |
| 61 | + ]); |
| 62 | + |
| 63 | + let result = line_locate_point(&lines, &points).unwrap(); |
| 64 | + assert_eq!(result.value(0), 0.5); |
| 65 | + assert!(result.is_null(1)); |
| 66 | + assert!(result.is_null(2)); |
| 67 | + // geo measures a zero-length line as Some(0.0), thus not a null row. |
| 68 | + assert_eq!(result.value(3), 0.0); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn length_mismatch_errors() { |
| 73 | + let lines = geometry_array(vec![line(vec![(0.0, 0.0), (1.0, 0.0)])]); |
| 74 | + let points = geometry_array(vec![ |
| 75 | + Some(Geometry::from(point!(x: 0., y: 0.))), |
| 76 | + Some(Geometry::from(point!(x: 1., y: 1.))), |
| 77 | + ]); |
| 78 | + assert!(line_locate_point(&lines, &points).is_err()); |
| 79 | + } |
| 80 | +} |
0 commit comments