# Methods overloaded

To facilitate writing julian code, we have overloaded a number of methods from `Base`

. These methods should remove the need to interact with the `grid`

field directly, and also allow to set and get values using the geographic coordinates (as opposed to the grid positions).

## From `Base`

`Base.convert`

— Function`Base.convert(::Type{SimpleSDMResponse}, layer::T) where {T <: SimpleSDMPredictor}`

Returns a response with the same grid and bounding box as the predictor.

`Base.convert(::Type{SimpleSDMPredictor}, layer::T) where {T <: SimpleSDMResponse}`

Returns a predictor with the same grid and bounding box as the response.

`Base.convert(::Type{T}, layer::TL) where {T <: Number, TL <: SimpleSDMLayer}`

Returns a copy of the layer with the same type (response or predictor), but the element type has been changed to `T`

(which must be a number). This function is *extremely useful* (required, in fact) for plotting, as the `nothing`

values are changed to `NaN`

in the heatmaps.

`Base.convert(::Type{Matrix}, layer::T) where {T <: SimpleSDMLayer}`

Returns the grid as an array.

`Base.copy`

— Function`Base.copy(l::T) where {T <: SimpleSDMLayer}`

Returns a new copy of the layer, which has the same type.

`Base.collect`

— Function`Base.collect(l::T) where {T <: SimpleSDMLayer}`

Returns the non-`nothing`

values of a layer.

`Base.eltype`

— Function`Base.eltype(layer::SimpleSDMLayer{T}) where {T}`

Returns the type of the values stored in the grid, where the `Nothing`

type is omitted.

`Base.size`

— Function`Base.size(layer::T) where {T <: SimpleSDMLayer}`

Returns the size of the grid.

`Base.size(layer::T, i...) where {T <: SimpleSDMLayer}`

Returns the size of the grid alongside a dimension.

`Base.stride`

— Function`Base.stride(layer::T; dims::Union{Nothing,Integer}=nothing) where {T <: SimpleSDMLayer}`

Returns the stride, *i.e.* half the length, of cell dimensions, possibly alongside a side of the grid. The first position is the length of the *longitude* cells, the second the *latitude*.

`Base.eachindex`

— Function`Base.eachindex(layer::T) where {T <: SimpleSDMLayer}`

Returns the index of the grid.

`Base.getindex`

— FunctionExtracts a value from a layer by its grid position.

`Base.getindex(layer::T, i::R, j::R) where {T <: SimpleSDMLayer, R <: UnitRange}`

Extracts a series of positions in a layer, and returns a layer corresponding to the result. This is essentially a way to rapidly crop a layer to a given subset of its extent. The `i`

and `j`

arguments are `UnitRange`

s (of `Integer`

).

The layer returned by this function will have the same type as the layer passed as its argument, but this can be changed using `convert`

. Note that this function performs additional checks to ensure that the range is not empty, and to also ensure that it does not overflows from the size of the layer.

`Base.getindex(layer::T, longitude::K, latitude::K) where {T <: SimpleSDMLayer, K <: AbstractFloat}`

Extracts the value of a layer at a given latitude and longitude. If values outside the range are requested, will return `nothing`

.

`Base.getindex(layer::T; left=nothing, right=nothing, top=nothing, bottom=nothing) where {T <: SimpleSDMLayer, K <: Union{Nothing,AbstractFloat}}`

Returns a subset of the argument layer, where the new limits are given by `left`

, `right`

, `top`

, and `bottom`

. Up to three of these can be omitted, and if so these limits will not be affected.

`Base.getindex(layer::T, n::NT) where {T <: SimpleSDMLayer, NT <: NamedTuple}`

Returns a subset of the argument layer, where the new limits are given in a NamedTuple by `left`

, `right`

, `top`

, and `bottom`

, in any order. Up to three of these can be omitted, and if so these limits will not be affected.

`Base.getindex(layer1::T1, layer2::T2) where {T1 <: SimpleSDMLayer, T2 <: SimpleSDMLayer}`

Extract a layer based on a second layer. Note that the two layers must be *compatible*, which is to say they must have the same stride and the bounding coordinates of layer2 must be contained in layer1.

`Base.getindex(p::T, occurrence::GBIF.GBIFRecord) where {T <: SimpleSDMLayer}`

Extracts the value of a layer at a given position for a `GBIFRecord`

. If the `GBIFRecord`

has no latitude or longitude, this will return `nothing`

.

`Base.getindex(layer::T, records::GBIF.GBIFRecords) where {T <: SimpleSDMLayer}`

Returns the values of a layer at all occurrences in a `GBIFRecords`

collection.

`Base.getindex(layer::T, records::Vector{GBIF.GBIFRecord}) where {T <: SimpleSDMLayer}`

Returns the values of a layer at all occurrences in a `GBIFRecord`

array.

