Skip to content

Commit

Permalink
Fix urls, precompile vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
ateucher committed Dec 11, 2024
1 parent 6ed050b commit b085f91
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 241 deletions.
227 changes: 122 additions & 105 deletions vignettes/bcdata.Rmd

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion vignettes/bcdata.Rmd.orig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ library(ggplot2)

The `bcdata` [R](https://www.r-project.org/) package contains functions for searching & retrieving data from the [B.C. Data Catalogue]( https://catalogue.data.gov.bc.ca).

The [B.C. Data Catalogue](https://www2.gov.bc.ca/gov/content?id=79B5224167334667A44C9E8B5143D0C5) is the place to find British Columbia Government data, applications and web services. Much of the data are released under the [Open Government Licence --- British Columbia](https://www2.gov.bc.ca/gov/content/data/open-data/open-government-licence-bc), as well as numerous other [licences](https://catalogue.data.gov.bc.ca/dataset?download_audience=Public).
The [B.C. Data Catalogue](https://www2.gov.bc.ca/gov/content?id=79B5224167334667A44C9E8B5143D0C5) is the place to find British Columbia Government data, applications and web services. Much of the data are released under the [Open Government Licence --- British Columbia](https://www2.gov.bc.ca/gov/content/data/policy-standards/open-data/open-government-licence-bc), as well as numerous other [licences](https://catalogue.data.gov.bc.ca/dataset?download_audience=Public).



Expand Down
181 changes: 95 additions & 86 deletions vignettes/efficiently-query-spatial-data-in-the-bc-data-catalogue.Rmd

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ This output is what being created by the dplyr code outlined above.

## Using B.C. Geographic Warehouse (BCGW) layer names

If you are familiar with the [B.C. Geographic Warehouse (BCGW)](https://www2.gov.bc.ca/gov/content/data/geographic-data-services/bc-spatial-data-infrastructure/bc-geographic-warehouse),
If you are familiar with the [B.C. Geographic Warehouse (BCGW)](https://www2.gov.bc.ca/gov/content/data/finding-and-sharing/bc-geographic-warehouse),
you may already know the name of a layer that you want from the BCGW.
`bcdc_query_geodata()` (as well as all other related functions)
supports supplying that name directly. For example, the
Expand Down
68 changes: 40 additions & 28 deletions vignettes/explore-silviculture-data-using-bcdata.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Exploring Silviculture Data with bcdata"
date: "2022-10-28"
date: "2024-12-11"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Exploring Silviculture Data with bcdata}
Expand Down Expand Up @@ -47,7 +47,7 @@ Let's use the `bcdata` package to search, retrieve and explore the RESULTS silvi
To start, let's load the `bcdata` package. We will also load the `dplyr` and `ggplot2` packages to help us work with the data. You can learn more about the `dplyr` package [here](https://dplyr.tidyverse.org/) and the `ggplot2` package [here](https://ggplot2.tidyverse.org/):


```r
``` r
library(bcdata)
library(dplyr)
library(ggplot2)
Expand All @@ -59,7 +59,7 @@ library(ggplot2)
We can gather the data we need to answer our question using the [RESULTS - silviculture forest cover dataset](https://catalogue.data.gov.bc.ca/dataset/258bb088-4113-47b1-b568-ce20bd64e3e3). First, let's take a look at the metadata record using `bcdc_get_record()`:


```r
``` r
# Get the metadata using the human-readable record name
bcdc_get_record("results-forest-cover-silviculture")
#> B.C. Data Catalogue Record: RESULTS - Forest Cover Silviculture
Expand All @@ -84,12 +84,12 @@ bcdc_get_record("results-forest-cover-silviculture")
We see that this is a [Web Feature Service-enabled](https://en.wikipedia.org/wiki/Web_Feature_Service) geospatial data set--the list of data resources includes `WMS getCapabilities request`--so we can query and retrieve this geospatial data set using `bcdc_query_geodata()`:


```r
``` r
# Query the data using the permanent ID of the record to guard against name changes
bcdc_query_geodata("258bb088-4113-47b1-b568-ce20bd64e3e3")
#> Querying 'results-forest-cover-silviculture' record
#> • Using collect() on this object will return 920718 features and 159 fields
#> • Accessing this record requires pagination and will make 921 separate requests to the WFS.
#> • Using collect() on this object will return 957189 features and 159 fields
#> • Accessing this record requires pagination and will make 96 separate requests to the WFS.
#> • See ?bcdc_options
#> • At most six rows of the record are printed here
#> ────────────────────────────────────────────────────────────────────────────────────────────────────
Expand All @@ -99,21 +99,21 @@ bcdc_query_geodata("258bb088-4113-47b1-b568-ce20bd64e3e3")
#> Bounding box: xmin: 1184167 ymin: 526455.5 xmax: 1801754 ymax: 1083545
#> Projected CRS: NAD83 / BC Albers
#> # A tibble: 6 × 160
#> id FORES…¹ STOCK…² OPENI…³ STAND…⁴ SILV_…⁵ SILV_…⁶ SILV_…⁷ SILV_…⁸ STOCK…⁹ STOCK…˟ STOCK…˟
#> <chr> <int> <int> <int> <chr> <chr> <dbl> <dbl> <int> <chr> <chr> <chr>
#> 1 WHSE_FORE… 4177991 2243439 1724262 1 C 1.6 1.6 0 IMM ART <NA>
#> 2 WHSE_FORE… 3994007 NA 1248495 <NA> 91 1.2 1.2 0 MAT NAT <NA>
#> 3 WHSE_FORE… 3994067 NA 1120935 <NA> 86 0.6 0.6 0 MAT NAT <NA>
#> 4 WHSE_FORE… 3994009 1404558 1248495 A AA 22.1 22.1 0 IMM ART <NA>
#> 5 WHSE_FORE… 3994057 1211894 1120935 B 2B 16.9 16.9 0 IMM ART <NA>
#> 6 WHSE_FORE… 3994063 NA 1120935 <NA> 82 0.1 0.1 0 MAT NAT <NA>
#> # … with 148 more variables: SILV_RESERVE_CODE <chr>, SILV_RESERVE_OBJECTIVE_CODE <chr>,
#> id FOREST_COVER_ID STOCKING_STANDARD_UN…¹ OPENING_ID STANDARDS_UNIT_ID SILV_POLYGON_NUMBER
#> <chr> <int> <int> <int> <chr> <chr>
#> 1 WHSE_FORE… 4177991 2243439 1724262 1 C
#> 2 WHSE_FORE… 3994007 NA 1248495 <NA> 91
#> 3 WHSE_FORE… 3994067 NA 1120935 <NA> 86
#> 4 WHSE_FORE… 3994009 1404558 1248495 A AA
#> 5 WHSE_FORE… 3994057 1211894 1120935 B 2B
#> 6 WHSE_FORE… 3994063 NA 1120935 <NA> 82
#> # ℹ abbreviated name: ¹​STOCKING_STANDARD_UNIT_ID
#> # ℹ 154 more variables: SILV_POLYGON_AREA <dbl>, SILV_POLYGON_NET_AREA <dbl>,
#> # SILV_NON_MAPPED_AREA <int>, STOCKING_STATUS_CODE <chr>, STOCKING_TYPE_CODE <chr>,
#> # STOCKING_CLASS_CODE <chr>, SILV_RESERVE_CODE <chr>, SILV_RESERVE_OBJECTIVE_CODE <chr>,
#> # TREE_COVER_PATTERN_CODE <chr>, REENTRY_YEAR <chr>, REFERENCE_YEAR <int>, SITE_INDEX <int>,
#> # SITE_INDEX_SOURCE_CODE <chr>, BGC_ZONE_CODE <chr>, BGC_SUBZONE_CODE <chr>, BGC_VARIANT <chr>,
#> # BGC_PHASE <chr>, BEC_SITE_SERIES <chr>, BEC_SITE_TYPE <chr>, BEC_SERAL <chr>,
#> # IS_SILV_IMPLIED_IND <chr>, FOREST_COVER_SILV_TYPE <chr>, S_FOREST_COVER_LAYER_ID <int>,
#> # S_TOTAL_STEMS_PER_HA <chr>, S_TOTAL_WELL_SPACED_STEMS_HA <int>,
#> # S_WELL_SPACED_STEMS_PER_HA <int>, S_FREE_GROWING_STEMS_PER_HA <int>, …
#> # BGC_PHASE <chr>, BEC_SITE_SERIES <chr>, BEC_SITE_TYPE <chr>, BEC_SERAL <chr>, …
```

This query shows that this data set has many features and over 150 fields. Each feature is a treatment unit within a harvested opening, and contains information on the leading five tree species that are present in each treatment unit, including stems per hectare, age, and height.
Expand All @@ -128,7 +128,7 @@ To address our question, we need the treatment data (1) from the Prince George N
First, we can use the `bcdata` package to download the spatial boundary for the Prince George Natural Resource District&mdash;`DPG` is the `ORG_UNIT` for Prince George Natural Resource District:


```r
``` r
## Create a spatial feature object named dpg
dpg <- bcdc_query_geodata("natural-resource-nr-district") %>%
filter(ORG_UNIT=="DPG") %>% # filter for Prince George Natural Resource District
Expand All @@ -138,21 +138,24 @@ dpg <- bcdc_query_geodata("natural-resource-nr-district") %>%
Let's plot this spatial object and double check we have we what we need:


```r
``` r
dpg %>%
ggplot() +
geom_sf() +
theme_minimal()
```

<div class="figure">
<img src="vignette-fig-plot-dpg-1.png" alt="plot of chunk plot-dpg" width="100%" />
<p class="caption">plot of chunk plot-dpg</p>
</div>

Now we have a spatial object that we can use as a bounding box to filter and download records in the RESULTS - silviculture layer from the Prince George Natural Resource District.

We only need to download the treatments that have western larch planted. We can use the `bcdc_describe_feature()` helper function to examine the column names and types of the layer. In this case, we want to keep rows where the five `S_SPECIES_CODE_*` columns contain `"LW"`, the code for western larch.


```r
``` r
# Make a vector of tree species we are interested in
# (in this case only LW for western larch)
spp_list = c("LW")
Expand All @@ -177,23 +180,26 @@ trees_dpg <-
Let's look at the dimensions of this now much more manageable data object we have downloaded from the B.C. Data Catalogue:


```r
``` r
dim(trees_dpg)
#> [1] 212 160
#> [1] 261 160
```

We can see there are several treatment units planted with western larch, and we can make a quick map of these harvested openings for the Prince George Natural Resource District:


```r
``` r
trees_dpg %>%
ggplot() +
geom_sf() +
geom_sf(data = dpg, fill = NA) + #add the DPG spatial boundary
theme_minimal()
```

<div class="figure">
<img src="vignette-fig-map-larch-plantations-dpg-1.png" alt="plot of chunk map-larch-plantations-dpg" width="100%" />
<p class="caption">plot of chunk map-larch-plantations-dpg</p>
</div>


We can also create some quick descriptive summaries of the data, treating the geospatial attribute table as a data frame in R, and answer our original question---how much western larch has been planted in the Prince George Natural Resource District?
Expand All @@ -202,7 +208,7 @@ We can also create some quick descriptive summaries of the data, treating the ge
#### What is the size and age distribution of larch plantations in the Prince George Natural Resource District in the year 2020?


```r
``` r
trees_dpg %>%
mutate(age = 2020 - REFERENCE_YEAR + S_SPECIES_AGE_1) %>% #create a plantation age column
ggplot() + #start a plot
Expand All @@ -220,15 +226,18 @@ trees_dpg %>%
theme(legend.position = "none")
```

<div class="figure">
<img src="vignette-fig-unnamed-chunk-1-1.png" alt="plot of chunk unnamed-chunk-1" width="100%" />
<p class="caption">plot of chunk unnamed-chunk-1</p>
</div>

#### What is the Biogeoclimatic Ecosystem Classification (BEC) distribution of western larch plantations in the Prince George Natural Resource District?

We can download [British Columbia biogeoclimatic (BEC) data](https://catalogue.data.gov.bc.ca/dataset/bec-map) from the B.C. Data Catalogue using `bcdata` and join to our existing `trees_dpg` geospatial dataset using the `st_join` function from the `sf` package. You can learn more about `sf` [here](https://r-spatial.github.io/sf/).



```r
``` r
library(sf) #load the sf package

# Load the BEC data for Prince George Natural Resource District
Expand All @@ -245,7 +254,7 @@ trees_bec_dpg <- trees_dpg %>%
Now, we can summarize the area planted with western larch by biogeoclimatic unit:


```r
``` r
trees_bec_dpg %>%
group_by(MAP_LABEL) %>% # group polygons by biogeoclimatic unit
summarise(Area = sum(FEATURE_AREA_SQM)/10000) %>%
Expand All @@ -262,4 +271,7 @@ trees_bec_dpg %>%
theme(legend.position = "none")
```

<div class="figure">
<img src="vignette-fig-unnamed-chunk-3-1.png" alt="plot of chunk unnamed-chunk-3" width="100%" />
<p class="caption">plot of chunk unnamed-chunk-3</p>
</div>
41 changes: 21 additions & 20 deletions vignettes/local-filter.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Update to `filter()` behaviour in bcdata v0.4.0"
date: "2022-10-28"
date: "2024-12-11"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Update to `filter()` behaviour in bcdata v0.4.0}
Expand Down Expand Up @@ -33,7 +33,7 @@ bounding box.



```r
``` r
library(sf)
library(bcdata)

Expand All @@ -46,49 +46,50 @@ Previously, we could just do this, with `sf::st_bbox()`
embedded in the call:


```r
``` r
bcdc_query_geodata("local-and-regional-greenspaces") %>%
filter(BBOX(st_bbox(two_points, crs = st_crs(two_points))))
```

```
## Error: Unable to process query. Did you use a function that should be evaluated locally? If so, try wrapping it in 'local()'.
## Error: Error : Cannot translate a <sfc_POINT> object to SQL.
## ℹ Do you want to force evaluation in R with (e.g.) `!!x` or `local(x)`?
```

However you must now use `local()` to force local evaluation of
`st_bbox()` on your machine in R, before it is translated
into a query plan to be executed on the server:


```r
``` r
bcdc_query_geodata("local-and-regional-greenspaces") %>%
filter(BBOX(local(st_bbox(two_points, crs = st_crs(two_points)))))
```

```
## Querying 'local-and-regional-greenspaces' record
## • Using collect() on this object will return 1154 features and 19 fields
## • Using collect() on this object will return 1158 features and 19 fields
## • At most six rows of the record are printed here
## ────────────────────────────────────────────────────────────────────────────────────────────────────
## Simple feature collection with 6 features and 19 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 1200113 ymin: 385903.5 xmax: 1202608 ymax: 386561.8
## Bounding box: xmin: 1200113 ymin: 385903.5 xmax: 1202130 ymax: 388026
## Projected CRS: NAD83 / BC Albers
## # A tibble: 6 × 20
## id LOCAL…¹ PARK_…² PARK_…³ PARK_…⁴ REGIO…⁵ MUNIC…⁶ CIVIC…⁷ CIVIC…⁸ STREE…⁹ LATIT…˟ LONGI…˟
## <chr> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 WHSE_BASE3347 Konuks… Local Green … Capital Distri… <NA> <NA> <NA> 48.5 -123.
## 2 WHSE_BASE3304 <NA> Local Trail Capital Distri… <NA> <NA> <NA> 48.5 -123.
## 3 WHSE_BASE3380 <NA> Local Water … Capital Distri… <NA> <NA> <NA> 48.5 -123.
## 4 WHSE_BASE3369 <NA> Local Water … Capital Distri… <NA> <NA> <NA> 48.5 -123.
## 5 WHSE_BASE3453 <NA> Local Water … Capital Distri… <NA> <NA> <NA> 48.5 -123.
## 6 WHSE_BASE3361 <NA> Local Trail Capital Distri… <NA> <NA> <NA> 48.5 -123.
## # … with 8 more variables: WHEN_UPDATED <date>, WEBSITE_URL <chr>, LICENCE_COMMENTS <chr>,
## # FEATURE_AREA_SQM <dbl>, FEATURE_LENGTH_M <dbl>, OBJECTID <int>, SE_ANNO_CAD_DATA <chr>,
## # geometry <POLYGON [m]>, and abbreviated variable names ¹​LOCAL_REG_GREENSPACE_ID, ²​PARK_NAME,
## # ³​PARK_TYPE, ⁴​PARK_PRIMARY_USE, ⁵​REGIONAL_DISTRICT, ⁶​MUNICIPALITY, ⁷​CIVIC_NUMBER,
## # ⁸​CIVIC_NUMBER_SUFFIX, ⁹​STREET_NAME, ˟​LATITUDE, ˟​LONGITUDE
## id LOCAL_REG_GREENSPACE…¹ PARK_NAME PARK_TYPE PARK_PRIMARY_USE REGIONAL_DISTRICT MUNICIPALITY
## <chr> <int> <chr> <chr> <chr> <chr> <chr>
## 1 WHSE_B 689 Cranford… Local Water Access Capital District of…
## 2 WHSE_B 634 <NA> Local Water Access Capital District of…
## 3 WHSE_B 725 <NA> Local Water Access Capital District of…
## 4 WHSE_B 665 Konukson… Local Green Space Capital District of…
## 5 WHSE_B 622 <NA> Local Trail Capital District of…
## 6 WHSE_B 698 <NA> Local Water Access Capital District of…
## # ℹ abbreviated name: ¹​LOCAL_REG_GREENSPACE_ID
## # ℹ 13 more variables: CIVIC_NUMBER <chr>, CIVIC_NUMBER_SUFFIX <chr>, STREET_NAME <chr>,
## # LATITUDE <dbl>, LONGITUDE <dbl>, WHEN_UPDATED <date>, WEBSITE_URL <chr>,
## # LICENCE_COMMENTS <chr>, FEATURE_AREA_SQM <dbl>, FEATURE_LENGTH_M <dbl>, OBJECTID <int>,
## # SE_ANNO_CAD_DATA <chr>, geometry <POLYGON [m]>
```

There is another illustration in the ["querying spatial data vignette"](https://bcgov.github.io/bcdata/articles/efficiently-query-spatial-data-in-the-bc-data-catalogue.html#a-note-about-using-local-r-functions-in-constructing-filter-queries).
Binary file modified vignettes/vignette-fig-air_zones-1.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 modified vignettes/vignette-fig-bbox-1.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 modified vignettes/vignette-fig-district_parks-1.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 modified vignettes/vignette-fig-dp_join-1.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 modified vignettes/vignette-fig-map-larch-plantations-dpg-1.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 modified vignettes/vignette-fig-plot-dpg-1.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 modified vignettes/vignette-fig-regional_districts-1.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 modified vignettes/vignette-fig-unnamed-chunk-1-1.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 modified vignettes/vignette-fig-unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b085f91

Please sign in to comment.