【R】bcdata

1. はじめに

bcdataは、B.C. Data Catalogue.のデータを検索したり取得するパッケージです。

2. インストール

CRANからインストールできます。

install.packages("bcdata")

3. つかってみる

限定されたデータは認証が必要ですが、一般公開のデータは認証なしで利用できます。

どのようなデータがあるか、カタログを見たいときは、以下の関数を使います。実行するとデフォルトのブラウザーが開いてカタログを見ることができます。

library(bcdata)

bcdc_browse()

データを検索するときは、bcdc_search()関数を用います。miningに関するデータを探してみます。

bcdc_search("mining")
List of B.C. Data Catalogue Records
Number of records: 72
 (Showing the top 10)
Titles:
1: Permitted Mine Areas - Major Mine (wms, kml, other, pdf)
 ID: 01e8a35e-35e3-4b48-93a7-b0d7c9705b62
 Name: permitted-mine-areas-major-mine
2: Summary Table of DSI Reports for Permitted Metal and Coal Mines (csv, xlsx)
 ID: 6a46a409-7367-45ee-bdfe-edd2a8685ca7
 Name: summary-table-of-dsi-reports-for-permitted-metal-and-coal-mines
3: Mining Regions (other)
 ID: 5338498c-52ba-4fca-a310-79cca09fd8c8
 Name: mining-regions
4: MTA - Mineral Mining Divisions with Codes (other, wms, kml)
 ID: 57be7cc5-76e3-4e93-9251-18750210dc40
 Name: mta-mineral-mining-divisions-with-codes
5: Regional Geochemical MineMatch Anomaly Clusters (other)
 ID: a6f51d25-3d32-4dcf-aba5-446ad2dc85ec
 Name: regional-geochemical-minematch-anomaly-clusters
6: Purchasing Card Expenditures - Ministry of Energy and Mines - FYE11 (csv)
 ID: 9263723c-a31a-4034-9dae-7235f4840206
 Name: purchasing-card-expenditures-ministry-of-energy-and-mines-fye11
7: BC Annual Coal Production from 1866 Onwards (csv, xls)
 ID: 387692b1-ff47-4a68-ab36-e88591474e7a
 Name: bc-annual-coal-production-from-1866-onwards
8: BC Annual Construction Aggregate Production from 1930 Onwards (csv, xls)
 ID: aacdfc51-34c0-4882-8ea0-6b342ec34e3d
 Name: bc-annual-construction-aggregate-production-from-1930-onwards
9: Employment by industry sector and place of work, aggregated to census division (other)
 ID: f5c79d19-5d2a-489d-818c-57701b62cddf
 Name: employment-by-industry-sector-and-place-of-work-aggregated-to-census-division
10: Till Geochemistry Data (csv, xlsx)
 ID: 0fc80a28-032c-401b-bbd1-7d609b21bce8
 Name: till-geochemistry-data

Access a single record by calling `bcdc_get_record(ID)` with the ID from the desired record.

データの取得は、bcdc_get_data()関数です。NameやIDをキーとして取得できます。「6: Purchasing Card Expenditures – Ministry of Energy and Mines – FYE11 (csv)」を取得してみます。

