From ac1bbf949c9fdeae0a98c19465d27a382f421263 Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:09:19 -0300 Subject: [PATCH] Add tests for 'metadata' function & Add 'geokeydouble' and 'geokeyascii' (#6) --- src/userutils.jl | 21 ++++++++ test/runtests.jl | 127 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 134 insertions(+), 14 deletions(-) diff --git a/src/userutils.jl b/src/userutils.jl index 394af30..f628580 100644 --- a/src/userutils.jl +++ b/src/userutils.jl @@ -13,6 +13,27 @@ function geokeyvalue(metadata::Metadata, id::GeoKeyID) isnothing(gk) ? nothing : gk.value end +function geokeydouble(metadata::Metadata, id::GeoKeyID) + dp = metadata.geodoubleparams + gk = geokey(metadata, id) + if isnothing(dp) || isnothing(gk) || gk.count > 1 + nothing + else + dp.params[gk.value + 1] + end +end + +function geokeyascii(metadata::Metadata, id::GeoKeyID) + ap = metadata.geoasciiparams + gk = geokey(metadata, id) + if isnothing(ap) || isnothing(gk) + nothing + else + str = ap.params[(gk.value + 1):(gk.value + gk.count)] + rstrip(str, '|') # terminator + end +end + rastertype(metadata::Metadata) = geokeyvalue(metadata, GTRasterTypeGeoKey) modeltype(metadata::Metadata) = geokeyvalue(metadata, GTModelTypeGeoKey) diff --git a/test/runtests.jl b/test/runtests.jl index 19fdc2e..1d3a2bb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,12 +9,18 @@ savedir = mktempdir() @testset "GeoTIFF.jl" begin # default affine parameters - A = [ + A2D = [ + 1.0 0.0 + 0.0 1.0 + ] + b2D = [0.0, 0.0] + + A3D = [ 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 ] - b = [0.0, 0.0, 0.0] + b3D = [0.0, 0.0, 0.0] @testset "load" begin # tiff files without metadata @@ -24,7 +30,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) geotiff = GeoTIFF.load(joinpath(datadir, "test_gray.tif")) metadata = GeoTIFF.metadata(geotiff) @@ -32,7 +39,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) # tiff files with metadata geotiff = GeoTIFF.load(joinpath(datadir, "utm.tif")) @@ -41,8 +49,9 @@ savedir = mktempdir() @test GeoTIFF.rastertype(metadata) == GeoTIFF.PixelIsArea @test GeoTIFF.modeltype(metadata) == GeoTIFF.Projected2D @test GeoTIFF.epsgcode(metadata) == 32617 - @test GeoTIFF.affineparams2D(metadata) == - ([121.52985600000001 0.0; 0.0 -164.762688], [688258.223819, 4.555765966137e6]) + A = [121.52985600000001 0.0; 0.0 -164.762688] + b = [688258.223819, 4.555765966137e6] + @test GeoTIFF.affineparams2D(metadata) == (A, b) end @testset "GeoTIFFImage" begin @@ -58,6 +67,7 @@ savedir = mktempdir() color = RGB(1.0, 0.0, 0.0) geotiff[1, 1] = color @test geotiff[1, 1] == color + @test IndexStyle(geotiff) === IndexCartesian() end @testset "save" begin @@ -111,7 +121,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) # array of colors file = joinpath(savedir, "array.tiff") @@ -123,7 +134,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) # channel arrays # float @@ -138,7 +150,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) # multiple channels file = joinpath(savedir, "float_multi.tiff") channel1 = rand(10, 10) @@ -153,7 +166,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) # int # single channel @@ -167,7 +181,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) # multiple channels file = joinpath(savedir, "int_multi.tiff") channel1 = rand(1:10, 10, 10) @@ -182,7 +197,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) # uint # single channel @@ -196,7 +212,8 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) # multiple channels file = joinpath(savedir, "uint_multi.tiff") channel1 = rand(UInt(1):UInt(10), 10, 10) @@ -211,6 +228,88 @@ savedir = mktempdir() @test isnothing(GeoTIFF.rastertype(metadata)) @test isnothing(GeoTIFF.modeltype(metadata)) @test isnothing(GeoTIFF.epsgcode(metadata)) - @test GeoTIFF.affineparams3D(metadata) == (A, b) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) + end + + @testset "metadata" begin + # default + metadata = GeoTIFF.metadata() + @test isnothing(GeoTIFF.rastertype(metadata)) + @test isnothing(GeoTIFF.modeltype(metadata)) + @test isnothing(GeoTIFF.epsgcode(metadata)) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) + + # transformation + θ = π / 4 + A = [cos(θ) -sin(θ); sin(θ) cos(θ)] + b = [10.0, 50.0] + metadata = GeoTIFF.metadata(transformation=(A, b)) + @test isnothing(GeoTIFF.rastertype(metadata)) + @test isnothing(GeoTIFF.modeltype(metadata)) + @test isnothing(GeoTIFF.epsgcode(metadata)) + @test GeoTIFF.affineparams2D(metadata) == (A, b) + + # tiepoint and pixelscale + metadata = GeoTIFF.metadata(tiepoint=(0.0, 0.0, 0.0, 0.0, 0.0, 0.0), pixelscale=(1.0, -1.0, 1.0)) + @test isnothing(GeoTIFF.rastertype(metadata)) + @test isnothing(GeoTIFF.modeltype(metadata)) + @test isnothing(GeoTIFF.epsgcode(metadata)) + @test GeoTIFF.affineparams2D(metadata) == (A2D, b2D) + @test GeoTIFF.affineparams3D(metadata) == (A3D, b3D) + + # EPSG CRS + # WGS 84: https://epsg.io/4326 + metadata = GeoTIFF.metadata(rastertype=GeoTIFF.PixelIsArea, modeltype=GeoTIFF.Geographic2D, geodeticcrs=4326) + @test GeoTIFF.rastertype(metadata) == GeoTIFF.PixelIsArea + @test GeoTIFF.modeltype(metadata) == GeoTIFF.Geographic2D + @test GeoTIFF.epsgcode(metadata) == 4326 + + # Web Mercator: https://epsg.io/3857 + metadata = GeoTIFF.metadata(rastertype=GeoTIFF.PixelIsArea, modeltype=GeoTIFF.Projected2D, projectedcrs=3857) + @test GeoTIFF.rastertype(metadata) == GeoTIFF.PixelIsArea + @test GeoTIFF.modeltype(metadata) == GeoTIFF.Projected2D + @test GeoTIFF.epsgcode(metadata) == 3857 + + # citation + metadata = GeoTIFF.metadata( + rastertype=GeoTIFF.PixelIsArea, + modeltype=GeoTIFF.Geographic2D, + geodeticcrs=4326, + citation="Test citation" + ) + @test GeoTIFF.rastertype(metadata) == GeoTIFF.PixelIsArea + @test GeoTIFF.modeltype(metadata) == GeoTIFF.Geographic2D + @test GeoTIFF.epsgcode(metadata) == 4326 + @test GeoTIFF.geokeyascii(metadata, GeoTIFF.GTCitationGeoKey) == "Test citation" + + # user defined CRS + metadata = GeoTIFF.metadata( + rastertype=GeoTIFF.PixelIsArea, + modeltype=GeoTIFF.Projected2D, + projectedcrs=GeoTIFF.UserDefined, + geodeticcrs=4326, # WGS 84: https://epsg.io/4326 + citation="Test citation", + projectedcitation="TransverseMercator", + projlinearunits=9001, # EPSG meters: https://epsg.org/unit_9001/metre.html + projection=GeoTIFF.UserDefined, + projmethod=1, # GeoTIFF Transverse Mercator: https://docs.ogc.org/is/19-008r4/19-008r4.html#_map_projection_methods + projscaleatnatorigin=0.9996, # scale factor + projnatoriginlat=15.0, # latitude of origin + projnatoriginlong=25.0 # longitude of origin + ) + @test GeoTIFF.rastertype(metadata) == GeoTIFF.PixelIsArea + @test GeoTIFF.modeltype(metadata) == GeoTIFF.Projected2D + @test GeoTIFF.geokeyvalue(metadata, GeoTIFF.ProjectedCRSGeoKey) == GeoTIFF.UserDefined + @test GeoTIFF.geokeyvalue(metadata, GeoTIFF.GeodeticCRSGeoKey) == 4326 + @test GeoTIFF.geokeyascii(metadata, GeoTIFF.GTCitationGeoKey) == "Test citation" + @test GeoTIFF.geokeyascii(metadata, GeoTIFF.ProjectedCitationGeoKey) == "TransverseMercator" + @test GeoTIFF.geokeyvalue(metadata, GeoTIFF.ProjLinearUnitsGeoKey) == 9001 + @test GeoTIFF.geokeyvalue(metadata, GeoTIFF.ProjectionGeoKey) == GeoTIFF.UserDefined + @test GeoTIFF.geokeyvalue(metadata, GeoTIFF.ProjMethodGeoKey) == 1 + @test GeoTIFF.geokeydouble(metadata, GeoTIFF.ProjScaleAtNatOriginGeoKey) == 0.9996 + @test GeoTIFF.geokeydouble(metadata, GeoTIFF.ProjNatOriginLatGeoKey) == 15.0 + @test GeoTIFF.geokeydouble(metadata, GeoTIFF.ProjNatOriginLongGeoKey) == 25.0 end end