Skip to contents

Perform spatial join to assign each site to its containing lake polygon.

Usage

assign_sites_to_lakes(sites_sf, water_polygons, tolerance_m = NULL)

Arguments

sites_sf

sf object with site points

water_polygons

sf object with lake polygons

tolerance_m

Buffer distance for matching sites near lake edges

Value

sf object with sites and added columns for lake_osm_id, lake_name, lake_area_km2

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_data <- 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

# Assign sites to their containing lakes
sites_assigned <- assign_sites_to_lakes(
  lake_data$sites,
  lake_data$all_lakes,
  tolerance_m = 50
)
#> Assigning sites to lakes...
#>   Checking direct intersections...
#>     2 sites matched directly
#>   Site assignment summary:
#>     Matched: 2/2
#>   Sites per lake:
#>     Approximate Boundary: 2 sites

# Check assignments
table(sites_assigned$lake_name)
#> 
#> Approximate Boundary 
#>                    2 
# }