Skip to content

Instantly share code, notes, and snippets.

View obrl-soil's full-sized avatar

obrl_soil obrl-soil

View GitHub Profile
# Source this file at the top of an R or Rmd file using `rgrass` together
# with GRASS 8 installed via OSGeo4W (https://trac.osgeo.org/osgeo4w/) on a
# Windows machine.
# e.g. source(file.path(getwd(), 'grass8-setup-win-osgeo4w.R'))
# You can alternatively add the contents to a project-specific .Rprofile.
# Its better to set these variables in an R session rather than in System,
# especially if you have other GIS software like PostGIS installed, which has
# In reference to https://fosstodon.org/@TheRealJimShady/109559871547727070
# Caution! Much like h3jsr itself, just because you can do this doesn't mean you should :P
# Nonetheless, a demonstration:
library(terra)
library(sf)
library(dplyr)
library(h3jsr)
@obrl-soil
obrl-soil / munsell2hex
Created August 13, 2021 00:23
Munsell colour notation to RGB and hex values
library(munsellinterpol)
library(dplyr)
library(tibble)
library(purrr)
rgb2hex <- function(r,g,b) rgb(r, g, b, maxColorValue = 255)
munsells <- tribble(~ID, ~HUE, ~VALUE, ~CHROMA,
1, '10YR', '3', '6',
2, '5Y', '5', '1',
@obrl-soil
obrl-soil / tilt_stack_sf.R
Last active December 7, 2023 15:29
Tilted and stacked maps with sf
# further to https://urbandemographics.blogspot.com/2016/04/creating-tilted-and-stacked-maps-in-r.html,
# an sf-friendly approach.
library(sf)
nc <- st_read(system.file('shape/nc.shp', package = 'sf'))
plot(nc)
sm <- matrix(c(2, 1.2, 0, 1), 2, 2)
@obrl-soil
obrl-soil / rgrass7-setup-win-osgeo4w.R
Last active March 14, 2024 20:41
rgrass7 and OsGeo4W on Windows - environment variables for R
# Source this file at the top of an R or Rmd file using rgrass7 together
# with GRASS installed via OSGeo4W (https://trac.osgeo.org/osgeo4w/) on a
# Windows machine.
# e.g. source(file.path(getwd(), 'rgrass7-setup-win-osgeo4w.R'))
# You can alternatively add the contents to a project-specific .Rprofile.
# Its better to set these variables in an R session rather than in System,
# especially if you have other GIS software like PostGIS installed, which has
@obrl-soil
obrl-soil / maternish.R
Last active October 7, 2020 17:09
'random' sampling with minimum distance separation with sf in R
# re https://twitter.com/Dale_Masch/status/1184744060428242944
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# I should get this damn epsg code tattooed somewhere
nc1 <- st_transform(nc[1, ], 32617)
# randomise grid origin a little
rx <- sample(seq(-250, 250), 1)
@obrl-soil
obrl-soil / satimg_WMS_R
Created October 14, 2019 04:06
How to extract and georeference satellite WMS imagery in R
# Recipe for pulling imagery crops from the Queensland Government's public imagery basemap
# remember to credit output images appropriately using the metadata info at
# http://qldspatial.information.qld.gov.au/catalogue/custom/search.page?q=%22Queensland+Imagery+Whole+Of+State+Satellite+Public+Basemap+Service%22
library(sf)
library(raster)
library(httr)
# web service - QLD Imagery Whole of State Public basemap (no WCS unforts, hence all this)
qldimg_wms <- 'https://spatial-img.information.qld.gov.au/arcgis/services/Basemaps/LatestSatelliteWOS_AllUsers/ImageServer/WMSServer?'
@obrl-soil
obrl-soil / grow_by_one.R
Created June 22, 2019 00:07
R - Grow data patches in a raster by one cell at a time
# after https://twitter.com/ColinJCarlson/status/1142202184324530176
# kind of riffs of the game of life demo in ?raster::focal
grow_by_one <- function(x = NULL) {
focal(x, w = matrix(1, ncol = 3, nrow = 3),
fun = function(win) {
if(!is.na(win[5])) {
return(win[5])
}
@obrl-soil
obrl-soil / nc_pop.R
Last active April 29, 2019 05:44
population data for `nc`, `sf`'s demo polygon dataset (map of North Carolina's counties)
# datapasta'd http://worldpopulationreview.com/us-counties/nc/
#library(datapasta)
#df_paste()
nc_pop <- data.frame(stringsAsFactors=FALSE,
V1 = c("Mecklenburg County", "Wake County", "Guilford County",
"Forsyth County", "Cumberland County", "Durham County",
"Buncombe County", "Union County", "New Hanover County",
"Gaston County", "Cabarrus County", "Johnston County", "Onslow County",
"Pitt County", "Iredell County", "Davidson County",
"Alamance County", "Catawba County", "Orange County", "Randolph County",
# faffing about with methods to approximate a "lag vertex polygon" (Poore, 1986)
# or "best-fit regular geometric feature" (Stoddard, 1965) for polygon shape
# metric
# References:
# Poore 1986 - MSc Thesis on soil map quantification at
# https://ir.library.oregonstate.edu/downloads/zk51vk04q
# Stoddart, D. (1965). The shape of atolls. Marine Geology, 3(5), 369–383.
# doi:10.1016/0025-3227(65)90025-3 (its on scihub)
# also apparently Fridland 1972 "The pattern of the soil cover" but that's