Working with spatial data from the portal

There is a ton of spatial data on the City of Toronto Open Data Portal. Spatial resources are retrieved the same way as all other resources, by using get_resource(), and may require the sf and geojsonsf packages.

We can look at bicycle parking in Toronto. The result is an sf object with WGS84 projection.

library(opendatatoronto)
library(dplyr)

bike_parking_racks <- search_packages("Bicycle Parking Racks") %>%
  list_package_resources() %>%
  filter(name == "Bicycle Parking Racks Data") %>%
  get_resource()

bike_parking_racks
#> Simple feature collection with 241 features and 30 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: -79.59575 ymin: 43.60367 xmax: -79.25724 ymax: 43.82303
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#> # A tibble: 241 x 31
#>    `_id` ADDRESS_POINT_ID ADDRESS_NUMBER LINEAR_NAME_FULL ADDRESS_FULL
#>    <int>            <int> <chr>          <chr>            <chr>       
#>  1  4097         10335148 538            Lansdowne Ave    538 Lansdow…
#>  2  3857            51630 5              Bartonville Ave… 5 Bartonvil…
#>  3  3858           310564 150            Borough Dr       150 Borough…
#>  4  3859           367443 71             New Forest Sq    71 New Fore…
#>  5  3860           379258 95             River Grove Dr   95 River Gr…
#>  6  3861           394585 24             Victoria Park A… 24 Victoria…
#>  7  3862           772775 315            Bloor St W       315 Bloor S…
#>  8  3863           772800 398            Bloor St W       398 Bloor S…
#>  9  3864           772833 481            Bloor St W       481 Bloor S…
#> 10  3865           772852 509            Bloor St W       509 Bloor S…
#> # … with 231 more rows, and 26 more variables: POSTAL_CODE <chr>,
#> #   MUNICIPALITY <chr>, CITY <chr>, CENTRELINE_ID <int>, LO_NUM <int>,
#> #   LO_NUM_SUF <chr>, HI_NUM <int>, HI_NUM_SUF <lgl>,
#> #   LINEAR_NAME_ID <int>, WARD_NAME <chr>, X <dbl>, Y <dbl>,
#> #   LONGITUDE <dbl>, LATITUDE <dbl>, MI_PRINX <int>, OBJECTID <int>,
#> #   CAPACITY <int>, MULTIMODAL <chr>, SEASONAL <chr>, SHELTERED <chr>,
#> #   SURFACE <chr>, STATUS <chr>, LOCATION <chr>, NOTES <chr>,
#> #   MAP_CLASS <chr>, geometry <POINT [°]>

If we want to plot this data on a map of Toronto, data to map the different neighbourhoods of Toronto is also available from the portal!

neighbourhoods <- list_package_resources("https://open.toronto.ca/dataset/neighbourhoods/") %>%
  get_resource()

neighbourhoods[c("AREA_NAME", "geometry")]
#> Simple feature collection with 140 features and 1 field
#> geometry type:  POLYGON
#> dimension:      XY
#> bbox:           xmin: -79.63926 ymin: 43.581 xmax: -79.11545 ymax: 43.85546
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#> # A tibble: 140 x 2
#>    AREA_NAME                                                       geometry
#>    <chr>                                                      <POLYGON [°]>
#>  1 Wychwood (94)        ((-79.43592 43.68015, -79.43492 43.68037, -79.4339…
#>  2 Yonge-Eglinton (100) ((-79.41096 43.70408, -79.40962 43.70436, -79.4085…
#>  3 Yonge-St.Clair (97)  ((-79.39119 43.68108, -79.39141 43.68097, -79.3932…
#>  4 York University Hei… ((-79.50529 43.75987, -79.50488 43.75996, -79.5049…
#>  5 Yorkdale-Glen Park … ((-79.43969 43.70561, -79.44011 43.70559, -79.4410…
#>  6 Lambton Baby Point … ((-79.50552 43.66281, -79.50577 43.66291, -79.5061…
#>  7 Lansing-Westgate (3… ((-79.43998 43.76156, -79.44004 43.76177, -79.4404…
#>  8 Lawrence Park North… ((-79.39008 43.72768, -79.39199 43.72726, -79.3939…
#>  9 Lawrence Park South… ((-79.41096 43.70408, -79.41165 43.70394, -79.4120…
#> 10 Leaside-Bennington … ((-79.37749 43.71309, -79.37762 43.71385, -79.3779…
#> # … with 130 more rows

Then, we can plot the bike racks along with a map of Toronto:

library(ggplot2)

ggplot() +
  geom_sf(data = neighbourhoods[["geometry"]]) +
  geom_sf(data = bike_parking_racks) +
  theme_minimal()