`Base.getindex(layer::T, df::DataFrames.DataFrame; latitude = :latitude, longitude = :longitude) where {T <: SimpleSDMLayer}`

Returns the values of a layer at all occurrences in a `DataFrame`

. Note that the function looks for columns named `:latitude`

and `:longitude`

by default, but these can be changed using the `latitude`

and `longitude`

arguments.

`Base.setindex!`

— Function` Base.setindex!(layer::SimpleSDMResponse{T}, v::T, i...) where {T}`

Changes the value of a cell, or a range of cells, as indicated by their grid positions.

`Base.setindex!(layer::T, v, lon::Float64, lat::Float64) where {T <: SimpleSDMResponse}`

Changes the values of the cell including the point at the requested latitude and longitude.

`Base.setindex!(layer::T, v, record::GBIFRecord) where {T <: SimpleSDMResponse}`

Changes the values of the cell including the point at the requested latitude and longitude. **Be careful**, this function will not update a cell that has `nothing`

.

`Base.similar`

— Function`Base.similar(layer::T, ::Type{TC}) where {TC <: Any, T <: SimpleSDMLayer}`

Returns a `SimpleSDMResponse`

of the same dimensions as the original layer, with `nothing`

in the same positions. The rest of the values are replaced by the output of `zero(TC)`

, which implies that there must be a way to get a zero for the type. If not, the same result can always be achieved through the use of `copy`

, manual update, and `convert`

.

`Base.similar(layer::T) where {T <: SimpleSDMLayer}`

Returns a `SimpleSDMResponse`

of the same dimensions as the original layer, with `nothing`

in the same positions. The rest of the values are replaced by the output of `zero(element_type)`

, which implies that there must be a way to get a zero for the type. If not, the same result can always be achieved through the use of `copy`

, manual update, and `convert`

.

`Base.replace`

— Function`Base.replace(layer::T, old_new::Pair...) where {T <: SimpleSDMResponse}`

Replaces the elements of `layer`

according to a series of pairs. Returns a copy.

`Base.replace(layer::T, old_new::Pair...) where {T <: SimpleSDMPredictor}`

Replaces the elements of `layer`

according to a series of pairs. Copies the layer as a response before.

`Base.replace!`

— Function`Base.replace!(layer::T, old_new::Pair...) where {T <: SimpleSDMLayer}`

Replaces the elements of `layer`

according to a series of pairs. In place. Only possible for `SimpleSDMResponse`

elements (which are mutable) and will throw an error if called on a `SimpleSDMPredictor`

element (which is not mutable).

`Base.sum`

— Function`Base.sum(l::SimpleSDMResponse{T}) where {T <: Number}`

Applies `sum`

(from `Base`

) to an object of type `SimpleSDMResponse`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Base.sum(l::SimpleSDMPredictor{T}) where {T <: Number}`

Applies `sum`

(from `Base`

) to an object of type `SimpleSDMPredictor`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Base.maximum`

— Function`Base.maximum(l::SimpleSDMResponse{T}) where {T <: Number}`

Applies `maximum`

(from `Base`

) to an object of type `SimpleSDMResponse`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Base.maximum(l::SimpleSDMPredictor{T}) where {T <: Number}`

Applies `maximum`

(from `Base`

) to an object of type `SimpleSDMPredictor`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Base.minimum`

— Function`Base.minimum(l::SimpleSDMResponse{T}) where {T <: Number}`

Applies `minimum`

(from `Base`

) to an object of type `SimpleSDMResponse`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Base.minimum(l::SimpleSDMPredictor{T}) where {T <: Number}`

Applies `minimum`

(from `Base`

) to an object of type `SimpleSDMPredictor`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Base.extrema`

— Function`Base.extrema(l::SimpleSDMResponse{T}) where {T <: Number}`

Applies `extrema`

(from `Base`

) to an object of type `SimpleSDMResponse`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Base.extrema(l::SimpleSDMPredictor{T}) where {T <: Number}`

Applies `extrema`

(from `Base`

) to an object of type `SimpleSDMPredictor`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Base.max`

— Function`Base.max(l1::SimpleSDMLayer, l2::SimpleSDMLayer)`

Applies `max`

(from `Base`

) to every pair of cells from two `SimpleSDMLayers`

and returns the result as a new `SimpleSDMResponse`

layer. Note that `max`

is only applied to the pairs without a `nothing`

element, and returns `nothing`

for the pairs with one. This function has been automatically generated.

`Base.min`

— Function`Base.min(l1::SimpleSDMLayer, l2::SimpleSDMLayer)`

Applies `min`

(from `Base`

) to every pair of cells from two `SimpleSDMLayers`

and returns the result as a new `SimpleSDMResponse`

layer. Note that `min`

is only applied to the pairs without a `nothing`

element, and returns `nothing`

for the pairs with one. This function has been automatically generated.

`Base.:+`

— Function`Base.+(l1::SimpleSDMLayer, l2::SimpleSDMLayer)`

