Skip to contents

Add hydrological context to fetch results using the National Hydrography Dataset (NHD). Includes outlet/inlet locations, watershed area, connectivity classification, and stream order.

Usage

add_lake_context(fetch_results, lake_polygons, utm_epsg)

Arguments

fetch_results

sf object with fetch calculation results

lake_polygons

sf object with lake polygons

utm_epsg

EPSG code for UTM projection

Value

sf object with additional columns for NHD context

Details

Requires the nhdplusTools package. If not available, returns the input with NA columns added for consistent output format.

Added columns include:

  • nhd_permanent_id: NHD permanent identifier

  • nhd_gnis_name: GNIS name from NHD

  • nhd_areasqkm: Area in square kilometers from NHD

  • outlet_dist_m: Distance to outlet in meters

  • outlet_bearing: Compass direction to outlet

  • inlet_nearest_dist_m: Distance to nearest inlet

  • inlet_nearest_bearing: Compass direction to nearest inlet

  • inlet_count: Number of inlets

  • connectivity_class: Headwater/Drainage/Terminal/Isolated

  • outlet_stream_order: Strahler stream order at outlet

  • watershed_area_ha: Watershed area in hectares

  • lake_watershed_ratio: Lake area / watershed area

Examples

# \donttest{
csv_path <- system.file("extdata", "sample_sites.csv", package = "lakefetch")
sites <- load_sites(csv_path)
#> Loading data from: /github/home/R/x86_64-pc-linux-gnu-library/4.6/lakefetch/extdata/sample_sites.csv
#>   Loaded 2 rows with columns: Site, latitude, longitude, lake.name
#>   Using columns: Latitude = latitude, Longitude = longitude
#>   Preserved lake name column: lake.name
#>   Final valid samples: 2
#>   Detected location from column 'lake.name': Blue Mountain Lake
lake <- get_lake_boundary(sites)
#> Converting to spatial format...
#> Spherical geometry (s2) switched off
#> Downloading lake boundaries from OpenStreetMap...
#>   Bounding box: [-74.4561, 43.8545, -74.4304, 43.8751]
#>     Trying name-filtered query for: Blue Mountain Lake
#>     Error: arguments imply differing number of rows: 1160, 0
#>     Server error, trying another server in 10s...
#>     Failed after 3 attempts: arguments imply differing number of rows: 1160, 0
#>     Querying natural=water...
#>     Error: arguments imply differing number of rows: 1160, 0
#>     Server error, trying another server in 6s...
#>     Failed after 3 attempts: arguments imply differing number of rows: 1160, 0
#>     Querying water=lake...
#>     Error: arguments imply differing number of rows: 1160, 0
#>     Server error, trying another server in 6s...
#>     Failed after 3 attempts: arguments imply differing number of rows: 1160, 0
#> Warning: No water bodies found in OpenStreetMap - creating approximate boundary
#> Spherical geometry (s2) switched on
results <- fetch_calculate(sites, lake)
#> Effective fetch method: top3
#> Using default depth: 10 m
#> Assigning sites to lakes...
#>   Checking direct intersections...
#>     2 sites matched directly
#>   Site assignment summary:
#>     Matched: 2/2
#>   Sites per lake:
#>     Approximate Boundary: 2 sites
#> Calculating fetch for multiple lakes...
#>   Buffering sites 10m inward
#>   Angle resolution: 5 degrees
#> Processing 1 lake(s)...
#> Using sequential processing
#>   Processing 2 samples in lake: Approximate Boundary
#> Fetch calculation complete.
#> Adding lake context (NHD integration)...
#>   Fetching NHD waterbodies...
#>     Found 68 NHD waterbodies
#>   Processing lake: fallback
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#>     Found 7 outlet(s) and 4 inlet(s)
#> Warning: st_centroid assumes attributes are constant over geometries
#> Lake context complete.
results_with_context <- add_lake_context(results$results, results$lakes, lake$utm_epsg)
#> Adding lake context (NHD integration)...
#>   Fetching NHD waterbodies...
#>     Found 68 NHD waterbodies
#>   Processing lake: fallback
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
#>     Found 7 outlet(s) and 4 inlet(s)
#> Warning: st_centroid assumes attributes are constant over geometries
#> Lake context complete.
# }