-
Notifications
You must be signed in to change notification settings - Fork 34
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
mask
fails to update existing missing values when missingval
is set
#505
Comments
That makes sense to me. You could also use |
julia> with = boolmask(b)
2×2 Raster{Bool,2} with dimensions: X, Y
extent: Extent(X = (1, 2), Y = (1, 2))
missingval: false
parent:
1 1
1 0
julia> Rasters.boolmask!(with, a)
2×2 Raster{Bool,2} with dimensions: X, Y
extent: Extent(X = (1, 2), Y = (1, 2))
missingval: false
parent:
1 0
1 1 If I understand this correctly, it looks like function boolmask!(dest::AbstractRaster, src::AbstractRaster; missingval=_missingval_or_missing(src))
broadcast_dims!(dest, src) do a
!isequal(a, missingval)
end
end |
I think this solution would work as well and has the advantage of avoiding additional allocations: function _mask!(A::AbstractRaster, with::AbstractRaster; missingval=missingval(A), values=A)
missingval isa Nothing && _nomissingerror()
missingval = convert(eltype(A), missingval)
broadcast_dims!(A, values, with) do x, w
if isequal(w, Rasters.missingval(with)) || isequal(x, Rasters.missingval(values))
missingval
else
convert(eltype(A), x)
end
end
return rebuild(A, missingval=missingval)
end This is the results of running in the REPL: julia> a = Raster([1.0 0.0; 1.0 1.0], dims=(X, Y), missingval=0);
julia> b = Raster([1.0 1.0; 1.0 0.0], dims=(X, Y), missingval=0);
julia> _mask!(a, b, missingval=5)
2×2 Raster{Float64,2} with dimensions: X, Y
extent: Extent(X = (1, 2), Y = (1, 2))
missingval: 5.0
parent:
1.0 5.0
1.0 5.0 |
Oh right yes that is intentional for Yes was going to say you could just do the broadcast manually its so little code. That looks ready for a PR if you can, with a test for the mixed missingval case |
Cool, I'll open a PR with the proposed changes and additional test cases. |
It seems that when
mask
is called with themissingval
keyword set to a new value, it fails to update the existing missing values in the masked array. For example:We can see that the top-right value is now considered non-missing. I think it's fair to say that this behaviour would be unexpected for most users.
I propose the following solution:
This solution should cause no changes when
missingval
is either unspecified or set to the same value. Otherwise, we add the missing values fromA
to the mask before masking. Becausemask
relies on_mask!
, this should fix the problem for bothmask
andmask!
.The text was updated successfully, but these errors were encountered: