Skip to content

... get the bounding box for an object?

julia
using SpeciesDistributionToolkit
using CairoMakie

The boundingbox method accepts most types that SpeciesDistributionToolkit knows about, and returns a tuple:

julia
occ = occurrences("hasCoordinate" => true, "country" => "CH", "limit" => 300)
SpeciesDistributionToolkit.boundingbox(occ)
(left = 6.001386, right = 10.182211, bottom = 46.005772, top = 47.706857)

We can also specify a padding (in degrees) that is added to the bounding box:

julia
SpeciesDistributionToolkit.boundingbox(occ; padding = 1.0)
(left = 5.001386, right = 11.182211, bottom = 45.005772, top = 48.706857)

Why specify the module?

Makie and CairoMakie export a method called boundingbox. To remove any ambiguities when the method is called at a time where any Makie backed is loaded, we must specific which module we are calling it from. Note that the dispatch signature of the method implemented by SpeciesDistributionToolkit is not ambiguous.

This is useful to restrict the part of a layer that is loaded, here one of the fractional layers from the copernicus landcover dataset:

julia
L = SDMLayer(
    RasterData(Copernicus, LandCover);
    layer = "Crops",
    SpeciesDistributionToolkit.boundingbox(occ; padding = 0.5)...,
)
🗺️  A 2724 × 5223 layer with 14227452 UInt8 cells
   Projection: +proj=longlat +datum=WGS84 +no_defs

Note that the bounding box is returned in WGS84, but the function to load any part of a layer will handle the conversion.

Code for the figure
julia
heatmap(L; colormap = :Greys)
scatter!(occ)

The same method also applies to polygons:

julia
CHE = SpeciesDistributionToolkit.openstreetmap("Switzerland");
SpeciesDistributionToolkit.boundingbox(CHE; padding = 0.5)
(left = 5.455911159515381, right = 10.992294311523438, bottom = 45.31795883178711, top = 48.30845260620117)
SpeciesDistributionToolkit.boundingbox Function
julia
boundingbox()

Returns a tuple with coordinates left, right, bottom, top in WGS84, which can be used to decide which part of a raster should be loaded.

source
julia
boundingbox(occ::AbstractOccurrenceCollection; padding=0.0)

Returns the bounding box for a collection of occurrences, with an additional padding.

source
julia
boundingbox(occ::Vector{<:AbstractOccurrence}; padding=0.0)

Returns the bounding box for a vector of abstract occurrences, with an additional padding.

source
julia
boundingbox(layer::SDMLayer; padding=0.0)

Returns the bounding box for a layer, with an additional padding.

source