Skip to content
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

fix error with undefined similar in polygonize #202

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading