Skip to content

perf(analysis): reuse terrain raster constants - #1122

Merged
pick2510 merged 1 commit into
opengeos:mainfrom
choupeanut:fix/1111-terrain-constants
Oct 29, 2026
Merged

pick2510 merged 1 commit into
opengeos:mainfrom
choupeanut:fix/1111-terrain-constants

Conversation

@choupeanut

@choupeanut choupeanut commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Slope, aspect and hillshade recomputed the raster's cell size for every pixel, including a CRS lookup and, for geographic data, a cosine. Compute it once per operation and reuse it for Horn's gradient. Hillshade also computes its fixed lighting-angle sine and cosine once. The formulas, no-data handling and Float32 output stay unchanged.

This is a small part of #1111's terrain work. The larger row-pass optimization and Byte output remain for separate work. The axis-orientation correction in #1085 is independent; the two changes merge without conflicts.

On a 2,500 × 2,500 synthetic geographic raster in the optimized test profile, one local run per version measured slope 1.76 s → 1.56 s, aspect 1.58 s → 1.39 s, and hillshade 2.42 s → 2.19 s. These are kernel timings, not full application timings. Checksums of every output pixel matched before and after.

Testing

  • uvx pre-commit run --all-files
  • cargo xtask ci (format, workspace Clippy, tests and layers)
  • cargo build --locked --target wasm32-unknown-unknown -p rustgis-web (with clang)
  • cargo test -p rustgis-analysis --lib terrain_
  • Added a small regression comparing slope, aspect and hillshade at edges and next to no-data against the prior results.

Related issue

Part of #1111.

Summary by CodeRabbit

  • Bug Fixes
    • Improved terrain slope, aspect, and hillshade calculations for rasters with no-data cells.
  • Tests
    • Added a regression test covering terrain calculations on a raster containing no-data values.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: ebd3aaf1-dc92-40bb-8457-98b869b64848
Commits

Reviewing files that changed from the base of the PR and between 13aee13 and 3e71e7d.

Files selected for processing: 1
  • crates/analysis/src/rasterops.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

Terrain gradient calculation now receives cell dimensions from its callers. Slope, aspect, and hillshade pass raster cell dimensions to it. Hillshade caches zenith trigonometric values. A regression test checks terrain outputs for a raster containing no-data.

Changes: Terrain gradient operations

Layer / File(s) Summary
Gradient inputs and terrain callers
crates/analysis/src/rasterops.rs
gradient now accepts cell dimensions as an argument. slope, aspect, and hillshade pass dimensions from Raster::cell_size_m(). Hillshade caches the zenith cosine and sine. A regression test checks slope and aspect arrays within 1e-4 and exact hillshade output for a 4×3 raster with no-data.

Priority: Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related issues

  • Issue 1111 describes slow hillshade related to repeated terrain constants and trigonometric calculations. This change caches hillshade zenith values, while the issue summary notes that other work remains separate.

Suggested reviewers: giswqs

Merge Risk: Minimal · up to 3e71e

No actionable merge-blocking issue is established for this terrain optimization. Merge after normal checks pass.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly identifies the performance optimization: reusing terrain raster constants in analysis operations.
Description check Passed The description includes a clear summary, testing details, and related issue. It appropriately omits UI evidence because the change has no visible UI impact.
Docstring Coverage Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 1 files.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the slopes in rows,
Where no-data rests, the raster shows.
The aspect turns, the hillshades gleam,
Cell sizes guide each gradient stream.
Then carrots crown the tested scene.

Comment @coderabbitai help to get the list of available commands.

let expected_aspect =
[285.89474, 305.4469, 311.66492, 288.18884, 358.39075, NODATA, 160.65001, 180.0, 7.9960175, 97.375114, 163.86998, 172.00398];
for (actual, expected) in
[(slope(&r, 0, 1.5).unwrap_or_default(), expected_slope), (aspect(&r, 0).unwrap_or_default(), expected_aspect)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: unwrap_or_default() turns an Err from slope/aspect into an empty Vec. The test still fails, but with a length mismatch (0 vs 12) instead of the error message. The rest of this test module (ramp()) reports errors with unwrap_or_else(|e| panic!("{e}")), so matching that keeps failures readable. The same applies to the hillshade(...) call at line 1085.

Suggested change
[(slope(&r, 0, 1.5).unwrap_or_default(), expected_slope), (aspect(&r, 0).unwrap_or_default(), expected_aspect)]
[(slope(&r, 0, 1.5).unwrap_or_else(|e| panic!("{e}")), expected_slope), (aspect(&r, 0).unwrap_or_else(|e| panic!("{e}")), expected_aspect)]

@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs: None found. Hoisting r.cell_size_m() out of the per-pixel gradient call is behaviour-preserving: cell_size_m reads only raster metadata (CRS, extent, cell size), so its value is the same for every pixel. The zen.cos()/zen.sin() hoist is also equivalent. I spot-checked the new test's expected values by hand for cell (0,0): slope ≈ 0.7096° and aspect ≈ 286°, consistent with the hard-coded 0.70946753 and 285.89474 (confidence: high for the logic, medium for the exact digits, since I did not run the test). I could not build or run the tests in this environment.

Security: None. No new input handling, parsing, or I/O.

Performance: The change is sound. It removes a CRS check, and for geographic rasters a cos(), from every pixel. The gain is modest, as the PR's timings suggest. The PR's kernel timings are not something I could reproduce here (confidence: medium).

Quality:

  • Test error handling (nit, confidence: high): unwrap_or_default() in the new test (rasterops.rs line 1079 for slope/aspect, line 1085 for hillshade) hides the error message behind a length mismatch. Inline comment posted with a suggestion.

AGENTS.md: Compliant. The new unwrap_or_else(|e| panic!) appears only in test code, following the existing ramp() helper in the same module. No new unwrap/expect in non-test code, and no new dependencies, assets, UI strings or layering changes.

Change size

Compared: the whole PR against main (merge base 13aee13 to PR head 3e71e7d).

Measure Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 64.2 KB (65,756 B)
Size after 66.3 KB (67,274 B)
Total size change +1.5 KB (+1,518 B)
Path Before After Change
crates/analysis/src/rasterops.rs 64.2 KB 66.3 KB +1.5 KB

@github-actions

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-1122/
Web app https://preview.rustgis.app/pr-1122/app/
Commit 3e71e7d

GitHub Pages takes a minute or two to publish a new commit.

@pick2510
pick2510 merged commit 1ce4878 into opengeos:main Oct 29, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants