MyNixOS website logo
Description

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 BuildStatus BuildStatus AppVeyor buildstatus Coveragestatus lifecycle CRAN_Status_Badge CRAN_Download_Badge R buildstatus

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
  • latmask() - mask a raster based on a latitude
  • latitudecircle() - create a spatial region based on a latitude

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)
#> Loading required package: sp
data(lux)
(exlux <- spex(lux))
#> class       : SpatialPolygonsDataFrame 
#> features    : 1 
#> extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#> Warning in proj4string(x): CRS object has comment, which is lost in output
#> crs         : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs 
#> variables   : 1
#> names       : SpatialPolygonsDataFrame_extent 
#> value       :                               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)
#> Warning in proj4string(x): CRS object has comment, which is lost in output
#> crs         : +proj=laea +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs 
#> variables   : 1
#> names       : Extent_extent 
#> value       :             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.386   0.004   0.390

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)
#> crs         : NA 
#> variables   : 1
#> names       : Extent_extent 
#> value       :             1

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.

Metadata

Version

0.7.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows