spex

Share:

Description

spex

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

Usage

1
2
3
4
5
6
7
spex(x, ...)

## Default S3 method:
spex(x, byid = FALSE, .id, ...)

## S3 method for class 'Extent'
spex(x, crs, ...)

Arguments

x

any object with a Extent

...

arguments for methods

byid

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

.id

optional name for output attribute name

crs

a projection string

Details

This function is to replace a common pattern in the 'sp'-'raster' family 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.

Value

'SpatialPolygonsDataFrame'

Spatial Extent

spex function to produce a fully fledged Spatial object 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.

Potentially Unwelcome Rant

Consider also that this is a common limitation, outside of R, where the "OGC standard" for serialized geometry in "well known text" (WKT) or "well known binary" (WKB) do not include projection metadata. Some GIS systems do provide serialized geometries that include this crucial metadata, and may we encourage this to become more common. Not only is "longitude / latitude" coordinates not always suitable, they are actually meaningless without knowing the _datum_ (the ellipsoid radii and orientation, often WGS84) and the units in use (usually 'degrees', but sometimes 'radians')

See Also

This pattern is displayed in the example code for cover.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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")
## Not run: 
 ## library(rgdal)
 ## p4 <- "+proj=laea +ellps=WGS84"
 ## plot(spTransform(lux, p4))
 ## warning, this is just 4 coordinates
 ## plot(spTransform(exlux, p4), add = TRUE)

## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.