Skip to content

Instantly share code, notes, and snippets.

@michaeldorman
Created May 14, 2020 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeldorman/ed0006b58bc47b3699eaec4faeb1b70f to your computer and use it in GitHub Desktop.
Save michaeldorman/ed0006b58bc47b3699eaec4faeb1b70f to your computer and use it in GitHub Desktop.
Plot city streets
library(sf)
library(osmdata)
location = "Tel-Aviv, Israel"
# Get data
q = opq(bbox = location)
dat = add_osm_feature(q, key = "highway")
dat = osmdata_sf(dat)
lines = dat$osm_lines
pol = dat$osm_polygons
pol = st_cast(pol, "MULTILINESTRING")
pol = st_cast(pol, "LINESTRING")
lines = rbind(lines, pol)
# Crop
bb = q$bbox
bb = strsplit(bb, ",")[[1]]
bb = as.numeric(bb)
bb = bb[c(2, 4, 1, 3)]
names(bb) = c("xmin", "xmax", "ymin", "ymax")
bb = st_bbox(bb, crs = 4326)
bb = st_as_sfc(bb)
lines = st_intersection(lines, bb)
lines = st_transform(lines, 32636)
# Plot
plot(lines[, "highway"], key.pos = NULL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment