Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
21395fc
feat(rust/sedona-raster-functions): add RS_Value
james-willis Jun 17, 2026
798b67c
chore(rust/sedona-raster-functions): benchmark RS_Value
james-willis Jun 17, 2026
82b7968
fix(rust/sedona-raster-functions): tighten RS_Value point sampling an…
james-willis Jun 17, 2026
62b8973
test(python): inline RS_Example and use assert_query_result for RS_Va…
james-willis Jun 22, 2026
d593bb5
feat(rust/sedona-raster-functions): RS_Value empty-point -> NULL, sam…
james-willis Jun 23, 2026
a61ec7d
docs(rs_value): use the 3-arg ST_Point(x, y, crs) form in the point e…
james-willis Jun 23, 2026
b9722e9
fix(rust/sedona-raster-functions): use checked arithmetic for RS_Valu…
james-willis Jun 23, 2026
ee7eaab
feat(rust/sedona-raster-functions): defer RS_Value grid-coordinate va…
james-willis Jun 23, 2026
14e0132
test(rust/sedona-raster-functions): benchmark RS_Value scalar-raster …
james-willis Jun 23, 2026
415357e
perf(rust/sedona-raster-functions): hoist scalar-raster state in RS_V…
james-willis Jun 23, 2026
450d9db
perf(rust/sedona-raster-functions): hoist RS_Value CRS-transform deci…
james-willis Jun 23, 2026
4ea333a
perf(rust/sedona-raster-functions): parse Point coords with a fixed-o…
james-willis Jun 23, 2026
e251419
refactor(rust/sedona-raster-functions): dedup RS_Value point sampling…
james-willis Jun 23, 2026
b9af45f
perf(rust/sedona-raster-functions): extend RS_Value scalar fast path …
james-willis Jun 24, 2026
9f6c94c
docs(rust/sedona-raster-functions): explain why RS_Value errors inste…
james-willis Jun 24, 2026
67af522
test(rust/sedona-geometry): hoist fixtures import to the test module
james-willis Jun 24, 2026
fc95617
Merge remote-tracking branch 'origin/main' into pr-974
james-willis Jun 24, 2026
526e9de
test(python/sedonadb): cross-check RS_Value against rasterio
james-willis Jun 24, 2026
7effb08
style(rust/sedona-geometry): fix rustfmt in wkb_header test
james-willis Jun 24, 2026
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
85 changes: 85 additions & 0 deletions docs/reference/sql/rs_value.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

title: RS_Value
description: >
Returns the value of a single raster pixel as a double, selected either by a
point geometry or by 1-based grid coordinates. Returns null if the location is
outside the raster or the pixel holds the band's nodata value.
kernels:
- returns: float64
args:
- raster
- name: point
type: geometry
description: >
The pixel that contains this point is sampled (no resampling).
Reprojected into the raster CRS when both carry one.
- returns: float64
args:
- raster
- name: point
type: geometry
- name: band
type: int
description: Band index (1-based). Defaults to 1 if not specified.
- returns: float64
args:
- raster
- name: colX
type: int
description: Column index (1-based).
- name: rowY
type: int
description: Row index (1-based).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Indexing is typically rows, cols (e.g., numpy, R) instead of cols, rows.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

punted the col/row version of the function for now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But the signature here is based on trying to match postgis as much as possible:

Here are PostGIS's ST_Value (raster) signatures, straight from the current docs:

  • ST_Value(raster rast, geometry pt, boolean exclude_nodata_value=true)
  • ST_Value(raster rast, integer band, geometry pt, boolean exclude_nodata_value=true, text
    resample='nearest')
  • ST_Value(raster rast, integer x, integer y, boolean exclude_nodata_value=true)
  • ST_Value(raster rast, integer band, integer x, integer y, boolean exclude_nodata_value=true)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure, but nobody asked me to review those 🙂 . Compared to GDAL and xarray the PostGIS and Sedona Spark APIs are not particularly popular...part of porting that to SedonaDB is to make it fast, but if anybody is ever going to use this we're also going to have to make it less awkward.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be better to keep st functions in sync between sedona and sedona_db? I understand the concern and bad decisions in the past on the orders of params, but if we'd make them different in signatures, users would need to change their SQL to adapt in between if they ever want to run on both engines.

- returns: float64
args:
- raster
- name: colX
type: int
- name: rowY
type: int
- name: band
type: int
description: Band index (1-based). Defaults to 1 if not specified.
---

## Description

`RS_Value` samples one pixel of a raster. The location is given either as a
point geometry — the value of the pixel that contains the point is returned, with
no interpolation — or as 1-based `(colX, rowY)` grid coordinates. The band
defaults to 1.

The result is `NULL` when the point or grid cell falls outside the raster, or
when the sampled pixel equals the band's nodata value. Only 2-D rasters are
supported.

## Examples

```sql
SELECT RS_Value(RS_Example(), 2, 1);
```

```sql
SELECT RS_Value(RS_Example(), 2, 1, 2);
```

```sql
SELECT RS_Value(RS_Example(), ST_SetCRS(ST_Point(74.58, 110.57), 'OGC:CRS84'));
Comment thread
james-willis marked this conversation as resolved.
Outdated
```
49 changes: 49 additions & 0 deletions python/sedonadb/tests/functions/test_raster_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,52 @@ def test_rs_ensureloaded(con, sedona_testing):
assert arr.shape == (512, 512)
assert arr.dtype == "uint16"
assert arr[0, 0] == 2324


