Skip to content

Commit

Permalink
Add 'image' function
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Nov 25, 2024
1 parent e432807 commit b490915
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ julia> metadata = GeoTIFF.metadata(geotiff)
GeoTIFF.Metadata(...)
```

And use the `GeoTIFF.image` function to get the image with corrected axes:

```julia
julia> GeoTIFF.image(geotiff)
100×100 PermutedDimsArray(::TiffImages.DenseTaggedImage{...}, (2, 1)) with eltype RGB{N0f16}:
RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0)
RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0)
RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0)
RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0)
RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0) RGB(1.0, 1.0, 1.0)
```

GeoTIFF.jl defines several utilities to easily retrieve metadata information:

```julia
Expand Down
27 changes: 18 additions & 9 deletions src/image.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
Image type returned by the [`GeoTIFF.load`](@ref) function.
See the [`GeoTIFF.tiff`](@ref) and [`GeoTIFF.metadata`](@ref) functions
to get the TIFF image and metadata respectively.
See the [`GeoTIFF.metadata`](@ref) and [`GeoTIFF.image`](@ref) functions
to get the metadata and the image with corrected axes, respectively.
### Notes
* The [`GeoTIFF.image`](@ref) function is necessary because
the GeoTIFF format swaps the order of the image axes;
"""
struct GeoTIFFImage{T,N,I<:AbstractTIFF{T,N}} <: AbstractArray{T,N}
tiff::I
Expand All @@ -29,6 +34,13 @@ GeoTIFF metadata of the `geotiff` image.
"""
metadata(geotiff::GeoTIFFImage) = geotiff.metadata

"""
GeoTIFF.image(geotiff)
Image of the `geotiff` with corrected axis.
"""
image(geotiff::GeoTIFFImage) = PermutedDimsArray(geotiff.tiff, (2, 1))

"""
GeoTIFF.nchannels(geotiff)
Expand All @@ -41,13 +53,10 @@ nchannels(geotiff::GeoTIFFImage) = nchannels(geotiff.tiff)
`i`'th channel of the `geotiff` image.
"""
function channel(geotiff::GeoTIFFImage, i)
C = mappedarray(c -> channel(c, i), geotiff.tiff)
PermutedDimsArray(C, (2, 1))
end
channel(geotiff::GeoTIFFImage, i) = mappedarray(c -> channel(c, i), image(geotiff))

# AbstractArray interface
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.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.IndexStyle(::Type{GeoTIFFImage{T,N,I}}) where {T,N,I} = IndexStyle(I)
13 changes: 10 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ savedir = mktempdir()
b = [688258.223819, 4.555765966137e6]
@test GeoTIFF.affineparams2D(metadata) == (A, b)

# GeoTIFF permutes the image by default
# GeoTIFF format swaps axis order
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 size(geotiff) == (81, 162)
@test size(GeoTIFF.image(geotiff)) == (162, 81)
@test GeoTIFF.rastertype(metadata) == GeoTIFF.PixelIsArea
@test GeoTIFF.modeltype(metadata) == GeoTIFF.Geographic2D
@test GeoTIFF.epsgcode(metadata) == 4326
Expand All @@ -85,6 +85,13 @@ savedir = mktempdir()
@test geotiff[1, 1] == color
@test IndexStyle(geotiff) === IndexCartesian()

# image function
geotiff = GeoTIFF.load(joinpath(datadir, "natural_earth_1.tif"))
img = GeoTIFF.image(geotiff)
@test size(img) == reverse(size(geotiff))
@test eltype(img) === eltype(geotiff)
@test img == permutedims(geotiff, (2, 1))

# multi-channel image
file = joinpath(savedir, "multi.tiff")
channel1 = rand(10, 10)
Expand Down

0 comments on commit b490915

Please sign in to comment.