Skip to content

Commit

Permalink
improves style
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowosad committed Aug 26, 2023
1 parent 45c2e5a commit 1d3d626
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 42 deletions.
4 changes: 3 additions & 1 deletion 02-spatial-data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ install.packages("spData")
install.packages("spDataLarge", repos = "https://nowosad.r-universe.dev")
```

<!-- toDo: explain (maybe also move tmapv4 to main) -->

```{r}
#| message: FALSE
#| results: hide
Expand Down Expand Up @@ -834,7 +836,7 @@ sf_use_s2(FALSE)
india_buffer_without_s2 = st_buffer(india, 1)
```

```{r s2example, echo=FALSE, fig.cap="Example of the consequences of turning off the S2 geometry engine. Both representations of a buffer around India were created with the same command but the purple polygon object was created with S2 switched on, resulting in a buffer of 1 m. The larger light green polygon was created with S2 switched off, resulting in a buffer with inaccurate units of degrees longitude/latitude.", fig.asp=0.75}
```{r s2example, echo=FALSE, fig.cap="Example of the consequences of turning off the S2 geometry engine. Both representations of a buffer around India were created with the same command but the purple polygon object was created with S2 switched on, resulting in a buffer of 1 m. The larger light green polygon was created with S2 switched off, resulting in a buffer with inaccurate units of degrees longitude/latitude.", message=FALSE}
library(tmap)
tm1 = tm_shape(india_buffer_with_s2) +
tm_fill(fill = hcl.colors(4, palette = "purple green")[3]) +
Expand Down
2 changes: 2 additions & 0 deletions 03-attribute-operations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ library(dplyr) # tidyverse package for data frame manipulation
- It relies on **spData**, which loads datasets used in the code examples of this chapter:

```{r 03-attribute-operations-2, results='hide'}
#| message: FALSE
#| results: hide
library(spData) # spatial data package introduced in Chapter 2
```

Expand Down
5 changes: 2 additions & 3 deletions 05-geometry-operations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The simplified geometry was created by the following command:
seine_simp = st_simplify(seine, dTolerance = 2000) # 2000 m
```

```{r seine-simp, echo=FALSE, fig.cap="Comparison of the original and simplified geometry of the seine object.", warning=FALSE, fig.scap="Simplification in action.", message=FALSE}
```{r seine-simp, echo=FALSE, fig.cap="Comparison of the original and simplified geometry of the seine object.", warning=FALSE, fig.scap="Simplification in action.", message=FALSE, fig.asp=0.5}
library(tmap)
p_simp1 = tm_shape(seine) + tm_lines() +
tm_title("Original data")
Expand All @@ -78,7 +78,6 @@ Therefore, the first step is to project the data into some adequate projected CR

```{r 05-geometry-operations-4}
us_states2163 = st_transform(us_states, "EPSG:2163")
us_states2163 = us_states2163
```

`st_simplify()` works equally well with projected polygons:
Expand Down Expand Up @@ -461,7 +460,7 @@ regions2 = us_states |>
# summarize(africa[buff, "pop"], pop = sum(pop, na.rm = TRUE))
```

```{r us-regions, fig.cap="Spatial aggregation on contiguous polygons, illustrated by aggregating the population of US states into regions, with population represented by color. Note the operation automatically dissolves boundaries between states.", echo=FALSE, warning=FALSE, fig.asp=0.2, out.width="100%", fig.scap="Spatial aggregation on contiguous polygons."}
```{r us-regions, fig.cap="Spatial aggregation on contiguous polygons, illustrated by aggregating the population of US states into regions, with population represented by color. Note the operation automatically dissolves boundaries between states.", echo=FALSE, warning=FALSE, out.width="100%", fig.scap="Spatial aggregation on contiguous polygons."}
source("code/05-us-regions.R", print.eval = TRUE)
```

Expand Down
2 changes: 2 additions & 0 deletions 06-raster-vector.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Changing the settings of `mask()` yields different results.
Setting `inverse = TRUE` will mask everything *inside* the bounds of the park (see `?mask` for details) (Figure \@ref(fig:cropmask)(D)), while setting `updatevalue = 0` will set all pixels outside the national park to 0.

