How to import kml file into R and make nice table

The sf package is a really great way to work with spatial data in R. You can use the st_read() function to import many spatial file formats like KML, GeoJSON, and shapefiles into R.

One great thing about the sf package is that you can use tidyverse tools like dplyr to join and manipulate the spatial data in the same way you work with non-spatial data frames in R. Below, I import the CCG region KML file and then plot it.

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3

ccg <- st_read("https://opendata.arcgis.com/datasets/de108fe659f2430d9558c20fe172638f_2.kml")
#> Reading layer `OGRGeoJSON' from data source `https://opendata.arcgis.com/datasets/de108fe659f2430d9558c20fe172638f_2.kml' using driver `KML'
#> Simple feature collection with 207 features and 2 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -6.418524 ymin: 49.86474 xmax: 1.762942 ymax: 55.81107
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs

plot(ccg[1])

Created on 2018-12-07 by the reprex package (v0.2.1)

2 Likes