Importing and Plotting Vector Data
sf
Packagesf
is a relatively new package for working with vector data. It combines the functionality of the older sp
package, as well as select functionality from rgdal
and rgeos
.
sf
stands for ‘simple features’, which is a standard (developed by the Open Geospatial Consortium) for storing various geometry types in a hierarchical data model.
A ‘feature’ is just a representation of a thing in the real world (e.g. a building, a city…).
sf
offers numerous specific benefits, including:
To import spatial data, you have to specify a source and layer.
The Source can be a:
folder: "./gis_data"
geodatabase (which is really a folder): "./gis_data/yose_roads.gdb"
file: "trail_heads.kml"
, "cell_towers.geojson"
database connection string: "PG:dbname=postgis"
In sf
functions, the argument where you provide the source is often named dsn
(data source name)
The Layer can be a:
Shapefile name (minus the shp extension)
a named layer in the database / file
The two main functions for importing vector data are:
st_layers(source) - returns the names of available layers in a source
st_read(source, layer) - import into R
Most of the functions in the sf package start with st_
, which stands for ‘space time’, and matches the names of similar functions in PostGIS.
To view the metadata of a layer before bringing it into R, use rgdal::ogrInfo()
st_read()
:source - shp filename
layer - can be omitted
## Reading layer `yose_boundary' from data source `D:\Workshops\R-Spatial\rspatial_mod\outputs\rspatial_data\data' using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 11 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -119.8864 ymin: 37.4947 xmax: -119.1964 ymax: 38.18515
## Geodetic CRS: North_American_Datum_1983
## Simple feature collection with 1 feature and 11 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -119.8864 ymin: 37.4947 xmax: -119.1964 ymax: 38.18515
## Geodetic CRS: North_American_Datum_1983
## UNIT_CODE GIS_NOTES UNIT_NAME DATE_EDIT STATE REGION GNIS_ID
## 1 YOSE Lands - http://landsnet.nps.gov/tractsnet/documents/YOSE/METADATA/yose_metadata.xml Yosemite National Park 2016-01-27 CA PW 255923
## UNIT_TYPE CREATED_BY METADATA PARKNAME geometry
## 1 National Park Lands http://nrdata.nps.gov/programs/Lands/YOSE_METADATA.xml Yosemite POLYGON ((-119.8456 37.8327...
st_read()
(at least not on Windows). Unzip it first then import the doc.kml filest_read()
can not currently import the attribute table from a kml file on Windows (more info)st_read()
:source - kml filename
layer - name of a layer (required)
st_read()
:source - geojson filename
layer - named layer (optional if only 1 layer exists)
st_read()
:source - folder containing the geodatabase
layer - named layer (required)
GeoPackage is a relatively new open format for geospatial data. It is similar to a file geodatabase in that:
The GeoPackage is different from a file geodatabase in that GeoPackages are:
st_read()
:
source - .gpkg filename
layer - named layer (required)
SpatiaLite - see RSQLite.spatialite
The workshop datasets descriptions includes sample import code for most of the layers.
To plot just the geometry of a sf object (i.e., no symbology from the attribute table), use st_geometry()
:
yose_bnd_ll <- st_read(dsn="./data", layer="yose_boundary", quiet=TRUE)
## Plot the geometry (outline) of the Yosemite boundary
plot(yose_bnd_ll %>% st_geometry(), asp=1)
We add %>% st_geometry()
to indicate we want to plot the polygon with a single color/fill (more on that later).
The asp=1
argument sets the aspect ratio. It is optional but generally a good idea for spatial data.
To overlay more than one layer on a plot:
plot()
does not reproject on the fly)add=TRUE
to the second (and all subsequent) plot() statementsTo turn a data frame into a sf object, use
st_as_sf(df, coords, crs)
where:
quakes
is a dataframe that comes with R (in the datasets package). Here we turn it into a sf data frame.
## Make a sf object from the built-in 'quakes' dataset
head(quakes)
quakes_sf <- st_as_sf(quakes, coords=c("long", "lat"), crs=4326)
plot(quakes_sf %>% st_geometry(), axes = TRUE, asp = 1)
## lat long depth mag stations
## 1 -20.42 181.62 562 4.8 41
## 2 -20.62 181.03 650 4.2 15
## 3 -26.00 184.10 42 5.4 43
## 4 -17.97 181.66 626 4.1 19
## 5 -20.42 181.96 649 4.0 11
## 6 -19.68 184.31 195 4.0 12
If you don’t know the datum for geographic coordinates, use WGS84 (epsg 4326).
nb_import-vector_scgis21.Rmd
Import all kinds of vector data into R
sf is relatively new, and many R packages still use the older data classes from the sp package.
Fortunately it’s relatively easy to convert back and forth:
## Loading required package: sp
## Checking rgeos availability: TRUE
data(wrld_simpl)
class(wrld_simpl)
## Convert sp to sf
wrld_simpl_sf <- sf::st_as_sf(wrld_simpl)
class(wrld_simpl_sf)
## Plot the sf object
plot(wrld_simpl_sf["REGION"])
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
## [1] "sf" "data.frame"
In this section we saw how to:
st_layers()
views the layer namesst_read()
imports the layerplot()
for basic plottingplot(..., add=TRUE)
to overlay additional layers with the same CRSst_as_Sf()
Additional Resources
Importing spatial data with rgdal
, by Zev Ross
Plotting Simple Features, sf package Vignette