Create Spatial Polygons with projection metadata from a 'Spatial Extent'.

spex(x, crs, byid = FALSE, .id, ..., clipboard = FALSE)

# S3 method for default
spex(x, crs = NULL, byid = FALSE, .id, ...,
  clipboard = FALSE)

# S3 method for sf
spex(x, crs, byid = FALSE, .id, ..., clipboard = FALSE)

Arguments

x

any object with a Extent

crs

a projection string

byid

return a separate object for every input sub-object (not yet implemented)

.id

optional name for output attribute name

...

arguments for methods

clipboard

WIP this special-case allows x to be the result of the leafem clipboard copy process

Value

'SpatialPolygonsDataFrame'

Details

Called with no arguments will return the extent of the current 'par("usr")' setting.

Called with a matrix, list, or data frame it will create an extent from a two columned thing.

Called with clipboard = TRUE and x will be treated as the JSON-ic output of the clipboard copy from leafem (WIP). If x is missing, it will be attempted to be read from the clipboard. Clipboard read cannot work on RStudio Server, so we allow the text value to be passed in. I.e. spex(clipboard = TRUE) will read from the clipboard, spex(tx, clipboard = TRUE) will read from tx with value like '{"_southWest":{"lat":-1.307259612275665,"lng":23.411865234375},"_north...}"'.

This function is to replace a common pattern in spatial packages which is

  • create an Extent, a bounding box in xmin,xmax,ymin,ymax but without projection metadata

  • coerce the Extent to SpatialPolygons

  • restore the 'CRS', the "coordinate reference system", i.e. projection metadata

  • elevate the object to be a SpatialPolygonsDataFrame.

In short, this pattern exists because there is no projection metadata stored with either sp's bbox or raster's Extent.

Warning

Please note that an extent converted to polygons consists of only four unique coordinates, and so this is not necessarily suited for projection transformations.

See also

This pattern is displayed in the example code for cover.

Examples

library(raster) data(lux) exlux <- spex(lux) plot(lux)
plot(exlux, add = TRUE)
## put an extent and a CRS together spex(extent(0, 1, 0, 1), crs = "+proj=laea +ellps=WGS84")
#> class : SpatialPolygonsDataFrame #> features : 1 #> extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax) #> crs : +proj=laea +ellps=WGS84 #> variables : 1 #> names : Extent_extent #> value : 1