Repository navigation
feat(map): mask a layer by a raster's values, with a soft edge (#3147, part 1) #3327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elpidioassinaturas
merged 3 commits into
opengeos:main
from
elpidioassinaturas:feat/3147-raster-mask
Nov 29, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
0e6478c
feat(map): mask a layer by a raster's values, with a soft edge (#3147…
elpidiofilho 828d1de
fix(render): a raster mask with no pixels, or past a cut end, hides t…
elpidiofilho 05fcea8
fix(render): a raster mask reads its raster as the map draws it, and …
elpidiofilho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| //! `layer.mask` by a raster's values (#3147). | ||
|
|
||
| use rustgis_map::effects::ValueMask; | ||
| use serde_json::{Value, json}; | ||
|
|
||
| use crate::Session; | ||
|
|
||
| /// The sample world with the hill DEM (values 200 to 300, and a column of 400): its name. | ||
| fn with_dem() -> (Session, String) { | ||
| let mut s = super::world(); | ||
| s.run("layer.addDataBytes", &json!({"fileName": "dem.tif", "data": super::dem_b64()})).unwrap(); | ||
| let dem = s.map().layers.iter().find(|l| l.is_raster()).unwrap().name.clone(); | ||
| (s, dem) | ||
| } | ||
|
|
||
| fn masks(s: &Session, layer: &str) -> (Option<String>, Option<ValueMask>) { | ||
| let l = s.map().layer_by_name(layer).unwrap(); | ||
| (l.mask.clone(), l.raster_mask.clone()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn layer_mask_takes_a_rasters_values_in_place_of_polygons() { | ||
| let (mut s, dem) = with_dem(); | ||
| s.run("layer.mask", &json!({"layer": "Cities", "mask": "Countries"})).unwrap(); | ||
| let range = json!({"raster": dem.to_uppercase(), "min": 250, "max": "280", "inside": false, "feather": 5}); | ||
| s.run("layer.mask", &json!({"layer": "Cities", "mask": range})).unwrap(); | ||
| let values = ValueMask { raster: dem.clone(), min: Some(250.0), max: Some(280.0), inside: false, feather: 5.0 }; | ||
| assert_eq!(masks(&s, "Cities"), (None, Some(values.clone())), "under the raster's own name, in place of the polygons"); | ||
| // Saved, it is named for a reader that would draw the layer whole. | ||
| assert_eq!(serde_json::to_value(&s.project).unwrap()["requires"], json!(["layer-raster-mask"])); | ||
| // It follows the raster's new name, as a polygon mask does. | ||
| s.run("layer.rename", &json!({"layer": dem, "name": "Altitude"})).unwrap(); | ||
| assert_eq!(masks(&s, "Cities").1.map(|m| m.raster), Some("Altitude".to_string())); | ||
| s.run("layer.mask", &json!({"layer": "Cities", "mask": "Countries"})).unwrap(); | ||
| assert_eq!(masks(&s, "Cities"), (Some("Countries".to_string()), None), "the polygons in place of the values"); | ||
| s.run("layer.mask", &json!({"layer": "Cities", "mask": {"raster": "Altitude"}})).unwrap(); | ||
| let open = ValueMask { raster: "Altitude".into(), min: None, max: None, inside: true, feather: 0.0 }; | ||
| assert_eq!(masks(&s, "Cities").1, Some(open), "both ends open, inside, with no feather"); | ||
| s.run("layer.mask", &json!({"layer": "Cities", "mask": null})).unwrap(); | ||
| assert_eq!(masks(&s, "Cities"), (None, None)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_raster_mask_that_cant_mask_is_refused_and_changes_nothing() { | ||
| let (mut s, dem) = with_dem(); | ||
| s.run("layer.mask", &json!({"layer": "Cities", "mask": {"raster": dem, "min": 250}})).unwrap(); | ||
| let (before, depth) = (masks(&s, "Cities"), s.undo_depth()); | ||
| for (bad, says) in [ | ||
| (json!({"raster": "Countries", "min": 1}), "no raster layer 'Countries'"), | ||
| (json!({"min": 1}), "names its raster"), | ||
| (json!({"raster": dem, "min": 5, "max": 1}), "min 5 is above max 1"), | ||
| (json!({"raster": dem, "min": "steep"}), "min is a number"), | ||
| (json!({"raster": dem, "feather": -1}), "feather is 0 or more"), | ||
| (json!({"raster": dem, "inside": "maybe"}), "inside is true or false"), | ||
| (json!(7), "mask is a polygon layer's name"), | ||
| ] { | ||
| let err = s.run("layer.mask", &json!({"layer": "Cities", "mask": bad})).expect_err(&bad.to_string()); | ||
| assert!(err.contains(says), "{bad}: {err}"); | ||
| } | ||
| let err = s.run("layer.mask", &json!({"layer": dem, "mask": {"raster": dem}})).unwrap_err(); | ||
| assert!(err.contains("can't mask itself"), "{err}"); | ||
| assert_eq!((masks(&s, "Cities"), s.undo_depth()), (before, depth)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_layer_shows_only_where_the_rasters_values_are_in_its_range() { | ||
| use crate::render::View; | ||
| let (mut s, dem) = with_dem(); | ||
| // Over the DEM only, and only the countries drawn. | ||
| s.run("map.goToXY", &json!({"lon": 10.02, "lat": 45.02, "scale": 20_000})).unwrap(); | ||
| let others: Vec<String> = s.map().layers.iter().map(|l| l.name.clone()).filter(|n| n != "Countries").collect(); | ||
| for l in others { | ||
| s.run("layer.visibility", &json!({"layer": l, "visible": false})).unwrap(); | ||
| } | ||
| let frame = |s: &mut Session| { | ||
| let v = View { width: 200.0, height: 150.0, pixels_per_point: 1.0, camera: s.map().camera, sky: false }; | ||
| crate::render::render(s.map_mut(), &v).unwrap().pixmap.data().to_vec() | ||
| }; | ||
| let shown = frame(&mut s); | ||
| s.run("layer.visibility", &json!({"layer": "Countries", "visible": false})).unwrap(); | ||
| let hidden = frame(&mut s); | ||
| s.run("layer.visibility", &json!({"layer": "Countries", "visible": true})).unwrap(); | ||
| assert_ne!(shown, hidden, "the countries are drawn here"); | ||
| let mut masked = |range: Value| { | ||
| s.run("layer.mask", &json!({"layer": "Countries", "mask": range})).unwrap(); | ||
| frame(&mut s) | ||
| }; | ||
| assert!(masked(json!({"raster": dem, "min": 1000})) == hidden, "no value in the range: hidden"); | ||
| assert!(masked(json!({"raster": dem, "max": 1000})) == shown, "every value in it: shown whole"); | ||
| assert!(masked(json!({"raster": dem, "min": 1000, "inside": false})) == shown, "outside a range nothing is in"); | ||
| let hill = masked(json!({"raster": dem, "min": 250})); | ||
| assert!(hill != shown && hill != hidden, "only on the hill"); | ||
| let soft = masked(json!({"raster": dem, "min": 250, "feather": 40})); | ||
| assert!(soft != hill, "a feather fades it in below the hill's top"); | ||
| // A mask raster with no pixels (a read that failed) has no value anywhere: hidden. | ||
| let id = s.map().layer_by_name(&dem).unwrap().id; | ||
| s.map_mut().layer_mut(id).unwrap().raster.as_mut().unwrap().data = None; | ||
| s.run("layer.mask", &json!({"layer": "Countries", "mask": {"raster": dem, "max": 1000}})).unwrap(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| assert!(frame(&mut s) == hidden, "no pixels: hidden, as no value is"); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.