Let’s get started with some hands-on exercises and exploring some data! This exercise focuses on importing different vector data geometries from various data sources and getting used to simple feature data tables.

In the folder data, you can find the data files we prepped for all the exercises which rely on preexisting data sets. However, if you like to play around with your own data, feel free to do so! .

1

Load the administrative borders of Germany. Therefore, import the shapefile “GER_COUNTRY” from the respective folder and assign it to an object named “germany”.

Plot the newly created object.

Don’t forget to load the packages “sf” and set your working directory before starting this exercise.
library(sf)
## Warning: package 'sf' was built under R version 4.0.3
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
germany <- sf::st_read(dsn = "../data",
                  layer = "GER_COUNTRY") 
## Reading layer `GER_COUNTRY' from data source `C:\Users\annes\Documents\gesis-workshop-geospatial-techniques-R\data' using driver `ESRI Shapefile'
## Simple feature collection with 11 features and 4 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 5.86625 ymin: 47.27012 xmax: 15.04182 ymax: 55.09917
## geographic CRS: ETRS89
plot(germany)

The output in the console of the RStudio interface gives you already some information on the data you just loaded. You also see that the object “germany” appeared in your environment. Though, we will explore the data set a little bit closer.

2

Explore the data and answer the following questions:

  • What is the geometry type?
  • What is the id of the coordinate reference system?
  • Which kind of class is the object?
  • What is the name of the column containing the information on geometries?
  • Which attributes are assigned to the geometries?
  • What was the population size in Germany in 2017?
sf::st_geometry(germany) # Multipolygon: Set of several Polygons. Each row is a polygon.
## Geometry set for 11 features 
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 5.86625 ymin: 47.27012 xmax: 15.04182 ymax: 55.09917
## geographic CRS: ETRS89
## First 5 geometries:
## MULTIPOLYGON (((12.10372 54.1676, 12.10271 54.1...
## MULTIPOLYGON (((13.5847 54.48249, 13.5868 54.48...
## MULTIPOLYGON (((9.108044 47.68539, 9.109004 47....
## MULTIPOLYGON (((13.34737 54.18185, 13.34797 54....
## MULTIPOLYGON (((9.18314 53.86462, 9.183156 53.8...
class(germany) # "sf" and "data.frame"
## [1] "sf"         "data.frame"
attr(germany, "sf_column") # geometry
## [1] "geometry"
sf::st_crs(germany) # ETRS89, ID = EPSG:4258
## Coordinate Reference System:
##   User input: ETRS89 
##   wkt:
## GEOGCRS["ETRS89",
##     DATUM["European Terrestrial Reference System 1989",
##         ELLIPSOID["GRS 1980",6378137,298.257222101,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["Europe - ETRS89"],
##         BBOX[32.88,-16.1,84.17,40.18]],
##     ID["EPSG",4258]]
head(germany) # Name, Area Type, Area Size, Population Size
## Simple feature collection with 6 features and 4 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 5.86625 ymin: 47.27012 xmax: 15.04182 ymax: 55.05838
## geographic CRS: ETRS89
##          name area_type area_size population                       geometry
## 1 Deutschland         4  357582.4   82792351 MULTIPOLYGON (((12.10372 54...
## 2 Deutschland         3       0.0          0 MULTIPOLYGON (((13.5847 54....
## 3 Deutschland         2       0.0          0 MULTIPOLYGON (((9.108044 47...
## 4 Deutschland         2       0.0          0 MULTIPOLYGON (((13.34737 54...
## 5 Deutschland         2       0.0          0 MULTIPOLYGON (((9.18314 53....
## 6 Deutschland         1       0.0          0 MULTIPOLYGON (((8.992169 47...
table(germany$population) # 82,792,351 inhabitants
## 
##        0 82792351 
##       10        1

3

Do you have an idea why there are 11 observations instead of only one, even though we expected a shapefile with a polygon of just Germany? Create a new object which contains only one observation holding information on area size and population of Germany.

# It seems like the shapefile contains not only the land area of Germany but also coastal areas and lakes.

# filter 
germany_new <-
  germany %>% 
  dplyr::filter(. , area_size != 0)

germany_new
## Simple feature collection with 1 feature and 4 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 5.86625 ymin: 47.27012 xmax: 15.04182 ymax: 55.05838
## geographic CRS: ETRS89
##          name area_type area_size population                       geometry
## 1 Deutschland         4  357582.4   82792351 MULTIPOLYGON (((12.10372 54...