Skip to content

Commit 19cd9d7

Browse files
committed
updated raster template post
1 parent 71ede39 commit 19cd9d7

31 files changed

+138
-14082
lines changed

_posts/rastertemplate/rastertemplate.Rmd

+37-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
title: "Raster Operations"
33
description: |
4-
Learn how to obtain a template raster from the `{d6geodata}` package, expanding the raster in different ways, rasterizing vector data, and extracting information from a raster using vector data. The methods described involve the use of packages like `{terra}` and `{sf}`, and include examples and plots to illustrate the processes. Additionally, we show the use of the {tidyterra} package for plotting raster data with ggplot.
4+
Learn how to 1) obtain a template raster from the `{d6geodata}` package, 2) expand the raster in different ways, 3) rasterize vector data, and 4) extract information from a raster using vector data.
5+
The methods described involve the use of packages like `{terra}` and `{sf}`, and include examples and plots to illustrate the processes. Additionally, we show how to use the `{tidyterra}` package for plotting raster data with `{ggplot2}`.
56
67
preview: ./img/wiki/preview-rastertemplate.png
78
categories:
@@ -47,7 +48,7 @@ theme_set(
4748

4849
## Raster Template {#template}
4950

50-
First get the template raster. The origin is the extent of Berlin with the projection laea (EPSG:3035).
51+
First get the template raster. The origin is the extent of Berlin with the projection LAEA (EPSG:3035).
5152

5253
```{r template}
5354
temp <- d6geodata::temp_ras(1000) # resolution is 1000m
@@ -62,37 +63,41 @@ We load Berlin districts data set from our cloud to show the borders of Berlin.
6263
berlin <- d6geodata::get_geodata(data_name = "districs_berlin_2022_poly_03035_gpkg",
6364
path_to_cloud = "E:/PopDynCloud",
6465
download_data = FALSE) %>%
65-
st_union() %>% # using `st_union()` to combine
66+
st_union() %>% # using `st_union()` to combine geometries
6667
st_cast("POLYGON")
6768
6869
p_berlin <- geom_sf(data = berlin, alpha = 0, color = "black", lwd = 0.7)
6970
```
7071

72+
Now we plot the map.
7173

7274
```{r plot template}
7375
p1 <- d6geodata::plot_qualitative_map(temp) # you can use this build-in plot function from the `{d6geodata}` package
7476
7577
p1 + p_berlin
7678
```
7779

80+
7881
## Expand Raster {#expand}
7982

8083
### Expand On All Sides
8184

82-
If you want to expand the raster on all size (similar to a buffer around a polygon) you can just add cells to all sides. You can do this by adding an integer number in the y argument in the function `extend` of the `{terra}` package.
85+
If you want to expand the raster (similar to a buffer around a polygon) you can just add cells to all sides. You can do this by adding an integer number in the y argument in the function `extend` of the `{terra}` package.
8386

8487
```{r}
88+
# expand the map
8589
temp_exp_all <- terra::extend(x = temp, # template raster as input
8690
y = 10, # number of cells to add
8791
fill = 2) # value for new cells
8892
93+
# plot the map again
8994
p2 <- d6geodata::plot_qualitative_map(temp_exp_all)
9095
p2 + p_berlin
9196
```
9297

9398
### Expand On Two Sides
9499

95-
In this case you can add cells on two opposite sides.
100+
You can also expand the raster in one or two different directions only by adding cells on two opposite sides.
96101

97102
```{r}
98103
temp_exp_tb_sides <- terra::extend(x = temp,
@@ -102,9 +107,9 @@ p3 <- d6geodata::plot_qualitative_map(temp_exp_tb_sides)
102107
p3 + p_berlin
103108
```
104109

105-
### Expand On One Side With A Costum Extent
110+
### Expand On One Side With A Custom Extent
106111

107-
In this case we use a workaround by using a costum extent. We are adding on the left (western) side cells by using the xmin of the larger extend. Xmax, ymin, ymax are from the given extent.
112+
In this case we use a workaround by using a custom extent. Here we are adding cells on the left (western) side by using the *xmin* of the larger extend. Xmax, ymin, ymax are from the given extent.
108113

109114
```{r}
110115
temp_exp_1_sides <- terra::extend(x = temp,
@@ -123,19 +128,22 @@ p4 + p_berlin
123128
Here you see a combination of all plots made to compare them directly.
124129

125130
```{r, preview = TRUE}
131+
# arrange plots in a 2x2 grid
126132
(p1 + p_berlin + theme(legend.position="none") + p2 + p_berlin) / (p3 + p_berlin + p4 + p_berlin) +
127133
plot_layout(guides = "collect") +
128-
plot_annotation(tag_levels = "1", tag_prefix = "(P", tag_suffix = ")")
134+
plot_annotation(tag_levels = "1", tag_prefix = "(P", tag_suffix = ")") # add tag to every plot
129135
```
130136

131137
## Rasterize Vector Data {#rasterize}
132138

133-
With `vect()` you can make a spatVector (a kind of shapfile) out of an extent object.
139+
With `vect()` you can make a spatVector (similar to a shapefile) from an extent object.
134140
Afterwards, we can use this spatVector to rasterize the data. This works similar with an sf object.
135141

136142
```{r plot}
137-
temp_vec <- terra::vect(ext(temp), crs(temp)) # spatVector out of smalest raster extent
143+
# create spatVector
144+
temp_vec <- terra::vect(ext(temp), crs(temp)) # spatVector out of smallest raster extent
138145
146+
# rasterize spatVector
139147
temp_rast <- rasterize(temp_vec, # vector data
140148
temp_exp_all %>% disagg(10),
141149
# raster data with larger extent, disaggregated to 100m
@@ -144,28 +152,34 @@ temp_rast <- rasterize(temp_vec, # vector data
144152
temp_rast
145153
```
146154

155+
147156
### Rasterized Plot
148157

158+
View the results.
159+
149160
```{r}
150161
d6geodata::plot_qualitative_map(temp_rast)
151162
```
152163

153164
## Extract With Vector Data {#extract}
154165

155-
Imagine you have a track of your animal and you want to see, which type of information lays in this track. In this case, you can use your line and extract the values from a raster below. There are several ways to this but the easiest way is to use the `extract()` function form the `{terra}` package to get the values laying under the vector data.
166+
Imagine you have a track of an animal and you want to know what information lays in this track. In this case, you can use your line and extract the values from a raster below. There are several ways to this but the easiest way is to use the `extract()` function form the `{terra}` package to get the values laying under the vector data.
156167

157168
For this, you can use any vector data (points, lines, polygons) with the same projection and extent to extract values from a raster. The `extract()` function uses the raster as first input and the vector data as second. Depending on the type of data you are using you can define a function for the extraction like min, max or mean. 'Cells' gives you the call ID, 'xy' the coordinates and 'ID' the row number of your extraction.
169+
As mentioned, it is important that both the vector and the raster have the same projection.
158170

159171
### Line For Extraction
160172

161-
For this we just creating a dummy line with coordinates laying within the raster
173+
For this we are just creating a dummy line with coordinates laying within the raster
162174

163175
```{r}
176+
# create points
164177
pts <- tibble(x = c(4528790, 4576890), # a point out of two coordinates from the raster
165178
y = c(3271740, 3272140))
166179
180+
# create line from points
167181
dummy_line <- st_as_sf(pts, # a line made out of the point data
168-
coords = c("x", "y"),
182+
coords = c("x", "y"), # specify columns of xy coordinates
169183
crs = st_crs(temp)$wkt, # using the same projection as for the template raster
170184
sf_column_name = "geometry",
171185
remove = FALSE) %>%
@@ -177,8 +191,8 @@ dummy_line <- st_as_sf(pts, # a line made out of the point data
177191
### Plot Of Raster And Line
178192

179193
```{r}
180-
p5 <- d6geodata::plot_qualitative_map(temp_rast) +
181-
geom_sf(data = dummy_line)
194+
p5 <- d6geodata::plot_qualitative_map(temp_rast) + # first plot raster
195+
geom_sf(data = dummy_line) # plot line on top of the raster
182196
183197
p5
184198
@@ -193,31 +207,31 @@ ext_tab <- terra::extract(x = temp_rast, # template raster as first input
193207
y = dummy_line, # dummy line as second input
194208
fun=NULL, # here you can set a function to summarise the data directly
195209
method="simple",
196-
cells=FALSE, # here you can get the cell number as well
197-
xy=FALSE, # here the xy coordinates
210+
cells=FALSE, # here you can get the cell number as well if cell = TRUE
211+
xy=FALSE, # here the xy coordinates if xy = TRUE
198212
ID=TRUE # and the id in front of each row
199213
)
200214
```
201215

202-
As you already can see on the plot, the largest part of the line lays within the purple area (1). This function can be used in cases like you need to know in which landuse class your track lays.
216+
As you already can see on the plot, the largest part of the line lays within the purple area (value = 1). This function can be used to know on which land use class your track lays, for example.
203217

204218
```{r}
205219
table(ext_tab$layer)
206220
```
207221

208-
## Visualize data with tidyterra {#tidyterra}
222+
## Visualize Data With Tidyterra {#tidyterra}
209223

210-
There are several ways to plot raster data with ggplot and one relatively new way is to do it with the package `{tidyterra}`. It has some well developed `{ggplot}` functions, but can sometimes not be combined with the base functions of `{ggplot}`. If you use this pacakge you may have to stay within the package functions.
224+
There are several ways to plot raster data with ggplot and one relatively new way is to do it with the package `{tidyterra}`. It has some well developed `{ggplot}` functions, but it cannot always be combined with the base functions of `{ggplot}`. If you use this package you may have to stay within the package functions.
211225

212226

213227
```{r}
214228
library(tidyterra)
215229
216230
ggplot() +
217-
geom_spatraster(data = temp_rast) +
218-
p_berlin
219-
231+
geom_spatraster(data = temp_rast) + # use geom_spatraster() to plot the raster data with {tidyterra}
232+
p_berlin # add vector data of Berlin
220233
234+
# same a before, but add some color to the plot
221235
ggplot() +
222236
geom_spatraster(data = temp_rast) +
223237
scale_fill_whitebox_c(palette = "purple") +

0 commit comments

Comments
 (0)