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 utility functions to easily extract values from metadata #3

Merged
merged 3 commits into from
Nov 7, 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
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
Loading