# sf uses the geojson driver, based on the file extension
# (data from moodle → Spatial Databases II → Database Files)
<- read_sf("data/uebung1/stationen_schweiz.geojson") stationen_schweiz
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
,WKB
other 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_drivers
for 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)
<- read_sf("data/Spatial_Analysis_I/Hoheitsgebiet/HOHEITSGEBIET_FR.shp") hoheitsgebiet
- Note: The
read_sf()
function is a wrapper around thest_read()
function, which is the actual function that reads the data. I useread_sf
since this wrapper’s default value for thequiet
argument isTRUE
(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
<- "data/Spatial_Analysis_II/swiss_TLM3D.gpkg"
tlm3d_path
# Note the warning:
<- read_sf(tlm3d_path) tlm3d
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
<- read_sf(tlm3d_path, "tlm_bb") tlm_bb
SQL queries during import
read_sf()
understands an 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)
<- read_sf(
tlm_seen
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 ansf
object 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")
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
- Import the datasets from Spatial Analysis I and II using R
- Explore these datasets by printing the
sf
objects to the console - Try different visualization methods (see Plotting simple features)