Vector data I/O

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_drivers for a full list
  • {sf} tries to guess the correct driver based on the file extension (see below)
# 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")
# 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")
  • Note: The read_sf() function is a wrapper around the st_read() function, which is the actual function that reads the data. I use read_sf since this wrapper’s default value for the quiet argument is TRUE (less verbose)

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
tlm_bb <- read_sf(tlm3d_path, "tlm_bb")

SQL queries during import

  • read_sf() understands an Spatialite SQL query provided in the query = argument
  • In case query = is used, the layers = 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 an sf object to file
  • Most vector drivers support reading and writing (see st_drivers / column write)
  • Many file formats support appending to the dataset (see append =)
st_write(tlm_seen, "data-out/seen.geojson")
Writing layer `seen' to data source `data-out/seen.geojson' using driver `GeoJSON'
Writing 1189 features with 1 fields and geometry type 3D Multi Polygon.

🪕 Tasks

  1. Import the datasets from Spatial Analysis I and II using R
  2. Explore these datasets by printing the sf objects to the console
  3. Try different visualization methods (see Plotting simple features)