Skip to content

Commit

Permalink
Merge branch 'rmnp' into 'master'
Browse files Browse the repository at this point in the history
Riding Mountain National Park

See merge request WEEL_grp/study-area-figures!6
  • Loading branch information
robitalec committed May 20, 2020
2 parents 511fcc4 + 566e83a commit 4127566
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 0 deletions.
15 changes: 15 additions & 0 deletions R/00-palette.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@ coastcol <- '#b59f78'

# Land
islandcol <- '#d0c2a9'
landcol <- '#d9ceba'

# Anthro
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)
73 changes: 73 additions & 0 deletions R/10-riding-mountain-prep.R
Original file line number Diff line number Diff line change
@@ -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')
77 changes: 77 additions & 0 deletions R/11-riding-mountain-figure.R
Original file line number Diff line number Diff line change
@@ -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
)
12 changes: 12 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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')
```

Binary file added graphics/11-riding-mountain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added output/rmnp-bounds.gpkg
Binary file not shown.
Binary file added output/rmnp-forest.gpkg
Binary file not shown.
Binary file added output/rmnp-roads.gpkg
Binary file not shown.
Binary file added output/rmnp-water.gpkg
Binary file not shown.

0 comments on commit 4127566

Please sign in to comment.