# RS_Example fills band `b` with the constant value `b`, except the top-left
# pixel (colX=1, rowY=1) which is set to the nodata value (127). Grid
# coordinates are 1-based. (The `needs_pixels` -> RS_EnsureLoaded planner path
# is covered against a real OutDb raster by `test_rs_ensureloaded`.)
@pytest.mark.parametrize(
Comment thread
james-willis marked this conversation as resolved.
("expr", "expected"),
[
("RS_Value(RS_Example(), 2, 1)", 1.0), # band 1, away from the nodata corner
("RS_Value(RS_Example(), 64, 32)", 1.0), # bottom-right pixel, in bounds
("RS_Value(RS_Example(), 2, 1, 1)", 1.0), # explicit band 1 == the default
("RS_Value(RS_Example(), 2, 1, 2)", 2.0), # band 2
("RS_Value(RS_Example(), 2, 1, 3)", 3.0), # band 3
("RS_Value(RS_Example(), 1, 1)", None), # top-left pixel is nodata
("RS_Value(RS_Example(), 1, 1, 2)", None), # nodata on band 2 too
("RS_Value(RS_Example(), 65, 1)", None), # colX past the width (64)
("RS_Value(RS_Example(), 1, 33)", None), # rowY past the height (32)
("RS_Value(RS_Example(), 0, 1)", None), # colX 0 -> off the left edge
("RS_Value(RS_Example(), 2, 1, CAST(NULL AS INT))", None), # NULL band -> NULL
],
)
def test_rs_value_grid(expr, expected):
SedonaDB().assert_query_result(f"SELECT {expr}", expected)


# Point sampling. (74.58, 110.57) is the centroid of pixel (10, 10) (0-based)
# in the raster's OGC:CRS84 space; the point and raster share a CRS so no
# reprojection happens. A point far outside the footprint yields NULL.
@pytest.mark.parametrize(
("expr", "expected"),
[
(
"RS_Value(RS_Example(), ST_SetCRS(ST_Point(74.58, 110.57), 'OGC:CRS84'))",
1.0,
),
(
"RS_Value(RS_Example(), ST_SetCRS(ST_Point(74.58, 110.57), 'OGC:CRS84'), 2)",
2.0,
),
(
"RS_Value(RS_Example(), ST_SetCRS(ST_Point(74.58, 110.57), 'OGC:CRS84'), 3)",
3.0,
),
("RS_Value(RS_Example(), ST_SetCRS(ST_Point(0.0, 0.0), 'OGC:CRS84'))", None),
],
)
def test_rs_value_point(expr, expected):
SedonaDB().assert_query_result(f"SELECT {expr}", expected)
2 changes: 1 addition & 1 deletion rust/sedona-raster-functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ arrow-buffer = { workspace = true }
async-trait = { workspace = true }
datafusion-common = { workspace = true }
datafusion-expr = { workspace = true }
geo-traits = { workspace = true }
sedona-common = { workspace = true }
sedona-expr = { workspace = true }
sedona-geometry = { workspace = true }
Expand All @@ -49,7 +50,6 @@ wkb = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }
geo-traits = { workspace = true }
sedona-testing = { workspace = true, features = ["criterion"] }
sedona-proj = { workspace = true, features = ["proj-sys"] }
rstest = { workspace = true }
Expand Down
32 changes: 32 additions & 0 deletions rust/sedona-raster-functions/benches/native-raster-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,38 @@ fn criterion_benchmark(c: &mut Criterion) {
),
);

// RS_Value(raster, colX, rowY) - grid sampling
benchmark::scalar(
c,
&f,
"native-raster",
"rs_value",
BenchmarkArgs::ArrayScalarScalar(Raster(64, 64), Int32(1, 64), Int32(1, 64)),
);
// RS_Value(raster, point)
benchmark::scalar(
c,
&f,
"native-raster",
"rs_value",
BenchmarkArgs::ArrayArray(
Raster(64, 64),
Transformed(Box::new(Point), sd_apply_default_crs_udf().into()),
),
);
// RS_Value(raster, point, band)
benchmark::scalar(
c,
&f,
"native-raster",
"rs_value",
BenchmarkArgs::ArrayArrayScalar(
Raster(64, 64),
Transformed(Box::new(Point), sd_apply_default_crs_udf().into()),
Int32(1, 2),
),
);

// RS_Intersects(raster, geometry) - point
benchmark::scalar(
c,
Expand Down
1 change: 1 addition & 0 deletions rust/sedona-raster-functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ pub mod rs_size;
pub mod rs_slice;
pub mod rs_spatial_predicates;
pub mod rs_srid;
pub mod rs_value;
pub mod rs_worldcoordinate;
1 change: 1 addition & 0 deletions rust/sedona-raster-functions/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn default_function_set() -> FunctionSet {
crate::rs_spatial_predicates::rs_within_udf,
crate::rs_srid::rs_crs_udf,
crate::rs_srid::rs_srid_udf,
crate::rs_value::rs_value_udf,
crate::rs_worldcoordinate::rs_rastertoworldcoord_udf,
crate::rs_worldcoordinate::rs_rastertoworldcoordx_udf,
crate::rs_worldcoordinate::rs_rastertoworldcoordy_udf,
Expand Down
Loading
Loading