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 / geo_bounds.R
Created July 3, 2023 10:47
Get admin boundaries for any country with R.
View geo_bounds.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:
geo_bounds <- function(country, admin_level = c("ADM0", "ADM1", "ADM2")) {
h-a-graham / terrain_surface_buffer.R
Last active October 16, 2023 15:56
generates buffer along terrain surface from point
View terrain_surface_buffer.R
#' 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 / ggplotly_raster_sf.R
Created October 18, 2022 14:01
plotly labels with raster and sf
View ggplotly_raster_sf.R
.d <- dim(volcano)
h-a-graham / drive_downloaders.R
Last active January 5, 2023 11:09
Download a Google Drive Directory
View drive_downloaders.R
#' 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 /
Created September 26, 2022 09:02
reorder columns of a tibble based on the values of another with equal dimensions - see

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

#> # A tibble: 10 × 2
#>    `C1 Template` `C2 Template`
h-a-graham /
Last active July 20, 2022 01:14
using a circular focal with terra
#> 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.")
#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is TRUE
#> WARNING: different compile-time and runtime versions for GEOS found:
#> Linked against: 3.10.2-CAPI-1.16.0 compiled against: 3.10.1-CAPI-1.16.0
#> It is probably a good idea to reinstall sf, and maybe rgeos and rgdal too

mpc_dem <- function(aoi, src = c("cop-dem-glo-30", "alos-dem")) {
h-a-graham / gps_nearest_clean.R
Created June 28, 2022 16:17
get nearest points and set geometry as the point closest to the original measurement
View gps_nearest_clean.R
mapviewOptions(fgb = FALSE)
plts <- read_sf('data/PlotData/KuamutLocations.shp')
pnts <- read_sf("data/Kuamut_2022_NewGPS_Points_Shapefiles/PlotCentre.shp")
pnts_clean <- pnts %>%
filter(|NUMBER!=100) %>%
mutate(nearest = st_nearest_feature(., plts),
h-a-graham / tar_vis_style.R
Created June 10, 2022 11:01
A function to style a visNetwork object from {targets} - either from tar_visnetork of tar_glimpse
View tar_vis_style.R
#' Customise style of a targets-derived visNetwork object
#' @param v object class "visNetwork" from either `tar_visnetwork` or `tar_glimpse`
#' @param colors character vector of length 1 or 3. Hex colour codes to define:
#' Up to Date, Errored and Outdated targets (in that order). If an object from
#' `tar_glimpse` is provided, only the first colour is used to fill the nodes.
#' @param shapes a character vector of length 3. any of the following: diamond,
#' dot, star, triangle, triangleDown, hexagon, square.
#' @param background character - Hex colour codes to define the background
#' @param leg_shape_col character - Hex colour code. for tar_visnetwork an
h-a-graham /
Last active May 11, 2022 09:43
A comparison of how to convert a matrix into a spatial raster in either raster, terra or stars format
#> Loading required package: sp
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE
#> terra 1.5.23