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

Allow additional drivers to the geotiff writer #47

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/SpeciesDistributionToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using Reexport
# Functions for IO
include("io/geotiff.jl")
include("io/ascii.jl")
include("io/read_write.jl")

# Integrate the datasets to the layers
include("integrations/datasets_layers.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/integrations/datasets_layers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function SimpleSDMLayers.SimpleSDMPredictor(data::R; kwargs...) where {R <: Rast
filepath, filetype, bandnumber, crs = downloader(data; arguments...)

if isequal(SimpleSDMDatasets._tiff)(filetype)
return geotiff(SimpleSDMPredictor, filepath, bandnumber; boundingbox...)
return SimpleSDMPredictor(filepath, "tiff"; bandnumber = bandnumber, boundingbox...)
end

return nothing
Expand All @@ -24,7 +24,7 @@ function SimpleSDMLayers.SimpleSDMPredictor(
filepath, filetype, bandnumber, crs = downloader(data, future; arguments...)

if isequal(SimpleSDMDatasets._tiff)(filetype)
return geotiff(SimpleSDMPredictor, filepath, bandnumber; boundingbox...)
return SimpleSDMPredictor(filepath, "tiff"; bandnumber = bandnumber, boundingbox...)
end

return nothing
Expand Down
28 changes: 14 additions & 14 deletions src/io/geotiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function _find_span(n, m, M, pos, side)
end

"""
geotiff(::Type{LT}, file, bandnumber::Integer=1; left=nothing, right=nothing, bottom=nothing, top=nothing) where {LT <: SimpleSDMLayer}
geotiff(file, ::Type{LT}; bandnumber::Integer=1, left=nothing, right=nothing, bottom=nothing, top=nothing) where {LT <: SimpleSDMLayer}

The geotiff function reads a geotiff file, and returns it as a matrix of the
correct type. The optional arguments `left`, `right`, `bottom`, and `left` are
Expand All @@ -30,16 +30,15 @@ you want to get a small subset from large files.

The first argument is the type of the `SimpleSDMLayer` to be returned.
"""
function geotiff(
::Type{LT},
function _read_geotiff(
file::AbstractString,
bandnumber::Integer = 1;
::Type{LT};
bandnumber::Integer = 1,
left = -180.0,
right = 180.0,
bottom = -90.0,
top = 90.0,
) where {LT <: SimpleSDMLayer}

try
ArchGDAL.read(file) do stuff
wkt = ArchGDAL.importPROJ4(ArchGDAL.getproj(stuff))
Expand Down Expand Up @@ -135,7 +134,7 @@ end
Write a single `layer` to a `file`, where the `nodata` field is set to an
arbitrary value.
"""
function geotiff(
function _write_geotiff(
file::AbstractString,
layer::SimpleSDMPredictor{T};
nodata::T = convert(T, -9999),
Expand Down Expand Up @@ -198,10 +197,11 @@ end
Stores a series of `layers` in a `file`, where every layer in a band. See
`geotiff` for other options.
"""
function geotiff(
function _write_geotiff(
file::AbstractString,
layers::Vector{SimpleSDMPredictor{T}};
nodata::T = convert(T, -9999),
driver::String = "GTiff",
) where {T <: Number}
bands = 1:length(layers)
SimpleSDMLayers._layers_are_compatible(layers)
Expand Down Expand Up @@ -239,7 +239,7 @@ function geotiff(
return ArchGDAL.write(
dataset,
file;
driver = ArchGDAL.getdriver("GTiff"),
driver = ArchGDAL.getdriver(driver),
options = ["COMPRESS=LZW"],
)
end
Expand All @@ -251,23 +251,23 @@ end

Write a single `SimpleSDMResponse` layer to a file.
"""
function geotiff(
function _write_geotiff(
file::AbstractString,
layer::SimpleSDMResponse{T};
nodata::T = convert(T, -9999),
kwargs...,
) where {T <: Number}
return geotiff(file, convert(SimpleSDMPredictor, layer); nodata = nodata)
return _write_geotiff(file, convert(SimpleSDMPredictor, layer); kwargs...)
end

"""
geotiff(file::AbstractString, layers::Vector{SimpleSDMResponse{T}}; nodata::T=convert(T, -9999)) where {T <: Number}

Write a vector of `SimpleSDMResponse` layers to bands in a file.
"""
function geotiff(
function _write_geotiff(
file::AbstractString,
layers::Vector{SimpleSDMResponse{T}};
nodata::T = convert(T, -9999),
kwargs...,
) where {T <: Number}
return geotiff(file, convert.(SimpleSDMPredictor, layers); nodata = nodata)
return _write_geotiff(file, convert.(SimpleSDMPredictor, layers); kwargs...)
end
43 changes: 43 additions & 0 deletions src/io/read_write.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
for layertype in (:SimpleSDMResponse, :SimpleSDMPredictor)
eval(
quote
function SimpleSDMLayers.$layertype(
file::String,
format = "tiff";
bandnumber::Integer = 1,
left = nothing,
right = nothing,
bottom = nothing,
top = nothing,
)
@assert isfile(file)
if endswith(file, ".tif") | endswith(file, ".tiff") |
(format in ["tiff", "tif"])
return SpeciesDistributionToolkit._read_geotiff(
file, $layertype; bandnumber = bandnumber, left = left,
right = right, bottom = bottom, top = top,
)
end
end

function save(
file::String,
layers::Vector{$layertype{T}};
kwargs...,
) where {T <: Number}
if endswith(file, ".tif") | endswith(file, ".tiff")
_write_geotiff(file, layers; kwargs...)
return file
end
end

function save(
file::String,
layer::$layertype{T};
kwargs...,
) where {T <: Number}
return SpeciesDistributionToolkit.save(file, [layer]; kwargs...)
end
end,
)
end