Applies `+`

(from `Base`

) to every pair of cells from two `SimpleSDMLayers`

and returns the result as a new `SimpleSDMResponse`

layer. Note that `+`

is only applied to the pairs without a `nothing`

element, and returns `nothing`

for the pairs with one. This function has been automatically generated.

`Base.:-`

— Function`Base.-(l1::SimpleSDMLayer, l2::SimpleSDMLayer)`

Applies `-`

(from `Base`

) to every pair of cells from two `SimpleSDMLayers`

and returns the result as a new `SimpleSDMResponse`

layer. Note that `-`

is only applied to the pairs without a `nothing`

element, and returns `nothing`

for the pairs with one. This function has been automatically generated.

`Base.:*`

— Function`Base.*(l1::SimpleSDMLayer, l2::SimpleSDMLayer)`

Applies `*`

(from `Base`

) to every pair of cells from two `SimpleSDMLayers`

and returns the result as a new `SimpleSDMResponse`

layer. Note that `*`

is only applied to the pairs without a `nothing`

element, and returns `nothing`

for the pairs with one. This function has been automatically generated.

`Base.:/`

— Function`Base./(l1::SimpleSDMLayer, l2::SimpleSDMLayer)`

Applies `/`

(from `Base`

) to every pair of cells from two `SimpleSDMLayers`

and returns the result as a new `SimpleSDMResponse`

layer. Note that `/`

is only applied to the pairs without a `nothing`

element, and returns `nothing`

for the pairs with one. This function has been automatically generated.

`Base.:==`

— Function`==(layer1::SimpleSDMLayer, layer2::SimpleSDMLayer)`

Tests whether two `SimpleSDMLayer`

elements are equal. The layers are equal if all their fields (`grid`

, `left`

, `right`

, `bottom`

, `top`

) are equal, as verified with `==`

(e.g., `layer1.grid == layer2.grid`

).

`Base.isequal`

— Function`isequal(layer1::SimpleSDMLayer, layer2::SimpleSDMLayer)`

Tests whether two `SimpleSDMLayer`

elements are equal. The layers are equal if all their fields (`grid`

, `left`

, `right`

, `bottom`

, `top`

) are equal, as verified with `isequal`

(e.g., `isequal(layer1.grid, layer2.grid)`

).

## From `Broadcast`

`Base.Broadcast.broadcast`

— FunctionBroadcast.broadcast(f, L::LT) where {LT <: SimpleSDMLayer}

TODO

## From `Statistics`

`Statistics.mean`

— Function`Statistics.mean(l::SimpleSDMResponse{T}) where {T <: Number}`

Applies `mean`

(from `Statistics`

) to an object of type `SimpleSDMResponse`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Statistics.mean(l::SimpleSDMPredictor{T}) where {T <: Number}`

Applies `mean`

(from `Statistics`

) to an object of type `SimpleSDMPredictor`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Statistics.mean(layers::Array{T}) where {T <: SimpleSDMLayer}`

Applies `mean`

(from `Statistics`

) to the elements in corresponding positions from the different layers (similar to `mean(a::Array{Matrix})`

) and returns the result as a new `SimpleSDMResponse`

layer. Note that `mean`

is only applied to the positions without a `nothing`

element and returns `nothing`

for the pairs with one. This function has been automatically generated.

`Statistics.median`

— Function`Statistics.median(l::SimpleSDMResponse{T}) where {T <: Number}`

Applies `median`

(from `Statistics`

) to an object of type `SimpleSDMResponse`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Statistics.median(l::SimpleSDMPredictor{T}) where {T <: Number}`

Applies `median`

(from `Statistics`

) to an object of type `SimpleSDMPredictor`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Statistics.std`

— Function`Statistics.std(l::SimpleSDMResponse{T}) where {T <: Number}`

Applies `std`

(from `Statistics`

) to an object of type `SimpleSDMResponse`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Statistics.std(l::SimpleSDMPredictor{T}) where {T <: Number}`

Applies `std`

(from `Statistics`

) to an object of type `SimpleSDMPredictor`

. This function has been automatically generated. Note that this function is only applied to the non-`nothing`

elements of the layer, and has no method to work on the `dims`

keyword; the grid itself can be extracted with `convert(Matrix, l)`

.

`Statistics.std(layers::Array{T}) where {T <: SimpleSDMLayer}`

Applies `std`

(from `Statistics`

) to the elements in corresponding positions from the different layers (similar to `mean(a::Array{Matrix})`

) and returns the result as a new `SimpleSDMResponse`

layer. Note that `std`

is only applied to the positions without a `nothing`

element and returns `nothing`

for the pairs with one. This function has been automatically generated.

`Statistics.quantile`

— Function`quantile(layer::T, p) where {T <: SimpleSDMLayer}`

Returns the quantiles of `layer`

at `p`

, using `Statistics.quantile`

.