From 8172907d38fa39c14090ae4790f5ecd6cda004b5 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 9 Dec 2024 11:21:54 -0300 Subject: [PATCH] [WIP] Handle TIFFs with more than one image --- src/GeoTIFF.jl | 2 +- src/image.jl | 11 +++++++++-- src/load.jl | 15 +++++++++++---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/GeoTIFF.jl b/src/GeoTIFF.jl index d4b85bb..42a138b 100644 --- a/src/GeoTIFF.jl +++ b/src/GeoTIFF.jl @@ -4,7 +4,7 @@ module GeoTIFF -using TiffImages: AbstractTIFF, DenseTaggedImage, WidePixel +using TiffImages: AbstractTIFF, DenseTaggedImage, StridedTaggedImage, WidePixel using ColorTypes: Colorant, Gray using MappedArrays: mappedarray using StaticArrays: SVector, SMatrix, SA diff --git a/src/image.jl b/src/image.jl index 28a32e9..2ce503f 100644 --- a/src/image.jl +++ b/src/image.jl @@ -15,11 +15,15 @@ to get the metadata and the image with corrected axes, respectively. * 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} +struct GeoTIFFImage{T,I<:AbstractMatrix{T}} <: AbstractMatrix{T} tiff::I metadata::Metadata end +struct GeoTIFFImages{I<:GeoTIFFImage} <: AbstractVector{I} + geotiffs::Vector{I} +end + """ GeoTIFF.tiff(geotiff) @@ -59,4 +63,7 @@ channel(geotiff::GeoTIFFImage, i) = mappedarray(c -> channel(c, i), image(geotif 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) +Base.IndexStyle(::Type{GeoTIFFImage{T,I}}) where {T,I} = IndexStyle(I) + +Base.size(geotiff::GeoTIFFImages) = size(geotiff.geotiffs) +Base.getindex(geotiff::GeoTIFFImages, i) = getindex(geotiff.geotiffs, i) diff --git a/src/load.jl b/src/load.jl index 713fe53..37fc9f8 100644 --- a/src/load.jl +++ b/src/load.jl @@ -15,17 +15,24 @@ The keyword arguments are forward to the `TiffImages.load` function. * [`ModelTransformation`](@ref) with `A` as identity matrix and `b` as vector of zeros. """ function load(fname; kwargs...) + geotiff(img, ifd) = GeoTIFFImage(img, _getmetadata(ifd)) tiff = TiffImages.load(fname; kwargs...) - metadata = _getmetadata(tiff) - GeoTIFFImage(tiff, metadata) + ifds = TiffImages.ifds(tiff) + if tiff isa StridedTaggedImage + GeoTIFFImages(geotiff.(tiff, ifds)) + elseif ndims(tiff) == 3 + imgs = eachslice(tiff, dims=3) + GeoTIFFImages(geotiff.(imgs, ifds)) + else + geotiff(tiff, ifds) + end end # ----------------- # HELPER FUNCTIONS # ----------------- -function _getmetadata(tiff) - ifd = TiffImages.ifds(tiff) +function _getmetadata(ifd) geokeydirectory = _gettag(ifd, GeoKeyDirectoryTag, GeoKeyDirectory) geodoubleparams = _gettag(ifd, GeoDoubleParamsTag, GeoDoubleParams) geoasciiparams = _gettag(ifd, GeoAsciiParamsTag, GeoAsciiParams)