Skip to content

Instantly share code, notes, and snippets.

View mhweber's full-sized avatar

Marc Weber mhweber

View GitHub Profile
@mhweber
mhweber / Overlaps.r
Created November 6, 2024 21:53
Filter overlapping sf polygon features in a spatial data frame in R
# poly is any polygon spatial data frame
overlap <- st_intersection(poly)
overlap <- overlap[overlap$n.overlaps > 1, ]
# The result of st_intersection contains a column called n.overlaps
# that represents the number of overlaps for the resulting geometry.
# If n.overlaps > 1 then multiple polygons are overlaping.
overlap <- overlap |>
dplyr::filter(grepl("POLYGON", st_geometry_type(geom))) |>
dplyr::filter(as.numeric(st_area(overlap)) > 100)
# This filtering reduces what is returned to only results that are of
@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
Loading
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),