rafaqz / Rasters.jl

Raster manipulation for the Julia language
MIT License
214 stars 36 forks source link

behaviour of `crop` to a `Dimension` #712

Closed tiemvanderdeure closed 3 months ago

tiemvanderdeure commented 3 months ago

Crop is (too?) picky about what dimensions it will crop to.

MWE:

xdim = X(1.0:1:2.0); ydim = Y(1.0:1:2.0)
ga = Raster(A, (xdim, ydim); missingval=missing) 
crop(ga; to = dims(ga, X))
crop(ga; to = xdim)

Here the latter crop errors, but the former works, which is kind of weird. It seems to be due to this:

lookup(dims(ga, X)) isa Rasters.AbstractSampled # is true
lookup(xdim) isa Rasters.AbstractSampled # is false

But I don't really know why one is a sampled and the other is not?

The error message also isn't particularly helpful:

ERROR: UndefKeywordError: keyword argument `geometrycolumn` not assigned
Stacktrace:
 [1] _crop_to(x::Raster{…}, to::Tuple{…}; touches::Bool, kw::@Kwargs{…})
   @ Rasters ~/juliadev/Rasters/src/methods/crop_extend.jl:104
 [2] _crop_to(x::Raster{…}, to::X{…}; kw::@Kwargs{…})
   @ Rasters ~/juliadev/Rasters/src/methods/crop_extend.jl:98
 [3] crop(x::Raster{…}; to::X{…}, geometrycolumn::Nothing, kw::@Kwargs{})
   @ Rasters ~/juliadev/Rasters/src/methods/crop_extend.jl:82
 [4] top-level scope
felixcremer commented 3 months ago

I suspect the difference is because the Raster constructor calls Dimensions.format on the input in line 262 of array.jl

  A = Raster(A, Dimensions.format(dims, A), refdims, name, metadata, missingval)
rafaqz commented 3 months ago

Yeah thats a weird error, maybe some assumptions being broken because X(1.0:1:2.0) doesn't hold a Lookup from lack of format as @felixcremer mentions

rafaqz commented 3 months ago

Turns out it works fine if we fix the keyword oops no it doesn't

We need to call format on it so it has an Extent - a range on its own has no extent because we don't know if its points or intervals or categories or whatever.

rafaqz commented 3 months ago

Ok and format compiles away if its already a Lookup so I'll just add that in.