nhdR
An R interface to the US National Hydrography Dataset.
https://github.com/jsta/nhdr
Category: Natural Resources
Sub Category: Water Supply and Quality
Keywords
cran geospatial national-hydrography-dataset nhd rstats water-quality water-resources
Keywords from Contributors
lake estuary ecology limnology
Last synced: about 2 hours ago
JSON representation
Repository metadata
R interface to the National Hydrography Dataset :droplet:
- Host: GitHub
- URL: https://github.com/jsta/nhdr
- Owner: jsta
- Created: 2016-12-01T22:41:39.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-08-14T13:25:27.000Z (over 2 years ago)
- Last Synced: 2025-12-21T01:47:03.368Z (5 days ago)
- Topics: cran, geospatial, national-hydrography-dataset, nhd, rstats, water-quality, water-resources
- Language: R
- Homepage: https://jsta.github.io/nhdR/
- Size: 9.9 MB
- Stars: 39
- Watchers: 2
- Forks: 12
- Open Issues: 16
- Releases: 13
-
Metadata Files:
- Readme: README.Rmd
README.Rmd
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://cran.r-project.org/package=nhdR)
[](https://github.com/jsta/nhdR/actions/workflows/R-CMD-check.yml)
[](https://zenodo.org/badge/latestdoi/75339263)
# nhdR
Tools for querying, downloading, and networking both the [National Hydrography Dataset (NHD)](https://www.usgs.gov/national-hydrography) and [NHDPlus](https://www.epa.gov/waterdata/nhdplus-national-hydrography-dataset-plus) datasets.
## Installation
CRAN policy is that no package can write to a persistent location by default. As a result, `nhdR` writes all data to a temporary location unless a `temporary = FALSE` argument is passed to the `nhd_plus_get`/`nhd_get` functions. Alternatively, `nhdR` will automatically write data to a persistent location if the `nhdR_path` environment variable is set. To do this, add the following line to your `.Rprofile`:
```r
Sys.setenv(nhdR_path = file.path(rappdirs::user_data_dir(appname = "nhdR",
appauthor = "nhdR")))
```
Your `.Rprofile` file can be edited using the `usethis::edit_r_profile()` function.
### Stable version from CRAN
```{r, eval=FALSE}
install.packages("nhdR")
```
### or development version from GitHub
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("jsta/nhdR")
```
This package also requires an installation of [7-zip](https://www.7-zip.org/) that can be called via the command line using `7z` or `7za.exe` (check if your machine is good to go with `nhdR:::has_7z()`).
## Usage
### Load package
```{r message=FALSE, results='hide'}
library(nhdR)
```
### NHD Plus
NHD-Plus exports are organized by vector processing unit (vpu). See below for a low resolution vpu map (also `nhdR::vpu_shp`). A hi-res version can be found [here](https://www.epa.gov/waterdata/nhdplus-global-data).
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(ggplot2)
library(stringr)
dt <- nhdR::vpu_shp # [,"UnitID"]
dt <- dt[dt$UnitType == "VPU", ]
centroid_xy <- sf::st_as_text(sf::st_geometry(sf::st_centroid(dt[, "UnitID"])))
extract_coords <- function(messy_centroid) {
res <- stringr::str_split(messy_centroid, "\\(", simplify = TRUE)[2]
res <- stringr::str_split(res, "\\)", simplify = TRUE)[1]
stringr::str_split(res, " ", simplify = TRUE)
}
coords <- data.frame(matrix(
as.numeric(unlist(lapply(centroid_xy, extract_coords))),
ncol = 2, byrow = TRUE), stringsAsFactors = FALSE)
names(coords) <- c("x", "y")
coords$x <- coords$x * -1
coords$UnitID <- dt[, "UnitID"]$UnitID
ggplot(dt) +
geom_sf(aes(fill = UnitID), show.legend = FALSE) +
xlim(126, 70) +
ylim(23, 52) +
geom_text(data = coords, aes(x = x, y = y, label = UnitID)) +
theme_minimal() +
theme(axis.title = element_blank()) +
ggtitle("Vector Processing Units (VPU)")
```
```{r eval=FALSE}
# get a vpu export
nhd_plus_get(vpu = 4, "NHDSnapshot")
nhd_plus_get(vpu = 4, "NHDPlusAttributes")
nhd_plus_get(vpu = 4, "NHDPlusCatchment")
```
``` r
# list layers
nhd_plus_list(vpu = 4, "NHDSnapshot")
#> [1] "NHDArea.dbf" "NHDAreaEventFC.dbf"
#> [3] "NHDAreaEventFC.shp" "NHDArea.shp"
#> [5] "NHDFCode.dbf" "NHDFlowline.dbf"
#> [7] "NHDFlowline.shp" "NHDFlowline.shp.xml"
#> [9] "NHDLine.dbf" "NHDLineEventFC.dbf"
#> [11] "NHDLineEventFC.shp" "NHDLine.shp"
#> [13] "NHDPoint.dbf" "NHDPointEventFC.dbf"
#> [15] "NHDPointEventFC.shp" "NHDPoint.shp"
#> [17] "NHDReachCode_Comid.dbf" "NHDReachCrossReference.dbf"
#> [19] "NHDWaterbody.dbf" "NHDWaterbody.shp"
nhd_plus_list(vpu = 4, "NHDPlusAttributes")
#> [1] "CumulativeArea.dbf" "DivFracMP.dbf"
#> [3] "elevslope.dbf" "HeadwaterNodeArea.dbf"
#> [5] "MegaDiv.dbf" "PlusARPointEvent.dbf"
#> [7] "PlusFlowAR.dbf" "PlusFlow.dbf"
#> [9] "PlusFlowlineLakeMorphology.dbf" "PlusFlowlineVAA.dbf"
#> [11] "PlusWaterbodyLakeMorphology.dbf"
nhd_plus_list(vpu = 4, "NHDPlusCatchment")
#> [1] "Catchment.dbf" "Catchment.shp" "featureidgridcode.dbf"
```
``` r
# get layer info
nhd_plus_info(vpu = 4, "NHDSnapshot", "NHDWaterbody")
```
#> [1] "Driver: ESRI Shapefile; number of rows: 31830 "
#> [2] "Feature type: wkbPolygon with 3 dimensions"
#> [3] "Extent: (-93.24332 40.43575) - (-73.61814 48.11344)"
#> [4] "CRS: +proj=longlat +datum=NAD83 +no_defs "
#> [5] "LDID: 87 "
#> [6] "Number of fields: 12 "
#> [7] " name type length typeName"
#> [8] "1 COMID 0 9 Integer"
#> [9] "2 FDATE 9 10 Date"
#> [10] "3 RESOLUTION 4 7 String"
#> [11] "4 GNIS_ID 4 10 String"
#> [12] "5 GNIS_NAME 4 65 String"
#> [13] "6 AREASQKM 2 19 Real"
#> [14] "7 ELEVATION 2 19 Real"
#> [15] "8 REACHCODE 4 14 String"
#> [16] "9 FTYPE 4 24 String"
#> [17] "10 FCODE 0 9 Integer"
#> [18] "11 SHAPE_LENG 2 19 Real"
#> [19] "12 SHAPE_AREA 2 19 Real"
```{r eval=FALSE, echo=FALSE}
info <- capture.output(nhd_plus_info(vpu = 4, "NHDSnapshot", "NHDWaterbody"))
# gsub("/home/jemma", "~", info)
info[2:length(info)]
```
``` r
# load layer
dt <- nhd_plus_load(vpu = 4, "NHDSnapshot", "NHDWaterbody")
#> Reading layer `NHDWaterbody' from data source
#> `/home/jemma/.local/share/nhdR/NHDPlus/GL_04_NHDSnapshot/NHDWaterbody.shp'
#> using driver `ESRI Shapefile'
#> Simple feature collection with 31830 features and 12 fields
#> Geometry type: POLYGON
#> Dimension: XYZ
#> Bounding box: xmin: -93.24332 ymin: 40.43575 xmax: -73.61814 ymax: 48.11344
#> z_range: zmin: 0 zmax: 0
#> Geodetic CRS: NAD83
```
### NHD
NHD exports are organized by US state.
```{r eval=FALSE}
nhd_get(state = c("DC", "HI"))
```
``` r
nhd_list(state = "DC")
#> [1] "ExternalCrosswalk" "NHDFCode"
#> [3] "NHDFeatureToMetadata" "NHDFlow"
#> [5] "NHDFlowlineVAA" "NHDMetadata"
#> [7] "NHDProcessingParameters" "NHDReachCodeMaintenance"
#> [9] "NHDReachCrossReference" "NHDSourceCitation"
#> [11] "NHDStatus" "NHDVerticalRelationship"
#> [13] "NHDPoint" "NHDFlowline"
#> [15] "NHDLine" "NHDArea"
#> [17] "NHDWaterbody" "NHDAreaEventFC"
#> [19] "NHDLineEventFC" "NHDPointEventFC"
#> [21] "WBDLine" "NonContributingDrainageArea"
#> [23] "NWISBoundary" "NWISDrainageArea"
#> [25] "WBDHU14" "WBDHU8"
#> [27] "WBDHU2" "WBDHU4"
#> [29] "WBDHU6" "WBDHU10"
#> [31] "WBDHU12" "WBDHU16"
#> [33] "HYDRO_NET_Junctions"
#> attr(,"driver")
#> [1] "OpenFileGDB"
#> attr(,"nlayers")
#> [1] 33
```
``` r
nhd_info(state = "DC", dsn = "NHDWaterbody")
#> Source: "/home/jemma/.local/share/nhdR/NHD_H_District_of_Columbia_State_GDB.gdb", layer: "NHDWaterbody"
#> Driver: OpenFileGDB; number of rows: 8011
#> Feature type: wkbPolygon with 3 dimensions
#> Extent: (-78.07095 38.52142) - (-76.82219 39.64683)
#> CRS: +proj=longlat +datum=NAD83 +no_defs
#> Number of fields: 13
#> name type length typeName
#> 1 Permanent_Identifier 4 40 String
#> 2 FDate 11 0 DateTime
#> 3 Resolution 0 0 Integer
#> 4 GNIS_ID 4 10 String
#> 5 GNIS_Name 4 65 String
#> 6 AreaSqKm 2 0 Real
#> 7 Elevation 2 0 Real
#> 8 ReachCode 4 14 String
#> 9 FType 0 0 Integer
#> 10 FCode 0 0 Integer
#> 11 VisibilityFilter 0 0 Integer
#> 12 Shape_Length 2 0 Real
#> 13 Shape_Area 2 0 Real
```
``` r
head(nhd_load(state = "DC", dsn = "NHDWaterbody"))
#> Reading layer `NHDWaterbody' from data source
#> `/home/jemma/.local/share/nhdR/NHD_H_District_of_Columbia_State_GDB.gdb'
#> using driver `OpenFileGDB'
#> Simple feature collection with 8011 features and 13 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XYZ
#> Bounding box: xmin: -78.07095 ymin: 38.52142 xmax: -76.82219 ymax: 39.64683
#> z_range: zmin: 0 zmax: 0
#> Geodetic CRS: NAD83
#> Reading query `SELECT * from NHDWaterbody LIMIT 1' from data source `/home/jemma/.local/share/nhdR/NHD_H_District_of_Columbia_State_GDB.gdb'
#> using driver `OpenFileGDB'
#> Simple feature collection with 1 feature and 13 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XYZ
#> Bounding box: xmin: -76.99652 ymin: 38.68957 xmax: -76.99631 ymax: 38.6897
#> z_range: zmin: 0 zmax: 0
#> Geodetic CRS: NAD83
#> Simple feature collection with 6 features and 13 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: -77.5767 ymin: 38.68957 xmax: -76.99631 ymax: 39.5882
#> Geodetic CRS: WGS 84
#> Permanent_Identifier FDate Resolution GNIS_ID GNIS_Name
#> 1 46565431 2002-07-21 18:00:00 2
#> 2 51767181 2002-08-14 18:00:00 2
#> 3 51767223 2002-08-14 18:00:00 2
#> 4 51767287 2002-08-14 18:00:00 2
#> 5 51767709 2002-08-14 18:00:00 2
#> 6 51768273 2002-08-14 18:00:00 2
#> AreaSqKm Elevation ReachCode FType FCode VisibilityFilter Shape_Length
#> 1 0.000 NA 02070010004605 436 43624 0 0.0005402029
#> 2 0.002 NA 02070008004808 390 39004 50000 0.0017289109
#> 3 0.001 NA 02070008004829 390 39004 2000000 0.0013369633
#> 4 0.001 NA 02070008004860 390 39004 24000 0.0011083831
#> 5 0.002 NA 02070008005063 390 39004 50000 0.0016429957
#> 6 0.001 NA 02070008005335 390 39004 24000 0.0012442057
#> Shape_Area Shape
#> 1 1.879174e-08 POLYGON ((-76.99631 38.6896...
#> 2 1.954519e-07 POLYGON ((-77.56946 39.5881...
#> 3 1.239613e-07 POLYGON ((-77.56954 39.5567...
#> 4 8.130533e-08 POLYGON ((-77.57658 39.5250...
#> 5 1.745505e-07 POLYGON ((-77.46919 39.3298...
#> 6 8.126193e-08 POLYGON ((-77.2087 39.18799...
```
Owner metadata
- Name: Jemma Stachelek
- Login: jsta
- Email:
- Kind: user
- Description:
- Website: http://jsta.rbind.io
- Location: New Mexico, USA
- Twitter:
- Company: Los Alamos National Laboratory
- Icon url: https://avatars.githubusercontent.com/u/7844578?u=2c08be6712c9b29c88700915a70ff0a33982b067&v=4
- Repositories: 366
- Last ynced at: 2024-06-11T15:37:46.189Z
- Profile URL: https://github.com/jsta
GitHub Events
Total
- Issues event: 1
- Watch event: 2
- Issue comment event: 1
Last Year
- Issues event: 1
- Watch event: 2
- Issue comment event: 1
Committers metadata
Last synced: about 15 hours ago
Total Commits: 449
Total Committers: 5
Avg Commits per committer: 89.8
Development Distribution Score (DDS): 0.223
Commits in past year: 0
Committers in past year: 0
Avg Commits per committer in past year: 0.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| jsta | s****2@m****u | 349 |
| jsta | j****k@u****u | 91 |
| Mike Vlah | v****3@g****m | 4 |
| Fisheries FWL Wildlife | F****e | 4 |
| github-actions[bot] | 4****] | 1 |
Committer domains:
- utexas.edu: 1
- msu.edu: 1
Issue and Pull Request metadata
Last synced: about 2 months ago
Total issues: 90
Total pull requests: 4
Average time to close issues: 2 months
Average time to close pull requests: 17 days
Total issue authors: 14
Total pull request authors: 2
Average comments per issue: 0.71
Average comments per pull request: 0.75
Merged pull request: 3
Bot issues: 0
Bot pull requests: 0
Past year issues: 1
Past year pull requests: 0
Past year average time to close issues: N/A
Past year average time to close pull requests: N/A
Past year issue authors: 1
Past year pull request authors: 0
Past year average comments per issue: 0.0
Past year average comments per pull request: 0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- jsta (75)
- edzer (2)
- vlahm (2)
- deankoch (1)
- rsbivand (1)
- LZarri (1)
- jpshanno (1)
- alherca73 (1)
- hrecht (1)
- matthewross07 (1)
- thomasp85 (1)
- rstessier (1)
- aaronRcarr (1)
- glorious1 (1)
Top Pull Request Authors
- vlahm (2)
- jsta (2)
Top Issue Labels
Top Pull Request Labels
Package metadata
- Total packages: 1
-
Total downloads:
- cran: 186 last-month
- Total dependent packages: 0
- Total dependent repositories: 1
- Total versions: 12
- Total maintainers: 1
cran.r-project.org: nhdR
Tools for Working with the National Hydrography Dataset
- Homepage: https://github.com/jsta/nhdR
- Documentation: http://cran.r-project.org/web/packages/nhdR/nhdR.pdf
- Licenses: GPL-2 | GPL-3 [expanded from: GPL]
- Latest release: 0.6.1 (published over 2 years ago)
- Last Synced: 2025-12-23T19:02:18.666Z (2 days ago)
- Versions: 12
- Dependent Packages: 0
- Dependent Repositories: 1
- Downloads: 186 Last month
-
Rankings:
- Forks count: 6.345%
- Stargazers count: 8.505%
- Average: 18.582%
- Dependent repos count: 24.291%
- Downloads: 25.92%
- Dependent packages count: 27.846%
- Maintainers (1)
Dependencies
- R >= 3.5.0 depends
- maps * depends
- curl * imports
- dplyr * imports
- foreign * imports
- ggplot2 * imports
- httr * imports
- memoise * imports
- purrr * imports
- rappdirs * imports
- rgdal * imports
- rlang * imports
- rvest * imports
- sf * imports
- stringr * imports
- units * imports
- xml2 * imports
- covr * suggests
- crul * suggests
- knitr * suggests
- lwgeom * suggests
- rgeos * suggests
- rmarkdown * suggests
- s2 * suggests
- sp * suggests
- testthat >= 2.1.0 suggests
- wikilake * suggests
- actions/cache v2 composite
- actions/checkout v2 composite
- actions/upload-artifact main composite
- r-lib/actions/setup-pandoc v1 composite
- r-lib/actions/setup-r v1 composite
Score: 10.85321306049652