-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtutorial-2-points-lines-polygons.Rmd
More file actions
120 lines (81 loc) · 3.75 KB
/
tutorial-2-points-lines-polygons.Rmd
File metadata and controls
120 lines (81 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: "Geo-spatial tutorial: points / lines / polygons"
author: "JR Ferrer-Paris"
institute: "Centre for Ecosystem Science<br/>UNSW Data Science Hub"
date: "March 2021 (updated: `r Sys.Date()`)"
output:
learnr::tutorial:
progressive: true
allow_skip: true
runtime: shiny_prerendered
description: >
Learn how to work with basic vector objects with R packages `sf` and plot with `ggplot2`.
---
```{r setup, include=FALSE}
library(learnr)
library(tidyverse)
library(sf)
library(mapview)
library(units)
require(RColorBrewer)
tutorial_options(
exercise.timelimit = 60,
# A simple checker function that just returns the message in the check chunk
exercise.checker = function(check_code, ...) {
list(
message = eval(parse(text = check_code)),
correct = logical(0),
type = "info",
location = "append"
)
}
)
knitr::opts_chunk$set(error = TRUE)
UBT <- st_sf(data.frame(full_name='Universität Bayreuth',
url='https://uni-bayreuth.de/'),
crs=st_crs("EPSG:4326"),
geometry=st_sfc(
st_point(c(11.585833, 49.928889))))
st_crs(franconia) <- "EPSG:4326"
st_crs(breweries) <- "EPSG:4326"
st_crs(trails) <- "EPSG:32632"
franconia_utm = franconia %>% st_transform(st_crs(trails))
UBT_utm = UBT %>% st_transform(st_crs(trails))
trails %>% mutate(length=st_length(geometry)) %>% filter(length>set_units(40000,'m')) -> long_trails
```
## Welcome
In this tutorial, you will learn some basic steps to start working with (geo)-spatial data in R. I prepared this tutorial as a intuitive "hands on" introduction, but I provide links for those interested in more background and theory.
We will cover how to:
* work with points, lines and polygons (vector data) using package `sf`
* apply basic spatial functions,
* visualise spatial data with `ggplot`
### Setup
I've preloaded the packages for this tutorial with
```{r eval = FALSE}
library(sf)
library(units)
require(mapview)
require(RColorBrewer)
```
Throughout this tutorial we will be using the pipe operator, `%>%`. You can use the pipe to rewrite multiple operations in a way that you can read left-to-right, top-to-bottom. We'll use piping frequently because it considerably improves the readability of code. The pipe is a defining feature of the tidyverse, so we will load this set of packages as well.
```{r eval = FALSE}
library(tidyverse)
```
The tidyverse package also includes `ggplot2`, which allow us to create static maps in a similar modular fashion, but using the `+` operator instead of the pipe.
We will use the `franconia`, `breweries` and `trails` data sets from package `mapview`. These data sets are documented in `help(package=mapview)`.
```{r, eval=TRUE, child="02-points-section.Rmd"}
```
###
We learned how to plot and query spatial and non-spatial information from a `sf` object, well done!
We will come back to the questions of plotting and querying spatial objects when we have a look at other types of spatial objects. Please continue to the next section.
```{r, eval=TRUE, child="03-polygons-section.Rmd"}
```
###
We just learned to work with polygons, how to use spatial functions `st_union`, `st_intersection` and how to plot spatial objects with `ggplot`, well done!
In the next section we will work with objects in different projections.
```{r, eval=TRUE, child="04-projection-section.Rmd"}
```
### That's it from me!
In this last section we learned to work with lines, how to use spatial functions `st_length` and how to handle projections of spatial objects with ggplot` and with the function `st_transform`. Great!
I hope this has been useful and you feel more confident to start working with geo-spatial data in R.
Check the next tutorials in our [geospatial-data repository](https://github.com/UNSW-codeRs/geospatial-data-in-R)