Sf: st_bbox() could return a geometry

Created on 28 Nov 2017  路  5Comments  路  Source: r-spatial/sf

This is a suggestion derived from https://github.com/r-spatial/stars/issues/12.

Postgis uses ST_Envelope to return the geometry of a bounding box (it's also the expression in the OGC spec), but since we're not very fond of adding new functions, I'd like to express my preference for having st_bbox() return a geometry by default (polygon with five vertex). User could use st_as_text() when in need of a legible version.

While the current option to rather output a custom bbox class better expresses the max and min, there aren't as many methods to handle bbox objects than there are to handle geometries. For instance union of two bbox would require a transformation. I like the idea to keep the output in the most usable class. My two cents.

Most helpful comment

> methods(class = "bbox")
[1] is.na     print     st_as_sfc st_crs   
see '?methods' for accessing help and source code
> demo(nc, ask = FALSE, echo = FALSE)
> (bb = st_as_sfc(st_bbox(nc)))
Geometry set for 1 feature 
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID):    4267
proj4string:    +proj=longlat +datum=NAD27 +no_defs
POLYGON ((-84.3238525390625 33.8819923400879, -...

so you'd need

st_envelope = function(x) st_as_sfc(st_bbox(x))

and, yes, we could add this function, as we can add many more, and in PostGIS this is indeed the only thing you can do.

Regarding scaling: try Ops:

bb * 1.1 + 0.5

All 5 comments

My cents: Agree this would be useful for many users. I wrote a function (stplanr::geo_bb) that does this for Spatial and sf objects as I found myself wanting to clip and do other operations on an object using the bb of another. Borrowing from the very useful tmaptools::bb function it also allows you to scale it. Not sure if that is adding too much complexity but my view is that such things are useful enough to go into 'core' geo libraries like sf so v. happy at this suggestion (and up for helping if I can in any way).

> methods(class = "bbox")
[1] is.na     print     st_as_sfc st_crs   
see '?methods' for accessing help and source code
> demo(nc, ask = FALSE, echo = FALSE)
> (bb = st_as_sfc(st_bbox(nc)))
Geometry set for 1 feature 
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID):    4267
proj4string:    +proj=longlat +datum=NAD27 +no_defs
POLYGON ((-84.3238525390625 33.8819923400879, -...

so you'd need

st_envelope = function(x) st_as_sfc(st_bbox(x))

and, yes, we could add this function, as we can add many more, and in PostGIS this is indeed the only thing you can do.

Regarding scaling: try Ops:

bb * 1.1 + 0.5

I think that most r-spatial people do not come here from PostGIS, but for those who do, a list (wiki?) of PostGIS functions not in sf, and how to do them with sf would be useful. There's also the difference of how we do e.g. st_intersects (on two _lists_ of geometries), and how PostGIS does it (on two _geometries_).

Didn't know that (bb = st_as_sfc(st_bbox(nc))) works. Very useful. Working on a PR to help communicate this.

@etiennebr the problem of st_bbox returning a geometry: how would you obtain the bounding box of that?

Was this page helpful?
0 / 5 - 0 ratings