Skip to content

Instantly share code, notes, and snippets.

View brownag's full-sized avatar

Andrew Gene Brown brownag

View GitHub Profile
@brownag
brownag / huc12_to_dem.R
Last active October 21, 2022 17:56
Download soil survey area boundary(ies), intersect with watershed boundaries, then download the USGS/NED DEM from Google Earth Engine
Sys.setenv(PYTHONHOME = "C:\\Program Files\\Python310")
library(rgeedim)
library(terra)
library(soilDB)
officeid <- "2SON"
ssaid <- "CA732"
scaleres <- 10
destdir <- paste0("D:/", officeid)
@brownag
brownag / get_NSM_current.R
Created September 18, 2022 01:18
Download nationalsoilmoisture.com CONUS Soil Moisture Percentile ASCII Grids
#' Download nationalsoilmoisture.com CONUS Soil Moisture Percentile ASCII Grids
#'
#' @return data.frame containing file name root, grid label, source URL and download status
#' @export
#'
#' @examples
#' # library(terra)
#'
#' # options(timeout = 300)
#' # nsm_products <- get_NSM_current()
@brownag
brownag / rgeedim-s2-ndvi-animated.R
Created July 27, 2022 00:25
Create animated Sentinel-2 (COPERNICUS/S2_SR) NDVI graphic for summer months
# remotes::install_github("brownag/rgeedim")
library(rgeedim)
library(terra)
library(soilDB)
library(gifski)
# this could be any extent, using a single MU polygon as a small example
bb <- fetchSDA_spatial(460954)
bb$pID <- 1:nrow(bb)
bb2 <- terra::intersect(vect(data.frame(y=39.974942, x=-121.961947), geom=c("x","y"), crs="EPSG:4326"), vect(bb))
@brownag
brownag / terra-gpkg-Rtree.md
Created July 26, 2022 00:19
terra and GeoPackage Rtree spatial index

The GeoPackage standard has an extension that allows for Rtree spatial indexes (http://www.geopackage.org/guidance/extensions/rtree_spatial_indexes.html).

When writing a SpatVector to GeoPackage, we get an Rtree "for free." But I currently don't have any workflows that really make use of it. Most of my GPKG usage is as a "storage" medium, rather than for analysis. GPKG is so much better than a directory full of shapefiles (and all their ancillary files) exploring first-class geopackage support for {terra} is https://github.com/brownag/gpkg.

For example:

library(terra)
v &lt;- vect(system.file("ex", "lux.shp", package = "terra"))
@brownag
brownag / gnatsgo-gpkg-raster.R
Last active July 25, 2022 22:44
gNATSGO Geopackage Raster Data Demo
# demonstration of soilDB SSURGO geopackage functionality for _raster_ (gNATSGO) data
library(soilDB)
library(terra)
library(gpkg) # remotes::install_github("brownag/gpkg")
# download gSSURGO for an arbitrary extent
bb <- fetchSDA_spatial("CA760", by.col = "areasymbol", geom.src = "sapolygon")
# get mukey raster from SoilWeb web coverage service (TODO: limited to 5000x5000)
wcs <- mukey.wcs(as.polygons(ext(bb) / 2, "EPSG:4326"), db = "gNATSGO")
@brownag
brownag / ssurgo-gpkg-vector.R
Created July 25, 2022 21:10
SSURGO Geopackage Vector Data Demo
# demonstration of soilDB SSURGO geopackage functionality for _vector_ data
library(soilDB)
library(terra)
# increase timeout for VPN
options(timeout = 360)
# download SSURGO databases
downloadSSURGO(areasymbols = c("CA802","CA740","CA732","CA792","CA668","CA760"), destdir = "myproject")
@brownag
brownag / rgeedim-ndvi-example.R
Created July 22, 2022 17:42
Example of calculating NDVI from latest NAIP (2020) images composited for an arbitrary extent. Example uses CA630 soil survey area boundary extent for demonstration.
# remotes::install_github("brownag/rgeedim")
library(rgeedim)
library(terra)
library(soilDB)
# this could be any extent, using a single SSA as a small example
bb <- fetchSDA_spatial("CA630", by.col = "areasymbol", geom.src = 'sapolygon')
## authenticate if needed
# gd_authenticate(auth_mode = "notebook")
@brownag
brownag / jacinto.R
Created July 18, 2022 19:44
The Jacinto soils occur on low, gently undulating ridges near old abandoned stream beds. Underlying alluvium is wind modified and was derived from sedimentary and metasedimentary rocks in the Sacramento Valley of California.
library(soilDB)
library(rgeedim) # remotes::install_github("brownag/rgeedim")
f <- fetchSDA("compname = 'Jacinto'", duplicates = TRUE)
s <- fetchSDA_spatial(f$nationalmusym, by.col = 'nationalmusym')
plot(s["nationalmusym"])
gd_initialize()
@brownag
brownag / sketch-solus-comparison.md
Last active July 14, 2022 16:33
A brief sketch of comparison of SOLUS predictions against abiotic factors (e.g. elevation gradient) and local KSSL data

2022-07-14

library(aqp)
## This is aqp 1.43
library(soilDB)
@brownag
brownag / rgeedim-demo.R
Last active July 13, 2022 21:20
rgeedim demo
# remotes::install_github("brownag/rgeedim")
library(rgeedim)
library(soilDB)
library(terra)
library(viridis)
# earth engine setup
rgeedim::gd_initialize()
# ee <- reticulate::import("ee")