b_box: Get bbox from various spatial objects

View source: R/b_box.R

b_boxR Documentation

Get bbox from various spatial objects

Description

Get bbox from various spatial objects

Usage

b_box(x)

Arguments

x

various inputs, see examples

Value

bounding box numeric values in the order '[minX, minY, maxX, maxY]'

Examples

# sf classses
if (requireNamespace("sf", quietly = TRUE)) {

library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
class(nc)
b_box(nc)

(mix <- st_sfc(st_geometrycollection(list(st_point(1:2))),
  st_geometrycollection(list(st_linestring(matrix(1:4,2))))))
class(mix)
b_box(mix)

(x <- st_point(c(1,2,3,4)))
b_box(x)

}

# SpatialPolygons class
if (requireNamespace("sp", quietly = TRUE)) {

library('sp')
poly1 <- Polygons(list(Polygon(cbind(c(-100,-90,-85,-100),
   c(40,50,45,40)))), "1")
poly2 <- Polygons(list(Polygon(cbind(c(-90,-80,-75,-90),
   c(30,40,35,30)))), "2")
sp_poly <- SpatialPolygons(list(poly1, poly2), 1:2)
class(sp_poly)
b_box(sp_poly)

# SpatialGrid class
x <- GridTopology(c(0,0), c(1,1), c(5,5))
sp_grid <- SpatialGrid(x)
b_box(sp_grid)

}

# Character strings
if (requireNamespace("geojson") && requireNamespace("lawn") && requireNamespace("sf")) {

## GeoJSON
### featurecollection
geo_fc <- '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-114.3457031,39.436193],[-114.3457031,43.4529189],[-106.6113281,43.4529189],[-106.6113281,39.436193],[-114.3457031,39.436193]]]},"properties":{}}]}'
b_box(geo_fc)
### feature
geo_feature <- '{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-114.3457031,39.436193],[-114.3457031,43.4529189],[-106.6113281,43.4529189],[-106.6113281,39.436193],[-114.3457031,39.436193]]]}}'
b_box(geo_feature)
### polygon
geo_poly <- '{"type":"Polygon","coordinates":[[[-114.3457031,39.436193],[-114.3457031,43.4529189],[-106.6113281,43.4529189],[-106.6113281,39.436193],[-114.3457031,39.436193]]]}'
b_box(geo_poly)

## Well known text
wkt_poly <- "POLYGON ((100.001 0.001, 101.1235 0.0010, 101.001 1.001, 100.001 0.001))"
b_box(wkt_poly)

# many classes of varying types at once, passed in a list
b_box(list(nc, mix, sp_poly, sp_grid))
## with an invalid type
b_box(list(nc, mix, sp_poly, sp_grid, 5))
## remove it easily with Filter
Filter(Negate(is.null), b_box(list(nc, mix, sp_poly, sp_grid, 5)))

}

ropensci/bbox documentation built on May 15, 2022, 9:06 a.m.