```{r 06-raster-vector-6 }
#| message: FALSE
#| results: hide
srtm_inv_masked = mask(srtm, zion, inverse = TRUE)
```

Expand Down
2 changes: 2 additions & 0 deletions 07-reproj.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ london_buff_projected = st_buffer(london_proj, 100000)
The geometries of the three `london_buff*` objects that *have* a specified CRS created above (`london_buff_s2`, `london_buff_lonlat` and `london_buff_projected`) created in the preceding code chunks are illustrated in Figure \@ref(fig:crs-buf).

```{r crs-buf-old, include=FALSE, eval=FALSE}
#| message: FALSE
#| results: hide
uk = rnaturalearth::ne_countries(scale = 50) |>
st_as_sf() |>
filter(grepl(pattern = "United Kingdom|Ire", x = name_long))
Expand Down
20 changes: 12 additions & 8 deletions 09-mapping.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ This figure shows four ways of coloring regions in New Zealand depending on medi
```{r 08-mapping-12, eval=FALSE}
tm_shape(nz) + tm_polygons(fill = "Median_income")
tm_shape(nz) + tm_polygons(fill = "Median_income",
fill.scale = tm_scale(breaks = c(0, 30000, 40000, 50000)))
fill.scale = tm_scale(breaks = c(0, 30000, 40000, 50000)))
tm_shape(nz) + tm_polygons(fill = "Median_income",
fill.scale = tm_scale(n = 10))
tm_shape(nz) + tm_polygons(fill = "Median_income",
Expand Down Expand Up @@ -381,8 +381,10 @@ These follow a gradient, for example from light to dark colors (light colors oft
Sequential palettes can be single (`greens` goes from light to dark blue, for example) or multi-color/hue (`yl_gn_bu` is gradient from light yellow to blue via green, for example), as demonstrated in the code chunk below --- output not shown, run the code yourself to see the results!

```{r 08-mapping-13, eval=FALSE}
tm_shape(nz) + tm_polygons("Median_income", fill.scale = tm_scale(values = "greens"))
tm_shape(nz) + tm_polygons("Median_income", fill.scale = tm_scale(values = "yl_gn_bu"))
tm_shape(nz) +
tm_polygons("Median_income", fill.scale = tm_scale(values = "greens"))
tm_shape(nz) +
tm_polygons("Median_income", fill.scale = tm_scale(values = "yl_gn_bu"))
```

The third group, diverging palettes, typically range between three distinct colors (purple-white-green in Figure \@ref(fig:colpal)) and are usually created by joining two single-color sequential palettes with the darker colors at each end.
Expand All @@ -392,7 +394,8 @@ The reference point's value can be adjusted in **tmap** using the `midpoint` arg
```{r 08-mapping-13b, eval=FALSE}
tm_shape(nz) +
tm_polygons("Median_income",
fill.scale = tm_scale_continuous(values = "pu_gn_div", midpoint = 28000))
fill.scale = tm_scale_continuous(values = "pu_gn_div",
midpoint = 28000))
```

```{r colpal, echo=FALSE, message=FALSE, fig.cap="Examples of categorical, sequential and diverging palettes.", out.width="75%"}
Expand Down Expand Up @@ -449,9 +452,10 @@ Other than that, we can also customize the location of the legend using the `pos

```{r}
map_nza2 = tm_shape(nz) +
tm_polygons(fill = "Land_area", fill.legend = tm_legend(title = legend_title,
orientation = "landscape",
position = tm_pos_out("center", "bottom")))
tm_polygons(fill = "Land_area",
fill.legend = tm_legend(title = legend_title,
orientation = "landscape",
position = tm_pos_out("center", "bottom")))
```

The legend position (and also the position of several other map elements in **tmap**) can be customized using one of a few functions.
Expand Down Expand Up @@ -1166,7 +1170,7 @@ nz_carto = cartogram_cont(nz, "Median_income", itermax = 5)
tm_shape(nz_carto) + tm_polygons("Median_income")
```

```{r cartomap1, echo=FALSE, message=FALSE, fig.cap="Comparison of standard map (left) and continuous area cartogram (right).", fig.scap="Comparison of standard map and continuous area cartogram."}
```{r cartomap1, echo=FALSE, message=FALSE, fig.cap="Comparison of standard map (left) and continuous area cartogram (right).", fig.scap="Comparison of standard map and continuous area cartogram.", warning=FALSE}
carto_map1 = tm_shape(nz) +
tm_polygons("Median_income",
fill.scale = tm_scale(values = "Greens"),
Expand Down
51 changes: 21 additions & 30 deletions 10-gis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,15 @@ We use again the incongruent polygons\index{spatial congruence} we have already
Both polygon datasets are available in the **spData** package, and for both we would like to use a geographic CRS\index{CRS!geographic} (see also Chapter \@ref(reproj-geo-data)).

```{r 09-gis-4}
data("incongruent", "aggregating_zones", package = "spData")
incongr_wgs = st_transform(incongruent, "EPSG:4326")
aggzone_wgs = st_transform(aggregating_zones, "EPSG:4326")
```

```{r uniondata, echo=FALSE, fig.cap="Illustration of two areal units: incongruent (black lines) and aggregating zones (red borders)."}
#| message: FALSE
#| results: hide
library(tmap)
tm_shape(incongr_wgs) +
tm_polygons(col = "grey5") +
Expand Down Expand Up @@ -922,11 +925,9 @@ library(rstac)
# and search for images intersecting our AOI
s = stac("https://earth-search.aws.element84.com/v0")
items = s |>
stac_search(
collections = "sentinel-s2-l2a-cogs",
bbox = c(7.1, 51.8, 7.2, 52.8),
datetime = "2020-01-01/2020-12-31"
) |>
stac_search(collections = "sentinel-s2-l2a-cogs",
bbox = c(7.1, 51.8, 7.2, 52.8),
datetime = "2020-01-01/2020-12-31") |>
post_request() |>
items_fetch()
```
Expand Down Expand Up @@ -993,36 +994,26 @@ p = processes() # load available processes
collections = list_collections() # load available collections
formats = list_file_formats() # load available output formats
# Load Sentinel-2 collection
s2 = p$load_collection(
id = "SENTINEL2_L2A",
spatial_extent = list(
west = 7.5, east = 8.5,
north = 51.1, south = 50.1
),
temporal_extent = list("2021-01-01", "2021-01-31"),
bands = list("B04", "B08")
)
s2 = p$load_collection(id = "SENTINEL2_L2A",
spatial_extent = list(west = 7.5, east = 8.5,
north = 51.1, south = 50.1),
temporal_extent = list("2021-01-01", "2021-01-31"),
bands = list("B04", "B08"))
# Compute NDVI vegetation index
compute_ndvi = p$reduce_dimension(
data = s2, dimension = "bands",
reducer = function(data, context) {
(data[2] - data[1]) / (data[2] + data[1])
}
)
compute_ndvi = p$reduce_dimension(data = s2, dimension = "bands",
reducer = function(data, context) {
(data[2] - data[1]) / (data[2] + data[1])
})
# Compute maximum over time
reduce_max = p$reduce_dimension(
data = compute_ndvi, dimension = "t",
reducer = function(x, y) {
max(x)
}
)
reduce_max = p$reduce_dimension(data = compute_ndvi, dimension = "t",
reducer = function(x, y) {
max(x)
})
# Export as GeoTIFF
result = p$save_result(reduce_max, formats$output$GTiff)
# Login, see https://docs.openeo.cloud/getting-started/r/#authentication
login(
login_type = "oidc", provider = "egi",
config = list(client_id = "...", secret = "...")
)
login(login_type = "oidc", provider = "egi",
config = list(client_id = "...", secret = "..."))
# Execute processes
compute_result(graph = result, output_file = tempfile(fileext = ".tif"))
```
Expand Down
1 change: 1 addition & 0 deletions 16-synthesis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ A feature of R\index{R}, and open source software in general, is that there are
The code chunk below illustrates this by using three functions, covered in Chapters \@ref(attr) and \@ref(geometry-operations), to combine the 16 regions of New Zealand into a single geometry:

```{r 16-synnthesis-1}
#| message: FALSE
library(spData)
nz_u1 = sf::st_union(nz)
nz_u2 = aggregate(nz["Population"], list(rep(1, nrow(nz))), sum)
Expand Down

0 comments on commit 1d3d626

Please sign in to comment.