Skip to contents

This function uses a continent GeoJSON or shape file (.shp & .shx) file to clip range polygons to land boundaries.

Usage

clip_polygons_to_land(convex_hulls, continent_sf)

Arguments

convex_hulls

A nested list of sf polygon objects representing the convex hulls of species ranges. Typically generated using get_range_convex_hulls().

continent_sf

An sf object containing continent polygons (e.g., from Natural Earth GeoJSON or shapefiles). Coordinate reference systems will be matched automatically

Value

A nested list of sf polygon objects representing the clipped species ranges. Each top-level list element corresponds to a species; each sublist contains one or more clipped polygons for that species.

Details

The function sets the CRS of each input polygon to match that of continent_sf using sf::st_crs(), then uses rmapshaper::ms_clip() to perform the clipping operation. Invalid geometries are fixed with sf::st_make_valid(). If a species range does not intersect land (i.e., an entirely oceanic polygon), it is removed from the output. Errors during clipping are caught and silently ignored.

Examples

# Generate a test dataframe with species Abies cephalonica
test_data <- data.frame(species_name = c("Acer_campestre"))
# Retrieve the unclipped range convex hulls 
unclipped_hulls <- get_range_convex_hulls(test_data)
#> Processing species: Acer_campestre
# Get continent data from a GeoJSON file
continent_sf_example <- get_continent_sf()
#> Error in get_continent_sf(): Default continent GeoJSON file not found. Ensure the file is in the 'extdata' directory of the package.
# Clip the range polygons to continent bounds 
clipped_polygons <- clip_polygons_to_land(convex_hulls = unclipped_hulls, continent_sf = continent_sf_example)
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
#> Warning: restarting interrupted promise evaluation
print(clipped_polygons)
#> $Acer_campestre
#> named list()
#>