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.

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

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)
#> Warning: GEOS support is provided by the sf and terra packages among others
raster::plot(dat_sea, col = "skyblue")

graphics::par(pp)
# The CRS of the two objects is identical
raster::crs(dat_coast)
#> Coordinate Reference System:
#> Deprecated Proj.4 representation:
#>  +proj=utm +zone=29 +datum=WGS84 +units=m +no_defs 
#> WKT2 2019 representation:
#> PROJCRS["WGS 84 / UTM zone 29N",
#>     BASEGEOGCRS["WGS 84",
#>         ENSEMBLE["World Geodetic System 1984 ensemble",
#>             MEMBER["World Geodetic System 1984 (Transit)"],
#>             MEMBER["World Geodetic System 1984 (G730)"],
#>             MEMBER["World Geodetic System 1984 (G873)"],
#>             MEMBER["World Geodetic System 1984 (G1150)"],
#>             MEMBER["World Geodetic System 1984 (G1674)"],
#>             MEMBER["World Geodetic System 1984 (G1762)"],
#>             MEMBER["World Geodetic System 1984 (G2139)"],
#>             ELLIPSOID["WGS 84",6378137,298.257223563,
#>                 LENGTHUNIT["metre",1]],
#>             ENSEMBLEACCURACY[2.0]],
#>         PRIMEM["Greenwich",0,
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         ID["EPSG",4326]],
#>     CONVERSION["UTM zone 29N",
#>         METHOD["Transverse Mercator",
#>             ID["EPSG",9807]],
#>         PARAMETER["Latitude of natural origin",0,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8801]],
#>         PARAMETER["Longitude of natural origin",-9,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8802]],
#>         PARAMETER["Scale factor at natural origin",0.9996,
#>             SCALEUNIT["unity",1],
#>             ID["EPSG",8805]],
#>         PARAMETER["False easting",500000,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8806]],
#>         PARAMETER["False northing",0,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8807]]],
#>     CS[Cartesian,2],
#>         AXIS["(E)",east,
#>             ORDER[1],
#>             LENGTHUNIT["metre",1]],
#>         AXIS["(N)",north,
#>             ORDER[2],
#>             LENGTHUNIT["metre",1]],
#>     USAGE[
#>         SCOPE["Engineering survey, topographic mapping."],
#>         AREA["Between 12degreesW and 6degreesW, northern hemisphere between equator and 84degreesN, onshore and offshore. Algeria. Cote D'Ivoire (Ivory Coast). Faroe Islands. Guinea. Ireland. Jan Mayen. Mali. Mauritania. Morocco. Portugal. Sierra Leone. Spain. United Kingdom (UK). Western Sahara."],
#>         BBOX[0,-12,84,-6]],
#>     ID["EPSG",32629]] 
raster::crs(dat_sea)
#> Coordinate Reference System:
#> Deprecated Proj.4 representation:
#>  +proj=utm +zone=29 +datum=WGS84 +units=m +no_defs 
#> WKT2 2019 representation:
#> PROJCRS["WGS 84 / UTM zone 29N",
#>     BASEGEOGCRS["WGS 84",
#>         ENSEMBLE["World Geodetic System 1984 ensemble",
#>             MEMBER["World Geodetic System 1984 (Transit)"],
#>             MEMBER["World Geodetic System 1984 (G730)"],
#>             MEMBER["World Geodetic System 1984 (G873)"],
#>             MEMBER["World Geodetic System 1984 (G1150)"],
#>             MEMBER["World Geodetic System 1984 (G1674)"],
#>             MEMBER["World Geodetic System 1984 (G1762)"],
#>             MEMBER["World Geodetic System 1984 (G2139)"],
#>             ELLIPSOID["WGS 84",6378137,298.257223563,
#>                 LENGTHUNIT["metre",1]],
#>             ENSEMBLEACCURACY[2.0]],
#>         PRIMEM["Greenwich",0,
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         ID["EPSG",4326]],
#>     CONVERSION["UTM zone 29N",
#>         METHOD["Transverse Mercator",
#>             ID["EPSG",9807]],
#>         PARAMETER["Latitude of natural origin",0,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8801]],
#>         PARAMETER["Longitude of natural origin",-9,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8802]],
#>         PARAMETER["Scale factor at natural origin",0.9996,
#>             SCALEUNIT["unity",1],
#>             ID["EPSG",8805]],
#>         PARAMETER["False easting",500000,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8806]],
#>         PARAMETER["False northing",0,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8807]]],
#>     CS[Cartesian,2],
#>         AXIS["(E)",east,
#>             ORDER[1],
#>             LENGTHUNIT["metre",1]],
#>         AXIS["(N)",north,
#>             ORDER[2],
#>             LENGTHUNIT["metre",1]],
#>     USAGE[
#>         SCOPE["Engineering survey, topographic mapping."],
#>         AREA["Between 12degreesW and 6degreesW, northern hemisphere between equator and 84degreesN, onshore and offshore. Algeria. Cote D'Ivoire (Ivory Coast). Faroe Islands. Guinea. Ireland. Jan Mayen. Mali. Mauritania. Morocco. Portugal. Sierra Leone. Spain. United Kingdom (UK). Western Sahara."],
#>         BBOX[0,-12,84,-6]],
#>     ID["EPSG",32629]] 
# Compare the classes of the two objects
class(dat_coast)
#> [1] "SpatialPolygonsDataFrame"
#> attr(,"package")
#> [1] "sp"
class(dat_sea)
#> [1] "SpatialPolygons"
#> attr(,"package")
#> [1] "sp"