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
Last active November 28, 2024 19:44
Get admin boundaries for any country with R.
#' @title Get administritive outlines for a country
#' @description using the geoBoundaires API, get the administritive polygon(s)
#' for a country
#' @param country character vector: a country name
#' @param admin_level character vector: the admin level to download
#' @param quiet logical, should st_read be quiet?
#' @return sf object of the outlines
#' @details check out the documentation for the geoboundaries API at:
#' geoBoundaries.org
#'
@h-a-graham
h-a-graham / hls-functions.R
Last active January 13, 2025 16:26
create Harmonized Sentinel Landsat Mosaic with R
# Description: This script is used to download HLS data from NASA's Earthdata
# STAC API and build a cloud-free composite image.
# ----- Functions -----
# band mapping for HLS SL data
#' @return a named character vector of band mappings for HLS SL data
hlssl_band_mapping <- function() {
c(
B01 = "A", B02 = "B", B03 = "G", B04 = "R",
@h-a-graham
h-a-graham / to-generic-projected.R
Last active October 25, 2024 09:55
An R function to convert an sf/sfc object to an appropriate utm or laea projection.
#' Project an sf/sfc object to a generic projected coordinate system
#' @param x an sf or sfc object
#' @param proj a character vector. The projection to use. One of "laea", "aeqd",
#' "utm", "pconic", or "eqdc".
#' @param ellps a character vector. The ellipsoid to use. Select from
#' `sf_proj_info(type = "ellps")`.
#' @param opts a character vector. Additional proj options to pass to the
#' proj string. see details for more information.
#' @return an sf or sfc object
#' @details For further info about the available "generic" projects see:
as_the_crow_flies <- function(city1, city2,
                              country1 = NULL, country2 = NULL,
                              units = "km", quiet = FALSE) {
  gcl <- tidygeocoder::geo(
    city = c(city1, city2), country = c(country1, country2),
    method = "osm", quiet = TRUE, progress_bar = FALSE, verbose = FALSE
  )
  l1 <- gcl[1, ]
  l2 <- gcl[2, ]
@h-a-graham
h-a-graham / prop_aggregate.md
Last active December 12, 2024 10:27
aggregate a categorical raster - yielding a band for each class with the proportion of cover for the class
library(terra)
#> terra 1.8.0

# create example landcover raster
r <- rast(
  extent = c(0, 15000, 0, 15000),
  resolution = 30,
  crs = "+proj=laea"
)