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

Add image function #14

Merged
merged 2 commits into from
Nov 25, 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
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
18 changes: 15 additions & 3 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,7 +53,7 @@ nchannels(geotiff::GeoTIFFImage) = nchannels(geotiff.tiff)

`i`'th channel of the `geotiff` image.
"""
channel(geotiff::GeoTIFFImage, i) = mappedarray(c -> channel(c, i), geotiff.tiff)
channel(geotiff::GeoTIFFImage, i) = mappedarray(c -> channel(c, i), image(geotiff))

# AbstractArray interface
Base.size(geotiff::GeoTIFFImage) = size(geotiff.tiff)
Expand Down
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.
23 changes: 23 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 format swaps axis order
geotiff = GeoTIFF.load(joinpath(datadir, "natural_earth_1.tif"))
metadata = GeoTIFF.metadata(geotiff)
@test eltype(geotiff) <: RGB
@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
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 All @@ -69,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