Skip to content

Instantly share code, notes, and snippets.

View ateucher's full-sized avatar

Andy Teucher ateucher

View GitHub Profile
@ateucher
ateucher / bc_wms_tiles.R
Last active March 28, 2019 22:45
BC WMSTiles
library(bcmaps)
library(leaflet)
library(sf)
st_transform(bc_bound(), 4326) %>%
leaflet() %>%
#add basemap
#addpolygons and make it show in three different layers, by risk level
addPolygons() %>%
addProviderTiles(providers$CartoDB.DarkMatter,
@ateucher
ateucher / cake.R
Last active January 18, 2019 21:43
library(datasauRus)
library(ggplot2)
library(ggrepel)
library(viridisLite)
is_equal <- function(x, y, ...) {
vapply(x, function(i) identical(all.equal(i, y, ...), TRUE), FUN.VALUE = logical(1))
}
up <- 12
-- Before running, load ecoregions, ecosections, bec, nr regions
-- to the db with the same schema.table name as in bcgw
-- This assumes using ogr2ogr for the load, it creates the objectid columns
-- ----------------------------
-- first, overlay designatedlands with nr regions
-- ----------------------------
DROP TABLE IF EXISTS nr_sub;
CREATE TABLE nr_sub AS
@ateucher
ateucher / rasterizing
Created September 26, 2018 19:04 — forked from boshek/rasterizing
Comapring POSTGIS rasterization with fasterize
library(sf)
library(dplyr)
library(fasterize)
shape <- "MULTIPOLYGON (((1126095 1246073, 1126221 1246053, 1126358 1246059, 1126383 1245909, 1126457 1245800, 1126605 1245831, 1126756 1245812, 1126812 1245652, 1126805 1245526, 1126841 1245377, 1126859 1245227, 1126868 1245077, 1126881 1244927, 1126949 1244805, 1126954 1244667, 1126806 1244635, 1126658 1244602, 1126508 1244591, 1126358 1244591, 1126283 1244701, 1126248 1244825, 1126256 1244951, 1126219 1245100, 1126197 1245249, 1126175 1245374, 1126145 1245498, 1126114 1245622, 1126025 1245743, 1125919 1245839, 1125803 1245897, 1125638 1245928, 1125547 1246037, 1125693 1246143, 1125842 1246150, 1126095 1246073)))"
tictoc::tic()
## Actually connect to the database
@ateucher
ateucher / mgcv-posterior-animate.R
Created September 5, 2018 23:39 — forked from noamross/mgcv-posterior-animate.R
Animating smoothing uncertainty in a GAM
library(tidyverse)
library(gganimate)
library(mgcv)
library(mvtnorm)
# Fit a GAM to the data
mod <- gam(hp ~ s(mpg), data=mtcars, method="REML")
# Get the linear prediction matrix
newdat = data.frame(
---
title: "PostGIS+sf+Rmarkdown"
output: html_document
---
Use `bash` chunk to execute commands
```{bash message=FALSE, eval=FALSE}
createdb test123 -U postgres
psql -d test123 -U postgres -c "CREATE EXTENSION postgis;"
bcdc_to_sf <- function(layer, crs = 3005) {
if (!requireNamespace("httr") || !requireNamespace("sf")) {
stop("You must have the sf and httr packages installed.")
}
base_url <- sprintf("https://openmaps.gov.bc.ca/geo/pub/%s/ows", layer)
srs <- paste0("epsg:", crs)
response <- httr::GET(base_url,
query = list(service = "WFS",
@ateucher
ateucher / derivSimulCI.R
Created June 22, 2018 21:25 — forked from gavinsimpson/derivSimulCI.R
First derivatives and simultaneous confidence intervals for GAM spline terms
`derivSimulCI` <- function(mod, n = 200, eps = 1e-7, newdata, term,
samples = 10000) {
stopifnot(require("MASS"))
if(inherits(mod, "gamm"))
mod <- mod$gam
m.terms <- attr(terms(mod), "term.labels")
if(missing(newdata)) {
newD <- sapply(model.frame(mod)[, m.terms, drop = FALSE],
function(x) seq(min(x), max(x) - (2*eps), length = n))
names(newD) <- m.terms
@ateucher
ateucher / simulate.gamm.R
Created June 22, 2018 21:24 — forked from gavinsimpson/simulate.gamm.R
S3 method for simulate() for "gamm" objects from package mgcv
`simulate.gamm` <- function(object, nsim = 1, seed = NULL, newdata,
freq = FALSE, unconditional = FALSE, ...) {
if (!exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE))
runif(1)
if (is.null(seed))
RNGstate <- get(".Random.seed", envir = .GlobalEnv)
else {
R.seed <- get(".Random.seed", envir = .GlobalEnv)
set.seed(seed)
RNGstate <- structure(seed, kind = as.list(RNGkind()))
get_tz_list <- function() {
big_list <- lapply(OlsonNames(), function(tz) {
dates <- as.POSIXlt(seq(as.POSIXlt("2000-01-01 12:00:00", tz = tz),
as.POSIXlt("2000-12-31 12:00:00", tz = tz),
by = "1 day"))
gmt_offset <- dates$gmtoff
tz_code <- dates$zone
is_dst <- c(NA, FALSE, TRUE)[dates$isdst + 2]
gmt_offset[is.null(gmt_offset)] <- 0L
tz_code[is.null(tz_code)] <- NA_character_