Skip to content

Instantly share code, notes, and snippets.

View h-a-graham's full-sized avatar

Hugh Graham h-a-graham

View GitHub Profile
@h-a-graham
h-a-graham / circular_focal.md
Last active July 20, 2022 01:14
using a circular focal with terra
 library(raster)
#> Loading required package: sp
#> Warning: no function found corresponding to methods exports from 'raster' for:
#> 'area'
 
 circ_matrix <- function(n){
   if (!n %% 2){
     stop("n must be an odd number.")
   } 
@h-a-graham
h-a-graham / reorder-from-tibbles.md
Created September 26, 2022 09:02
reorder columns of a tibble based on the values of another with equal dimensions - see https://twitter.com/ProfJamesCurran/status/1574135627498475520
library(tibble)
library(purrr)

t1 <- tibble(`C1 Template` = sample(c(166:180), 10),
             `C2 Template` = sample(c(120:130), 10))

t1
#> # A tibble: 10 × 2
#>    `C1 Template` `C2 Template`
@h-a-graham
h-a-graham / drive_downloaders.R
Last active January 5, 2023 11:09
Download a Google Drive Directory
#' download drive files from dribble with path column
#'
#' @param x a dribble with path column
#' @param .overwrite logical. Should files be overwritten
#'
#' @return The original input dribble
#' @noRd
drive_down_files <- function(x, .overwrite = TRUE) {
@h-a-graham
h-a-graham / ggplotly_raster_sf.R
Created October 18, 2022 14:01
plotly labels with raster and sf
#https://twitter.com/AnalyticalEdge/status/1582206425585324034
library(ggplot2)
library(stars)
library(sf)
library(dplyr)
library(plotly)
.d <- dim(volcano)
@h-a-graham
h-a-graham / terrain_surface_buffer.R
Last active October 16, 2023 15:56
generates buffer along terrain surface from point
#' Buffer a point along a terrain surface.
#'
#' @param dtm A SpatRaster Digital Terrain Model
#' @param pnts A SpatVector of points
#' @param .dist Numeric - the buffer distance.
#' @param .res Numeric - approximate resampling resolution of the (internally
#' calculated) cost raster.
#' @param .smooth Logical default FALSE. Should the output buffer be smoothed using `smoothr::smooth`?
#' @param .method The smoothing method to use - see `smoothr::smooth` for details.
#' @param ... Passed to `smoothr::smooth` for eg. to control smoothness.
@h-a-graham
h-a-graham / geo_bounds.R
Created July 3, 2023 10:47
Get admin boundaries for any country with R.
#' @title get administritive outlines for a country
#' @description using the geoBoundaires API,
#' download the geojson outline of a country
#' @param country character vector of country names
#' @param admin_level character vector of admin levels to download
#' @return sf object of the outlines
#' @details check out the documentation for the geoboundaries API at:
#' geoBoundaries.org
#'
geo_bounds <- function(country, admin_level = c("ADM0", "ADM1", "ADM2")) {