Skip to contents

Looks up or estimates depth for each lake in the fetch results and adds depth columns.

Usage

add_lake_depth(fetch_results, lakes, user_depths = NULL)

Arguments

fetch_results

sf object with fetch results

lakes

sf object with lake polygons

user_depths

Named vector of user-provided depths (names = lake IDs)

Value

fetch_results with added depth columns

Examples

# \donttest{
data(adirondack_sites)
sites <- load_sites(adirondack_sites)
#>   Loaded 12 rows with columns: Site, lake.name, latitude, longitude, datetime
#>   Using columns: Latitude = latitude, Longitude = longitude
#>   Detected datetime column: datetime
#>   Preserved lake name column: lake.name
#>   Final valid samples: 12
#>   Detected location from column 'lake.name': 4 lakes
lake <- get_lake_boundary(sites)
#> Converting to spatial format...
#> Spherical geometry (s2) switched off
#> Downloading lake boundaries from OpenStreetMap...
#>   Bounding box: [-74.7115, 43.7742, -74.3775, 44.2712]
#>     Trying name-filtered query for: Blue Mountain Lake, Raquette Lake, Long Lake, Tupper Lake
#>     Server error, trying another server in 5s...
#>     Server error, trying another server in 10s...
#>     Failed after 3 attempts: arguments imply differing number of rows: 1160, 0
#>     Found 23 polygons
#>     Found 1 multipolygons
#>     Found 17 polygons
#>     Found 1 multipolygons
#> Waiting 4s for retry backoff ■■■■■■■■■                       
#> Waiting 4s for retry backoff ■■■■■■■■■■■■■■■■■■■             
#> Waiting 4s for retry backoff ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  
#> Waiting 6s for retry backoff ■■■■■■                          
#> Waiting 6s for retry backoff ■■■■■■■■■                       
#> Waiting 6s for retry backoff ■■■■■■■■■■■■■■■■■■■■■■■■■■■     
#> Waiting 6s for retry backoff ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 
#>     Error: arguments imply differing number of rows: 3426, 0
#>     Server error, trying another server in 10s...
#>     Failed after 3 attempts: arguments imply differing number of rows: 3426, 0
#>     Name-filtered query found results, skipping broad query
#>   Combining results...
#>   Total water bodies found: 42
#>   Auto-detected UTM Zone: 18N
#>   Transforming to UTM for analysis...
#>   Processing lake polygons...
#>   Filtered 4 water bodies < 1e-04 km2
#>   Processed 38 unique lake polygons
#>   Total area: 38.99 km2
#> 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...
#>     1 sites matched directly
#>   Checking 11 unmatched sites with 100m tolerance...
#>     1 additional sites matched within tolerance
#>   Trying name-based matching using 'lake.name' column...
#>     WARNING: No OSM lake found matching 'Blue Mountain Lake' (3 sites)
#>     Matched 2 sites to 'Raquette Lake' via name matching from 'Raquette Lake'
#> Warning: Skipped 1 site(s) named 'Raquette Lake': too far from OSM lake 'Raquette Lake' (>500m). Site(s) may be on a different water body. Sites: Raquette_3_20240716
#>     Matched 1 sites to 'Long Lake' via name matching from 'Long Lake'
#>     WARNING: No OSM lake found matching 'Tupper Lake' (3 sites)
#>   Site assignment summary:
#>     Matched: 5/12
#>     Unmatched: 7
#>     Unmatched sites claim to be in: Blue Mountain Lake, Raquette Lake, Tupper Lake
#>     Unmatched coordinate range:
#>       Lat: 4852677.9675 to 4897537.8688
#>       Lon: 526526.3106 to 545349.3624
#>     TIP: Try lakefetch_options(gps_tolerance_m = 100) for larger buffer
#>     TIP: Check if the lake exists in OpenStreetMap at openstreetmap.org
#>   Sites per lake:
#>     ID: 1871997: 2 sites
#>     ID: 555231: 3 sites
#> Calculating fetch for multiple lakes...
#>   Buffering sites 10m inward
#>   Angle resolution: 5 degrees
#> Warning: Skipping 7 sites not matched to any lake: Blue_Mountain_1_20240715, Blue_Mountain_2_20240715, Blue_Mountain_3_20240715, Raquette_3_20240716, Tupper_1_20240718, Tupper_2_20240718, Tupper_3_20240718
#> Processing 2 lake(s)...
#> Using parallel processing with 2 cores for 2 lakes
#> Fetch calculation complete.
#> Adding lake context (NHD integration)...
#>   Fetching NHD waterbodies...
#>     Found 297 NHD waterbodies
#>   Processing lake: 555231
#> 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 1 outlet(s) and 8 inlet(s)
#> Warning: st_centroid assumes attributes are constant over geometries
#>   Processing lake: 1871997
#> 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 1 outlet(s) and 10 inlet(s)
#> Warning: st_centroid assumes attributes are constant over geometries
#> Lake context complete.

# Add depth estimates
results$results <- add_lake_depth(results$results, results$lakes)
#>   Lake depth estimated from area (22 km2): mean ~ 22.3m, max ~ 55.8m
#>   Lake depth estimated from area (16.28 km2): mean ~ 20.7m, max ~ 51.7m

# Or provide known depths using an actual lake_osm_id from results
lake_id <- results$lakes$osm_id[1]
depths <- setNames(15.5, lake_id)
results$results <- add_lake_depth(results$results, results$lakes, user_depths = depths)
#>   Lake depth estimated from area (16.28 km2): mean ~ 20.7m, max ~ 51.7m
# }