Skip to content

Instantly share code, notes, and snippets.

View mikemahoney218's full-sized avatar
🌲
I'm defending my PhD on May 16th, so will have limited available until then!

Michael Mahoney mikemahoney218

🌲
I'm defending my PhD on May 16th, so will have limited available until then!
View GitHub Profile
# values specific to your stac source
# in this case, from
# https://planetarycomputer.microsoft.com/dataset/usgs-lcmap-conus-v13
COLLECTION="usgs-lcmap-conus-v13"
ASSET="lcpri"
# query parameters: bbox (wgs84) and datetime (utc)
# I grabbed these values from R
WGS84_BBOX="-79.762,40.496,-71.856,45.013"
DATETIME="2021-01-01/2021-12-31"
@mikemahoney218
mikemahoney218 / stack_rasters.R
Created July 28, 2023 17:43
Combine multiple multi-band rasters into a single VRT
stack_rasters <- function(rasters,
raster_path,
...,
reference_layer = 1,
resampling_method = "bilinear") {
check_type_and_length(
reference_layer = integer(1)
)
@mikemahoney218
mikemahoney218 / Dockerfile
Created October 7, 2022 01:21 — forked from gaborcsardi/Dockerfile
Dockerfile for R-devel with clang 15 and libc++-15
FROM tuxmake/clang-15
RUN apt-get update -y ; apt-get install -y curl
RUN curl -Ls https://github.com/r-lib/rig/releases/download/latest/rig-linux-latest.tar.gz | tar xz -C /usr/local
RUN rig add devel
RUN mkdir ~/.R
library(spatialsample)
# Might need to get the CRS if you haven't before:
sf::sf_proj_network(TRUE)
sf::sf_add_proj_units()
spatial_clustering_cv(boston_canopy, v = 5) |>
autoplot()
spatial_block_cv(boston_canopy, v = 5, method = "snake") |>
@mikemahoney218
mikemahoney218 / make_clusters.R
Last active June 15, 2022 22:10
Testing several clustering methods in spatial_clustering_cv
# remotes::install_github('tidymodels/spatialsample')
library(spatialsample)
library(cluster)
make_clusters <- function(func) {
set.seed(123)
boston_canopy |>
spatial_clustering_cv(cluster_function = func) |>
autoplot()
}
@mikemahoney218
mikemahoney218 / tidy_geocomp.R
Last active August 31, 2022 07:21
Proof-of-concept replication of https://geocompr.robinlovelace.net/spatial-cv.html with tidymodels
# 12.5 Spatial CV (with spatialsample)
library(tidymodels)
library(spatialsample)
library(sf)
data("lsl", "study_mask", package = "spDataLarge")
lsl <- lsl |>
st_as_sf(coords = c("x", "y"), crs = "EPSG:32717")
ta <- terra::rast(system.file("raster/ta.tif", package = "spDataLarge"))
@mikemahoney218
mikemahoney218 / make_turmite.R
Created March 15, 2022 21:02
Quick code to make turmites in R
.cursor <- R6::R6Class(
"cursor",
list(
x = 0,
y = 0,
direction = 0,
left = function() {
self$direction <- self$direction + 270
invisible(self)
},
@mikemahoney218
mikemahoney218 / usa_pop.R
Created January 26, 2022 23:52
Quick population map
library(sf)
library(tidycensus)
library(dplyr)
library(ggplot2)
options(tigris_use_cache = TRUE)
acs_pop <- do.call(
rbind,
lapply(
setdiff(state.abb, c("AK", "HI")),
@mikemahoney218
mikemahoney218 / 20211128.R
Created November 28, 2021 19:28
Code for my map for the 30 Day Map Challenge day 28 (November 28 2021) "The world is not flat!"
library(terrainr)
library(raster)
library(rayshader)
library(sf)
baker <- tmaptools::geocode_OSM("Mount Baker")
baker <- st_as_sf(as.data.frame(t(as.data.frame(baker$coords))), coords = c("x", "y"), crs = 4326)
zscale <- 5
baker <- set_bbox_side_length(
baker,
@mikemahoney218
mikemahoney218 / 20211116.R
Created November 15, 2021 15:27
Code for my map for the 2021 30 Day Map Challenge day 15 ("Population")
library(sf)
library(tidycensus)
library(ggplot2)
library(dplyr)
acs_pop <- lapply(
setdiff(state.abb, c("AK", "HI")),
\(x) {
get_acs(
geography = "block group",