Skip to content

Instantly share code, notes, and snippets.

View brownag's full-sized avatar

Andrew Gene Brown brownag

View GitHub Profile
@brownag
brownag / rsamgeo-testing.ipynb
Created May 6, 2023 20:06
rsamgeo-testing.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@brownag
brownag / alphoo.R
Last active April 25, 2023 01:21
alphanumeric sequence counts
c("abcd123", "abcd124", "abcd133", "acbd123", "dbac312", "dcba321") |>
(\(x) {
strsplit(x, "") |>
lapply(\(x) sapply(x, charToRaw)) |>
sapply(\(x) c(0, diff(as.integer(x)) == 1)) |>
colSums()
})()
#> [1] 5 4 3 2 1 0
@brownag
brownag / torus.R
Created March 30, 2023 00:53
torme, toryou, torus
# torus
ratio <- sort(c((seq(1, 10, 0.101)) / 10))#$, (seq(1, 10, 2))))
plt <- function(i){
d <- expand.grid(data.frame(
R = 1,
r = 1 * i,
theta = seq(0, 2 * pi, 0.25),
phi = seq(0, 2 * pi, 0.25)
@brownag
brownag / id_to_huc.R
Last active March 23, 2023 19:42
Convert hydrologic unit IDs to boundaries at the specified level.
#' Convert hydrologic unit IDs to boundaries at the specified level.
#'
#' Hydrologic unit boundaries are retrieved from the USGS NationalMap ArcGIS MapServer web services: `"https://hydro.nationalmap.gov/arcgis/rest/services/wbd/MapServer/"`.
#'
#' @param x character. Hydrologic Unit IDs
#' @param layerid numeric. `1:8` where `2*layerid` is equal to the number of digits in the Hydrologic Unit Code (HUC). Default: `5` for "HUC-10" watershed. See details.
#' @param base_url Default: `"https://hydro.nationalmap.gov/arcgis/rest/services/wbd/MapServer/"`
#' @details
#' The levels of `layerid` correspond to the numeric codes for the following hydrologic units (HU):
#' - WBDLine (0)
@brownag
brownag / rpostgis-raster.Rmd
Created March 20, 2023 14:38
Testing different ways of setting up postgis grid data and querying it in R
---
title: "rpostgis"
output: html_document
date: "2023-03-18"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
@brownag
brownag / point_to_huc.R
Created March 19, 2023 21:37
Convert X,Y coordinates to hydrologic unit boundaries at the specified level
#' Convert X,Y coordinates to hydrologic unit boundaries at the specified level.
#'
#' Hydrologic unit boundaries are retrieved from the USGS NationalMap ArcGIS MapServer web services: `"https://hydro.nationalmap.gov/arcgis/rest/services/wbd/MapServer/"`.
#'
#' @param x A `SpatVector` object or `numeric` vector.
#' @param y `NULL` (when x is a spatial object); otherwise a `numeric` vector equal in length to `x`
#' @param layerid numeric. `1:8` where `2*layerid` is equal to the number of digits in the Hydrologic Unit Code (HUC). Default: `5` for "HUC-10" watershed. See details.
#' @param sr_in integer. Spatial Reference System of input (`x`, `y`) as specified with numeric code. Default: `4326` for `"EPSG:4326"`.
#' @param sr_out integer. Spatial Reference System of result as specified with numeric code. Default: `sr_in`; equivalent to input.
@brownag
brownag / similar-soils-taxpartsize.md
Last active March 14, 2023 18:36
Example using `dplyr::case_match()` for assigning codes to groups of `taxpartsize` for a "similar soils" style key
library(aqp)
library(soilDB)
f <- fetchNASIS()
#> NOTE: some records are missing surface fragment cover
#> mixing dry colors ... [1 of 43 horizons]
#> mixing moist colors ... [1 of 43 horizons]
#> NOTE: some records are missing rock fragment volume
#> NOTE: all records are missing artifact volume
@brownag
brownag / test-multi-CRS.R
Created March 6, 2023 21:48
reprex for bug in files with multiple layers and SRS in terra (gpkg example)
library(terra)
gdal(lib = "")
data(us_states, package = "spData")
x <- vect(us_states)[1,]
y <- project(x, "EPSG:5070")
writeVector(x, "test.gpkg")
writeVector(y, "test.gpkg", layer = "test2", insert = TRUE)
@brownag
brownag / SSURGOPortal-DataLoader-Commands.md
Last active December 23, 2022 21:34
Generate SSURGO Portal Data Loader Commands with R
#' Generate Requests for SSURGO Portal Data Loader
#' 
#' Generate commands for or JSON output from the SSURGO Portal Data Loader command-line interface.
#'
#' @param request One of the available request types, see Details.
#' @param pyz_path Path to "SSURGO Portal" .pyz file. Default `"SSURGOPortal.pyz"`
#' @param ... Additional parameters for `request`
#' @param schema Return request and response schemas? Default: `FALSE`
#' @param command_only Return command string rather than executing it? Default: `FALSE`
@brownag
brownag / soil_co3_NY.md
Last active November 8, 2022 16:57
Soils with carbonate parent materials in the state of New York
library(soilDB)
library(terra)
#> terra 1.6.40
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE

data(us_states, package = "spData")

# query soil data access for dominant condition (by mapunit) parent material information for NY