Skip to content

Commit

Permalink
Permute the TIFF dimensions by default
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Nov 22, 2024
1 parent f87939c commit e432807
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/image.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ nchannels(geotiff::GeoTIFFImage) = nchannels(geotiff.tiff)
`i`'th channel of the `geotiff` image.
"""
channel(geotiff::GeoTIFFImage, i) = mappedarray(c -> channel(c, i), geotiff.tiff)
function channel(geotiff::GeoTIFFImage, i)
C = mappedarray(c -> channel(c, i), geotiff.tiff)
PermutedDimsArray(C, (2, 1))
end

# AbstractArray interface
Base.size(geotiff::GeoTIFFImage) = size(geotiff.tiff)
Base.getindex(geotiff::GeoTIFFImage, i...) = getindex(geotiff.tiff, i...)
Base.setindex!(geotiff::GeoTIFFImage, v, i...) = setindex!(geotiff.tiff, v, i...)
Base.size(geotiff::GeoTIFFImage) = reverse(size(geotiff.tiff))
Base.getindex(geotiff::GeoTIFFImage, i, j) = getindex(geotiff.tiff, j, i)
Base.setindex!(geotiff::GeoTIFFImage, v, i, j) = setindex!(geotiff.tiff, v, j, i)
Base.IndexStyle(::Type{GeoTIFFImage{T,N,I}}) where {T,N,I} = IndexStyle(I)
5 changes: 4 additions & 1 deletion src/save.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ end
Save in the file `fname` an image (array of colors) `img` with passed GeoTIFF `metadata`.
"""
save(fname, img::AbstractArray{<:WidePixelOrColorant}; kwargs...) = save(fname, DenseTaggedImage(img); kwargs...)
function save(fname, img::AbstractArray{<:WidePixelOrColorant}; kwargs...)
tiff = DenseTaggedImage(PermutedDimsArray(img, (2, 1)))
save(fname, tiff; kwargs...)
end

"""
GeoTIFF.save(fname, channels...; metadata=GeoTIFF.Metadata())
Expand Down
Binary file added test/data/natural_earth_1.tif
Binary file not shown.
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ savedir = mktempdir()
geotiff = GeoTIFF.load(joinpath(datadir, "test.tif"))
metadata = GeoTIFF.metadata(geotiff)
@test eltype(geotiff) <: RGB
@test size(geotiff) == (100, 100)
@test isnothing(GeoTIFF.rastertype(metadata))
@test isnothing(GeoTIFF.modeltype(metadata))
@test isnothing(GeoTIFF.epsgcode(metadata))
Expand All @@ -36,6 +37,7 @@ savedir = mktempdir()
geotiff = GeoTIFF.load(joinpath(datadir, "test_gray.tif"))
metadata = GeoTIFF.metadata(geotiff)
@test eltype(geotiff) <: Gray
@test size(geotiff) == (108, 108)
@test isnothing(GeoTIFF.rastertype(metadata))
@test isnothing(GeoTIFF.modeltype(metadata))
@test isnothing(GeoTIFF.epsgcode(metadata))
Expand All @@ -46,12 +48,26 @@ savedir = mktempdir()
geotiff = GeoTIFF.load(joinpath(datadir, "utm.tif"))
metadata = GeoTIFF.metadata(geotiff)
@test eltype(geotiff) <: RGB
@test size(geotiff) == (100, 100)
@test GeoTIFF.rastertype(metadata) == GeoTIFF.PixelIsArea
@test GeoTIFF.modeltype(metadata) == GeoTIFF.Projected2D
@test GeoTIFF.epsgcode(metadata) == 32617
A = [121.52985600000001 0.0; 0.0 -164.762688]
b = [688258.223819, 4.555765966137e6]
@test GeoTIFF.affineparams2D(metadata) == (A, b)

# GeoTIFF permutes the image by default
geotiff = GeoTIFF.load(joinpath(datadir, "natural_earth_1.tif"))
metadata = GeoTIFF.metadata(geotiff)
@test eltype(geotiff) <: RGB
@test size(geotiff) == (162, 81)
@test size(geotiff) == reverse(size(GeoTIFF.tiff(geotiff)))
@test GeoTIFF.rastertype(metadata) == GeoTIFF.PixelIsArea
@test GeoTIFF.modeltype(metadata) == GeoTIFF.Geographic2D
@test GeoTIFF.epsgcode(metadata) == 4326
A = [2.222222222222001 0.0; 0.0 -2.222222222222001]
b = [-180.0, 90.0]
@test GeoTIFF.affineparams2D(metadata) == (A, b)
end

@testset "GeoTIFFImage" begin
Expand Down

0 comments on commit e432807

Please sign in to comment.