Skip to contents

Trim an osmdata object to within a bounding polygon

Usage

trim_osmdata(dat, bb_poly, exclude = TRUE)

Arguments

dat

An osmdata object returned from osmdata_sf or osmdata_sp.

bb_poly

A matrix representing a bounding polygon obtained with getbb (..., format_out = "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.

Value

A trimmed version of dat, reduced only to those components lying within the bounding polygon.

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.

See also

Examples

# Bounding box of "Colchester UK":
bb <- c (0.6993788, 51.7657055, 1.026803, 51.977153)
query <- opq (bb)
query <- add_osm_feature (query, key = "highway")
# Equivalent to:
if (FALSE) { # \dontrun{
query <- opq ("colchester uk") |>
    add_osm_feature (key = "highway")
} # }
# Then extract data from 'Overpass' API
if (FALSE) { # \dontrun{
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:
if (FALSE) { # \dontrun{
bb <- getbb ("colchester uk", format_out = "polygon")
library (sf) # required for this function to work
dat_tr <- trim_osmdata (dat, bb)
bb <- getbb ("colchester uk", format_out = "sf_polygon")
class (bb) # sf data.frame
dat_tr <- trim_osmdata (dat, bb)
bb <- as (bb, "Spatial")
class (bb) # SpatialPolygonsDataFrame
dat_tr <- trim_osmdata (dat, bb)
} # }