Trim an osmdata
object to within a bounding polygon
Arguments
- dat
An
osmdata
object returned fromosmdata_sf()
orosmdata_sc()
.- bb_poly
An
sf
orsfc
object, or matrix representing a bounding polygon. Can be obtained withgetbb (..., format_out = "polygon")
orgetbb (..., format_out = "sf_polygon")
(and possibly selected from resultant list where multiple polygons are returned).- exclude
If
TRUE
, objects are trimmed exclusively, only retaining those strictly within the bounding polygon; otherwise all objects which partly extend within the bounding polygon are retained.
Note
It will generally be necessary to pre-load the sf package for this function to work correctly.
Caution is advised when using polygons obtained from Nominatim via
getbb(..., format_out = "polygon"|"sf_polygon")
. These shapes can be
outdated and thus could cause the trimming operation to not give results
expected based on the current state of the OSM data.
To reduce the downloaded data from Overpass, you can do the trimming in
the server-side using getbb(..., format_out = "osm_type_id")
(see examples).
See also
Other transform:
osm_elevation()
,
osm_poly2line()
,
unique_osmdata()
,
unname_osmdata_sf()
Examples
if (FALSE) { # \dontrun{
bb <- getbb ("colchester uk")
query <- opq (bb) |>
add_osm_feature (key = "highway")
# Then extract data from 'Overpass' API
dat <- osmdata_sf (query, quiet = FALSE)
# Then get bounding *polygon* for Colchester, as opposed to rectangular
# bounding box, and use that to trim data within that polygon:
bb_pol <- getbb ("colchester uk", format_out = "polygon")
library (sf) # required for this function to work
dat_tr <- trim_osmdata (dat, bb_pol)
bb_sf <- getbb ("colchester uk", format_out = "sf_polygon")
class (bb_sf) # sf data.frame
dat_tr <- trim_osmdata (dat, bb_sf)
bb_sp <- as (bb_sf, "Spatial")
class (bb_sp) # SpatialPolygonsDataFrame
dat_tr <- trim_osmdata (dat, bb_sp)
# Server-side trimming equivalent
bb <- getbb ("colchester uk", format_out = "osm_type_id")
query <- opq (bb) |>
add_osm_feature (key = "highway")
dat <- osmdata_sf (query, quiet = FALSE)
} # }