From 1fea23f09cd7637821c08560683f4e3b905c8d91 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Tue, 7 Jan 2025 10:34:33 +0100 Subject: [PATCH 01/10] add a dispatch to _extent so it returns itself --- src/methods/burning/extents.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/methods/burning/extents.jl b/src/methods/burning/extents.jl index e1b308296..01cac8b26 100644 --- a/src/methods/burning/extents.jl +++ b/src/methods/burning/extents.jl @@ -50,6 +50,7 @@ function _extent(::GI.AbstractFeatureCollectionTrait, features; kw...)::XYExtent end return _float64_xy_extent(ext) end +_extent(ext::XYExtent; kw...) = ext function _float64_xy_extent(ext::Extents.Extent) xbounds = map(Float64, ext.X) From 96a0e223baff9781a8405630efeca33d3715e2fc Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Tue, 7 Jan 2025 12:25:33 +0100 Subject: [PATCH 02/10] Update src/methods/burning/extents.jl --- src/methods/burning/extents.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/methods/burning/extents.jl b/src/methods/burning/extents.jl index 01cac8b26..63034a46d 100644 --- a/src/methods/burning/extents.jl +++ b/src/methods/burning/extents.jl @@ -50,7 +50,7 @@ function _extent(::GI.AbstractFeatureCollectionTrait, features; kw...)::XYExtent end return _float64_xy_extent(ext) end -_extent(ext::XYExtent; kw...) = ext +_extent(ext::XYExtent; kw...) = _float64_xy_extent(ext) function _float64_xy_extent(ext::Extents.Extent) xbounds = map(Float64, ext.X) From 59d7b12a1136bece6fe50704e4439bea5b1da63f Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Tue, 7 Jan 2025 12:26:45 +0100 Subject: [PATCH 03/10] Update src/methods/burning/extents.jl --- src/methods/burning/extents.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/methods/burning/extents.jl b/src/methods/burning/extents.jl index 63034a46d..2a957d362 100644 --- a/src/methods/burning/extents.jl +++ b/src/methods/burning/extents.jl @@ -50,7 +50,7 @@ function _extent(::GI.AbstractFeatureCollectionTrait, features; kw...)::XYExtent end return _float64_xy_extent(ext) end -_extent(ext::XYExtent; kw...) = _float64_xy_extent(ext) +_extent(ext::Extent; kw...) = _float64_xy_extent(ext) function _float64_xy_extent(ext::Extents.Extent) xbounds = map(Float64, ext.X) From 749dd3310f810c14d3a9670ca1397cae8ffdf907 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Tue, 7 Jan 2025 12:29:46 +0100 Subject: [PATCH 04/10] test extent --- test/methods.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/methods.jl b/test/methods.jl index 81823e524..a15e6e3eb 100644 --- a/test/methods.jl +++ b/test/methods.jl @@ -762,3 +762,10 @@ test = rebuild(ga; name = :test) @test_throws "strictly positive" Rasters.sample(StableRNG(123), test, 3, skipmissing = true, replace = false) @test_throws "Cannot draw" Rasters.sample(StableRNG(123), test, 5, replace = false) end + +@testset "extent" begin + ga = Raster(A, (X(1.0:1:2.0), Y(1.0:1:2.0)); missingval=missing) + ext = extent(ga) + @test ext === Extent(X=(1.0,2.0), Y=(1.0,2.0)) + @test extent(ext) === ext +end \ No newline at end of file From 11256938acd5e31ed9e704f4f74698aa47b14278 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Tue, 7 Jan 2025 12:30:05 +0100 Subject: [PATCH 05/10] uncomment a test that isn't broken anymore --- test/resample.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/resample.jl b/test/resample.jl index 27067984e..7a76863c9 100644 --- a/test/resample.jl +++ b/test/resample.jl @@ -64,9 +64,7 @@ include(joinpath(dirname(pathof(Rasters)), "../test/test_utils.jl")) resampled = resample(cea; method) @test crs(cea) == crs(resampled) @test cea == resampled - # There is some floating point error here after Rasters -> GDAL -> Rasterss... - # Should we correct it by detecting almost identical extent and using the original? - # @test_broken extent(cea) == extent(resampled) + @test extent(cea) == extent(resampled) end @testset "only `res` kw changes the array size predictably" begin From 5312187515b56a98305f0e8b26e87d8d78c0aabe Mon Sep 17 00:00:00 2001 From: Tiem van der Deure Date: Tue, 7 Jan 2025 12:37:17 +0100 Subject: [PATCH 06/10] use Rasters._extent Co-authored-by: Rafael Schouten --- test/methods.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/methods.jl b/test/methods.jl index a15e6e3eb..15fd1d51b 100644 --- a/test/methods.jl +++ b/test/methods.jl @@ -767,5 +767,5 @@ end ga = Raster(A, (X(1.0:1:2.0), Y(1.0:1:2.0)); missingval=missing) ext = extent(ga) @test ext === Extent(X=(1.0,2.0), Y=(1.0,2.0)) - @test extent(ext) === ext + @test Rasters._extent(ext) === ext end \ No newline at end of file From e8e0adde430bd8438dde5255bd297755830d853d Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Wed, 8 Jan 2025 11:33:02 +0100 Subject: [PATCH 07/10] forward keywords --- src/methods/burning/extents.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/methods/burning/extents.jl b/src/methods/burning/extents.jl index 2a957d362..0ff79685e 100644 --- a/src/methods/burning/extents.jl +++ b/src/methods/burning/extents.jl @@ -12,7 +12,7 @@ function _extent(::Nothing, data; geometrycolumn, kw...)::XYExtent return _float64_xy_extent(Extents.Extent(X=xs, Y=ys)) else ext = reduce(geoms; init=_extent(first(geoms))) do ext, geom - Extents.union(ext, _extent(geom)) + Extents.union(ext, _extent(geom; kw...)) end return _float64_xy_extent(ext) end @@ -36,17 +36,17 @@ end _extent(::GI.AbstractFeatureTrait, feature; kw...)::XYExtent = _extent(GI.geometry(feature); kw...) function _extent(::GI.GeometryCollectionTrait, collection; kw...)::XYExtent geometries = GI.getgeom(collection) - init = _float64_xy_extent(_extent(first(geometries))) + init = _float64_xy_extent(_extent(first(geometries); kw...)) ext = reduce(geometries; init) do acc, g - Extents.union(acc, _extent(g)) + Extents.union(acc, _extent(g; kw...)) end return _float64_xy_extent(ext) end function _extent(::GI.AbstractFeatureCollectionTrait, features; kw...)::XYExtent features = GI.getfeature(features) - init = _float64_xy_extent(_extent(first(features))) + init = _float64_xy_extent(_extent(first(features); kw...)) ext = reduce(features; init) do acc, f - Extents.union(acc, _extent(f)) + Extents.union(acc, _extent(f; kw...)) end return _float64_xy_extent(ext) end From 3aff33f3a380952cd63dc6393265a8b716749b48 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Wed, 8 Jan 2025 12:27:47 +0100 Subject: [PATCH 08/10] make better use of _get_geometries --- src/methods/burning/extents.jl | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/methods/burning/extents.jl b/src/methods/burning/extents.jl index 0ff79685e..1024adf4c 100644 --- a/src/methods/burning/extents.jl +++ b/src/methods/burning/extents.jl @@ -1,18 +1,20 @@ const XYExtent = Extents.Extent{(:X,:Y),Tuple{Tuple{Float64,Float64},Tuple{Float64,Float64}}} # Get the bounds of a geometry -_extent(geom; kw...)::XYExtent = _extent(GI.trait(geom), geom; kw...) -function _extent(::Nothing, data; geometrycolumn, kw...)::XYExtent +function _extent(data; geometrycolumn=nothing, kw...)::XYExtent geoms = _get_geometries(data, geometrycolumn) - + _extent(GI.trait(geoms), geoms) +end +function _extent(::Nothing, geoms)::XYExtent + # because geoms was returned from _get_geometries, it must be an iterable of valid geometries g1 = first(geoms) if GI.trait(g1) isa GI.PointTrait xs = extrema(p -> GI.x(p), geoms) ys = extrema(p -> GI.y(p), geoms) return _float64_xy_extent(Extents.Extent(X=xs, Y=ys)) else - ext = reduce(geoms; init=_extent(first(geoms))) do ext, geom - Extents.union(ext, _extent(geom; kw...)) + ext = reduce(geoms; init=_extent(GI.trait(g1), g1)) do ext, geom + Extents.union(ext, _extent(GI.trait(geom), geom)) end return _float64_xy_extent(ext) end @@ -33,23 +35,7 @@ function _extent(::GI.AbstractGeometryTrait, geom; kw...)::XYExtent return _float64_xy_extent(geomextent) end end -_extent(::GI.AbstractFeatureTrait, feature; kw...)::XYExtent = _extent(GI.geometry(feature); kw...) -function _extent(::GI.GeometryCollectionTrait, collection; kw...)::XYExtent - geometries = GI.getgeom(collection) - init = _float64_xy_extent(_extent(first(geometries); kw...)) - ext = reduce(geometries; init) do acc, g - Extents.union(acc, _extent(g; kw...)) - end - return _float64_xy_extent(ext) -end -function _extent(::GI.AbstractFeatureCollectionTrait, features; kw...)::XYExtent - features = GI.getfeature(features) - init = _float64_xy_extent(_extent(first(features); kw...)) - ext = reduce(features; init) do acc, f - Extents.union(acc, _extent(f; kw...)) - end - return _float64_xy_extent(ext) -end + _extent(ext::Extent; kw...) = _float64_xy_extent(ext) function _float64_xy_extent(ext::Extents.Extent) From adf1aacca6a24863b5b14c5bf4bcff1aff061260 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Wed, 15 Jan 2025 12:09:20 +0100 Subject: [PATCH 09/10] add a test for float32 --- test/methods.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/methods.jl b/test/methods.jl index 15fd1d51b..24909d7d3 100644 --- a/test/methods.jl +++ b/test/methods.jl @@ -768,4 +768,8 @@ end ext = extent(ga) @test ext === Extent(X=(1.0,2.0), Y=(1.0,2.0)) @test Rasters._extent(ext) === ext + + ga2 = Raster(A, (X(Float32.(1:2)), Y(Float32.(1:2)))) + @test ext2 === Extent(X=(1.0f0,2.0f0), Y=(1.0f0,2.0f0)) + @test Rasters._extent(ext2) === ext # currently this converts to float64! end \ No newline at end of file From 87a371f37fed25ee564cbec2a09bebb3718b1830 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Wed, 15 Jan 2025 14:17:45 +0100 Subject: [PATCH 10/10] fix test --- test/methods.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/methods.jl b/test/methods.jl index 24909d7d3..2a78e91f8 100644 --- a/test/methods.jl +++ b/test/methods.jl @@ -770,6 +770,7 @@ end @test Rasters._extent(ext) === ext ga2 = Raster(A, (X(Float32.(1:2)), Y(Float32.(1:2)))) + ext2 = extent(ga2) @test ext2 === Extent(X=(1.0f0,2.0f0), Y=(1.0f0,2.0f0)) @test Rasters._extent(ext2) === ext # currently this converts to float64! end \ No newline at end of file