Skip to content

Commit

Permalink
Add tests for 'metadata' function & Add 'geokeydouble' and 'geokeyasc…
Browse files Browse the repository at this point in the history
…ii' (#6)
  • Loading branch information
eliascarv authored Nov 13, 2024
1 parent b77ab8f commit ac1bbf9
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/userutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
127 changes: 113 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,15 +30,17 @@ 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)
@test eltype(geotiff) <: Gray
@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"))
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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

0 comments on commit ac1bbf9

Please sign in to comment.