Skip to content

feat(raster): slope in percent as an option (#1998) - #2245

Merged
Mavrick-F merged 2 commits into
opengeos:mainfrom
Mavrick-F:fix/slope-percent
Nov 16, 2026
Merged

Mavrick-F merged 2 commits into
opengeos:mainfrom
Mavrick-F:fix/slope-percent

Conversation

@Mavrick-F

@Mavrick-F Mavrick-F commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Slope can now come out in percent. #2068 said the unit (degrees) and gave the conversion; the trial in #1998 needed mean slope in percent and had to work it out by hand.

  • raster.function and raster.functionChain take an optional "units": "degrees" | "percent" for slope. Degrees stay the default, so existing scripts, models and chains are unchanged. Percent is 100 × the gradient (100 × tan of the angle), from the same Horn gradient, Z factor and no-data handling, computed directly rather than through degrees.
  • The result's band is named Slope (percent) or Slope (degrees).
  • The Raster Functions form and the Function Editor show a Slope units choice when slope is picked; the help, gallery and ribbon text say degrees or percent; the analysis guide documents the option.
  • Percent grows without bound towards vertical; a result past Float32's range is refused with the number of cells, rather than written as infinity.

Kind of change: any maintainer, an optional parameter.

Reaches: the desktop forms, command line, Python and scripts (the command parameter), the Assistant and MCP (the parameter help), and the browser (no file access involved). Undo removes the result layer as for any raster function.

Testing

  • slope_results_use_chosen_units: a 45° plane gives 100 percent and 45 degrees (default and explicit), Z factor 0.5 gives 50 percent; projected and geographic grids, both commands, invalid units refused, band names through raster.info, a GeoTIFF and a project round trip, and a later threshold on percent cells.
  • On a crop of the trial's elevation raster, every percent cell matched 100 × tan(degrees) to within 0.000002.
  • cargo xtask ci on the fork's CI.

UI evidence

Raster Functions with slope:

Raster Functions with Slope units set to Percent

Function Editor:

Function Editor with a percent slope step

Related issue

Part of #1998 (the slope line's percent option).

Summary by CodeRabbit

  • New Features
    • Slope analysis now supports percent output alongside degrees in raster functions and the function editor. Degrees remain the default, and output band names identify the selected units.
  • Bug Fixes
    • Percent slope calculations now report an error when results exceed the supported numeric range, rather than returning invalid values.
  • Documentation
    • Updated analysis and imagery guidance to describe percent slope, unit selection, and the calculation.
  • Compatibility
    • Projects, models, and shared tools using percent slope now declare the required capability so older readers can identify unsupported content.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Raster slope functions now support degree and percent output. Degrees remain the default. Workflow forms provide unit selection, output bands use unit-specific labels, and saved content records a capability requirement when it uses percent slope.

Changes: Slope Units and Saved-Content Capabilities

Layer / File(s) Summary
Percent slope calculation
crates/analysis/src/rasterops.rs, crates/engine/src/cmd/workflows.rs, crates/engine/src/tests.rs
A public function calculates percent slope from the terrain gradient. Raster functions accept degrees or percent, default to degrees, and reject other supplied units. Tests cover values, labels, export and reopen behavior, and out-of-range results.
Unit selection and guidance
crates/ui-egui/src/analysispane.rs, crates/ui-egui/src/ribbon.rs, docs/user-guide/analysis.md
Workflow forms offer degree and percent options. Interface help and user documentation describe the supported units and percent formula.
Saved-content capability requirements
crates/engine/src/model.rs, crates/engine/src/cmd/sharing.rs, crates/engine/src/project.rs, crates/engine/src/project_capabilities.rs, schema/project.schema.json, schema/fixtures/slope_percent.rgis, docs/maintaining.md
Model, web-tool, and project capability handling records slope-percent when executable content uses percent slope. The schema, fixture, and maintaining guide include the capability.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant WorkflowForm
  participant RasterFunction
  participant apply_function
  participant slope_percent
  participant derive_function
  WorkflowForm->>RasterFunction: Set slope units
  RasterFunction->>apply_function: Pass slope parameters
  apply_function->>slope_percent: Calculate percent slope
  slope_percent-->>apply_function: Return pixels or cell-count error
  apply_function->>derive_function: Provide selected units
  derive_function-->>RasterFunction: Return unit-specific band label
Loading

Possibly related issues

  • opengeos/RustGIS#1998: This change adds the percent-slope option described in the issue. It does not address the separate unit-statement follow-up.

Suggested reviewers: giswqs, elpidioassinaturas, pick2510

Merge Risk: Minimal · up to a108a

This adds an optional percent unit for slope while keeping degrees as the default. Saved content that uses percent slope is marked so older readers refuse it rather than silently running it in degrees. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "feat(raster): slope in percent as an option (#1998)" matches the main change. The changeset adds an optional units parameter for slope, with percent output alongside the default degrees, ac…
Description check Passed The description follows the template's Summary, Testing, UI evidence, and Related issue sections. The Summary explains the new optional units parameter, the default, the band naming, and the Float32 o…
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: Title check

Explanation

The title "feat(raster): slope in percent as an option (#1998)" matches the main change. The changeset adds an optional units parameter for slope, with percent output alongside the default degrees, across the analysis, engine, UI, and docs. The title is short, specific, and follows a conventional commit style. The issue number in the title is the only extra detail.

Full details: Description check

Explanation

The description follows the template's Summary, Testing, UI evidence, and Related issue sections. The Summary explains the new optional units parameter, the default, the band naming, and the Float32 overflow refusal. Testing names the new test and the manual trial check. UI evidence includes before-and-after style screenshots for the form and the Function Editor. The Related issue section links to #1998. The description does not give a Closes or Part of reference that matches the template example format exactly, and the Related issue line refers to a PR number that differs from the objectives text. These are minor gaps, so the description is mostly complete.

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 hillside slope,
Degrees and percent both measure the rise.
The pixels pass through one careful hop,
Their band names tell their chosen size.
Saved files mark the percent path,
Then I nibble greens beneath the skies.

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: e95d959e-9654-48ec-a235-6493748f8399
Commits

Reviewing files that changed from the base of the PR and between d5a17d4 and a108a5f.

Files selected for processing: 7

  • crates/engine/src/cmd/sharing.rs
  • crates/engine/src/model.rs
  • crates/engine/src/project.rs
  • crates/engine/src/project_capabilities.rs
  • docs/maintaining.md
  • schema/fixtures/slope_percent.rgis
  • schema/project.schema.json

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

let zf = f(p, "zFactor").unwrap_or(1.0).clamp(0.0001, 100_000.0);
match ps(p, "function").ok_or("function is required")? {
"slope" => rasterops::slope(r, band, zf),
"slope" => match p.get("units") {

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.

AGENTS.md (saved files): percent slope needs a requires capability. An older RustGIS ignores the new units key and silently computes degrees, so a saved model or chain with "units": "percent" gives different numbers in an older build with no notice. docs/maintaining.md#saved-files-and-settings says a change an older reader would misread names a capability in requires (see CAPABILITIES in crates/engine/src/project.rs). Model steps are saved in the project (ModelStep.params), so the writer should require a capability (for example slope-percent) when a step uses percent, and the reader should list it in CAPABILITIES. This is a maintainer call on whether model-level steps are covered, so confidence is moderate.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in a108a5f: saving anything that would run slope in percent now requires the slope-percent capability, so an older RustGIS refuses it instead of computing degrees. That covers projects (models, recorded tasks, tool history and notebook scripts) and model and web tool files; CAPABILITIES, the schema and docs/maintaining.md list it, with a fixture. A file whose steps use degrees, or no units, requires nothing new.

let overflow = AtomicUsize::new(0);
let pixels = terrain(r, d, zf, |gx, gy| {
let value = 100.0 * gx.hypot(gy);
if !value.is_finite() || value > f64::from(f32::MAX) {

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.

Quality nit (low confidence): the !value.is_finite() test is effectively unreachable for valid rasters, since is_valid drops non-finite inputs and the cell size is non-zero. If it does trigger (for example a degenerate cell size giving NaN), the message "exceed the Float32 range" is wrong. Consider counting only value > f64::from(f32::MAX), or giving NaN its own message. Also, the parallel test list in crates/analysis/tests/parallel_serial.rs covers slope but not slope_percent; the code path is shared, so this is low priority.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It guards the Float32 conversion for very large gradients (the 1e37 test case reaches it), not non-finite input; the wording of the message is left for the release quality pass.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs

  • None confirmed. The percent path reuses terrain, so Horn gradient, Z factor, no-data, edge handling and thread determinism match slope. The 1e37 test case was checked by hand: all nine cells exceed the Float32 range, so the "9 percent slope cells" assertion holds. (High confidence on the logic, not run.)

Security

  • None found. Input is hostile-safe: units is matched as a string, anything else is an error, and no panic paths were added.

Performance

  • None significant. Overflow is counted with an atomic that only changes on overflow cells, so the hot loop is unaffected and the output is unchanged across thread counts.

Quality

  • units is silently ignored for every function except slope, so {"function":"aspect","units":"percent"} returns aspect with no notice. The rule against silent loss covers data, so this is a usability gap. (Low-medium confidence.)
  • slope_percent routes NaN through the Float32 overflow message (rasterops.rs:214). This is only reachable with degenerate inputs, so it is low confidence. (Inline comment posted.)
  • Drive-by doc comments were added to commands(), workflows() and analysis_groups(), unrelated to this change. Also use std::sync::atomic is inside the function body. Both are nits.
  • No parallel-vs-serial test for slope_percent in crates/analysis/tests/parallel_serial.rs. Low priority, because the code path is shared.

AGENTS.md

  • Saved files, moderate confidence (inline comment posted at workflows.rs:1175): an older RustGIS reads a saved model step with "units": "percent" as degrees, silently. docs/maintaining.md says a change an older reader would misread names a capability in requires. CAPABILITIES in crates/engine/src/project.rs has no entry for this. Whether model steps are covered is a maintainer call.
  • Everything else checked out: the surfaces are covered (command parameter help, UI forms, docs, ribbon text), the new code runs on the existing background path, and the test covers both clean and lossy input.

Prior reviews: none on this PR, so no earlier concerns were skipped.

Change size

Compared with main (merge base ff3ae0e to PR head d5a17d4), the whole PR.

Measure Value
Files changed 6 (0 added, 6 modified, 0 removed, 0 renamed)
Size before 3.71 MB
Size after 3.71 MB
Total size change +5.3 KB
Path Before After Change
crates/engine/src/tests.rs 2.92 MB 2.92 MB +2.6 KB
crates/ui-egui/src/analysispane.rs 136.2 KB 137.2 KB +1.0 KB
crates/analysis/src/rasterops.rs 144.5 KB 145.3 KB +877 B
crates/engine/src/cmd/workflows.rs 104.6 KB 105.1 KB +570 B
docs/user-guide/analysis.md 154.7 KB 155.0 KB +287 B
crates/ui-egui/src/ribbon.rs 266.5 KB 266.6 KB +117 B

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-2245/
Web app https://preview.rustgis.app/pr-2245/app/
Commit a108a5f

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

Comment thread docs/maintaining.md
capabilities instead of opening and later saving away those settings; legacy format-1 files
still open.

Projects, model files and web tools that save a percent-slope function require `slope-percent`:

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.

Possible gap in saved-capability coverage (confidence 30%). This line lists projects, model files and web tools. Shortcut files (rustgis_shortcuts, version 1, in crates/ui-egui/src/shortcutfile.rs) and keymap preferences (Binding.params in crates/ui-egui/src/keymap.rs) also store params for a command. A binding of raster.function with "units": "percent" would therefore be read by an older build as degrees, with no requires check to stop it. I could not find a UI path that creates such a binding, so reachability looks narrow, most likely through a hand-edited or imported shortcut file. If that is judged reachable, the shortcut file needs its own capability or a refusal, and this line should say so.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A shortcut bound to a percent slope step is the one saved form the capability doesn't cover yet; shortcut files carry their own version rather than requires. Covering them is left for the release quality pass.

let mut step =
json!({"function": app.workflow.function, "expression": app.workflow.text, "value": num(&app.workflow.number, 0.0)});
if app.workflow.function == "slope" {
step["units"] = json!(if app.workflow.slope_percent { "percent" } else { "degrees" });

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.

Quality (confidence 85%): the Function Editor's step list (lines 658–661, not in this diff) shows only the function name, so a degrees slope step and a percent one look identical before Run Chain. The units are stored here in the step JSON but nothing on screen shows them. Suggested fix in the list: append the slope's units, for example if v["function"] == "slope" then format!(" ({u})") from v["units"]. This is a quality point, so it can wait for the release quality pass if maintainers prefer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Showing each step's units in the Function Editor's list is a display refinement, left for the release quality pass.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs

  • None found. I traced the percent path end to end and it holds:
    • The Float32 overflow count runs before terrain's as f32 cast, so out-of-range cells are counted and refused.
    • Add Step keeps units in the step JSON, so Run Chain receives it.
    • The capability scan and the executor both parse script lines with script_line, and an over-long script is refused whole.
    • Confidence: high.

Security

  • Saved-capability gap: shortcut files (rustgis_shortcuts v1) and keymap preferences store params (Binding.params), and a raster.function percent binding there would not set requires. An older build would run it in degrees. I found no UI path that creates such a binding, so reachability looks narrow. Confidence 30%. Posted inline on docs/maintaining.md:106.

Performance

  • Project::required_capabilities now parses every line of every native notebook code cell and script on each save, with a full JSON parse per line (up to 10,000 lines per cell). This is small for typical projects. Confidence 25%. A textual pre-filter would be unsafe because JSON escapes can hide "percent", so keep the parsed check.

Quality

  • The Function Editor step list shows only the function name, so degrees and percent slope steps look identical before Run Chain. Confidence 85%. Posted inline on crates/ui-egui/src/analysispane.rs:641; the list itself is at lines 658–661, outside the diff. This is a quality point and can wait for the release quality pass.
  • "units": null is refused with "slope units must be degrees or percent", while a missing key takes the default. Check this against how other optional parameters treat null. Confidence 40%.
  • Drive-by doc comments were added to commands(), analysis_groups, workflows and share_bytes_inner, which this change does not otherwise touch. Confidence 60%, low impact.

AGENTS.md

  • The saved-file rules are met for projects, model files and web tool files: requires is written, CAPABILITIES, the schema enum and the fixture are updated, and the reader tests cover both cases. The enum edits in project.schema.json are hand-checked against CAPABILITIES, not regenerated by cargo xtask schema.
  • The PR body lists desktop, CLI, Python, Assistant/MCP and browser. It does not mention plugins or the phone. ui-touch has no slope code, so the phone is not affected. Confidence 50%, low impact.
  • Not run: I had no shell, so I did not run cargo xtask ci, the schema check, the wasm build or the tests. Everything above is from reading the code.

Earlier review threads

  • I left 2 earlier concerns alone because they were already raised:
    • The capability gap was fixed in a108a5f. I checked the writers: to_file (workflows.rs:998) and to_web_tool_file (sharing.rs:277) are the only model-file writers, and both now set requires.
    • The finite-check message and the parallel-test list were deferred by the author to the quality pass.

Change size

Compared: the whole PR against main (merge base ff3ae0e to PR head a108a5f).

Measure Value
Files changed 13 (1 added, 12 modified, 0 removed, 0 renamed)
Size before 4.01 MB
Size after 4.03 MB
Total size change +15.6 KB
Path Before After Change
crates/engine/src/project_capabilities.rs 8.7 KB 14.2 KB +5.5 KB
crates/engine/src/model.rs 55.2 KB 58.7 KB +3.5 KB
crates/engine/src/tests.rs 2.92 MB 2.92 MB +2.6 KB
schema/fixtures/slope_percent.rgis 0 B 1.3 KB +1.3 KB
crates/ui-egui/src/analysispane.rs 136.2 KB 137.2 KB +1.0 KB
crates/analysis/src/rasterops.rs 144.5 KB 145.4 KB +877 B
crates/engine/src/cmd/workflows.rs 104.6 KB 105.1 KB +570 B
crates/engine/src/cmd/sharing.rs 89.3 KB 88.8 KB -489 B
docs/user-guide/analysis.md 154.7 KB 155.0 KB +287 B
docs/maintaining.md 6.8 KB 7.1 KB +279 B
crates/ui-egui/src/ribbon.rs 266.5 KB 266.6 KB +117 B
schema/project.schema.json 27.5 KB 27.5 KB +48 B
crates/engine/src/project.rs 124.0 KB 124.0 KB +47 B

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.

1 participant