Skip to content

Instantly share code, notes, and snippets.

View brownag's full-sized avatar

Andrew Gene Brown brownag

View GitHub Profile
@brownag
brownag / ca109_geopackage.R
Last active March 8, 2019 16:50
create a geopackage containing muaggatt table and spatial data from SSURGO (Tuolumne County)
library(sf)
library(RSQLite)
library(USAboundaries)
library(FedData)
label <- "ca109"
raw.dir <- "RAW/SSURGO"
extraction.dir <- "EXTRACTIONS"
output.file <- "ca109.gpkg"
@brownag
brownag / FedData_Demo.R
Last active March 8, 2019 18:57
Fetching SSURGO data for arbitrarily large (multi-SSA/multi-county) extents with the FedData package (example: Central Sierra Nevada, California)
## Example: Fetch SSURGO data for arbitrarily large/complex areas with the FedData package
#@author: Andrew Brown
# sf package for USAboundaries
library(sf)
# USAboundaries for getting county geometry
library(USAboundaries)
# FedData for routines for accessing/stitching multi-SSA SSURGO data
@brownag
brownag / chunkApply_demo.R
Created January 19, 2020 02:42
chunkApply -- new version of profileApply that scales linearly with tens of thousands of profiles
chunkApply <- function(object, FUN,
simplify = TRUE,
frameify = FALSE,
chunk.size = 100,
column.names = NULL,
...) {
if(simplify & frameify) {
message("simplify and frameify are both TRUE -- ignoring simplify argument", .call=FALSE)
simplify <- FALSE
}
library(microbenchmark)
library(data.table)
library(plyr)
# make some test data -- 200k records
# we will try rbind-filling two of these
test.data <- vector('list', length = 200)
test.data[] <- lapply(1:200, rnorm, n = 1000)
test.data <- as.data.frame(test.data, stringsAsFactors = FALSE)
#' Calculate quasi-Gower's Distance for all profiles in a SoilProfileCollection
#'
#' @description This experimental function calculates pairwise distances between profiles in a SoilProfileCollection by a method related to Gower's Distance metric. One or more site or horizon level attributes are supplied as arguments for use in the distance calculation. Both numeric and categorical attributes are allowed -- with "difference" values calculated for numeric values and for "different" and "equal" categories \code{1} and \code{0} are assigned respectively.
#'
#' This is a SoilProfileCollection-specific implementation of the concept. The distance calculations specified in the method are first applied to horizon data, and then to the site data, to produce a "final" distance matrix.
#'
#' The pairwise distance of all horizons in the collection is reduced to a single value for each profile-level comparison. The horizon data distance matrix is scaled by both the range within individual variables (first), and the to
library(aqp)
data(sp2)
depths(sp2) <- id ~ top + bottom
site(sp2) <- ~ surface
hzdesgnname(sp2) <- "name"
# update soil colors that we have data for plot
sp2$soil_color <- munsell2rgb(sp2$hue, sp2$value, sp2$chroma)
@brownag
brownag / get_component_from_SDA_test-ES-subquery.R
Created June 19, 2020 01:57
The following works as expected.
## add support for ecosite-based queries in fetchSDA_Component / get_component_from_SDA;
## only uses NRCS forest/range sites; test for possible many:one duplication issues
## brownag committed on Jun 3, 2019
## https://github.com/ncss-tech/soilDB/commit/4ad25e7b628e854fe0d1d21bdd2484022acfa922
devtools::install_github('ncss-tech/soilDB@4ad25e7b628e854fe0d1d21bdd2484022acfa922',
dependencies = FALSE)
library(soilDB)
@brownag
brownag / mpspline2-with-aqp.R
Last active July 13, 2020 01:26
Use aqp to handle missing data and typical pitfalls with field profile descriptions for IO from new R package mpspline2 by @obrl_soil
# making use of @obrl-soil's new mpspline2 package [soon to be on CRAN!]
#
# author: andrew g brown; 2020/07/12
#
# this is an attempt at creating a generic method for mass-preserving / equal-area splines
# on aqp SoilProfileCollection (SPC) data that may contain a large amount of missing values.
#
# it also provides an avenue to get 1cm continuous spline output back in a handy form (1cm sliced SPC).
#
# In the presence of missing values, getting the result back into the SPC is not always easy.
ogc_fun <- function(x,y,theta) {
sqrt(x^2 + y^2)*cos(theta - atan(y / x))
}
x <- y <- -100:100
plot(x, y,
type="n",
xlim=c(0,200),
ylim=c(-150,150))
@brownag
brownag / munsell2rgb-SPC.R
Last active July 15, 2020 22:31
Example: A wrapper method (should be dispatched via S4) for munsell2rgb for the SoilProfileCollection object
# I just named this "munsell2rgb2"... but there is no reason why it cant be
# "munsell2rgb,SoilProfileCollection-method" in aqp
munsell2rgb2 <- function(object,
hue = "hue", value = "value", chroma = "chroma",
as.spc = TRUE) {
h <- horizons(object)
# makes a data.frame
drgb <- munsell2rgb(h[[hue]], h[[value]], h[[chroma]], returnLAB = TRUE)