Skip to content

Instantly share code, notes, and snippets.

View mhweber's full-sized avatar

Marc Weber mhweber

View GitHub Profile
@mhweber
mhweber / combine.r
Created February 24, 2023 19:26
Combine multiple .csv files in R
require(readr) # for read_csv()
require(dplyr) # for mutate()
require(tidyr) # for unnest()
require(purrr) # for map(), reduce()
data_path <- 'c:/user/myname' # put your file path here
# find all file names ending in .csv
files <- dir(data_path, pattern = "*.csv")
data <- files %>%
library(sf)
library(mapview)
# read identifiers for Colorado River and Colorado River (TX) into simple features
colorado_river <- sf::read_sf("https://geoconnex.us/ref/mainstems/29559")
colorado_river_tx <- sf::read_sf("https://geoconnex.us/ref/mainstems/2639515")
## Function that constructs query to retrive the latest discharge observation from the USGS
@mhweber
mhweber / GetStreamCatMetricsList.ipynb
Last active June 9, 2022 21:06
Get StreamCat metrics from secure ftp for particular catchments and certain metrics
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mhweber
mhweber / ListDownloadStreamCat.py
Created January 21, 2022 15:33
list and download StreamCat files
from bs4 import BeautifulSoup
import requests
url = "https://gaftp.epa.gov/epadatacommons/ORD/NHDPlusLandscapeAttributes/StreamCat/HydroRegions"
ext = 'zip'
def listFD(url, ext=''):
page = requests.get(url, verify=False).text
print(page)
soup = BeautifulSoup(page, 'html.parser')
@mhweber
mhweber / iterate_waters.R
Created October 25, 2021 15:25 — forked from jhollist/iterate_waters.R
Example code for using a dataframe to access USEPA WATERS API. Shows two iteration examples: purrr and for loop.
library(purrr)
library(jsonlite)
library(dplyr)
library(usethis)
# The Data.gov API requires a key and has a 1000 request/hour limit
# Get Key from https://api.data.gov/signup/
# Save key in r environ
usethis::edit_r_environ()
# Add DATAGOV_KEY=YOURKEYGOESHERE to the file. Save and restart R
@mhweber
mhweber / WMS_NHDHR_NHDPlus.R
Created January 26, 2021 03:21
Display WMS of NHDPlus NHD HR with mapview
library(mapview)
library(tidyr)
library(sf)
library(leaflet)
points <- tribble(~name, ~lat, ~lon,
'Point A',44.564568,-123.262047, )
points_sf <- st_as_sf(points, coords = c("lon", "lat"), crs = 4269)
m <- mapview(points_sf, legend=FALSE)
m@map <- m@map %>% addWMSTiles(group = 'NHDPlus',
"https://watersgeo.epa.gov/arcgis/services/NHDPlus_NP21/NHDSnapshot_NP21/MapServer/WmsServer?",
@mhweber
mhweber / Combine_files_and_zip.py
Created September 10, 2020 00:54
Combine multiple .csv files into one pandas dataframe and write out to a zip file
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 9 16:46:15 2020
@author: mweber
"""
# Combine hydro-region tables if desired
import pandas as pd
@mhweber
mhweber / SpatialLinesEnpoints_sf
Created May 30, 2020 20:16
Derive endpoints for spatial lines in sf
#Create linestring
line <- st_as_sfc(c("LINESTRING(0 0 , 0.5 1 , 1 1 , 1 0.3)")) %>%
st_sf(ID = "poly1")
#Convert each vertex to point
pt <- st_cast(line, "POINT")
# Grab start or end point:
start <- pt[1,]
end <- pt[nrow(pt),]
@mhweber
mhweber / Mapview_wms.R
Last active January 28, 2020 18:31
R mapview display wms
library(sf)
library(dplyr)
library(mapview)
library(leaflet)
m <- mapview()
m@map = m@map %>% addWMSTiles(group = 'NHDPlus',
"https://watersgeo.epa.gov/arcgis/services/NHDPlus_NP21/NHDSnapshot_NP21/MapServer/WmsServer?",
layers = 4,
options = WMSTileOptions(format = "image/png", transparent = TRUE),
@mhweber
mhweber / RasterToPoint.py
Created January 23, 2020 20:21
raster to point
raster_2d = 2-dimensional array imported with gdal
nodata = nodata value from gdal import for 2-d array
geotransform = geotrasform from gdal import of 2-d array
def raster_to_point(raster_2d, nodata, geotransform):
raster_2d = raster_2d.flatten()
locs = np.where(raster_2d <> nodata)[0] #Get locs in array w/ data
raster_2d = raster_2d[locs] #Reduce data to those locs
geo = list(geotransform) #Make geotransform from raster a list
x = locs % xsize #Convert flattened locations to x coord