Skip to content

Instantly share code, notes, and snippets.

View dblodgett-usgs's full-sized avatar

David Blodgett dblodgett-usgs

View GitHub Profile
@dblodgett-usgs
dblodgett-usgs / area_weighted.R
Last active November 15, 2018 20:49
An R function to calculate area weighted intersections.
library(dplyr)
library(sf)
library(testthat)
#' Area Weighted Intersection
#' @description Returns the fractional percent of each
#' feature in y that is covered by each intersecting feature
#' in x. These can be used as the weights in an area-weighted
#' mean overlay analysis where y is the data source and area-
#' weighted means are being generated for x.
@dblodgett-usgs
dblodgett-usgs / regrid_daymet.R
Last active December 25, 2018 17:28
Regrid daymet data to a collection of polygon cells.
library(sf)
library(ncdf4)
library(dplyr)
library(retry)
# Need: https://github.com/USGS-R/ncdfgeom
# Need: https://github.com/ramhiser/retry/
get_time_nc <- function(t_dim) {
time_units<-strsplit(t_dim$units, " ")[[1]]
time_step<-time_units[1]
@dblodgett-usgs
dblodgett-usgs / elevtr_and_rayshader.R
Created August 28, 2018 13:57
Grab some elevation data near Cross Plains WI with elevatr and plot with rayshader.
# https://github.com/tylermorganwall/rayshader
# https://github.com/jhollist/elevatr
library(magrittr)
bb <- setNames(c(-89.735813,43.056221,-89.625778,43.122036), c("xmin", "ymin", "xmax", "ymax"))
class(bb) <- "bbox"
bb <- sp::bbox(sf::as_Spatial(sf::st_as_sfc(bb)))
tiles <- elevatr::get_aws_terrain(bb, 12, "+init=epsg:4326")
@dblodgett-usgs
dblodgett-usgs / ncdfgeom_sample.R
Created August 22, 2018 20:40
Create ncdfgeom sample file
devtools::install_github("USGS-R/ncdfgeom")
# Also need jsonlite and sf but devtools should install with ncdfgeom
fixtureData <- jsonlite::fromJSON(system.file("extdata/fixture_wkt.json",
package = "ncdfgeom"))
polygon <- sf::st_sf(geom = sf::st_as_sfc(fixtureData$`2d`$polygon),
crs = "+init=epsg:4326")
lon_lat <- sf::st_coordinates(sf::st_centroid(polygon))
@dblodgett-usgs
dblodgett-usgs / examples_netcdf_coordinates.md
Created July 16, 2018 12:24
examples for a thread about netcdf-cf geometries and the use of coordinates and bounds on the node_coordinates variables

In the example below, given that :featureType = "timeSeries" ;, data variables should be expected to have "coordinates" e.g. someData:coordinates = "time lat lon" ;.

dimensions:
  instance = 2 ;
  node = 5 ;
  time = 4 ;
variables:
  int time(time) ;
    time:units = "days since 2000-01-01" ;
@dblodgett-usgs
dblodgett-usgs / split_lines.R
Last active March 19, 2024 10:34
Splits lines longer than a given threshold into the minimum number of pieces to all be under the given threshold.
#' @title split lines
#' @description Splits lines longer than a given threshold into the minimum number of pieces to all be under the given threshold.
#' @param lines data.frame of class sf with LINESTRING sfc column.
#' @param max_length maximum segment length to return
#' @param id name of ID column in data.frame
#' @return only the split lines.
#' @importFrom dplyr group_by ungroup filter left_join select rename mutate
#' @export
#'
split_lines <- function(input_lines, max_length, id = "ID") {
@dblodgett-usgs
dblodgett-usgs / git_remote_setup.md
Last active May 24, 2017 13:18
A document I wrote for an OGC project that may be useful generally.

As described in the contributing section of the readme, content can be submitted via pull request or by opening a new issue pointing to content that needs to be brought in. This page describes how to add content to a fork of the repository and submit a pull request containing those changes.

git and git BASH

For mac and linux users, you will need the git command line tools. Windows users should install git-for-windows, which includes git BASH, a bash shell emulator.

ssh keys

If you want to type passwords for everything, skip this section and only use the "https" option when setting up remote repositories (lame). If you hate typing your username and password, follow the instructions here to get your ssh key setup on github. Pro tip: If you want to live dangerously and never type passwords, create a passwordles

@dblodgett-usgs
dblodgett-usgs / geoknife_zoo_analysis.R
Last active August 2, 2016 20:30
Some R code to pull down a time series of precipitation data for a HUC12 and plot annual maximums.
library(geoknife)
# Use the WBD feature service available from the National Water Census.
stencil <- webgeom(url = "http://cida.usgs.gov/nwc/geoserver/WBD/ows")
query(stencil,'geoms')
# [1] "WBD:huc08" "WBD:huc12" "WBD:huc12agg" "WBD:huc12all"
geom(stencil) <- 'WBD:huc12'
query(stencil, 'attributes')
# [1] "ogc_fid" "tnmid" "metasource" "sourcedata" "sourceorig" "sourcefeat" "loaddate" "gnis_id"
# [9] "areaacres" "areasqkm" "states" "huc12" "hutype" "humod" "tohuc" "noncontrib"
# [17] "noncontr_1" "shape_leng" "shape_area"
getPrecip <- function(states, startDate, endDate){
wg_s <- webgeom(geom = 'derivative:US_Counties', attribute = 'STATE')
wg_c <- webgeom(geom = 'derivative:US_Counties', attribute = 'COUNTY')
wg_f <- webgeom(geom = 'derivative:US_Counties', attribute = 'FIPS')
county_info <- data.frame(state = query(wg_s, 'values'), county = query(wg_c, 'values'),
fips = query(wg_f, 'values'), stringsAsFactors = FALSE) %>%
unique()
counties_fips <- county_info %>% filter(state %in% states) %>%