Rasters.jl icon indicating copy to clipboard operation
Rasters.jl copied to clipboard

`boolmask` should error if the lookup is Points but `boundary != :center`

Open asinghvi17 opened this issue 1 month ago • 1 comments

Rasters.create defaults to a point lookup instead of an interval lookup which is causing this confusion.

I think we should either error or convert to an interval lookup, if you have points X, Y dims but you try to rasterize / mask as anything but boundary = :center - it will do the same thing so your option won't be respected.

download

using Rasters
import GeoInterface as GI
using StatsBase

width, height = 10, 10

polygons = [
    GI.Polygon([[(0., 0.), (1., 0.), (1., 1.), (0., 1.), (0., 0.)]]),
    GI.Polygon([[(0.5, 0.), (1.5, 0.), (1.5, 1.), (0.5, 1.), (0.5, 0.)]]),
]

result = Rasters.rasterize(
    polygons;
    to = Rasters.Extents.grow(GI.extent(GI.GeometryCollection(polygons)), .25),
    fill = [[i] for i in 1:length(polygons)],
    missingval = Int[],
    init = Int[],
    op = vcat,
    eltype = Vector{Int},
    progress = true,
    boundary = :touches,
    size = (width, height),
)

result_bang = Rasters.create(
    Vector{Int},
    Rasters.Extents.grow(GI.extent(GI.GeometryCollection(polygons)), .25);
    size = (width, height),
    fill = Int[],
    missingval = Int[],
)

result_bang = rasterize!(
    result_bang,
    polygons;
    fill = [[i] for i in 1:length(polygons)],
    missingval = Int[],
    init = Int[],
    op = vcat,
    boundary = :touches,
    eltype = Vector{Int},
    progress = true,
)

StatsBase.countmap(result)
StatsBase.countmap(result_bang)

using CairoMakie
f, a, p = heatmap(length.(result); colorbar = false, axis = (; title = "rasterize", aspect = DataAspect()))
a2, p2 = heatmap(f[1, 2], length.(result_bang); colorbar = false, axis = (; title = "rasterize!", aspect = DataAspect()))
cb = Colorbar(f[2, 1:2], p; vertical = false, label = "length.(raster)", flipaxis = false)
f

Originally posted by @asinghvi17 in https://github.com/rafaqz/Rasters.jl/issues/1033#issuecomment-3445419345

asinghvi17 avatar Oct 25 '25 01:10 asinghvi17

Makes sense

rafaqz avatar Oct 26 '25 01:10 rafaqz