Learn R Programming

pgirmess (version 2.0.2)

bbox2sf: Convert a bounding box into a sf object.

Description

Convert a bounding box into a sf object.

Usage

bbox2sf(n,s,w,e,bbox=NA,crs=4326)

Value

A simple feature geometry of class "sfc_POLYGON" with a coordinate reference system, see st_sfc.

Arguments

n

the top north latitude

s

the bottom south latitude

w

the most western longitude

e

the most eastern longitude

bbox

a bounding box 2 x 2 matrix as produced e.g. by bbox, with first row = w, e and second row = s, n, or a numeric vector with xmin, ymin, xmax, ymax in this order as produced by st_bbox

crs

the ID number of a coordinate reference system as defined in the EPSG system https://epsg.io/ (default 4326, WGS84)

Details

This function converts a set of coordinates limiting a bounding box into a an object of class "sfc_POLYGON" (see st_sfc). It can be used for instance to clip a subset of a larger spatial object (e.g. using st_intersection)

Examples

Run this code

n<-79
s<--54
w<--166
e<-178


myPoly1<-bbox2sf(n,s,e,w)
plot(myPoly1)

# bbox as a 2 x 2 matrix as produced by sp:bbox
mybbox<-matrix(c(w,e,s,n),nrow=2,byrow=TRUE)
myPoly2<-bbox2sf(bbox=mybbox)
plot(myPoly2,border="red")

# bbox as produced by sf:st_bbox
if (require(sf)) {
myPoly3<-bbox2sf(bbox=st_bbox(myPoly2))
plot(myPoly3,border="blue")
}

Run the code above in your browser using DataLab