Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
cforgaci authored Jan 29, 2024
2 parents f4bb560 + 1639ed7 commit fece804
Show file tree
Hide file tree
Showing 173 changed files with 17,806 additions and 247 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sandpaper-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.13.0
0.16.2
75 changes: 53 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
# The Carpentries Workbench Template R Markdown Lesson
# Geospatial Data Carpentry for Urbanism

This lesson is a template lesson that uses [The Carpentries Workbench][workbench].

## Create a new repository from this template
## Lesson Description

To use this template to start a new lesson repository,
make sure you're logged into Github.
## Rendered Version of This Lesson

Version 1.0

## Contact Information

[Rbanism Community](mailto:rbanism@tudelf.nl)

## Contributing Information

Please see the [CONTRIBUTING page](https://github.com/carpentries-incubator/r-geospatial-urban/blob/main/CONTRIBUTING.md) for guidance on how to contribute to this lessom.

## Credits / Acknowledgements

- [Daniele Cannatella](https://github.com/dcannatella)
- [Clémentine Cottineau](https://github.com/ClementineCttn)
- [Claudiu Forgaci](https://github.com/cforgaci)
- [Jerome Francisco Conceicao](https://github.com/fcjerome)
- [Manuel Garcia](https://github.com/manuGil)
- [Kyri Janssen](https://github.com/KyriJanssen)
- [Selin Kubilay](https://github.com/Selkubi)
- [Ana Petrovic](https://github.com/ana-5r)
- [Aleksandra Wilczynska](https://github.com/alwil)

## Citation

## License

The R Geospatial Urban lesson, like most other software provided by The Carpentries, are made available under the OSI-approved MIT license. Please see the [LICENSE page](https://github.com/carpentries-incubator/r-geospatial-urban/blob/main/LICENSE.md) for further information.

To use this template to
start a new lesson repository,
make sure you're logged into Github.
Visit https://github.com/carpentries/workbench-template-rmd/generate
and follow the instructions.
Checking the 'Include all branches' option will save some time waiting for the first website build
when your new repository is initialised.

If you have any questions, contact [@zkamvar](https://github.com/zkamvar)

## Configure a new lesson
## Rendered Version of This Lesson

Follow the steps below to
complete the initial configuration of a new lesson repository built from this template:
Expand All @@ -36,7 +67,7 @@ complete the initial configuration of a new lesson repository built from this te
- `contact`
1. **Annotate the repository** with site URL and topic tags:
navigate back to the repository landing page and
click on the gear wheel/cog icon (similar to ⚙️)
click on the gear wheel/cog icon (similar to ⚙️)
at the top-right of the _About_ box.
Check the "Use your GitHub Pages website" option,
and [add some keywords and other annotations to describe your lesson](https://cdh.carpentries.org/the-carpentries-incubator.html#topic-tags)
Expand All @@ -47,24 +78,24 @@ complete the initial configuration of a new lesson repository built from this te
- the human language the lesson is written in (e.g. `deutsch`)
1. **Adjust the name of the `.Rproj` file.**
It is simplest to make this match the name of the repository.
1. **Adjust the
1. **Adjust the
`CODE_OF_CONDUCT.md`, `CONTRIBUTING.md`, and `LICENSE.md` files**
as appropriate for your project.
- `CODE_OF_CONDUCT.md`:
if you are using this template for a project outside The Carpentries,
you should adjust this file to describe
who should be contacted with Code of Conduct reports,
and how those reports will be handled.
- `CONTRIBUTING.md`:
depending on the current state and maturity of your project,
the contents of the template Contributing Guide may not be appropriate.
You should adjust the file to help guide contributors on how best
to get involved and make an impact on your lesson.
- `LICENSE.md`:
in line with the terms of the CC-BY license,
you should ensure that the copyright information
provided in the license file is accurate for your project.
1. **Update this README with
- `CODE_OF_CONDUCT.md`:
if you are using this template for a project outside The Carpentries,
you should adjust this file to describe
who should be contacted with Code of Conduct reports,
and how those reports will be handled.
- `CONTRIBUTING.md`:
depending on the current state and maturity of your project,
the contents of the template Contributing Guide may not be appropriate.
You should adjust the file to help guide contributors on how best
to get involved and make an impact on your lesson.
- `LICENSE.md`:
in line with the terms of the CC-BY license,
you should ensure that the copyright information
provided in the license file is accurate for your project.
1. **Update this README with
[relevant information about your lesson](https://carpentries.github.io/lesson-development-training/collaborating-newcomers.html#readme)**
and delete this section.

Expand Down
10 changes: 9 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ contact: 'c.forgaci@tudelft.nl'

# Order of episodes in your lesson
episodes:
- introduction.Rmd
- 01-intro-to-r.Rmd
- 02-data-structures.Rmd
- 03-explore-data.Rmd
- 04-intro-to-visualisation.Rmd
- 09-open-and-plot-vector-layers.Rmd
- 10-explore-and-plot-by-vector-layer-attributes.Rmd
- 11-plot-multiple-shape-files.Rmd
- 12-handling-spatial-projection-and-crs.Rmd
- 13-intro-to-raster-data.Rmd
- 14-plot-raster-data.Rmd
- 15-reproject-raster-data.Rmd
- 16-raster-calculations.Rmd
- 17-work-with-multi-band-rasters.Rmd

# Information for Learners
learners:
Expand Down
189 changes: 189 additions & 0 deletions episodes/09-open-and-plot-vector-layers.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
---
title: "Open and Plot Vector Layers"
teaching: 25
exercises: 5
---

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

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

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

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

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

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

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 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
minutes at the beginning of the first workshop day should be enough to tackle
installation issues.

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

::: prereq

If you have not installed the `sf` package yet, run `install.packages("sf")` first. Note that the `sf` package has some external dependencies, namely GEOS, PROJ.4, GDAL and UDUNITS, which need to be installed beforehand. Follow the workshop [setup instructions]() for the installation of `sf` and its dependencies.

:::

First we need to load the packages we will use in this lesson. We will use the packages `tidyverse` and `here` 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(here) # managing paths
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.

:::

## Import shapefiles

Let's start by opening a shapefile. Shapefiles 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")
```

::: callout

# All `sf` functions start with `st_`

Note that all functions from the `sf` package start with the standard prefix `st_` which stands for Spatial Type. This is helpful in at least two ways: (1) it makes the interaction with or translation to/from software using the simple features standard like PostGIS easy, and (2) it allows for easy autocompletion of function names in RStudio.

:::

## 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`.

```{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`.

```{r}
st_crs(boundary_Delft)
```

::: callout

# 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.

:::

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.

```{r}
st_bbox(boundary_Delft)
```

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.

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

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

```{r}
st_bbox(boundary_Delft)
```

We confirm the transformation by examining the reprojected shapefile.

```{r}
boundary_Delft
```

::: callout

More about CRS in [Handling Spatial Projection & CRS]().

:::



## 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.

```{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:

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?

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

```{r results='hide'}
lines_Delft <- st_read("data/delft-streets.shp")
point_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.

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

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

```{r}
st_crs(lines_Delft)$epsg
st_crs(point_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.

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

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

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



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

- 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.

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

0 comments on commit fece804

Please sign in to comment.