Skip to content

Instantly share code, notes, and snippets.

David Blodgett dblodgett-usgs

Block or report user

Report or block dblodgett-usgs

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@dblodgett-usgs
dblodgett-usgs / animate_accumulate_wbd.R
Created Dec 10, 2018
A visualization script to create frames of an animated gif of WBD accumulation where all paths terminate on the last time step.
View animate_accumulate_wbd.R
# https://github.com/dblodgett-usgs/nhdplusTools
library(nhdplusTools)
# https://github.com/USGS-R/HUCAgg
library(HUCAgg)
library(sf)
library(dplyr)
library(snow)
wbd_gdb <- "WBD_National_GDB.gdb"
@dblodgett-usgs
dblodgett-usgs / find_upstream_gages.R
Last active Dec 2, 2018
Script to determine what stream gages are upstream of each streamgage in the gage layer included in the NHDPlus V2 database. See comments below for description.
View find_upstream_gages.R
library(nhdplusTools)
library(sf)
library(igraph)
library(dplyr)
library(tidyr)
library(readr)
library(jsonlite)
nhdplus_path("../../4_data/NHDPlusNationalData/NHDPlusV21_National_Seamless.gdb/")
nhd_paths <- stage_national_data()
@dblodgett-usgs
dblodgett-usgs / area_weighted.R
Last active Nov 15, 2018
An R function to calculate area weighted intersections.
View area_weighted.R
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 Dec 25, 2018
Regrid daymet data to a collection of polygon cells.
View regrid_daymet.R
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 Aug 28, 2018
Grab some elevation data near Cross Plains WI with elevatr and plot with rayshader.
View elevtr_and_rayshader.R
# 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 Aug 22, 2018
Create ncdfgeom sample file
View ncdfgeom_sample.R
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 Jul 16, 2018
examples for a thread about netcdf-cf geometries and the use of coordinates and bounds on the node_coordinates variables
View examples_netcdf_coordinates.md

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 Feb 10, 2019
Splits lines longer than a given threshold into the minimum number of pieces to all be under the given threshold.
View split_lines.R
#' @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
A document I wrote for an OGC project that may be useful generally.
View git_remote_setup.md

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 Aug 2, 2016
Some R code to pull down a time series of precipitation data for a HUC12 and plot annual maximums.
View geoknife_zoo_analysis.R
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"
You can’t perform that action at this time.