Skip to content
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
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
^data-raw$
^\.github$
^codecov\.yml$
^_pkgdown\.yml$
^docs$
^pkgdown$
49 changes: 49 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
release:
types: [published]
workflow_dispatch:

name: pkgdown.yaml

permissions: read-all

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.5.0
with:
clean: false
branch: gh-pages
folder: docs
6 changes: 3 additions & 3 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage
name: test-coverage.yaml

permissions: read-all

Expand Down Expand Up @@ -40,7 +39,8 @@ jobs:

- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }}
# Fail if error if not on PR, or if on PR and token is given
fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }}
file: ./cobertura.xml
plugin: noop
disable_search: true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.DS_Store
/data-raw/geodata/
*.tif
docs
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Authors@R: c(
)
Description: What the package does (one paragraph).
License: MIT + file LICENSE
URL: https://github.com/idem-lab/sdmtools
URL: https://github.com/idem-lab/sdmtools, https://idem-lab.github.io/sdmtools/
BugReports: https://github.com/idem-lab/sdmtools/issues
Depends:
R (>= 4.1.0)
Expand All @@ -30,7 +30,7 @@ Imports:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Suggests:
testthat (>= 3.0.0)
Remotes:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(inside_mask)
export(make_africa_mask)
export(make_mpp_list)
export(mask_all)
export(mask_from_all)
export(maskpointsdf)
export(match_ref)
export(nearest_land)
Expand Down
20 changes: 13 additions & 7 deletions R/assign_nearest_land.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @title Assign to nearest raster cell on mask
#' @description
#' Adapted from seegSDM.
#' Reposition observations to a location within mask if within a specified distance.
#' Reposition point observations to a location within mask if within a specified distance.
#' Useful for when coastal observations drop off jagged mask and similar
#'
#' @param dat_object data.frame
Expand All @@ -19,15 +19,21 @@ assign_nearest_land <- function(dat_object,
max_distance,
verbose=TRUE) {

stopifnot(sum(colnames(dat_object) %in% c("longitude", "latitude"))==2)
message("dat_object object must contain columns 'longitude' and 'latitude'.")
if(!sum(colnames(dat_object) %in% c("longitude", "latitude"))==2) {

# ensure that data object is spatial data frame
spat_dat <- sf::st_as_sf(dat_object)
stop("Error: dat_object object must contain columns 'longitude' and 'latitude'.")

}

# ensure that data object is a simple features spatial data frame
spat_dat <- sf::st_as_sf(dat_object, coords = c("longitude", "latitude"))

# assert that mask is SpatRaster
stopifnot(inherits(mask_object, 'SpatRaster'))
message("mask must be a SpatRaster")
if(!inherits(mask_object, 'SpatRaster')) {

stop("Error: mask must be a SpatRaster")

}

# isolate coord info from spatial data frame
data_coords <- sf::st_coordinates(spat_dat)
Expand Down
7 changes: 4 additions & 3 deletions R/inside_mask.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#' @title title
#' @description Checks whether longitude and latitude coincide with
#' non-missing pixels of a raster. The function takes two arguments:
#' points, a dataframe containing columns named
#' longitude' and 'latitude', and mask is a raster. Returns a dataframe
#' of longitude and latitude only those rows with points falling on
#' points, a dataframe containing at a minimum columns named
#' longitude' and 'latitude' (but could include other attributes),
#' and mask is a raster. Returns a dataframe of the same dimensions as the
#' input object, containing only those rows with points falling on
#' non-missing pixels. If all points fall on missing pixels,
#' the function throws an error.
#'
Expand Down
58 changes: 58 additions & 0 deletions R/mask_from_all.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#' @title Create mask from raster layers
#' @description
#' Creates a mask where a cell in any layer of `r` that is `NA` will be returned
#' as `NA`.
#'
#' @details
#' Similar in intention to `mask_all`, but (a) will work on larger rasters
#' because it only holds the values of a single layer in memory at a time, and
#' (b) returns a mask layer, rather than masking each layer in `r`.
#' **Can be very slow**
#'
#'
#' @param r `SpatRaster` with >1 layer.
#'
#' @return `SpatRaster` with values `NA` or 1.
#' @export
#'
#' @examples
#' r <- example_raster(seed = 10)
#' s <- example_raster(seed = 11)
#'
#' r[10:20] <- NA
#'
#' s[5:15] <- NA
#'
#' q <- mask_from_all(c(r,s))
#'
#' library("terra")
#' plot(c(r,s,q))
#'
mask_from_all <- function(r){

j <- terra::nlyr(r)

if(j == 1){stop("Must have >1 layer")}

k <- which(is.na(terra::values(r[[1]])))

for(i in 2:j){

a <- which(is.na(terra::values(r[[i]])))

b <- c(k, a)

d <- duplicated(b)

k <- b[!d]

}

z <- r[[1]]

z[] <- 1
z[k] <- NA

z

}
6 changes: 4 additions & 2 deletions R/split_rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ split_rast <- function(
) |>
dplyr::mutate(
r = purrr::pmap(
.l = list(xmin, xmax, ymin, ymax, x, write_temp),
.l = list(xmin, xmax, ymin, ymax),
.f = function(xmin, xmax, ymin, ymax, x, write_temp){

xt <- terra::ext(c(xmin, xmax, ymin, ymax))
Expand All @@ -97,7 +97,9 @@ split_rast <- function(

z

}
},
x,
write_temp
)
) |>
dplyr::pull(r)
Expand Down
4 changes: 1 addition & 3 deletions R/standardise_rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#'
#' **Defunct**.
#'
#' DO NOT USE THIS FUNCTION
#'
#' Instead use `terra::scale`
#' DO NOT USE THIS FUNCTION; use `terra::scale` instead
#'
#' @param x A `SpatRaster` object.
#'
Expand Down
11 changes: 8 additions & 3 deletions R/std_rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' by dividing by the maximum value in each layer. *Only operates by layer*
#'
#' @param x `SpatRaster` to standardise
#' @param reverse `logical` if `TRUE` will subtract standardised values from 1
#' @param filename Optional `character` path and filename to write output
#' @param overwrite `logical` if `TRUE` will overwrite `filename`
#'
Expand All @@ -14,9 +15,9 @@
#'
#'
#' @examples
#' example_raster() |>
#' std_rast()
std_rast <- function(x, filename = NULL, overwrite = TRUE){
#' example_raster(seed = 3010) |>
#' std_rast(reverse = TRUE)
std_rast <- function(x, reverse = FALSE, filename = NULL, overwrite = TRUE){
vals <- terra::values(x)

nvs <- apply(
Expand All @@ -29,6 +30,10 @@ std_rast <- function(x, filename = NULL, overwrite = TRUE){

r <- x

if(reverse){
nvs <- 1 - nvs
}

r[] <- nvs


Expand Down
10 changes: 9 additions & 1 deletion R/writereadrast.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#'
#' @param x A `terra::SpatRaster`
#' @param filename A `character` file path and name to save `x` to disc.
#' @param overwrite `logical`; overwrite existing raster.
#' @param overwrite `logical`; overwrite existing raster. **NB:** by default,
#' `overwrite = TRUE`, this is the opposite of the default behaviour of
#' `terra::writeRaster`
#' @param layernames `character` of length `nlyr(x)`
#'
#' @return A `terra::SpatRaster` object reading from disc at `filename`.
Expand All @@ -16,6 +18,12 @@
#' \dontrun{
#' # create raster then assign
#' r <- sdmtools::example_raster()
#'
#' # usual workflow in two slow tedious boring steps
#' terra::writeRaster(r, "LowerSpringvale.tif")
#' s <- terra::rast("LowerSpringvale.tif")
#'
#' Better workflow in one fast enjoyable lithesome step with `sdmtools`
#' r <- writereadrast(r, "tootgarook.tif")
#'
#' # or roll into single step with pipe
Expand Down
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ knitr::opts_chunk$set(
![GitHub last commit](https://img.shields.io/github/last-commit/idem-lab/sdmtools)
![GitHub R package version](https://img.shields.io/github/r-package/v/idem-lab/sdmtools)
![GitHub commits since latest release](https://img.shields.io/github/commits-since/idem-lab/sdmtools/latest)
[![Codecov test coverage](https://codecov.io/gh/idem-lab/sdmtools/graph/badge.svg)](https://app.codecov.io/gh/idem-lab/sdmtools)
<!-- badges: end -->

A set of helper functions to facilitate species distribution modelling.
Expand Down
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ commit](https://img.shields.io/github/last-commit/idem-lab/sdmtools)
version](https://img.shields.io/github/r-package/v/idem-lab/sdmtools)
![GitHub commits since latest
release](https://img.shields.io/github/commits-since/idem-lab/sdmtools/latest)
[![Codecov test
coverage](https://codecov.io/gh/idem-lab/sdmtools/graph/badge.svg)](https://app.codecov.io/gh/idem-lab/sdmtools)
<!-- badges: end -->

A set of helper functions to facilitate species distribution modelling.
Expand Down Expand Up @@ -258,10 +260,7 @@ functions, rather than storing it.

``` r
library(terra)
#> terra 1.7.79
```

``` r
#> terra 1.8.5
r <- example_raster()
r
#> class : SpatRaster
Expand All @@ -273,9 +272,6 @@ r
#> name : example
#> min value : 0.0627102
#> max value : 7.3352526
```

``` r
plot(r)
```

Expand All @@ -292,9 +288,6 @@ v
#> dimensions : 10, 0 (geometries, attributes)
#> extent : 0.2293562, 8.00672, 1.375653, 8.951683 (xmin, xmax, ymin, ymax)
#> coord. ref. :
```

``` r
plot(v)
```

Expand All @@ -310,13 +303,12 @@ africa_mask <- make_africa_mask(type = "vector")
#> Loading ISO 19139 XML schemas...
#> Loading ISO 19115 codelists...
#> Please Note: Because you did not provide a version, by default the version being used is 202403 (This is the most recent version of admin unit shape data. To see other version options use function listShpVersions)
#> Start tag expected, '<' not found
#> Start tag expected, '<' not found
#> although coordinates are longitude/latitude, st_union assumes that they are
#> planar
#> Warning: [crs<-] not all geometries were transferred, use svc for a geometry
#> collection
```

``` r
plot(africa_mask)
```

Expand Down
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
url: https://idem-lab.github.io/sdmtools/
template:
bootstrap: 5

2 changes: 1 addition & 1 deletion man/assign_nearest_land.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/inside_mask.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading