In this mini-workshop we will introduce the sf package, show some examples of geospatial analysis, work with base plotting of sf objects, and show how mapview can be used to map these objects. It is assumed that you have R and RStudio installed and that you, at a minimum, understand the basic concepts of the R language (e.g. you can work through R For Cats).

Also as an aside, I am learning the sf package right now so, we will be learning all of this together!

The sf package

Things are changing quickly in the R/spatial analysis world and the most fundamental change is via the sf package. This package aims to replace sp, rgdal, and rgeos. There are a lot of reasons why this is a good thing, but that is a bit beyond the scope of this workshop Suffice it to say it should make things faster and simpler!

To get started, lets get sf installed:

install.packages("sf")
library("sf")

sf does rely on having access the GDAL, GEOS, and Proj.4 libraries. On Windows and Mac this should be pretty straightforward if we are installing from CRAN (which we are). If you use linux, you know nothing is straightforward and you are on your own!

Exercise 1

The first exercise won’t be too thrilling, but we need to make sure everyone has the packages installed.

  1. Install sf.
  2. Load sf.
  3. If you don’t have dplyr already, make sure it is installed.
  4. Load dplyr.

Reading in spatial data with sf

Simple Features

So, what does sf actually provide us? It is an implementation of an ISO standard for storing spatial data. It forms the basis for many of the common vector data models and is centered on the concept of a “feature”. Essentially a feature is any object in the real world. There are many different types of features and there are different details that get stored about each. The first sf vignette does a really nice job of explaining the details. For this mini-workshop we are going to focus on three feature types, POINT, LINESTRING, and POLYGON. For each of the types, there will be coordinates stored as dimensions, a coordinate reference system, and attributes.

Get some data to use

We can grab some data directly from the Rhode Island Geographic Information System (RIGIS) for these examples. This code assumes you have a data folder in your current workspace. Create one if you need it.

# Create a data folder if it doesn't exist, and yes R can do that
if(!dir.exists("data")){dir.create("data")}

# Municipal Boundaries
download.file(url = "http://www.rigis.org/geodata/bnd/muni97d.zip",
              destfile = "data/muni97d.zip")
unzip(zipfile = "data/muni97d.zip", 
      exdir = "data")

# Streams
download.file(url = "http://www.rigis.org/geodata/hydro/streams.zip",
              destfile = "data/streams.zip")
unzip(zipfile = "data/streams.zip", 
      exdir = "data")

# Potential Growth Centers
download.file(url = "http://www.rigis.org/geodata/plan/growth06.zip",
              destfile = "data/growth06.zip")
unzip(zipfile = "data/growth06.zip", 
      exdir = "data")

# Land Use/Land Cover
download.file(url = "http://www.rigis.org/geodata/plan/rilc11d.zip",
              destfile = "data/rilc11d.zip")
unzip(zipfile = "data/rilc11d.zip", 
      exdir = "data")

Data input

To pull the shapefiles in we can simply use the st_read() function. This will create an object which is a simple feature collection of, in our case, POINT, LINESTRING, or POLYGON. As an aside, many of the sf functions and all of the ones we will be using start with st_. This stands for “spatial” and “temporal”. Take a look below for examples reading in each of our datasets.

POINT

growth_cent <- st_read("data/growth06.shp")
## Reading layer `growth06' from data source `/home/jhollist/projects/geospatial_with_sf/data/growth06.shp' using driver `ESRI Shapefile'
## Simple feature collection with 21 features and 2 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 260137.3 ymin: 32916.7 xmax: 418116.3 ymax: 326549.2
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=41.08333333333334 +lon_0=-71.5 +k=0.99999375 +x_0=99999.99999999999 +y_0=0 +datum=NAD83 +units=us-ft +no_defs

LINESTRING

streams <- st_read("data/streams.shp")
## Reading layer `streams' from data source `/home/jhollist/projects/geospatial_with_sf/data/streams.shp' using driver `ESRI Shapefile'
## Simple feature collection with 4470 features and 8 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: 234010.1 ymin: 31361.37 xmax: 430921.9 ymax: 340865.8
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=41.08333333333334 +lon_0=-71.5 +k=0.99999375 +x_0=99999.99999999999 +y_0=0 +datum=NAD83 +units=us-ft +no_defs

POLYGON

muni <- st_read("data/muni97d.shp")
## Reading layer `muni97d' from data source `/home/jhollist/projects/geospatial_with_sf/data/muni97d.shp' using driver `ESRI Shapefile'
## Simple feature collection with 396 features and 12 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 220310.4 ymin: 23048.49 xmax: 432040.9 ymax: 340916.6
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=41.08333333333334 +lon_0=-71.5 +k=0.99999375 +x_0=99999.99999999999 +y_0=0 +datum=NAD83 +units=us-ft +no_defs
lulc <- st_read("data/rilc11d.shp")
## Reading layer `rilc11d' from data source `/home/jhollist/projects/geospatial_with_sf/data/rilc11d.shp' using driver `ESRI Shapefile'
## Simple feature collection with 68186 features and 5 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 218380.4 ymin: 23048.5 xmax: 434592 ymax: 343508
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=41.08333333333334 +lon_0=-71.5 +k=0.99999375 +x_0=99999.99999999999 +y_0=0 +datum=NAD83 +units=us-ft +no_defs

