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
# So this is just a test to see the fastests way to go from a dataframe with
# two coordinates per row (split into 4 columns - 2 for x and 2 for y) to a
# linestring for each set of coordinates.
# minimal example.
library(sfheaders)
library(wk)
library(sf)
library(tidyverse)
@h-a-graham
h-a-graham / rstac_swisstopo.R
Last active December 9, 2021 13:37
Use rstac to query the swisstopo lidar collection and load into R. plot with terra and rayshader...
#rstac example/test
library(vapour)
library(gdalio)
library(rstac)
library(sf)
library(dplyr)
library(scico)
source(system.file("raster_format/raster_format.codeR", package = "gdalio", mustWork = TRUE))
# get random swiss region
@h-a-graham
h-a-graham / GEE_driver_R.R
Last active April 29, 2022 15:50
GEE driver example in R
library(gdalio)
library(terra)
library(viridisLite)
source(system.file("raster_format/raster_format.codeR", package = "gdalio", mustWork = TRUE))
Sys.setenv(GOOGLE_APPLICATION_CREDENTIALS = "MYKEY.json")
gee_src <- "EEDAI:CGIAR/SRTM90_V4"
src_info <- vapour::vapour_raster_info(gee_src)
@h-a-graham
h-a-graham / geoid_warp_EGM2008.R
Created May 8, 2022 21:29
An example of how to directly warp a geoid model into R.
# Warp and plot the EGM2008 1' geoid model
# geoid COGS hosted here: https://www.agisoft.com/downloads/geoids/
library(vapour)
library(gdalio)
library(terra)
source(system.file("raster_format/raster_format.codeR", package = "gdalio", mustWork = TRUE))
geoid <- '/vsicurl/https://s3-eu-west-1.amazonaws.com/download.agisoft.com/gtg/us_nga_egm2008_1.tif'
@h-a-graham
h-a-graham / matrix_to_raster_stars.md
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
library(raster)
#> Loading required package: sp
library(stars)
#> 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
library(terra)
#> terra 1.5.23
@h-a-graham
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
#' 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
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
library(sf)
library(dplyr)
library(mapview)
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(is.na(NUMBER)|NUMBER!=100) %>%
mutate(nearest = st_nearest_feature(., plts),
@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.")
   } 
library(vapour)
library(sf)
#> 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
library(rstac)

mpc_dem <- function(aoi, src = c("cop-dem-glo-30", "alos-dem")) {
@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`