Swiss army knife for bounding boxes. Modify an existing bounding box or create a new bounding box from scratch. See details.
Usage
bb(
x = NA,
ext = NULL,
cx = NULL,
cy = NULL,
width = NULL,
height = NULL,
xlim = NULL,
ylim = NULL,
relative = FALSE,
asp.target = NULL,
asp.limit = NULL,
current.projection = NULL,
projection = NULL,
output = c("bbox", "matrix", "extent")
)Arguments
- x
One of the following:
A bounding box (
st_bbox,Extent(rasterpackage, which will no longer be supported in the future versions), numeric vector of 4 (default order: xmin, ymin, xmax, ymax), or a 2x2 matrix).Open Street Map search query. The bounding is automatically generated by querying
xfrom Open Street Map Nominatim. Seegeocode_OSM
.
- ext
Extension factor of the bounding box. If 1, the bounding box is unchanged. Values smaller than 1 reduces the bounding box, and values larger than 1 enlarges the bounding box. This argument is a shortcut for both
widthandheightwithrelative=TRUE. If a negative value is specified, then the shortest side of the bounding box (so width or height) is extended withext, and the longest side is extended with the same absolute value. This is especially useful for bounding boxes with very low or high aspect ratios.- cx
center x coordinate
- cy
center y coordinate
- width
width of the bounding box. These are either absolute or relative (depending on the argument
relative).- height
height of the bounding box. These are either absolute or relative (depending on the argument
relative).- xlim
limits of the x-axis. These are either absolute or relative (depending on the argument
relative).- ylim
limits of the y-axis. See
xlim.- relative
boolean that determines whether relative values are used for
width,height,xlimandylimor absolute. Ifxis unspecified,relativeis set to"FALSE".- asp.target
target aspect ratio, which is width/height, of the returned bounding box.
- asp.limit
maximum aspect ratio, which is width/height. Number greater than or equal to 1. For landscape bounding boxes,
1/asp.limitwill be used. The returned bounding box will have an aspect ratio between1/asp.limitandasp.limit.- current.projection
projection that corresponds to the bounding box specified by
x.- projection
projection to transform the bounding box to.
- output
output format of the bounding box, one of:
"bbox"asf::bboxobject, which is a numeric vector of 4: xmin, ymin, xmax, ymax. This representation used by thesfpackage."matrix"a 2 by 2 numeric matrix, where the rows correspond to x and y, and the columns to min and max. This representation used by thesppackage."extent"anraster::extentobject, which is a numeric vector of 4: xmin, xmax, ymin, ymax. This representation used by therasterpackage.
Details
An existing bounding box (defined by x) can be modified as follows:
Using the extension factor
ext.Changing the width and height with
widthandheight. The argumentrelavitvedetermines whether relative or absolute values are used.Setting the x and y limits. The argument
relavitvedetermines whether relative or absolute values are used.
A new bounding box can be created from scratch as follows:
Using the extension factor
ext.Setting the center coorinates
cxandcy, together with thewidthandheight.Setting the x and y limits
xlimandylim
Examples
if (require(tmap)) {
## load shapes
data(NLD_muni)
data(World)
## get bounding box (similar to sp's function bbox)
bb(NLD_muni)
## extent it by factor 1.10
bb(NLD_muni, ext=1.10)
## double the width
bb(NLD_muni, width=2, relative = TRUE)
## crop both dimensions from 0.25 to 0.75
bb(NLD_muni, xlim=c(.25, .75), ylim=c(.25, .75), relative = TRUE)
## extent it such that aspect ratio is 1
bb(NLD_muni, asp.target = 1)
## convert to longlat (EPSG 4326)
bb(NLD_muni, projection=4326)
}
#> xmin ymin xmax ymax
#> 3.253723 50.734191 7.243154 53.559126
if (FALSE) { # \dontrun{
if (require(tmap)) {
bb("Limburg", projection = 28992)
bb_italy <- bb("Italy", projection = "+proj=eck4")
tm_shape(World, bbox=bb_italy) + tm_polygons()
# shorter alternative: tm_shape(World, bbox="Italy") + tm_polygons()
}} # }