forked from bcgov/bcmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
215 lines (151 loc) · 7.88 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
output:
github_document:
html_preview: true
---
<!-- README.md is generated from README.Rmd. Please edit that file and re-knit-->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "tools/readme/"
)
```
# bcmaps <img src="tools/readme/bcmaps-sticker.png" height="139" align="right"/>
### Version `r read.dcf("DESCRIPTION", "Version")`
[](https://github.com/BCDevExchange/assets/blob/master/README.md) [](https://opensource.org/licenses/Apache-2.0) [](https://travis-ci.org/bcgov/bcmaps)
[](https://cran.r-project.org/package=bcmaps) [](https://CRAN.R-project.org/package=bcmaps)
## Overview
An [R](http://r-project.org) package of spatial map layers for British Columbia.
## Features
Provides access to various spatial layers of British Columbia, such as
administrative boundaries, natural resource management boundaries, watercourses etc.
All layers are available in the
[BC Albers](http://spatialreference.org/ref/epsg/nad83-bc-albers/) projection, which is the B.C. Government standard as `sf` or `Spatial` objects.
Layers are stored in the [bcmapsdata](https://github.com/bcgov/bcmapsdata) package
and loaded by this package, following the strategy recommended by [Anderson and Eddelbuettel](https://journal.r-project.org/archive/2017/RJ-2017-026/index.html).
## Installation
You can install `bcmaps` from CRAN:
```{r, echo=TRUE, eval=FALSE}
install.packages("bcmaps")
```
To install the development version of the `bcmaps` package, you need to install the `remotes` package then the `bcmaps` package.
```{r, echo=TRUE, eval=FALSE}
install.packages("remotes")
remotes::install_github("bcgov/bcmaps")
```
## Usage
To get full usage of the package, you will also need to install the
[**bcmapsdata**](https://github.com/bcgov/bcmapsdata) package, which holds
all of the datasets.
_Note that unlike most packages it is not necessary to actually load the **bcmapsdata** package
(i.e., with `library(bcmapsdata)`) - in fact it is less likely to cause problems if you don't._
```{r, eval=FALSE}
install.packages('bcmapsdata', repos='https://bcgov.github.io/drat/')
```
To see the layers that are available, run the `available_layers()` function:
```{r, echo=FALSE}
library(bcmaps)
```
```{r, eval=FALSE}
library(bcmaps)
available_layers()
```
Most layers are accessible by a shortcut function by the same name as the object.
Then you can use the data as you would any `sf` or `Spatial` object. For example:
```{r}
library(sf)
bc <- bc_bound()
plot(st_geometry(bc))
```
Alternatively, you can use the `get_layer` function - simply type `get_layer('layer_name')`,
where `'layer_name'` is the name of the layer of interest. The `get_layer` function is
useful if the back-end `bcmapsdata` package has had a layer added to it, but there is
as yet no shortcut function created in `bcmaps`.
```{r, message=FALSE}
library(sf)
library(dplyr)
ws <- get_layer("wsc_drainages", class = "sf")
plot(ws["SUB_SUB_DRAINAGE_AREA_NAME"], key.pos = NULL)
```
### Simple Features objects
By default, all layers are returned as [`sf` spatial objects](https://cran.r-project.org/package=sf):
```{r plot-maps, fig.height=5, warning=FALSE}
library(bcmaps)
library(sf)
# Load and plot the boundaries of B.C.
bc <- bc_bound()
plot(st_geometry(bc))
## Next load the Regional Districts data, then extract and plot the Kootenays
rd <- regional_districts()
kootenays <- rd[rd$ADMIN_AREA_NAME == "Regional District of Central Kootenay", ]
plot(st_geometry(kootenays), col = "lightseagreen", add = TRUE)
```
### It's a beautiful day in the neighbourhood
A handy layer for creating maps for display is the `bc_neighbours` layer, accessible with the function by the same name. This example also illustrates using the popular [ggplot2](https://ggplot2.tidyverse.org/) package to plot maps in R using `geom_sf`:
```{r bc_neighbours}
library(ggplot2)
ggplot() +
geom_sf(data = bc_neighbours(), mapping = aes(fill = name)) +
geom_sf(data = bc_cities()) +
coord_sf(datum = NA) +
scale_fill_discrete(name = "Jurisdiction") +
theme_minimal()
```
### Biogeoclimatic Zones
As of version 0.15.0 the B.C. BEC (Biogeoclimatic Ecosystem Classification) map
is available via the `bec()` function, and an accompanying function `bec_colours()`
function to colour it:
```{r include=FALSE}
bec <- bec(force = TRUE, ask = FALSE)
```
```{r bec, message=FALSE}
bec <- bec()
library(ggplot2)
ggplot() +
geom_sf(data = bec[bec$ZONE %in% c("BG", "PP"),],
aes(fill = ZONE, col = ZONE)) +
scale_fill_manual(values = bec_colors()) +
scale_colour_manual(values = bec_colours())
```
### Spatial (sp) objects
If you aren't using the `sf` package and prefer the old standard [`sp`](https://cran.r-project.org/package=sp)
way of doing things, set `class = "sp"` in either `get_layer` or the shortcut functions:
```{r watercourses, warning=FALSE}
library("sp")
# Load watercourse data and plot with boundaries of B.C.
plot(get_layer("bc_bound", class = "sp"))
plot(watercourses_15M(class = "sp"), add = TRUE)
```
### Vignettes
We have written a short vignette on plotting points on one of the layers from `bcmaps`.
You can view the vignette online [here](https://cran.r-project.org/web/packages/bcmaps/vignettes/add_points.html) or if you installed the package you can open it using `browseVignettes("bcmaps")`.
### Utility Functions
The package also contains a couple of handy utility functions:
1. `fix_geo_problems()` for fixing invalid topologies in `sf` or `Spatial` objects
such as orphaned holes and self-intersections
2. `transform_bc_albers()` for transforming any `sf` or `Spatial` object to
[BC Albers](https://epsg.io/3005) projection.
3. `self_union()` Union a `SpatialPolygons*` object with itself to remove overlaps, while retaining attributes
## Getting Help or Reporting an Issue
To report bugs/issues/feature requests, please file an [issue](https://github.com/bcgov/bcmaps/issues/).
## How to Contribute
Pull requests of new B.C. layers are welcome.
If you would like to contribute to the package, please see our
[CONTRIBUTING](CONTRIBUTING.md) guidelines.
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
## Source Data
The source datasets used in this package come from various sources under open licences, including [DataBC](http://data.gov.bc.ca) ([Open Government Licence - British Columbia](http://www2.gov.bc.ca/gov/content?id=A519A56BC2BF44E4A008B33FCF527F61)) and [Statistics Canada](http://www.statcan.gc.ca/start-debut-eng.html) ([Statistics Canada Open Licence Agreement](http://www.statcan.gc.ca/eng/reference/licence-eng)). See the `data-raw` folder for details on each source dataset.
## Licence
# Copyright 2017 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
This repository is maintained by [Environmental Reporting BC](http://www2.gov.bc.ca/gov/content?id=FF80E0B985F245CEA62808414D78C41B). Click [here](https://github.com/bcgov/EnvReportBC-RepoList) for a complete list of our repositories on GitHub.