2

I found this website here that contains a geographical map on internet coverage across different parts of Canada over different years: https://crtc.gc.ca/cartovista/LTEOverTheYearsYE2019_EN/index.html

My Question: I want to try and extract all relevant geographical information (e.g. polygon boundaries, shadings, years) such that I would be able to recreate this map myself using R (e.g. using the "leaflet" library).

Here is what I have tried so far:

This now shows me that there are several JSON/XML files associated with this map:

Using R, I then downloaded all the JSON files and parsed them:

library(jsonlite)
library(httr)

urls <- c("https://crtc.gc.ca/cartovista/LTEOverTheYearsYE2019_EN/map/LTE_YE2019.json",
          "https://crtc.gc.ca/cartovista/LTEOverTheYearsYE2019_EN/map/LTE_YE2018.json",
          "https://crtc.gc.ca/cartovista/LTEOverTheYearsYE2019_EN/map/LTE_YE2017.json",
          "https://crtc.gc.ca/cartovista/LTEOverTheYearsYE2019_EN/map/LTE_YE2016.json",
          "https://crtc.gc.ca/cartovista/LTEOverTheYearsYE2019_EN/map/LTE_YE2015.json",
          "https://crtc.gc.ca/cartovista/LTEOverTheYearsYE2019_EN/map/LTE_YE2014.json",
          "https://crtc.gc.ca/cartovista/LTEOverTheYearsYE2019_EN/map/LTEOverYearsByProv.json")

my_list <- list()

for (i in seq_along(urls)) {
    response <- GET(urls[i])
    content <- content(response, as = "text")
    data <- fromJSON(content)
    name <- sub(".*/(.*).json", "\\1", urls[i])
    my_list[[name]] <- data
}

list2env(my_list, envir = .GlobalEnv)

When I start inspecting the results, it seems like the JSON files all have a similar format - for example, here is the LTE_YE2019 file :

 > str(LTE_YE2019)

List of 5
 $ scaleUpFactor       : int 0
 $ type                : chr "FeatureCollection"
 $ proj                : chr "+proj=lcc +lat_1=49 +lat_2=77 +lat_0=49 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs "
 $ attributeDefinitions: chr [1, 1:2] "UniqueID" "String"
 $ f                   :'data.frame':   1 obs. of  3 variables:
  ..$ t: chr "Feature"
  ..$ g:'data.frame':   1 obs. of  2 variables:
  .. ..$ t: chr "Polygon"
  .. ..$ c:List of 1
  .. .. ..$ :List of 606
  .. .. .. ..$ : int [1:57382] 672569 15968 -1136 -165 -1136 -164 -1137 -164 -1136 -164 ...
  .. .. .. ..$ : int [1:12262] -1377281 534100 -1009 337 -1009 338 -1009 338 -1009 338 ...
  .. .. .. ..$ : int [1:3282] -1465443 375186 -1048 359 -1048 359 -1048 360 -1048 359 ...
  .. .. .. ..$ : int [1:2282] -843365 743147 -958 207 -958 206 -958 208 -958 207 ...

My Problem: From here, I am stuck - I do not know how to extract the geographical coordinates of the polygons from LTE_YE2019 - and then plot/shade these polygons on a map using leaflet. If I could figure this out, I could then repeat this for LTE_YE2018, LTE_YE2017, LTE_YE2016, etc.

leaflet() %>%
    addTiles() %>%
    addPolygons(data = LTE_YE2019)

Can someone please show me how to do this?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 1
    Package {sf} might make this quite straightforward, see, e.g. here: https://stackoverflow.com/questions/64602323/read-geojson-file-using-sf-library – I_O Jun 18 '23 at 22:17
  • @ I_O: thank you for pointing this out! I will read more about this ... – stats_noob Jun 18 '23 at 22:19
  • Glad it helped, I use {sf} *a lot* thanks to its simplicity. It has its own `geom_sf` to go along with ggplot; for linking it to leaflet, see here: https://stackoverflow.com/questions/61519042/plotting-sf-type-object-in-leaflet-r – I_O Jun 18 '23 at 22:22
  • I made a bit more progress on this question - I posted my progress here https://stackoverflow.com/questions/76502742/r-comparing-the-speed-of-geospatial-functions . Can you please check it out if you have time? – stats_noob Jun 18 '23 at 22:23

0 Answers0