diff --git a/R/00-palette.R b/R/00-palette.R index 24682a3..e3f5511 100644 --- a/R/00-palette.R +++ b/R/00-palette.R @@ -9,6 +9,7 @@ coastcol <- '#b59f78' # Land islandcol <- '#d0c2a9' +landcol <- '#d9ceba' # Anthro roadcol <- '#666666' @@ -16,5 +17,19 @@ roadcol <- '#666666' parkcol <- '#b4bc9c' parkboundcol <- '#90967c' +# Forest +forestcol <- '#A4BC9C' + # Map etc gridcol <- '#323232' + + +# Road hiearchy +levels <- c('motorway', 'trunk', 'primary', 'secondary', 'tertiary', + 'unclassified', 'residential', 'motorway_link', 'trunk_link', + 'primary_link', 'secondary_link', 'tertiary_link', + 'living_street', 'service', 'pedestrian', 'track', + 'bus_guideway', 'escape', 'raceway', 'road', 'footway', + 'bridleway', 'steps', 'corridor', 'path', 'sidewalk', + 'cycleway') +roadlevels <- factor(levels, levels = levels) diff --git a/R/10-riding-mountain-prep.R b/R/10-riding-mountain-prep.R new file mode 100644 index 0000000..f46f1ca --- /dev/null +++ b/R/10-riding-mountain-prep.R @@ -0,0 +1,73 @@ +# === Riding Mountain - Prep ---------------------------------------------- +# Alec L. Robitaille + + + +# Packages ---------------------------------------------------------------- +libs <- c('sf', 'osmdata', 'raster') +lapply(libs, require, character.only = TRUE) + + + +# Download OSM data ------------------------------------------------------- +# Bounding box (min xy, max xy) +bb <- c(-101.26, 50.16, -99.14, 51.50) + +# Download RMNP bounds +boundscall <- opq(bb) %>% + add_osm_feature(key = 'name', value = 'Riding Mountain National Park') %>% + osmdata_sf() + +bounds <- boundscall$osm_polygons + +# Download water +watercall <- opq(bb) %>% + add_osm_feature(key = 'natural', value = 'water') %>% + osmdata_sf() + +# Download forest +forestcall <- opq(bb) %>% + add_osm_feature(key = 'natural', value = c('forest', 'wood')) %>% + osmdata_sf() + +# Trails and roads +roadscall <- opq(bb) %>% + add_osm_feature(key = 'highway') %>% # , value = c('forest', 'wood')) %>% + osmdata_sf() + + +# Prep geometries --------------------------------------------------------- +utm <- st_crs(32614) + +## Combine water polygons +# Transform to UTM +wpolys <- st_transform(watercall$osm_polygons, utm) +wmpolys <- st_transform(watercall$osm_multipolygons, utm) + +# Calculate area +wpolys$area <- st_area(wpolys) +wmpolys$area <- st_area(wmpolys) + +thresharea <- quantile(wpolys$area, .70) + +w <- st_as_sf(st_combine(wpolys[wpolys$area > thresharea,])) +wm <- st_as_sf(st_combine(wmpolys[wmpolys$area > thresharea,])) + +water <- st_union(st_make_valid(w), wm) + +## Combine forest polygons +fmpolys <- st_transform(forestcall$osm_multipolygons, utm) + +forest <- st_as_sf(st_combine(st_simplify(fmpolys))) + +# Reproject +boundutm <- st_transform(bounds, utm) +roadutm <- st_transform(roadscall$osm_lines, utm) + + + +# Output ------------------------------------------------------------------ +st_write(boundutm, 'output/rmnp-bounds.gpkg') +st_write(roadutm, 'output/rmnp-roads.gpkg') +st_write(forest, 'output/rmnp-forest.gpkg') +st_write(water, 'output/rmnp-water.gpkg') diff --git a/R/11-riding-mountain-figure.R b/R/11-riding-mountain-figure.R new file mode 100644 index 0000000..4135ca3 --- /dev/null +++ b/R/11-riding-mountain-figure.R @@ -0,0 +1,77 @@ +# === Riding Mountain National Park - Figure ------------------------------ +# Alec L. Robitaille + + +# Packages ---------------------------------------------------------------- +libs <- c( + 'data.table', + 'ggplot2', + 'sf' +) +lapply(libs, require, character.only = TRUE) + + + +# Data -------------------------------------------------------------------- +rmnp <- st_read('output/rmnp-bounds.gpkg') +roads <- st_read('output/rmnp-roads.gpkg') +water <- st_read('output/rmnp-water.gpkg') +forest <- st_read('output/rmnp-forest.gpkg') + + +# CRS +utm <- st_crs(32614) + + +# Theme ------------------------------------------------------------------- +## Colors +source('R/00-palette.R') + +# Road colors +roads$highway <- factor(roads$highway, levels = levels(roadlevels)) +roadcols <- data.table(highway = c('trunk', 'trunk_link', 'primary', 'primary_link', + 'secondary', 'secondary_link', 'tertiary', + 'tertiary_link', + 'service', 'residential', 'construction' , + 'unclassified', 'cycleway', 'footway', 'bridleway', + 'path', 'track', 'steps' +)) +roadcols[, cols := gray.colors(.N, start = 0.1, end = 0.6)] +roadpal <- roadcols[, setNames(cols, highway)] + +## Theme +themeMap <- theme(panel.border = element_rect(size = 1, fill = NA), + panel.background = element_rect(fill = landcol), + panel.grid = element_line(color = gridcol, size = 0.3), + axis.text = element_text(size = 11, color = 'black'), + axis.title = element_blank()) + + +# Plot -------------------------------------------------------------------- +roads$geometry <- st_geometry(roads) + +# Base rmnp +bb <- st_bbox(st_buffer(st_centroid(rmnp), 6e4)) + +grmnp <- ggplot() + + geom_sf(fill = landcol, data = rmnp) + + geom_sf(fill = forestcol, color = forestcol, size = 0.1, data = forest) + + geom_sf(fill = watercol, color = streamcol, size = 0.1, data = water) + + geom_sf(aes(color = highway), alpha = 0.8, data = roads, size = 0.2) + + geom_sf(fill = NA, size = 0.5, color = 'black', data = rmnp) + + scale_color_manual(values = roadpal) + + guides(color = FALSE, fill = FALSE) + + coord_sf(xlim = c(bb['xmin'] - 1e4, bb['xmax']), + ylim = c(bb['ymin'] + 4.5e4, bb['ymax']) - 3e4) + + themeMap + + + +# Output ------------------------------------------------------------------ +ggsave( + 'graphics/11-riding-mountain.png', + grmnp, + width = 10, + height = 10, + dpi = 320 +) diff --git a/README.Rmd b/README.Rmd index 8cd788a..0014c57 100644 --- a/README.Rmd +++ b/README.Rmd @@ -92,3 +92,15 @@ Reproducing: run `03-newfoundland-prep.R` and `06-terra-nova-prep.R` to generat knitr::include_graphics('graphics/09-bloomfield.png') ``` + +7. Riding Mountain National Park + +This uses data from Open Street Map and six packages: `osmdata`, `sf`, `raster`, `data.table` and `ggplot`. + + +Reproducing: run `10-riding-mountain-prep.R` to generate the RMNP data and `11-riding-mountain-figure.R` to combine and generate the figure. + +```{r} +knitr::include_graphics('graphics/11-riding-mountain.png') +``` + diff --git a/graphics/11-riding-mountain.png b/graphics/11-riding-mountain.png new file mode 100644 index 0000000..d80ee20 Binary files /dev/null and b/graphics/11-riding-mountain.png differ diff --git a/output/rmnp-bounds.gpkg b/output/rmnp-bounds.gpkg new file mode 100644 index 0000000..edec222 Binary files /dev/null and b/output/rmnp-bounds.gpkg differ diff --git a/output/rmnp-forest.gpkg b/output/rmnp-forest.gpkg new file mode 100644 index 0000000..22676f0 Binary files /dev/null and b/output/rmnp-forest.gpkg differ diff --git a/output/rmnp-roads.gpkg b/output/rmnp-roads.gpkg new file mode 100644 index 0000000..87a455d Binary files /dev/null and b/output/rmnp-roads.gpkg differ diff --git a/output/rmnp-water.gpkg b/output/rmnp-water.gpkg new file mode 100644 index 0000000..491bb26 Binary files /dev/null and b/output/rmnp-water.gpkg differ