Skip to content

Instantly share code, notes, and snippets.

View jsta's full-sized avatar
🪼
...

Jemma Stachelek jsta

🪼
...
View GitHub Profile
@jsta
jsta / project_data.frame.R
Created May 4, 2017 16:13
Project a shapefile and view as data.frame
library(sp)
library(rgdal)
exts <- c(".shp", ".dbf", ".sbn", ".sbx", ".shx")
dt_path <- paste0(tempfile(), exts)
dt <- download.file(paste0("http://geog.uoregon.edu/GeogR/data/shp/orstations",
exts), dt_path)
dt <- readOGR(dt_path[1])
@jsta
jsta / norm_dist_mean_jags.R
Created May 4, 2017 19:26
Model normal distribution mean with jags
# See http://www.johnmyleswhite.com/notebook/2010/08/20/using-jags-in-r-with-the-rjags-package/
N <- 1000
x <- rnorm(N, 0, 5)
modelstring <- "
model {
for(i in 1:N) {
x[i] ~ dnorm(mu, tau)
}
@jsta
jsta / nlcd_2006_palette.R
Created May 29, 2017 23:41
National Land Cover Database 2006 R Palette Color Key
# National Landcover Database 2006 (NLCD)
nlcd_palette <- data.frame(
class = c("water", "water",
"developed", "developed", "developed", "developed",
"barren",
"forest", "forest", "forest",
"shrubland", "shrubland",
"herbaceous", "herbaceous", "herbaceous", "herbaceous",
"planted", "planted",
@jsta
jsta / linear_quarter-circle.R
Last active June 19, 2017 13:58
Linear to ellipse interpolator
x <- seq(0, 10, length.out = 80)
y <- x
#' @param slope numeric slope of the original line
#' @param frac_along numeric fraction along the original line to draw control point array
#' @param seq_len numeric fraction of the line to include as a control point
#'
generate_perp <- function(x, y, slope = 1, frac_along = 0.5, seq_len = c(0.25)){
lapply(frac_along, function(i) {
x_offset <- i * range(x)[2] - 5
@jsta
jsta / sf_text_sf.R
Created July 6, 2017 16:52
Convert from sf data.frame to sf text and back again
library(sf)
library(ggplot2)
# crs <- st_crs(orig_sf)
# test <- st_geometry(orig_sf)
# test_text <- st_as_text(test)
crs <- st_crs(32610)
test_text <- "LINESTRING(4683684.86627714 6127410.96399602, 4683791.86302937 6127277.7501861, 4683823.72428078 6127263.81933025, 4683937.27439384 6127249.11683816, 4683969.89585432 6127219.69092887, 4683951.89224796 6127168.37796377, 4683866.89142587 6127085.63923137, 4683850.50012205 6127051.83321428, 4683847.90414026 6126904.28325476, 4683913.46734349 6126776.99477916, 4683939.65765144 6126701.82749159, 4683999.27975858 6126605.97125794, 4684069.7019509 6126531.19991619, 4684137.39774915 6126498.57646262, 4684185.17287752 6126465.56639239)"
@jsta
jsta / ifelse_strategies.R
Created July 7, 2017 17:08
Multiple strategies to classify a numeric column using functions, sequential logic, and dplyr
dt <- data.frame(a = seq(1, 10) + rnorm(1))
# option 1 ####
key_function <- function(x) {
if(x > 7){
"x"
}else{
if(x > 4 & x < 7){
"y"
}else{
@jsta
jsta / estimate_linear_hypso.R
Last active July 19, 2017 15:00
Estimate hypsography as a linear function of the number of layers, surface area, and max depth
library(glmtools)
estimate_linear <- function(nlayers, surface_area, max_depth){
layer_thickness <- max_depth / nlayers
depth_vec <- seq(0, max_depth, by = layer_thickness)
df_pred <- data.frame(
areas = c(surface_area, 0),
layer_ind = c(1, nlayers + 1))
@jsta
jsta / nls_fit.R
Created August 9, 2017 16:48
Example of fitting a complex function with nls
x <- 1:10
y <- 1 - (1 / (1 + 2*x))
yeps <- y + rnorm(length(y), sd = 0.01) # added noise
fit <- nls(yeps ~ 1 - (1 / (1 + b*x)), start = list(b = 2))
coef(fit)
plot(x, y)
lines(x, predict(fit))
@jsta
jsta / gpkg.sh
Created August 25, 2017 13:45
Convert a ESRI filegeodatabase to geopackage (gpkg)
ogr2ogr -f GPKG foo.gpkg foo.gdb
@jsta
jsta / reservoir_morphology.R
Last active September 21, 2017 00:50
Retrieve the Reservoir Morphology Database from Sciencebase.gov
library(sbtools)
library(readxl)
library(dams)
library(dplyr)
library(sf)
library(maps)
library(ggplot2)
# Get Reservoir Morphology Database ####
temp_dir <- tempdir()