# sf uses the geojson driver, based on the file extension
# (data from moodle → Spatial Databases II → Database Files)
stationen_schweiz <- read_sf("data/uebung1/stationen_schweiz.geojson")Vector data I/O
- The library
{sf}can import data from various sources:- A multitude of file formats from disk (powered
gdal/ogr2ogr) - Various geospatial databases such as
PostGIS - From other sources (such as
WKT,WKBother geospatial R-libraires)
- A multitude of file formats from disk (powered
- We will only introduce the first method, since this is the most common use case
GDAL / ogr2ogr
GDAL:
- is an open source translator library for raster and vector geospatial data formats.
- stands for Geospatial Data Abstraction Library
- is used in most geospatial software, be it FOSS or proprietary. The list includes: ArcGIS, QGIS, R (
sf) and Python (geopandas)
GDAL Vector drivers (read)
- Since GDAL supports a long list of different geospatial file formats, most (all?) are in turn supported by
{sf} - Run the function
st_driversfor a full list {sf}tries to guess the correct driver based on the file extension (see below)
# sf uses the shapefile driver, based on the file extension
# (data from moodle → Spatial Analysis I → Exercise Data)
hoheitsgebiet <- read_sf("data/Spatial_Analysis_I/Hoheitsgebiet/HOHEITSGEBIET_FR.shp")Multiple layers in one file
- Some file formats, e.g. Geopackages (*.gpkg) or Geodatabases (*.gdb) support multiple datasets in a single file.
- If no specific layer is requested
read_sf()will import the first available layer - If more than 1 layers are available,
read_sf()will return a warning - The function
st_layers()will list all available layers
tlm3d_path <- "data/Spatial_Analysis_II/swiss_TLM3D.gpkg"
# Note the warning:
tlm3d <- read_sf(tlm3d_path)Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
automatically selected the first layer in a data source containing more than
one.
# This will list all layers, including some metadata
st_layers(tlm3d_path)Driver: GPKG
Available layers:
layer_name geometry_type features fields crs_name
1 tlm_bb 3D Multi Polygon 49321 13 CH1903+ / LV95
2 tlm_strassen 3D Multi Line String 191644 27 CH1903+ / LV95
# To import a single layer
tlm_bb <- read_sf(tlm3d_path, "tlm_bb")SQL queries during import
read_sf()can parse a (spatialite) SQL query provided in thequery =argument- In case
query =is used, thelayers =argument should be skipped (since the layers is specified in the query)
tlm_seen <- read_sf(
tlm3d_path,
query = "SELECT objektart, geom FROM tlm_bb WHERE objektart = 'Stehende Gewaesser'"
)GDAL Vector drivers (write)
- The function
st_write()is used to export ansfobject to file - Most vector drivers support reading and writing (see
st_drivers/ columnwrite) - Many file formats support appending to the dataset (see
append =)
st_write(tlm_seen, "data-out/seen.geojson")🪕 Tasks
- Import the datasets from Spatial Analysis I and II using R
- Explore these datasets by printing the
sfobjects to the console - Try different visualization methods (see Plotting simple features)