Other data

We won’t have time during this mini-workshop to look at reading in other data formats, but since sf uses GDAL it can read all of the files for which you have drivers.

drvrs <- st_drivers()
drvrs$long_name
##  [1] PCIDSK Database File                                
##  [2] Network Common Data Format                          
##  [3] JPEG-2000 driver based on OpenJPEG library          
##  [4] Geospatial PDF                                      
##  [5] ESRI Shapefile                                      
##  [6] MapInfo File                                        
##  [7] UK .NTF                                             
##  [8] SDTS                                                
##  [9] IHO S-57 (ENC)                                      
## [10] Microstation DGN                                    
## [11] VRT - Virtual Datasource                            
## [12] EPIInfo .REC                                        
## [13] Memory                                              
## [14] Atlas BNA                                           
## [15] Comma Separated Value (.csv)                        
## [16] NAS - ALKIS                                         
## [17] Geography Markup Language (GML)                     
## [18] GPX                                                 
## [19] Keyhole Markup Language (LIBKML)                    
## [20] Keyhole Markup Language (KML)                       
## [21] GeoJSON                                             
## [22] Interlis 1                                          
## [23] Interlis 2                                          
## [24] GMT ASCII Vectors (.gmt)                            
## [25] GeoPackage                                          
## [26] SQLite / Spatialite                                 
## [27] OGR_DODS                                            
## [28] ODBC                                                
## [29] WAsP .map format                                    
## [30] ESRI Personal GeoDatabase                           
## [31] Microsoft SQL Server Spatial Database               
## [32] OGDI Vectors (VPF, VMAP, DCW)                       
## [33] PostgreSQL/PostGIS                                  
## [34] MySQL                                               
## [35] ESRI FileGDB                                        
## [36] X-Plane/Flightgear aeronautical data                
## [37] AutoCAD DXF                                         
## [38] Geoconcept                                          
## [39] GeoRSS                                              
## [40] GPSTrackMaker                                       
## [41] Czech Cadastral Exchange Data Format                
## [42] PostgreSQL SQL dump                                 
## [43] OpenStreetMap XML and PBF                           
## [44] GPSBabel                                            
## [45] Tim Newport-Peace's Special Use Airspace Format     
## [46] OpenAir                                             
## [47] Planetary Data Systems TABLE                        
## [48] OGC WFS (Web Feature Service)                       
## [49] Hydrographic Transfer Vector                        
## [50] Aeronav FAA                                         
## [51] Geomedia .mdb                                       
## [52] French EDIGEO exchange format                       
## [53] Google Fusion Tables                                
## [54] Scalable Vector Graphics                            
## [55] CouchDB / GeoCouch                                  
## [56] Cloudant / CouchDB                                  
## [57] Idrisi Vector (.vct)                                
## [58] Arc/Info Generate                                   
## [59] SEG-P1 / UKOOA P1/90                                
## [60] SEG-Y                                               
## [61] MS Excel format                                     
## [62] Open Document/ LibreOffice / OpenOffice Spreadsheet 
## [63] MS Office Open XML spreadsheet                      
## [64] Elastic Search                                      
## [65] Walk                                                
## [66] Carto                                               
## [67] AmigoCloud                                          
## [68] Storage and eXchange Format                         
## [69] Selafin                                             
## [70] OpenJUMP JML                                        
## [71] Planet Labs Scenes API                              
## [72] OGC CSW (Catalog  Service for the Web)              
## [73] VDV-451/VDV-452/INTREST Data Format                 
## [74] U.S. Census TIGER/Line                              
## [75] Arc/Info Binary Coverage                            
## [76] Arc/Info E00 (ASCII) Coverage                       
## [77] HTTP Fetching Wrapper                               
## 201 Levels: ACE2 Aeronav FAA AirSAR Polarimetric Image ... ZMap Plus Grid

Additionally, you if you have a tabular dataset with coordinates, you can create an sf object with those. An example using EPA’s National Lakes Assessment data:

nla_url <- "https://www.epa.gov/sites/production/files/2016-12/nla2012_wide_siteinfo_08232016.csv"
nla_stations <- read.csv(nla_url, stringsAsFactors = FALSE)
nla_sf <- st_as_sf(nla_stations, coords = c("LON_DD83", "LAT_DD83"), crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

(HT to Mike Treglia for the suggestion!)

A word on performance

One of the benefits of using sf is the speed. In my tests it is about twice as fast as the prior standard of sp and rgdal. Let’s look at a biggish shape file with 1 million points!