+ - 0:00:00
Notes for current slide
Notes for next slide

Working with Geospatial Data

sf

Ben Baumer

SDS 192
April 6th, 2020
(http://beanumber.github.io/sds192/lectures/mdsr_geo_01-sf.html)

1 / 9

sf

2 / 9

sf is the new hotness

http://r-spatial.github.io/sf/

  • supersedes older sp package

    • bundles functions from rgdal and rgeos
  • spatial objects have class sf...

    • ...but also data.frame!!
  • tidyverse-compatible

    • dplyr, tidyr verbs
  • ggplot2 support

3 / 9
4 / 9

How does it work?

https://github.com/r-spatial/sf

library(sf)
## Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
5 / 9

How does it work?

https://github.com/r-spatial/sf

library(sf)
## Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
nc <- system.file("shape/nc.shp", package = "sf") %>%
st_read()
## Reading layer `nc' from data source `/home/bbaumer/R/x86_64-pc-linux-gnu-library/3.6/sf/shape/nc.shp' using driver `ESRI Shapefile'
## Simple feature collection with 100 features and 14 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## CRS: 4267
5 / 9

How does it work?

https://github.com/r-spatial/sf

library(sf)
## Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
nc <- system.file("shape/nc.shp", package = "sf") %>%
st_read()
## Reading layer `nc' from data source `/home/bbaumer/R/x86_64-pc-linux-gnu-library/3.6/sf/shape/nc.shp' using driver `ESRI Shapefile'
## Simple feature collection with 100 features and 14 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## CRS: 4267
class(nc)
## [1] "sf" "data.frame"
5 / 9

What does it look like?

https://edzer.github.io/rstudio_conf/#11

nc %>%
select(AREA, NAME, geometry)
## Simple feature collection with 100 features and 2 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## CRS: 4267
## First 10 features:
## AREA NAME geometry
## 1 0.114 Ashe MULTIPOLYGON (((-81.47276 3...
## 2 0.061 Alleghany MULTIPOLYGON (((-81.23989 3...
## 3 0.143 Surry MULTIPOLYGON (((-80.45634 3...
## 4 0.070 Currituck MULTIPOLYGON (((-76.00897 3...
## 5 0.153 Northampton MULTIPOLYGON (((-77.21767 3...
## 6 0.097 Hertford MULTIPOLYGON (((-76.74506 3...
## 7 0.062 Camden MULTIPOLYGON (((-76.00897 3...
## 8 0.091 Gates MULTIPOLYGON (((-76.56251 3...
## 9 0.118 Warren MULTIPOLYGON (((-78.30876 3...
## 10 0.124 Stokes MULTIPOLYGON (((-80.02567 3...
6 / 9

Every sf object has a geometry

nc %>%
st_geometry() %>%
pluck(1)
## MULTIPOLYGON (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.27359, -81.63306 36.34069, -81.74107 36.39178, -81.69828 36.47178, -81.7028 36.51934, -81.67 36.58965, -81.3453 36.57286, -81.34754 36.53791, -81.32478 36.51368, -81.31332 36.4807, -81.26624 36.43721, -81.26284 36.40504, -81.24069 36.37942, -81.23989 36.36536, -81.26424 36.35241, -81.32899 36.3635, -81.36137 36.35316, -81.36569 36.33905, -81.35413 36.29972, -81.36745 36.2787, -81.40639 36.28505, -81.41233 36.26729, -81.43104 36.26072, -81.45289 36.23959, -81.47276 36.23436)))
7 / 9

Can we see it?

nc %>%
select("AREA", geometry) %>%
plot()

8 / 9

ggplot2 support

  • Use geom_sf()

  • mapping (x, y) to geometry is implied!

ggplot(nc, aes(fill = AREA)) +
geom_sf()

9 / 9

sf

2 / 9
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow