Skip to content

Instantly share code, notes, and snippets.

View johnbaums's full-sized avatar

John johnbaums

  • University of Melbourne
  • Melbourne
View GitHub Profile
@johnbaums
johnbaums / diverge0.R
Last active February 15, 2024 09:42
Plot a rasterVis::levelplot with a colour ramp diverging around zero
diverge0 <- function(p, ramp) {
# p: a trellis object resulting from rasterVis::levelplot
# ramp: the name of an RColorBrewer palette (as character), a character
# vector of colour names to interpolate, or a colorRampPalette.
require(RColorBrewer)
require(rasterVis)
if(length(ramp)==1 && is.character(ramp) && ramp %in%
row.names(brewer.pal.info)) {
ramp <- suppressWarnings(colorRampPalette(brewer.pal(11, ramp)))
} else if(length(ramp) > 1 && is.character(ramp) && all(ramp %in% colors())) {
@johnbaums
johnbaums / lintemp.R
Last active December 19, 2023 10:36
Linear temporal interpolation and extrapolation of raster stacks
#### Temporal Interpolation ####################################################
# Perform cell-wise linear interpolation between multiple raster layers, and
# extrapolation beyond the upper limit of input data. Output is saved in .tif
# format.
#
# Arguments
# s: a rasterStack containing the time slices to be interpolated
#
# xin: a numeric vector that indicates the times associated with layers in s (in
# the same order as the layers of s - see names(s))
.bubbles {
stroke-width: 1px;
stroke: black;
opacity: .8
}
.bubbles:hover {
stroke: black;
}
* {
@johnbaums
johnbaums / exdet.R
Last active April 5, 2023 11:38
Faster extrapolation detection (ExDet) based on ecospat::ecospat.climan
exdet <- function (ref, p, mic=TRUE, tol, quiet=FALSE) {
# ref: data.frame of environments at reference locations.
# p: data.frame of environments to assess.
# mic: should the most influential covariates also be returned?
# quiet: should messages be suppressed?
# tol: tolerance, passed to mahalanobis(). See ?solve.
# =============================================================
# When passing `mic=TRUE`, a data.frame will be returned with 3
# columns giving the exdet score (D), the most influential
# covariate with respect to type 1 novelty (MIC1; equivalent to
@johnbaums
johnbaums / sf_arrow.R
Last active November 7, 2022 03:03
Create arrows along great circles between pairs of points
sf_arrow <- function(from, to, len, deg, prefixFrom='from.', prefixTo='to.') {
# from, to: sf objects. `from` should be either a single point, or the same
# number of points as in `to`.
# len: the length of the arrow head, in map units.
# deg: the angle (in degrees) of the arrow head.
# prefixFrom, prefixTo: the attributes of `from` and `to` are added to the
# returned sf object. These prefixes will be prepended to the existing names
# of `from` and `to`.
crs <- st_crs(to)
n <- nrow(to)
@johnbaums
johnbaums / get_zotero.R
Created March 28, 2022 03:50
Download zotero collection, with pagination
library(dplyr)
library(jsonlite)
# create an empty list to store results
items <- list()
i <- 1 # initialise counter to 1. This is used to specify the start index for queries
# We can download at most 100 records at a time, so we download in pages,
# increasing the start index by 100 each time. We do this until the result of
# the query has fewer than 100 results (i.e. we've retrieved the last page of
@johnbaums
johnbaums / dms2dd.R
Created March 2, 2022 02:41
Convert coords from DMS to Decimal Degrees
dms2dd <- function(x) {
dms <- strsplit(x, '[^0-9.]+')
sapply(dms, function(x) {
sum(as.numeric(x)/c(1, 60, 3600)[seq_along(x)])
})
}
dms2dd(c('34°53’07”S', '138°36’40”E', '138.61111', '34°53’'))
## [1] 34.88528 138.61111 138.61111 34.88333
@johnbaums
johnbaums / get_namespaces.R
Last active February 8, 2022 14:02
Get the unique set of namespaces specified in an R script
get_namespaces <- function(file, as_imports=FALSE) {
require(stringi)
require(dplyr)
nm <- readLines(file) %>%
stringi::stri_extract_all(regex='[\\w_.]+::[\\w_.]+') %>%
unlist %>%
setdiff(NA) %>%
sort
@johnbaums
johnbaums / awazon-linux-gdal-installation.sh
Created October 24, 2019 03:55 — forked from hervenivon/awazon-linux-gdal-installation.sh
Install GEOS, PROJ4 & GDAL on amazon linux
export PYTHON_VERSION=3.4.3
export PYTHON_SHORT_VERSION=3.4
export GEOS_VERSION=3.6.2
export GDAL_VERSION=2.2.2
export PROJ4_VERSION=4.9.3
sudo yum-config-manager --enable epel
sudo yum install gdal-python
sudo yum -y install make automake gcc gcc-c++ libcurl-devel proj-devel geos-devel
@johnbaums
johnbaums / gdal_mean.R
Last active July 15, 2021 19:04
Fast calculation of cellwise mean across a raster stack, using gdal_calc.py
gdal_mean <- function(infile, outfile, return_raster=FALSE, overwrite=FALSE) {
# Be aware that the outfile type will be the same as the infile type
require(rgdal)
if(return_raster) require(raster)
# infile: The multiband raster file (or a vector of paths to multiple
# raster files) for which to calculate cell mean.
# outfile: Path to raster output file.
# return_raster: (logical) Should the output raster be read back into R?
# overwrite: (logical) Should outfile be overwritten if it exists?
gdal_calc <- Sys.which('gdal_calc.py')