Skip to content

Commit

Permalink
fix error with undefined similar in polygonize (#202)
Browse files Browse the repository at this point in the history
* call length on AbstractRange

* explicitly set length of xbounds and ybounds

* add some tests

* use range with explicit step for bounds
  • Loading branch information
tiemvanderdeure authored Sep 6, 2024
1 parent e9e024a commit 9c87f03
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/methods/polygonize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ function _polygonize(f::Base.Callable, xs::AbstractRange, ys::AbstractRange, A::
xhalf = step(xs) / 2
yhalf = step(ys) / 2
# Make bounds ranges first to avoid floating point error making gaps or overlaps
xbounds = first(xs) - xhalf : step(xs) : last(xs) + xhalf
ybounds = first(ys) - yhalf : step(ys) : last(ys) + yhalf
xbounds = range(first(xs) - xhalf; step = step(xs), length = length(xs) + 1)
ybounds = range(first(ys) - yhalf; step = step(ys), length = length(ys) + 1)
Tx = eltype(xbounds)
Ty = eltype(ybounds)
xvec = similar(Vector{Tuple{Tx,Tx}}, xs)
yvec = similar(Vector{Tuple{Ty,Ty}}, ys)
xvec = similar(Vector{Tuple{Tx,Tx}}, length(xs))
yvec = similar(Vector{Tuple{Ty,Ty}}, length(ys))
for (xind, i) in enumerate(eachindex(xvec))
xvec[i] = xbounds[xind], xbounds[xind+1]
end
Expand Down
39 changes: 38 additions & 1 deletion test/methods/polygonize.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
using GeometryOps, GeoInterface, Test
import OffsetArrays, DimensionalData, Rasters
using ..TestHelpers

import GeometryOps as GO
import GeoInterface as GI

@testset "Polygonize with xs and ys, without offsetarrays" begin
@test !(@isdefined OffsetArrays) # to make sure this isn't loaded somewhere else
data = rand(1:4, 100, 100) .== 1
unitrange = 1:100
steprange = 1:1:100
steprangelen = range(1, 100; length = 100)
data_mp = polygonize(data)
for range in (unitrange, steprange, steprangelen)
data_mp_range = polygonize(range, range, data)
@test GO.equals(data_mp, data_mp_range)
end
# ideally we'd have a better test to make sure this returns what we think it does
data_mp_range200 = polygonize(2:2:200, 2:2:200, data)
@test length(GI.coordinates(data_mp_range200)) == length(GI.coordinates(data_mp))

# this is an example that could throw floating point error
range_floats = -1.333333333333343:0.041666666666666664:0.374999999999986
data2 = rand(1:4, length(range_floats), length(range_floats)) .== 1
data_mp_range_floats = polygonize(range_floats, range_floats, data2)
end

import OffsetArrays, DimensionalData, Rasters

# Missing holes throw a warning, so testing there are
# no warnings in a range of randomisation is one way to test
# that things are working, without testing explicit return values
Expand Down Expand Up @@ -68,3 +93,15 @@ end
@test GI.crs(evil_mp) == GI.crs(evil)
end
end

@testset "Polygonize with xs and ys, with offsetarrays" begin
data = rand(1:4, 100, 100) .== 1
unitrange = 1:100
steprange = 1:1:100
steprangelen = range(1, 100; length = 100)
data_mp = polygonize(data)
for range in (unitrange, steprange, steprangelen)
data_mp_range = polygonize(range, range, data)
@test GO.equals(data_mp, data_mp_range)
end
end

0 comments on commit 9c87f03

Please sign in to comment.