dat <- bcdc_get_data("9263723c-a31a-4034-9dae-7235f4840206")
> dat
# A tibble: 270 x 10
   `Energy & Mines:  From 04-01-2010 to 03-3~ X2               X3       X4          X5      X6    X7    X8    X9    X10  
   <chr>                                      <chr>            <chr>    <chr>       <chr>   <lgl> <lgl> <lgl> <lgl> <lgl>
 1 Supplier Name                              Total Transacti~ Net Amo~ City        Provin~ NA    NA    NA    NA    NA   
 2 A M E B C                                  1                $61.60   VANCOUVER   BC      NA    NA    NA    NA    NA   
 3 ACKLANDS-GRAINGER INC                      4                $701.33  PRINCE GEO~ BC      NA    NA    NA    NA    NA   
 4 ACME LABS                                  1                $659.46  VANCOUVER   BC      NA    NA    NA    NA    NA   
 5 ACT*IPAC VICTORIA                          1                $25.00   303-577-51~ BC      NA    NA    NA    NA    NA   
 6 ADVANCED LEARNING INST                     1                $2,648.~ 7736959400  IL      NA    NA    NA    NA    NA   
 7 ADVANTAGE PACKAGING                        2                $84.34   HAMILTON    ON      NA    NA    NA    NA    NA   
 8 AIR CAN  0140851152980                     1                $3,640.~ WINNIPEG    MB      NA    NA    NA    NA    NA   
 9 AIR CAN  0141752120015                     1                $2,584.~ WINNIPEG    MB      NA    NA    NA    NA    NA   
10 AIR CAN  0142182936334                     1                $158.48  WINNIPEG    MB      NA    NA    NA    NA    NA   
# ... with 260 more rows

もし、取得するデータがgeospatialデータの場合、自動的にsfファイルに変更してくれます。

空気(air)に関するデータを見てみます。

bcdc_search("air", res_format = "geojson")
> bcdc_search("air", res_format = "geojson")
List of B.C. Data Catalogue Records
Number of records: 2
Titles:
1: British Columbia Air Zones (shp, kml, geojson)
 ID: e8eeefc4-2826-47bc-8430-85703d328516
 Name: british-columbia-air-zones
2: Mental Health and Substance Use Health Services (xlsx, pdf, csv, geojson, kml, other, wms)
 ID: 2e469ff2-dadb-45ea-af9d-f5683a4b9465
 Name: mental-health-and-substance-use-health-services

Access a single record by calling `bcdc_get_record(ID)` with the ID from the desired record.

2番目のデータを見てみます。

bc_metadata <- bcdc_get_record("2e469ff2-dadb-45ea-af9d-f5683a4b9465")
bc_air <- bcdc_get_data(bc_metadata)
> bc_air <- bcdc_get_data(bc_metadata)
The record you are trying to access appears to have more than one resource.

Resources:
1) HealthLinkBC MHSU AIRS Taxonomy Classification
   format: xlsx
   url:
 https://catalogue.data.gov.bc.ca/dataset/2e469ff2-dadb-45ea-af9d-f5683a4b9465/resource/18510a60-de82-440a-b806-06fba70eaf9d/download/healthlinkbcmhsudatasetairstaxonomy.classification.definitionapril2019.xlsx
   resource: 18510a60-de82-440a-b806-06fba70eaf9d
   code: bcdc_get_data(record = '2e469ff2-dadb-45ea-af9d-f5683a4b9465', resource =
 '18510a60-de82-440a-b806-06fba70eaf9d')

2) mental-health.csv
   format: csv
   url:
 https://catalogue.data.gov.bc.ca/dataset/2e469ff2-dadb-45ea-af9d-f5683a4b9465/resource/de425d68-a87f-44ce-ab4d-fcb514c04975/download/mental-health.csv
   resource: de425d68-a87f-44ce-ab4d-fcb514c04975
   code: bcdc_get_data(record = '2e469ff2-dadb-45ea-af9d-f5683a4b9465', resource =
 'de425d68-a87f-44ce-ab4d-fcb514c04975')

3) mental-health.geojson
   format: geojson
   url:
 https://catalogue.data.gov.bc.ca/dataset/2e469ff2-dadb-45ea-af9d-f5683a4b9465/resource/6baaf199-0106-4dd6-8e2f-e24d629a81c6/download/mental-health.geojson
   resource: 6baaf199-0106-4dd6-8e2f-e24d629a81c6
   code: bcdc_get_data(record = '2e469ff2-dadb-45ea-af9d-f5683a4b9465', resource =
 '6baaf199-0106-4dd6-8e2f-e24d629a81c6')

4) mental-health.kml
   format: kml
   url:
 https://catalogue.data.gov.bc.ca/dataset/2e469ff2-dadb-45ea-af9d-f5683a4b9465/resource/48901888-a10f-418b-9172-bee09ecd6725/download/mental-health.kml
   resource: 48901888-a10f-418b-9172-bee09ecd6725
   code: bcdc_get_data(record = '2e469ff2-dadb-45ea-af9d-f5683a4b9465', resource =
 '48901888-a10f-418b-9172-bee09ecd6725')

5) HealthLinkBC MHSU Health Services
   format: csv
   url:
 https://catalogue.data.gov.bc.ca/dataset/2e469ff2-dadb-45ea-af9d-f5683a4b9465/resource/7499f921-5bb4-4ed2-bf28-6aca3ef22407/download/healthlinkbc-mhsu-health-services.csv
   resource: 7499f921-5bb4-4ed2-bf28-6aca3ef22407
   code: bcdc_get_data(record = '2e469ff2-dadb-45ea-af9d-f5683a4b9465', resource =
 '7499f921-5bb4-4ed2-bf28-6aca3ef22407')

6) WFS request (Spatial Data)
   format: wfs
   resource: dafd1f45-0ce3-4b6a-895c-a27303ea8ca9
   code: bcdc_get_data(record = '2e469ff2-dadb-45ea-af9d-f5683a4b9465', resource =
 'dafd1f45-0ce3-4b6a-895c-a27303ea8ca9')

--------
Please choose one option: 

1: HealthLinkBC MHSU AIRS Taxonomy Classification
2: mental-health.csv
3: mental-health.geojson
4: mental-health.kml
5: HealthLinkBC MHSU Health Services
6: WFS request (Spatial Data)

3番目のgeojsonデータを取得します。コードが書かれていますので、それをコピーして実行します。

air_Dat<-bcdc_get_data(record = '2e469ff2-dadb-45ea-af9d-f5683a4b9465', resource ='6baaf199-0106-4dd6-8e2f-e24d629a81c6')

取得したデータを地図上に表示してみます。

library(ggplot2)

air_Dat %>%
  ggplot() +
  geom_sf() +
  theme_minimal()

さいごに、bcdc_get_queryを使ってデータを取得する例です。

library(tidyverse)

bcdc_search("regional districts administrative areas", res_format = "wms", n = 1)
bc_regional_districts_metadata <- bcdc_get_record("d1aff64e-dbfe-45a6-af97-582b7f6418b9")
bcdc_describe_feature(bc_regional_districts_metadata)
my_regional_district <- bcdc_query_geodata(bc_regional_districts_metadata) %>%
  filter(ADMIN_AREA_NAME == "Capital Regional District") %>%
  collect()

my_regional_district  %>%
  ggplot() +
  geom_sf() +
  theme_minimal()

4. さいごに

非常に多くの有用なデータを扱えます。ジオデータを簡単に扱えるのが良いですね。

Add a Comment

メールアドレスが公開されることはありません。