|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +use std::{sync::Arc, vec}; |
| 18 | + |
| 19 | +use crate::executor::RasterExecutor; |
| 20 | +use arrow_array::builder::Float64Builder; |
| 21 | +use arrow_schema::DataType; |
| 22 | +use datafusion_common::{error::Result, exec_err, ScalarValue}; |
| 23 | +use datafusion_expr::{ |
| 24 | + scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, |
| 25 | +}; |
| 26 | +use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}; |
| 27 | +use sedona_raster::affine_transformation::to_world_coordinate; |
| 28 | +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; |
| 29 | + |
| 30 | +/// RS_RasterToWorldCoordY() scalar UDF implementation |
| 31 | +/// |
| 32 | +/// Converts pixel coordinates to world Y coordinate |
| 33 | +pub fn rs_rastertoworldcoordy_udf() -> SedonaScalarUDF { |
| 34 | + SedonaScalarUDF::new( |
| 35 | + "rs_rastertoworldcoordy", |
| 36 | + vec![Arc::new(RsCoordinateMapper { coord: Coord::Y })], |
| 37 | + Volatility::Immutable, |
| 38 | + Some(rs_rastertoworldcoordy_doc()), |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +/// RS_RasterToWorldCoordX() scalar UDF documentation |
| 43 | +/// |
| 44 | +/// Converts pixel coordinates to world X coordinate |
| 45 | +pub fn rs_rastertoworldcoordx_udf() -> SedonaScalarUDF { |
| 46 | + SedonaScalarUDF::new( |
| 47 | + "rs_rastertoworldcoordx", |
| 48 | + vec![Arc::new(RsCoordinateMapper { coord: Coord::X })], |
| 49 | + Volatility::Immutable, |
| 50 | + Some(rs_rastertoworldcoordx_doc()), |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +fn rs_rastertoworldcoordy_doc() -> Documentation { |
| 55 | + Documentation::builder( |
| 56 | + DOC_SECTION_OTHER, |
| 57 | + "Returns the upper left Y coordinate of the given row and column of the given raster geometric units of the geo-referenced raster. If any out of bounds values are given, the Y coordinate of the assumed point considering existing raster pixel size and skew values will be returned.".to_string(), |
| 58 | + "RS_RasterToWorldCoordY(raster: Raster)".to_string(), |
| 59 | + ) |
| 60 | + .with_argument("raster", "Raster: Input raster") |
| 61 | + .with_argument("x", "Integer: Column x into the raster") |
| 62 | + .with_argument("y", "Integer: Row y into the raster") |
| 63 | + .with_sql_example("SELECT RS_RasterToWorldCoordY(RS_Example(), 0, 0)".to_string()) |
| 64 | + .build() |
| 65 | +} |
| 66 | + |
| 67 | +fn rs_rastertoworldcoordx_doc() -> Documentation { |
| 68 | + Documentation::builder( |
| 69 | + DOC_SECTION_OTHER, |
| 70 | + "Returns the upper left X coordinate of the given row and column of the given raster geometric units of the geo-referenced raster. If any out of bounds values are given, the X coordinate of the assumed point considering existing raster pixel size and skew values will be returned.".to_string(), |
| 71 | + "RS_RasterToWorldCoordX(raster: Raster)".to_string(), |
| 72 | + ) |
| 73 | + .with_argument("raster", "Raster: Input raster") |
| 74 | + .with_argument("x", "Integer: Column x into the raster") |
| 75 | + .with_argument("y", "Integer: Row y into the raster") |
| 76 | + .with_sql_example("SELECT RS_RasterToWorldCoordX(RS_Example(), 0, 0)".to_string()) |
| 77 | + .build() |
| 78 | +} |
| 79 | + |
| 80 | +#[derive(Debug, Clone)] |
| 81 | +enum Coord { |
| 82 | + X, |
| 83 | + Y, |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Debug)] |
| 87 | +struct RsCoordinateMapper { |
| 88 | + coord: Coord, |
| 89 | +} |
| 90 | + |
| 91 | +impl SedonaScalarKernel for RsCoordinateMapper { |
| 92 | + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { |
| 93 | + let matcher = ArgMatcher::new( |
| 94 | + vec![ |
| 95 | + ArgMatcher::is_raster(), |
| 96 | + ArgMatcher::is_integer(), |
| 97 | + ArgMatcher::is_integer(), |
| 98 | + ], |
| 99 | + SedonaType::Arrow(DataType::Float64), |
| 100 | + ); |
| 101 | + |
| 102 | + matcher.match_args(args) |
| 103 | + } |
| 104 | + |
| 105 | + fn invoke_batch( |
| 106 | + &self, |
| 107 | + arg_types: &[SedonaType], |
| 108 | + args: &[ColumnarValue], |
| 109 | + ) -> Result<ColumnarValue> { |
| 110 | + let executor = RasterExecutor::new(arg_types, args); |
| 111 | + let mut builder = Float64Builder::with_capacity(executor.num_iterations()); |
| 112 | + |
| 113 | + let (x_opt, y_opt) = get_scalar_coord(&args[1], &args[2])?; |
| 114 | + |
| 115 | + executor.execute_raster_void(|_i, raster_opt| { |
| 116 | + match (raster_opt, x_opt, y_opt) { |
| 117 | + (Some(raster), Some(x), Some(y)) => { |
| 118 | + let (world_x, world_y) = to_world_coordinate(&raster, x, y); |
| 119 | + match self.coord { |
| 120 | + Coord::X => builder.append_value(world_x), |
| 121 | + Coord::Y => builder.append_value(world_y), |
| 122 | + }; |
| 123 | + } |
| 124 | + (_, _, _) => builder.append_null(), |
| 125 | + } |
| 126 | + Ok(()) |
| 127 | + })?; |
| 128 | + |
| 129 | + executor.finish(Arc::new(builder.finish())) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +fn extract_int_scalar(arg: &ColumnarValue) -> Result<Option<i64>> { |
| 134 | + match arg { |
| 135 | + ColumnarValue::Scalar(scalar) => { |
| 136 | + let i64_val = scalar.cast_to(&DataType::Int64)?; |
| 137 | + match i64_val { |
| 138 | + ScalarValue::Int64(Some(v)) => Ok(Some(v)), |
| 139 | + _ => Ok(None), |
| 140 | + } |
| 141 | + } |
| 142 | + _ => exec_err!("Expected scalar integer argument for coordinate"), |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +fn get_scalar_coord( |
| 147 | + x_arg: &ColumnarValue, |
| 148 | + y_arg: &ColumnarValue, |
| 149 | +) -> Result<(Option<i64>, Option<i64>)> { |
| 150 | + let x_opt = extract_int_scalar(x_arg)?; |
| 151 | + let y_opt = extract_int_scalar(y_arg)?; |
| 152 | + Ok((x_opt, y_opt)) |
| 153 | +} |
| 154 | + |
| 155 | +#[cfg(test)] |
| 156 | +mod tests { |
| 157 | + use super::*; |
| 158 | + use datafusion_expr::ScalarUDF; |
| 159 | + use rstest::rstest; |
| 160 | + use sedona_schema::datatypes::RASTER; |
| 161 | + use sedona_testing::compare::assert_array_equal; |
| 162 | + use sedona_testing::rasters::generate_test_rasters; |
| 163 | + use sedona_testing::testers::ScalarUdfTester; |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn udf_docs() { |
| 167 | + let udf: ScalarUDF = rs_rastertoworldcoordy_udf().into(); |
| 168 | + assert_eq!(udf.name(), "rs_rastertoworldcoordy"); |
| 169 | + assert!(udf.documentation().is_some()); |
| 170 | + |
| 171 | + let udf: ScalarUDF = rs_rastertoworldcoordx_udf().into(); |
| 172 | + assert_eq!(udf.name(), "rs_rastertoworldcoordx"); |
| 173 | + assert!(udf.documentation().is_some()); |
| 174 | + } |
| 175 | + |
| 176 | + #[rstest] |
| 177 | + fn udf_invoke(#[values(Coord::Y, Coord::X)] coord: Coord) { |
| 178 | + let udf = match coord { |
| 179 | + Coord::X => rs_rastertoworldcoordx_udf(), |
| 180 | + Coord::Y => rs_rastertoworldcoordy_udf(), |
| 181 | + }; |
| 182 | + let tester = ScalarUdfTester::new( |
| 183 | + udf.into(), |
| 184 | + vec![ |
| 185 | + RASTER, |
| 186 | + SedonaType::Arrow(DataType::Int32), |
| 187 | + SedonaType::Arrow(DataType::Int32), |
| 188 | + ], |
| 189 | + ); |
| 190 | + |
| 191 | + let rasters = generate_test_rasters(3, Some(1)).unwrap(); |
| 192 | + // At 0,0 expect the upper left corner of the test values |
| 193 | + let expected_values = match coord { |
| 194 | + Coord::X => vec![Some(1.0), None, Some(3.0)], |
| 195 | + Coord::Y => vec![Some(2.0), None, Some(4.0)], |
| 196 | + }; |
| 197 | + let expected: Arc<dyn arrow_array::Array> = |
| 198 | + Arc::new(arrow_array::Float64Array::from(expected_values)); |
| 199 | + |
| 200 | + let result = tester |
| 201 | + .invoke_array_scalar_scalar(Arc::new(rasters), 0_i32, 0_i32) |
| 202 | + .unwrap(); |
| 203 | + assert_array_equal(&result, &expected); |
| 204 | + } |
| 205 | +} |
0 commit comments