Skip to content

Commit

Permalink
Add utility functions to easily extract values from metadata (#3)
Browse files Browse the repository at this point in the history
* Add utility functions to easily extract values from metadata

* Add 'affineparams2D'

* Fix code
  • Loading branch information
eliascarv authored Nov 7, 2024
1 parent 5cd0186 commit 0d12b32
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/GeoTIFF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import TiffImages

include("geokeys.jl")
include("metadata.jl")
include("userutils.jl")
include("image.jl")
include("load.jl")
include("save.jl")
Expand Down
8 changes: 4 additions & 4 deletions src/metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ GeoKeyDirectory(; version=1, revision=1, minor=1, geokeys=GeoKey[]) =
function GeoKeyDirectory(params::Vector{UInt16})
nkeys = params[4]
geokeys = map((1:nkeys) * 4) do i
id = GeoKeyID(geokey[1 + i])
tag = geokey[2 + i]
count = geokey[3 + i]
value = geokey[4 + i]
id = GeoKeyID(params[1 + i])
tag = params[2 + i]
count = params[3 + i]
value = params[4 + i]
GeoKey(id, tag, count, value)
end

Expand Down
80 changes: 80 additions & 0 deletions src/userutils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -----------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# -----------------------------------------------------------------

function geokey(metadata::Metadata, id::GeoKeyID)
geokeys = metadata.geokeydirectory.geokeys
i = findfirst(geokey -> geokey.id == id, geokeys)
isnothing(i) ? nothing : geokeys[i]
end

function geokeyvalue(metadata::Metadata, id::GeoKeyID)
gk = geokey(metadata, id)
isnothing(gk) ? nothing : gk.value
end

rastertype(metadata::Metadata) = geokeyvalue(metadata, GTRasterTypeGeoKey)

modeltype(metadata::Metadata) = geokeyvalue(metadata, GTModelTypeGeoKey)

function epsgcode(metadata::Metadata)
mt = modeltype(metadata)
isnothing(mt) && return nothing
if mt == Projected2D
geokeyvalue(metadata, ProjectedCRSGeoKey)
elseif mt == Geographic2D || mt == Geocentric3D
geokeyvalue(metadata, GeodeticCRSGeoKey)
else
# Undefined or UserDefined
nothing
end
end

function affineparams2D(metadata::Metadata)
pixelscale = metadata.modelpixelscale
tiepoint = metadata.modeltiepoint
transformation = metadata.modeltransformation
if !isnothing(pixelscale) && !isnothing(tiepoint)
sx = pixelscale.x
sy = pixelscale.y
(; i, j, x, y) = tiepoint
tx = x - i / sx
ty = y + j / sy
A = [
sx 0
0 -sy
]
b = [tx, ty]
A, b
elseif !isnothing(transformation)
transformation.A[1:2, 1:2], transformation.b[1:2]
else
nothing
end
end

function affineparams3D(metadata::Metadata)
pixelscale = metadata.modelpixelscale
tiepoint = metadata.modeltiepoint
transformation = metadata.modeltransformation
if !isnothing(pixelscale) && !isnothing(tiepoint)
sx = pixelscale.x
sy = pixelscale.y
sz = pixelscale.z
(; i, j, k, x, y, z) = tiepoint
tx = x - i / sx
ty = y + j / sy
tz = z - k / sz
A = [
sx 0 0
0 -sy 0
0 0 sz
]
b = [tx, ty, tz]
A, b
elseif !isnothing(transformation)
transformation.A, transformation.b
else
nothing
end
end

0 comments on commit 0d12b32

Please sign in to comment.