Spatial Extent Tools

Functions to produce a fully fledged 'geo-spatial' object extent as a 'SpatialPolygonsDataFrame'. Also included are functions to generate polygons from raster data using 'quadmesh' techniques, a round number buffered extent, and general spatial-extent and 'raster-like' extent helpers missing from the originating packages. Some latitude-based tools for polar maps are included.


Travis-CI Build Status AppVeyor Build Status CRAN_Status_Badge Coverage Status CRAN RStudio mirror downloads

spex

Spex provides a small set of functions for working with spatial data. These are

  • spex() - a spatial extent with projection metadata
  • polygonize() - a fast quadmesh-based pixel-to-polygon translation
  • buffer_extent - a convenience function for tidy extents
  • xlim, ylim - convenience functions for the axes of an extent
  • extent - convenience functions for sf objects

Create a fully-fledged SpatialPolygonsDataFrame extent from any object understood by the 'raster' package function 'extent()'. If the input has projection metadata it will be carried through to the output. The intention is to support any object from packages sp, raster and sf. If you want this to work on other types, create an issue and get in touch to discuss!.

The polygonization approach is faster than rasterToPolygons, and multi-layer rasters are converted to multi-column spatial data frames. This only does the pixel-to-polygon case. It provides an sf POLYGON data frame, but there is a version qm_rasterToPolygons_sp that returns a Spatial version.

The "buffered extent" is used to create cleanly aligned extents, useful for generating exacting grid structures as raster or vector.

Installation

Install 'spex' from CRAN.

install.packages("spex")

Examples

Create a Spatial object as a single extent polygon from a raster.

library(spex)
library(raster)
data(lux)
(exlux <- spex(lux))
#> class       : SpatialPolygonsDataFrame 
#> features    : 1 
#> extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
#> variables   : 1
#> names       : lux 
#> min values  :   1 
#> max values  :   1
 
## 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)
#> coord. ref. : +proj=laea +ellps=WGS84 
#> variables   : 1
#> names       : p 
#> min values  : 1 
#> max values  : 1

Create a simple features POLYGON data frame from a raster.

library(spex)
library(raster)
r <- raster(volcano)
tm <- system.time(p <- qm_rasterToPolygons(r))
 
p1 <- polygonize(r)
 
identical(p, p1)
#> [1] TRUE
 
p3 <- qm_rasterToPolygons_sp(r)
class(p3)
#> [1] "SpatialPolygonsDataFrame"
#> attr(,"package")
#> [1] "sp"
 
nrow(p)
#> [1] 5307
 
class(p)
#> [1] "sf"         "data.frame"
 
class(p$geometry)
#> [1] "sfc_POLYGON" "sfc"
 
print(tm)
#>    user  system elapsed 
#>   0.225   0.040   0.265

Create a buffered extent with whole-number aligned edges.

library(spex)
 
(ex <- extent(lux))
#> class       : Extent 
#> xmin        : 5.74414 
#> xmax        : 6.528252 
#> ymin        : 49.44781 
#> ymax        : 50.18162
 
buffer_extent(ex, 10)
#> class       : Extent 
#> xmin        : 0 
#> xmax        : 10 
#> ymin        : 40 
#> ymax        : 60
 
buffer_extent(ex, 2)
#> class       : Extent 
#> xmin        : 4 
#> xmax        : 8 
#> ymin        : 48 
#> ymax        : 52

There are convenience functions for sf objects.

class(psf)
#> [1] "sf"         "data.frame"
extent(psf)
#> class       : Extent 
#> xmin        : 0 
#> xmax        : 1.23 
#> ymin        : 0 
#> ymax        : 1
spex(psf)
#> class       : SpatialPolygonsDataFrame 
#> features    : 1 
#> extent      : 0, 1.23, 0, 1  (xmin, xmax, ymin, ymax)
#> coord. ref. : NA 
#> variables   : 1
#> names       : p 
#> min values  : 1 
#> max values  : 1
spex(sf::st_set_crs(psf, 3031))
#> class       : SpatialPolygonsDataFrame 
#> features    : 1 
#> extent      : 0, 1.23, 0, 1  (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
#> variables   : 1
#> names       : p 
#> min values  : 1 
#> max values  : 1

Should we alias spex with sfex for the obvious corollary?

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

News

spex 0.5.0

  • New latitude functions latitudecircle to build a polar circle.

  • Now return all cells as polygons if raster has no values.

  • Improved handling of na.rm, also now TRUE by default. #13 Thanks to Adriano Fantini.

  • A modest speed-up (up to 2X) for polygonize by more careful use of R.

  • better column name for resulting Spatial data frame

spex 0.4.0

  • new functions xlim and ylim, supporting anything understandable by spex

  • new extent method for sf objects, otherwise missing in the core packages

  • new spex support for sf objects

  • spex() with no input argument will return the extents of the current par 'usr' setting (if only graphics devices had metadata registration for the space in use ...)

  • polygonize, qm_rasterToPolygons, and qm_rasterToPolygons_sp gain na.rm argument to optionally remove any cells that are NA (in the first layer)

spex 0.3.0

  • new function buffer_extent for whole-grain expansion

  • added S3 generic polygonize, alias of qm_rasterToPolygons

spex 0.2.0

  • Added function qm_rasterToPolygons.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("spex")

0.6.0 by Michael D. Sumner, 24 days ago


https://mdsumner.github.io/spex/


Report a bug at https://github.com/mdsumner/spex/issues


Browse source code at https://github.com/cran/spex


Authors: Michael D. Sumner [aut, cre]


Documentation:   PDF Manual  


GPL-3 license


Imports methods, quadmesh, raster, reproj, sp, stats

Suggests covr, testthat


Imported by tabularaster.


See at CRAN