Skip to content

Commit

Permalink
Revise episode 9
Browse files Browse the repository at this point in the history
  • Loading branch information
cforgaci committed Jan 15, 2024
1 parent 6b6ff74 commit b32f72c
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions episodes/09-open-and-plot-vector-layers.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ exercises: 5

:::::::::::::::::::::::::::::::::::::: questions

- How can I read, examine and visualize point, line and polygon vector data in R?
- How can you read, examine and visualize point, line and polygon vector data in R?

::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::: objectives

- Know the difference between point, line, and polygon vector data.
After completing this episode, participants should be able to…

- Differentiate between point, line, and polygon vector data.
- Load vector data into R.
- Access the attributes of a vector object in R.

Expand All @@ -21,11 +23,11 @@ exercises: 5
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: instructor

Make sure that the `sf` package and its dependencies are installed before the
workshop. The installation can take quite some time, so allocate enough extra
time before the workshop for solving installation problems. We recommend one
or two installation 'walk-in' hours on a day before the workshop and 15-30
workshop. The installation can be lengthy, so allocate enough extra time
before the workshop for solving installation problems. We recommend one
or two installation 'walk-in' hours on a day before the workshop. Also, 15-30
minutes at the beginning of the first workshop day should be enough to tackle
installation issues.
last-minute installation issues.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Expand All @@ -38,24 +40,24 @@ If you have not installed the `sf` package yet, run `install.packages("sf")` fir
First we need to load the packages we will use in this lesson. We will use the package `tidyverse` with which you are already familiar from the previous lesson. In addition, we need to load the [`sf`](https://r-spatial.github.io/sf/) package for working with spatial vector data.

```{r lib, message=FALSE}
library(tidyverse) # tools for wrangling, reshaping and visualizing data
library(tidyverse) # wrangle, reshape and visualize data
library(sf) # work with spatial vector data
```

::: callout

# The `sf` package

`sf` stands for Simple Features which is a standard defined by the Open Geospatial Consortium for storing and accessing geospatial vector data. PostGIS uses the same standard; so those of you who used PostGIS, might find some resemblances with the functions used by the `sf` package.
`sf` stands for Simple Features which is a standard defined by the Open Geospatial Consortium for storing and accessing geospatial vector data. PostGIS, for instance, uses the same standard, so PostGIS users might recognise the functions used by the `sf` package.

:::

## Import shapefiles

Let's start by opening a shapefile. Shapefiles are a common file format to store spatial vector data used in GIS software. We will read a shapefile with the administrative boundary of Delft with the function `st_read()` from the `sf` package.

```{r results='hide'}
boundary_Delft <- st_read("data/delft-boundary.shp")
```{r}
boundary_Delft <- st_read("data/delft-boundary.shp", quiet = TRUE)
```

::: callout
Expand All @@ -71,13 +73,15 @@ Note that all functions from the `sf` package start with the standard prefix `st

## Spatial Metadata

The `st_read()` function gave us a message with a summary of metadata about the file that was read in. To examine the metadata in more detail, we can use other, more specialised, functions from the `sf` package. The `st_geometry_type()` function, for instance, gives us information about the geometry type, which in this case is `POLYGON`.
By default (with `quiet = FALSE`), the `st_read()` function provides a message with a summary of metadata about the file that was read in. To examine the metadata in more detail, we can use other, more specialised, functions from the `sf` package.

The `st_geometry_type()` function, for instance, gives us information about the geometry type, which in this case is `POLYGON`.

```{r}
st_geometry_type(boundary_Delft)
```

The `st_crs()` function returns the coordinate reference system (CRS) used by the shapefile, which in this case is `WGS84` and has the unique reference code `EPSG: 4326`.
The `st_crs()` function returns the coordinate reference system (CRS) used by the shapefile, which in this case is ``r st_crs(boundary_Delft)$Name`` and has the unique reference code ``r paste0("EPSG: ", st_crs(boundary_Delft)$epsg)``.

```{r}
st_crs(boundary_Delft)
Expand All @@ -91,7 +95,7 @@ As the output of `st_crs()` can be long, you can use `$Name` and `$epsg` after t

:::

The `st_bbox()` function shows the extent of the layer. As `WGS84` is a **geographic CRS**, the extent of the shapefile is displayed in degrees.
The `st_bbox()` function shows the extent of the layer. As `WGS 84` is a **geographic CRS**, the extent of the shapefile is displayed in degrees.

```{r}
st_bbox(boundary_Delft)
Expand All @@ -101,13 +105,15 @@ We need a **projected CRS**, which in the case of the Netherlands is typically t

```{r}
boundary_Delft <- st_transform(boundary_Delft, 28992)
st_crs(boundary_Delft)
st_crs(boundary_Delft)$Name
st_crs(boundary_Delft)$epsg
```

Notice that the bounding box is measured in meters after the transformation.

```{r}
st_bbox(boundary_Delft)
st_crs(boundary_Delft)$units_gdal
```

We confirm the transformation by examining the reprojected shapefile.
Expand All @@ -118,30 +124,28 @@ boundary_Delft

::: callout

More about CRS in [Handling Spatial Projection & CRS]().
Read more about Coordinate Reference Systems [here](https://datacarpentry.org/organization-geospatial/03-crs.html). We will also practice transformation between CRS in [Handling Spatial Projection & CRS](../episodes/12-handling-spatial-projection-and-crs.Rmd).

:::



## Plot a vector layer

Now, let's plot this shapefile. You are already familiar with the `ggplot2` package from [Introduction to Visualisation](). `ggplot2` has special `geom_` functions for spatial data. We will use the `geom_sf()` function for `sf` data.
Now, let's plot this shapefile. You are already familiar with the `ggplot2` package from [Introduction to Visualisation](../episodes/04-intro-to-visualisation.Rmd). `ggplot2` has special `geom_` functions for spatial data. We will use the `geom_sf()` function for `sf` data.

```{r}
ggplot(data = boundary_Delft) +
geom_sf(size = 3, color = "black", fill = "cyan1") +
labs(title = "Delft Administrative Boundary") +
coord_sf(datum = st_crs(28992)) # this is needed to display the axes in meters
```

::::::::::::::::::::::::::::::::::::: challenge

### Challenge 1: Import line and point vector layers

Read in `delft-streets.shp` and `delft-leisure.shp` and assign them to `lines_Delft` and `point_Delft` respectively. Answer the following questions:
Read in `delft-streets.shp` and `delft-leisure.shp` and assign them to `lines_Delft` and `points_Delft` respectively. Answer the following questions:

1. What type of R spatial object is created when you import each layer?
2. What is the CRS and extent for each object?
Expand All @@ -152,28 +156,28 @@ Read in `delft-streets.shp` and `delft-leisure.shp` and assign them to `lines_De

```{r results='hide'}
lines_Delft <- st_read("data/delft-streets.shp")
point_Delft <- st_read("data/delft-leisure.shp")
points_Delft <- st_read("data/delft-leisure.shp")
```

We can check the type of data with the `class()` function from base R. Both `lines_Delft` and `point_Delft` are objects of class `"sf"`, which extends the `"data.frame"` class.
We can check the type of data with the `class()` function from base R. Both `lines_Delft` and `points_Delft` are objects of class `"sf"`, which extends the `"data.frame"` class.

```{r}
class(lines_Delft)
class(point_Delft)
class(points_Delft)
```

`lines_Delft` and `point_Delft` are in the correct CRS.
`lines_Delft` and `points_Delft` are in the correct CRS.

```{r}
st_crs(lines_Delft)$epsg
st_crs(point_Delft)$epsg
st_crs(points_Delft)$epsg
```

When looking at the bounding boxes with the `st_bbox()` function, we see the spatial extent of the two objects in a projected CRS using meters as units. `lines_Delft()` and `point_Delft` have similar extents.
When looking at the bounding boxes with the `st_bbox()` function, we see the spatial extent of the two objects in a projected CRS using meters as units. `lines_Delft()` and `points_Delft` have similar extents.

```{r}
st_bbox(lines_Delft)
st_bbox(point_Delft)
st_bbox(points_Delft)
```

:::::::::::::::::::::::::::::::::
Expand All @@ -186,6 +190,6 @@ st_bbox(point_Delft)

- Metadata for vector layers include geometry type, CRS, and extent.
- Load spatial objects into R with the `st_read()` function.
- Spatial objects can be plotted directly with `ggplot` using the `geom_sf()` function. No need to convert to a data frame.
- Spatial objects can be plotted directly with `ggplot2` using the `geom_sf()` function. No need to convert to a data frame.

::::::::::::::::::::::::::::::::::::::::::::::::

0 comments on commit b32f72c

Please sign in to comment.