Skip to content

Commit

Permalink
differences for PR #39
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Mar 12, 2024
1 parent 2f11867 commit 4dffe06
Show file tree
Hide file tree
Showing 24 changed files with 178 additions and 366 deletions.
7 changes: 5 additions & 2 deletions 08-intro-to-geospatial-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ author: 'Ana Petrović'
::: questions

- How do I describe the location of a geographic feature on the surface of the earth?
- What is coordinate reference system (CRS) and how do I describe different types of CRS?
- What is a coordinate reference system (CRS) and how do I describe different types of CRS?
- How do I decide on what CRS to use?

:::

::: objectives

After completing this episode, participants should be able to…

- Identify the CRS that is best fit for a specific research question.

:::
Expand Down Expand Up @@ -48,7 +50,7 @@ The black lines in the figure above show the equator and the prime meridian runn
<p class="caption">Map projection represented as flattening an orange peel. Source: Data Carpentry (2023).</p>
</div>

Many different map projections are in use for different purposes. Generally, they can be categorised into the following groups: cylindrical, conic, and azimuthal (see @fig-projections).
Many different map projections are in use for different purposes. Generally, they can be categorised into the following groups: cylindrical, conic, and azimuthal.

<div class="figure" style="text-align: center">
<img src="https://kartoweb.itc.nl/geometrics/Bitmaps/Intro%201.9a.gif" alt="Cylindrical, conic, and azimuthal map projections. Source: Knippers (2009)." />
Expand Down Expand Up @@ -162,6 +164,7 @@ Correct answer: b. EPSG:28992


::: callout

# Useful resources

- Campbell, J., Shin, M. E. (2011). Essentials of Geographic Information Systems. Textbooks. 2. <https://digitalcommons.liberty.edu/textbooks/2> (Accessed 22-01-2024)
Expand Down
71 changes: 52 additions & 19 deletions 09-open-and-plot-vector-layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ 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
last-minute installation issues.


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

::: prereq
Expand All @@ -48,9 +47,9 @@ library(sf) # work with spatial vector data

::: callout

# The `sf` package
# 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.
`sf` stands for Simple Features which is a standard defined by the Open Geospatial Consortium for storing and accessing geospatial vector data. Read more about simple features and its implementation in R [here](https://r-spatial.github.io/sf/articles/sf1.html).

:::

Expand All @@ -76,9 +75,25 @@ Note that all functions from the `sf` package start with the standard prefix `st

## Spatial Metadata

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.
By default (with `quiet = FALSE`), the `st_read()` function provides a message with a summary of metadata about the file that was read in.


```r
st_read("data/delft-boundary.shp")
```

```{.output}
Reading layer `delft-boundary' from data source
`/home/runner/work/r-geospatial-urban/r-geospatial-urban/site/built/data/delft-boundary.shp'
using driver `ESRI Shapefile'
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 4.320218 ymin: 51.96632 xmax: 4.407911 ymax: 52.0326
Geodetic CRS: WGS 84
```

The `st_geometry_type()` function, for instance, gives us information about the geometry type, which in this case is `POLYGON`.
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
Expand Down Expand Up @@ -121,11 +136,30 @@ GEOGCRS["WGS 84",

# Examining the output of `st_crs()`

As the output of `st_crs()` can be long, you can use `$Name` and `$epsg` after the `crs()` call to extract the projection name and EPSG code respectively.
As the output of `st_crs()` can be long, you can use `$Name` and `$epsg` after the `crs()` call to extract the projection name and EPSG code respectively.


```r
st_crs(boundary_Delft)$Name
```

```{.output}
[1] "WGS 84"
```

```r
st_crs(boundary_Delft)$epsg
```

```{.output}
[1] 4326
```

The `$` operator is used to extract a specific part of an object. We used it in a [previous episode]() to subset a data frame by column name. In this case, it is used to extract named elements stored in a `crs` object. For more information, see [the documentation of the `st_crs` function](https://r-spatial.github.io/sf/reference/st_crs.html#details).

:::

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.
The `st_bbox()` function shows the extent of the layer.



Expand All @@ -138,12 +172,12 @@ st_bbox(boundary_Delft)
4.320218 51.966316 4.407911 52.032599
```

We need a **projected CRS**, which in the case of the Netherlands is typically the Amersfort / RD New projection. To reproject our shapefile, we will use the `st_transform()` function. For the `crs` argument we can use the EPSG code of the CRS we want to use, which is `28992` for the `Amersfort / RD New` projection.
As `WGS 84` is a **geographic CRS**, the extent of the shapefile is displayed in degrees. We need a **projected CRS**, which in the case of the Netherlands is the `Amersfoort / RD New` projection. To reproject our shapefile, we will use the `st_transform()` function. For the `crs` argument we can use the EPSG code of the CRS we want to use, which is `28992` for the `Amersfort / RD New` projection.
To check the EPSG code of any CRS, we can check this website: https://epsg.io/


```r
boundary_Delft <- st_transform(boundary_Delft, 28992)
boundary_Delft <- st_transform(boundary_Delft, crs = 28992)
st_crs(boundary_Delft)$Name
```

Expand All @@ -159,7 +193,7 @@ st_crs(boundary_Delft)$epsg
[1] 28992
```

Notice that the bounding box is measured in meters after the transformation.
Notice that the bounding box is measured in meters after the transformation. The `$units_gdal` named element confirms that the new CRS uses metric units.


```r
Expand Down Expand Up @@ -206,7 +240,7 @@ Read more about Coordinate Reference Systems [here](https://datacarpentry.org/or

## Plot a vector layer

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.
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. We use `coord_sf()` to ensure that the coordinates shown on the two axes are displayed in meters.


```r
Expand All @@ -216,18 +250,17 @@ ggplot(data = boundary_Delft) +
coord_sf(datum = st_crs(28992)) # displays the axes in meters
```

<img src="fig/09-open-and-plot-vector-layers-rendered-unnamed-chunk-8-1.png" style="display: block; margin: auto;" />
<img src="fig/09-open-and-plot-vector-layers-rendered-unnamed-chunk-10-1.png" style="display: block; margin: auto;" />

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

### Challenge: Import line and point vector layers

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?
3. Do the files contain points, lines, or polygons?
4. How many features are in each file?
1. What is the CRS and extent for each object?
2. Do the files contain points, lines, or polygons?
3. How many features are in each file?

:::::::::::::::::::::::: solution

Expand Down Expand Up @@ -258,7 +291,7 @@ st_geometry_type(points_Delft)[2]
18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE
```

`lines_Delft` and `points_Delft` are in the correct CRS.
Both `lines_Delft` and `points_Delft` are in `EPSG:28992`.


```r
Expand Down Expand Up @@ -306,8 +339,8 @@ st_bbox(points_Delft)

::::::::::::::::::::::::::::::::::::: keypoints

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

::::::::::::::::::::::::::::::::::::::::::::::::
Loading

0 comments on commit 4dffe06

Please sign in to comment.