invert_poly: Invert a (spatial) polygon

View source: R/spatial_tools.R

invert_polyR Documentation

Invert a (spatial) polygon

Description

This function inverts a (spatial) polygon so that the ‘inside’ of the original polygon becomes the ‘outside’ and vice-versa. The function was motivated by marine applications in which polygons that define the coastline ‘contain’ land and need to be inverted to define the sea.

Usage

invert_poly(x, boundaries = raster::extent(x), ...)

Arguments

x

An SpatialPolygons-class or SpatialPolygonsDataFrame-class object.

boundaries

A extent object that defines the boundaries of the area under consideration. By default, this is defined by the extent of x.

...

Additional arguments passed to gDifference.

Value

The function returns a SpatialPolygons-class object.

Author(s)

Edward Lavender

Examples

#### Example (1): Compare original and inverted polygon
# In this example, we have a polygon that defines the coastline
# ... with the polygon enclosing the land. We can invert
# ... the polygon to return a polygon that defines the sea.
pp <- graphics::par(mfrow = c(1, 2))
raster::plot(dat_coast, col = "darkgreen")
dat_sea <- invert_poly(dat_coast)
raster::plot(dat_sea, col = "skyblue")
graphics::par(pp)
# The CRS of the two objects is identical
raster::crs(dat_coast)
raster::crs(dat_sea)
# Compare the classes of the two objects
class(dat_coast)
class(dat_sea)

edwardlavender/flapper documentation built on Jan. 22, 2025, 2:44 p.m.