1

I am new to the {tmap} package, and I am having difficulty creating a proper road map. Here is my code (not immediately runnable, because I have an exported OpenStreetMap .osm file on disk).

# Load OpenStreetMap (OSM) polygon data of the Holden area from disk
holden_bbox_shapes = sf::read_sf('./localdata/holden.osm', 'multipolygons')

# Load OSM line data from disk. These are almost all roads.
holden_lines = sf::read_sf('holden.osm', 'lines')

# Get just the administrative town of holden
holden_town_polygon = dplyr::filter(holden_bbox_shapes, name == 'Holden')

tm_shape(holden_town_polygon) +
tm_fill() +
tm_shape(holden_lines) +
tm_lines()

The image result is here, in which the roads go beyond the visible region of holden_town_polygon, but within what I would imagine is the boundary box of the holden_town_polygon object. I don't know why that happens nor how to configure it. I tried finagling with the bbox parameter in all tm_* functions, but nothing would change the result.

I would like to see only the part of the roads within the shaded polygon. What would be the proper way to do that?

J. Bortell
  • 33
  • 4

1 Answers1

0

You can get use st_intersection from the sf package.

It would look something like this:

library(sf)

inside_lines <- st_intersection(holden_lines, holden_town_polygon)

tm_shape(holden_town_polygon) +
tm_fill() +
tm_shape(inside_lines) +
tm_lines()
mrhellmann
  • 5,069
  • 11
  • 38