17 Geo/Spatial coding and visualization in R

17.1 Resources

There’s much effort across many GC departments to analyze and visualize geo-data. This discussion is the place to share your results, ideas or problems related to the problem.

Below is a great resource to start, which also provides a nice explanation on why R is believed to be the best language to do this kind of work.

Geocomputation with R, a book on geographic data analysis, visualization and modeling.

The online version of the book is hosted at https://geocompr.robinlovelace.net and kept up-to-date by GitHub Actions

17.2 Federal Geospatial Platform

From https://gcconnex.gc.ca/discussion/view/84695812/data-federation-federation-des-donnees

Yukon’s open geospatial data is now searchable on the Federal Geospatial Platform (FGP) (https://gcgeo.gc.ca/) and on Government of Canada Open Maps Portal. Check it out !

With the addition of Yukon’s resources, the FGP now offers over 5,000 datasets available to discover and download, for public servants and Canadians, all in one location.

Wondering how to create a map with your data? We can help you do so, from A to Z, at no cost. Contact us at

17.3 Tutorials

Check codes and notes for the two Tutorials that we had on this subject this summer:

https://gccode.ssc-spc.gc.ca/r4gc/resources/introSpatialAnalysis

17.4 Dealing with memory issues

A blog post that illustrates a few ways to avoid overloading R’s memory when working with large spatial objects (here’s looking at you, 30-m land cover map of North America!).

https://www.ecologi.st/post/big-spatial-data/

The two other posts on that blog also have some really nice tips for general R coding.

17.5 Canadian geo-data

Useful code and R packages from public domain to work with Canadian geo-data.

From https://mountainmath.ca - https://github.com/mountainMath/mountainmathHelpers - tongfen: Convenience functions for making data on different geometries, especially Canadian census geometries, comparable. - cancensus : R wrapper for calling CensusMapper APIs - cansim: Wrapper to access CANSIM data - CanCovidData: Collection of data import and processing functions focused on Canadian data

17.6 Code snippets

17.6.1 Using simplemaps.com

# 1.a  https://simplemaps.com/data/us-cities ----

load("datasets/geoCa/uszips.rda")

dtUS <- as.data.table(uszips) %>% 
  mutate(across(where(is.character), as.factor)) %>% 
  mutate(across(where(is.logical), as.factor));  dtUS %>% print(2); dtUS %>% summary
saveRDS(dtUS, "citiesUS_simplemaps.Rds")


# 1.b  https://simplemaps.com/data/canada-cities ----

dtCaCitiesGeoPop <- readRDS("citiesCa-simplemaps.Rds") %>% setDT

# dtCa2<- read_excel("geoCa/canadacities.xlsx", sheet=1) %>% data.table() #%T>% print()
dtCaSimplemaps <- fread("geoCa/canadacities.csv")

17.6.2 Using Google API

17.6.2.2 Using googleway

require(googleway)
set_key("your_google_key_of_39_characters")
x= google_geocode(address = "Southern Saskatchewan", simplify = TRUE)
x$results$geometry

17.6.3 Using tidygeocoder

tidygeocoder::geo_osm('Southern Saskatchewan')

17.6.5 Using Open Database of Addresses / Educational Facilities

https://www.statcan.gc.ca/eng/lode/databases/oda https://www.statcan.gc.ca/eng/lode/databases/odef

http://www150.statcan.gc.ca/n1/pub/46-26-0001/2021001/"

urlAddresses =  c(
  Alberta = "ODA_AB_v1.zip",
  BritishColumbia = "ODA_BC_v1.zip",
  Manitoba = "ODA_MB_v1.zip",
  NewBrunswick = "ODA_NB_v1.zip",
  NorthwestTerritories = "ODA_NT_v1.zip",
  NovaScotia = "ODA_NS_v1.zip",
  Ontario = "ODA_ON_v1.zip",
  PrinceEdwardIsland = "ODA_PE_v1.zip",
  Quebec = "ODA_QC_v1.zip",
  Saskatchewan = "ODA_SK_v1.zip"
)

dtUrlAddresses <- data.table(province=names(urlAddresses),url=urlAddresses) %>% setkey(province)

dtUrlAddresses$url <- str_c(strHome, dtUrlAddresses$url) 
dtUrlAddresses[, url:=str_c(strHome, url) ]
dtAddress <- fread (urlAddresses["Alberta"])

17.6.6 Getting Postal codes

NOT AVAILABLE TO PUBLIC, ONLY VIA UNIVERSITY

https://www.statcan.gc.ca/eng/mgeo/postalcode

https://www.statcan.gc.ca/eng/dli/dli

The Data Liberation Initiative (DLI) is a partnership between post-secondary institutions and Statistics Canada for improving access to Canadian data resources.

Please consult the list of participating institutions and their contacts. If your institution is already a member, please contact the person listed to gain access to products available through the DLI.

  University of Ottawa
  Chantal Ripp
  Data Research Librarian
  613-562-5800 ext.3881
  chantal.ripp@uottawa.ca