-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #147
- Loading branch information
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
tile!(tiles::Matrix{T}, layer::T, s::Tuple{Integer, Integer} = (5, 5)) where {T <: SimpleSDMLayer} | ||
Split a layer into a matrix of tiles (with size `s`), where the tiles are as | ||
evenly sized as possible, and have strictly matching boundaries. | ||
""" | ||
function tile!( | ||
tiles::Matrix{T}, | ||
layer::T, | ||
s::Tuple{Integer, Integer} = (5, 5), | ||
) where {T <: SimpleSDMLayer} | ||
@assert s[1] < size(layer, 1) | ||
@assert s[2] < size(layer, 2) | ||
@assert size(tiles) == s | ||
# Return type | ||
RT = T <: SimpleSDMPredictor ? SimpleSDMPredictor : SimpleSDMResponse | ||
# Boundaries from the bounding box | ||
l = layer.left | ||
r = layer.right | ||
b = layer.bottom | ||
t = layer.top | ||
l_r_points = LinRange(l, r, s[2] + 1) | ||
t_b_points = LinRange(t, b, s[1] + 1) | ||
# Split the coordinates | ||
dim_1_cutoff = floor.(Int, LinRange(1, size(layer, 1), s[1] + 1)) | ||
dim_2_cutoff = floor.(Int, LinRange(1, size(layer, 2), s[2] + 1)) | ||
# Add the layers to the tiles matrix | ||
for i in eachindex(dim_1_cutoff)[2:end] | ||
start1, stop1 = dim_1_cutoff[(i - 1):i] | ||
for j in eachindex(dim_2_cutoff)[2:end] | ||
start2, stop2 = dim_2_cutoff[(j - 1):j] | ||
tiles[(i - 1), (j - 1)] = | ||
RT( | ||
layer.grid[start1:stop1, start2:stop2], | ||
l_r_points[j - 1], | ||
l_r_points[j], | ||
t_b_points[i], | ||
t_b_points[i - 1], | ||
) | ||
end | ||
end | ||
return tiles | ||
end | ||
|
||
""" | ||
tile(layer::T, s::Tuple{Integer, Integer} = (5, 5)) where {T <: SimpleSDMLayer} | ||
Split a layer into a matrix of tiles (with size `s`), where the tiles are as | ||
evenly sized as possible, and have strictly matching boundaries. This function | ||
will allocate the return matrix. | ||
""" | ||
function tile(layer::T, s::Tuple{Integer, Integer} = (5, 5)) where {T <: SimpleSDMLayer} | ||
# Create the tiles | ||
tiles = Matrix{T}(undef, s...) | ||
tile!(tiles, layer, s) | ||
return tiles | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# # Splitting layers in tiles | ||
|
||
using SpeciesDistributionToolkit | ||
using CairoMakie | ||
|
||
# Get some data | ||
|
||
dataprovider = RasterData(EarthEnv, LandCover) | ||
spatial_extent = (left = -80.00, bottom = 43.19, right = -70.94, top = 46.93) | ||
trees = sum([ | ||
SimpleSDMPredictor(dataprovider; layer = i, full = true, spatial_extent...) for | ||
i in 1:4 | ||
]) | ||
|
||
# split into tiles | ||
|
||
tiles = tile(trees, (4, 5)) | ||
|
||
# plot | ||
|
||
tile_plot = heatmap( | ||
tiles[1,2]; | ||
colormap = :Greens, | ||
figure = (; resolution = (800, 350)), | ||
axis = (; | ||
aspect = DataAspect(), | ||
xlabel = "Latitude", | ||
ylabel = "Longitude", | ||
title = "Relative tree cover", | ||
), | ||
) | ||
Colorbar(tile_plot.figure[:, end + 1], tile_plot.plot; height = Relative(0.5)) | ||
current_figure() | ||
|
||
# |