From e2aa5f49a0df7facbe7b28801abcd099dee71933 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Wed, 6 Sep 2023 14:12:49 +0100 Subject: [PATCH 01/12] Tweak intro --- 01-introduction.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01-introduction.Rmd b/01-introduction.Rmd index 945a4e2d0..6943903ab 100644 --- a/01-introduction.Rmd +++ b/01-introduction.Rmd @@ -5,7 +5,7 @@ # Introduction {#intro} This book is about using the power of computers to *do things* with geographic data. -It teaches a range of spatial skills, including: reading, writing and manipulating geographic data; making static and interactive maps; applying geocomputation\index{geocomputation} to support more evidence-based decision making; and modeling a range of geographic phenomena, from ecosystems to transport systems. +It teaches a range of spatial skills, including: reading, writing and manipulating geographic file formats; making static and interactive maps; and applying geocomputation\index{geocomputation} to support more evidence-based decision making related to a range of geographic phenomena, from ecosystems to transport systems. By demonstrating how various geographic operations can be linked, in 'code chunks' that intersperse the prose, the book also teaches reproducible, open and thus scientific workflows. Learning how to use the wealth of *existing tools* for geocomputation can be exciting. From 370fbce37fcd45fbdab53b791a8fbca93c84beb5 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Wed, 6 Sep 2023 14:19:45 +0100 Subject: [PATCH 02/12] Shorten intro --- 01-introduction.Rmd | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/01-introduction.Rmd b/01-introduction.Rmd index 6943903ab..6037e6f8d 100644 --- a/01-introduction.Rmd +++ b/01-introduction.Rmd @@ -8,11 +8,10 @@ This book is about using the power of computers to *do things* with geographic d It teaches a range of spatial skills, including: reading, writing and manipulating geographic file formats; making static and interactive maps; and applying geocomputation\index{geocomputation} to support more evidence-based decision making related to a range of geographic phenomena, from ecosystems to transport systems. By demonstrating how various geographic operations can be linked, in 'code chunks' that intersperse the prose, the book also teaches reproducible, open and thus scientific workflows. -Learning how to use the wealth of *existing tools* for geocomputation can be exciting. -Creating *new tools* can be truly liberating. -The approach we teach throughout, and programming techniques covered in Chapter \@ref(algorithms)\index{algorithm}, can remove constraints on your creativity imposed by software. +The book is not just about using the wealth of *existing tools* for geocomputation: it's also about understanding the structure of geographic datasets and software from processing them, and enabling the development *new tools*. +The approach we teach throughout, and programming techniques covered in Chapter \@ref(algorithms)\index{algorithm} in particular, can remove constraints on your creativity imposed by software. After reading the book and completing the exercises you should feel empowered with a strong understanding of the possibilities opened up by R's\index{R} impressive geographic capabilities. -We aim to give you the confidence to use geocomputation to tackle real-world problems, inspire you to communicate your work with maps and reproducible code, and equip you to participate in reproducible research, free software and other communities building and using geographic tools for social and environmental good. +By the end of the book we hope you will be ready to apply your skills tackle real-world problems, to communicate your work in maps and reproducible code, and to participate in reproducible research and free software communities. Over the last few decades free and open source software for geospatial (FOSS4G\index{FOSS4G}) has progressed at an astonishing rate. Thanks to organizations such as OSGeo, geographic data analysis is no longer the preserve of those with expensive hardware and software: anyone can now download and run high-performance spatial libraries. From c04b15499b90bf8a0f08ac8d63eb24943bc17c25 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Wed, 6 Sep 2023 14:54:33 +0100 Subject: [PATCH 03/12] Explain advantages of getting started with R --- 01-introduction.Rmd | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/01-introduction.Rmd b/01-introduction.Rmd index 6037e6f8d..8a8a7bbcf 100644 --- a/01-introduction.Rmd +++ b/01-introduction.Rmd @@ -14,12 +14,13 @@ After reading the book and completing the exercises you should feel empowered wi By the end of the book we hope you will be ready to apply your skills tackle real-world problems, to communicate your work in maps and reproducible code, and to participate in reproducible research and free software communities. Over the last few decades free and open source software for geospatial (FOSS4G\index{FOSS4G}) has progressed at an astonishing rate. -Thanks to organizations such as OSGeo, geographic data analysis is no longer the preserve of those with expensive hardware and software: anyone can now download and run high-performance spatial libraries. +Thanks to organizations such as OSGeo, advanced geographic techniques are no longer the preserve of those with expensive hardware and software: anyone can now download and run high-performance software for geocomputation. Open source Geographic Information Systems (GIS\index{GIS}), such as [QGIS](http://qgis.org/en/site/)\index{QGIS}, have made geographic analysis accessible worldwide. -GIS programs tend to emphasize graphical user interfaces\index{graphical user interface} (GUIs), with the unintended consequence of discouraging reproducibility\index{reproducibility} (although many can be used from the command line as we'll see in Chapter \@ref(gis)). -R, by contrast, emphasizes the command line interface\index{command-line interface} (CLI). +GIS software products such as QGIS are powerful, but tend to emphasize a graphical user interface\index{graphical user interface} (GUI) approach over the command-line interface (CLI) approach advocated in this book. +The 'GUI-focus' of many GIS products has unintended consequence of disabling many users from making their work full reproducible,\index{reproducibility} a problem that can be overcome by calling 'geoalgorithms' contained in GIS software from the command line, as we'll see in Chapter \@ref(gis)). A simplistic comparison between the different approaches is illustrated in Table \@ref(tab:gdsl). + ```{r gdsl, echo=FALSE, message=FALSE} d = readr::read_csv("extdata/gis-vs-gds-table.csv") knitr::kable(x = d, @@ -30,6 +31,12 @@ knitr::kable(x = d, booktabs = TRUE) ``` +R is not the only language providing CLIs for computation. +A range of interactive notebooks and integrated development environments enable powerful and reproducible workflows for geocomputation in other languages, such as Python, Julia, and JavaScript. +Many of the techniques taught in this book can be undertaken in other languages, and we encourage curious readers to give them a try, perhaps with reference to the (currently in-progress) book Geocomputation with Python. +However, R has many advantages for geocomputation. +These include its ease of installation, comprehensive and well-maintained core packages for geographic data that have 'batteries included' to allow you to dive in and undertake many technique in few lines of code, and outstanding visualisation capabilities. + This book is motivated by the importance of reproducibility\index{reproducibility} for scientific research (see the note below). It aims to make reproducible geographic data analysis\index{geographic data analysis} workflows more accessible, and demonstrate the power of open geospatial software available from the command-line. "Interfaces to other software are part of R" [@eddelbuettel_extending_2018]. From b478da558bbfb611abcf620a48511e7efd38b249 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Wed, 6 Sep 2023 15:45:34 +0100 Subject: [PATCH 04/12] More prose refactoring --- 01-introduction.Rmd | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/01-introduction.Rmd b/01-introduction.Rmd index 8a8a7bbcf..15f661bd4 100644 --- a/01-introduction.Rmd +++ b/01-introduction.Rmd @@ -33,11 +33,13 @@ knitr::kable(x = d, R is not the only language providing CLIs for computation. A range of interactive notebooks and integrated development environments enable powerful and reproducible workflows for geocomputation in other languages, such as Python, Julia, and JavaScript. -Many of the techniques taught in this book can be undertaken in other languages, and we encourage curious readers to give them a try, perhaps with reference to the (currently in-progress) book Geocomputation with Python. -However, R has many advantages for geocomputation. -These include its ease of installation, comprehensive and well-maintained core packages for geographic data that have 'batteries included' to allow you to dive in and undertake many technique in few lines of code, and outstanding visualisation capabilities. +Many of the techniques taught in this book can be undertaken in other languages, and we encourage curious readers to give them a try, perhaps with reference to the book Geocomputation with Python. +However, R has some advantages that make it well-suited to geocomputation. +The 'R-spatial stack' is easy to install, has comprehensive and well-maintained core packages meaning less 'context switching' and more focus on techniques rather than getting the code working. +R has 'batteries included' with statistical functions as part of the base installation and hundreds of well-mainted packages implementing many cutting edge methods. +With R, you can dive and get things working with surprisingly few lines of code, and generate publication quality interactive maps thanks to excellent mapping packages, as outlined in Chapter \@ref(adv-map). -This book is motivated by the importance of reproducibility\index{reproducibility} for scientific research (see the note below). +This book is also motivated by the importance of reproducibility\index{reproducibility} for scientific research. It aims to make reproducible geographic data analysis\index{geographic data analysis} workflows more accessible, and demonstrate the power of open geospatial software available from the command-line. "Interfaces to other software are part of R" [@eddelbuettel_extending_2018]. This means that in addition to outstanding 'in house' capabilities, R allows access to many other spatial software libraries, explained in Section \@ref(why-use-r-for-geocomputation) and demonstrated in Chapter \@ref(gis). From de42369e2554b8950bf6d254a99aff66e8319294 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Wed, 6 Sep 2023 18:58:39 +0100 Subject: [PATCH 05/12] Turn some things off, fix CI See https://github.com/geocompx/geocompr/issues/985 and https://github.com/r-tmap/tmap/issues/766 --- 02-spatial-data.Rmd | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/02-spatial-data.Rmd b/02-spatial-data.Rmd index c00dd2657..d94a57129 100644 --- a/02-spatial-data.Rmd +++ b/02-spatial-data.Rmd @@ -41,6 +41,7 @@ install.packages("spDataLarge", repos = "https://nowosad.r-universe.dev") #| message: FALSE #| results: hide remotes::install_github("r-tmap/tmap@v4") +tmap_mode("plot") ``` \index{R!installation} @@ -892,7 +893,8 @@ However, in contrast to vector data, the cell of one raster layer can only hold The value might be numeric or categorical (Figure \@ref(fig:raster-intro-plot):C). ```{r raster-intro-plot, echo = FALSE, fig.cap = "Raster data types: (A) cell IDs, (B) cell values, (C) a colored raster map.", fig.scap="Raster data types.", fig.asp=0.5, message=FALSE} -source("https://github.com/geocompx/geocompr/raw/main/code/02-raster-intro-plot.R", print.eval = TRUE) +# TODO: uncomment when https://github.com/r-tmap/tmap/issues/766 is fixed +# source("https://github.com/geocompx/geocompr/raw/main/code/02-raster-intro-plot.R", print.eval = TRUE) ``` Raster maps usually represent continuous phenomena such as elevation, temperature, population density or spectral data. @@ -1034,14 +1036,14 @@ The `[[` and `$` operators can also be used to select layers, for example with c ] It accepts a layer number or its name as the second argument: -```{r} +```{r, eval=FALSE} multi_rast3 = subset(multi_rast, 3) multi_rast4 = subset(multi_rast, "landsat_4") ``` The opposite operation, combining several `SpatRaster` objects into one, can be done using the `c` function: -```{r} +```{r, eval=FALSE} multi_rast34 = c(multi_rast3, multi_rast4) ``` From 9a7377bce9c62722fbc8ad5978852bdc0fbf46e4 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Wed, 6 Sep 2023 19:09:52 +0100 Subject: [PATCH 06/12] Remove errant tmap_mode() --- 02-spatial-data.Rmd | 1 - 1 file changed, 1 deletion(-) diff --git a/02-spatial-data.Rmd b/02-spatial-data.Rmd index d94a57129..626ab8516 100644 --- a/02-spatial-data.Rmd +++ b/02-spatial-data.Rmd @@ -41,7 +41,6 @@ install.packages("spDataLarge", repos = "https://nowosad.r-universe.dev") #| message: FALSE #| results: hide remotes::install_github("r-tmap/tmap@v4") -tmap_mode("plot") ``` \index{R!installation} From 96be19771c23a29cb615690769b78073057c9d12 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Thu, 7 Sep 2023 05:32:40 +0100 Subject: [PATCH 07/12] Remove magick dependency --- code/09-urban-animation.R | 4 +- .../04_spatial_incongruence.R | 61 ------------------- 2 files changed, 2 insertions(+), 63 deletions(-) delete mode 100644 code/old-to-future-remove/04_spatial_incongruence.R diff --git a/code/09-urban-animation.R b/code/09-urban-animation.R index 057fde6ce..dd5c79ff6 100644 --- a/code/09-urban-animation.R +++ b/code/09-urban-animation.R @@ -9,5 +9,5 @@ m_save = tm_shape(world2) + tm_symbols(size = "population_millions", size.legend = tm_legend(title = "Population (m)"), fill = "red", fill_alpha = 0.5) + tm_facets(by = "year", nrow = 1, ncol = 1, free.coords = FALSE) -tmap:::tmap_animation(tm = m_save, filename = "/tmp/urban-animated.gif", width = 1200, height = 800) -magick::image_read("/tmp/urban-animated.gif") +tmap::tmap_animation(tm = m_save, filename = "/tmp/urban-animated.gif", width = 1200, height = 800) +# magick::image_read("/tmp/urban-animated.gif") diff --git a/code/old-to-future-remove/04_spatial_incongruence.R b/code/old-to-future-remove/04_spatial_incongruence.R deleted file mode 100644 index bc39979ec..000000000 --- a/code/old-to-future-remove/04_spatial_incongruence.R +++ /dev/null @@ -1,61 +0,0 @@ -# Filename: 04_spatial_incongruence.R (2017-11-03) -# -# TO DO: Difference between admin and postal code layers -# -# Author(s): Jannes Muenchow -# -#********************************************************** -# CONTENTS------------------------------------------------- -#********************************************************** -# -# 1. ATTACH PACKAGES AND DATA -# 2. -# -#********************************************************** -# 1 ATTACH PACKAGES AND DATA------------------------------- -#********************************************************** - -# attach packages -library(raster) -library(sp) -library(sf) -library(tidyverse) - -# download data -download.file(paste0("http://www.suche-postleitzahl.org/downloads?", - "download_file=plz-gebiete.shp.zip"), - destfile = file.path(tempdir(), "pc_germ.zip")) -unzip(zipfile = file.path(tempdir(), "pc_germ.zip"), exdir = tempdir()) -# load PC shapefile -shp_pc = read_sf(file.path(tempdir(), "plz-gebiete.shp")) - -ger_mun = getData(country = "DEU", level = 3) -ger_dis = getData(country = "DEU", level = 2) -ger_mun = st_as_sf(ger_mun) -ger_dis = st_as_sf(ger_dis) -save.image("congruence.Rdata") - - -weimar = filter(ger_dis, NAME_2 %in% "Weimar") -weimar_2 = ger_dis[weimar, ] -plot(st_geometry(ger_dis[weimar, ])) -ind = unlist(st_contains(ger_dis[weimar, ], ger_mun)) -plot(st_geometry(ger_dis[weimar, ]), border = "steelblue2", add = TRUE, lwd = 5) -plot(st_geometry(ger_mun[ind, ]), border = "red3", add = TRUE) -# one polygon is missing, don't know why... -plot(st_geometry(ger_mun[weimar_2, ][17, ]), add = TRUE, border = "green") -ger_mun[weimar_2, ][17, ] # 4674 -ind = c(ind, 4674) -plot(st_geometry(ger_dis[weimar, ]), border = "steelblue2", lwd = 5) -plot(st_geometry(ger_mun[ind, ]), border = "red3", add = TRUE) - -# show postal code problem -plot(st_geometry(shp_pc[weimar, ]), lwd = 3, border = "red3") # pcs around Weimar -plot(st_geometry(weimar), add = TRUE, border = "steelblue2", lwd = 2) # weimar district/municipality -# the same as, i.e., Weimar municipality is also a district -# plot(st_geometry(filter(ger_mun, NAME_3 %in% "Weimar")), add = TRUE, border = "blue") -png(filename = "figures/04_incongruence_example.png", width = 950, height = 555) -# show postal code problem -plot(st_geometry(shp_pc[weimar, ]), lwd = 3, border = "red3") # pcs around Weimar -plot(st_geometry(weimar), add = TRUE, border = "steelblue2", lwd = 2) # weimar district/municipality -dev.off() From 46a5467f8620b602fd18755ef01c96725424414a Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Thu, 7 Sep 2023 05:44:12 +0100 Subject: [PATCH 08/12] Uncomment another line... --- 02-spatial-data.Rmd | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/02-spatial-data.Rmd b/02-spatial-data.Rmd index 626ab8516..f002e35e1 100644 --- a/02-spatial-data.Rmd +++ b/02-spatial-data.Rmd @@ -311,7 +311,7 @@ For workflows that depend on the legacy class system, `sf` objects can be conver library(sp) world_sp = as(world, "Spatial") # from an sf object to sp # sp functions ... -world_sf = st_as_sf(world_sp) # from sp to sf +world_sf = st_as_sf(world_sp) # from sp to sf ``` ### Basic map making {#basic-map} @@ -902,8 +902,9 @@ Both uses of raster datasets are illustrated in Figure \@ref(fig:raster-intro-pl Depending on the nature of the application, vector representations of discrete features may be more suitable. ```{r raster-intro-plot2, echo=FALSE, fig.cap="Examples of continuous and categorical rasters.", warning=FALSE, message=FALSE} -source("code/02-raster-intro-plot2.R", print.eval = TRUE) -# knitr::include_graphics("https://user-images.githubusercontent.com/1825120/146617327-45919232-a6a3-4d9d-a158-afa87f47381b.png") +# TODO: uncomment when code is fixed: https://github.com/r-tmap/tmap/issues/768 +# source("code/02-raster-intro-plot2.R", print.eval = TRUE) +knitr::include_graphics("https://user-images.githubusercontent.com/1825120/146617327-45919232-a6a3-4d9d-a158-afa87f47381b.png") ``` ### R packages for working with raster data From f41e2a8bf69c9a990325ae92a7737e7c18d8a8bc Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Fri, 8 Sep 2023 17:26:53 +0100 Subject: [PATCH 09/12] Use ghcr for binder image --- .binder/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.binder/Dockerfile b/.binder/Dockerfile index 820244b7b..2a80a720a 100644 --- a/.binder/Dockerfile +++ b/.binder/Dockerfile @@ -1,4 +1,4 @@ -FROM geocompr/geocompr:binder +FROM ghcr.io/geocompx/docker:binder ## Declares build arguments ARG NB_USER From 34fc0f53bbc4f6c02741767166c4dc94ecb64589 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Fri, 8 Sep 2023 17:29:08 +0100 Subject: [PATCH 10/12] Uncomment tmap-related lines --- 02-spatial-data.Rmd | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/02-spatial-data.Rmd b/02-spatial-data.Rmd index f002e35e1..e6076f059 100644 --- a/02-spatial-data.Rmd +++ b/02-spatial-data.Rmd @@ -892,8 +892,7 @@ However, in contrast to vector data, the cell of one raster layer can only hold The value might be numeric or categorical (Figure \@ref(fig:raster-intro-plot):C). ```{r raster-intro-plot, echo = FALSE, fig.cap = "Raster data types: (A) cell IDs, (B) cell values, (C) a colored raster map.", fig.scap="Raster data types.", fig.asp=0.5, message=FALSE} -# TODO: uncomment when https://github.com/r-tmap/tmap/issues/766 is fixed -# source("https://github.com/geocompx/geocompr/raw/main/code/02-raster-intro-plot.R", print.eval = TRUE) +source("https://github.com/geocompx/geocompr/raw/main/code/02-raster-intro-plot.R", print.eval = TRUE) ``` Raster maps usually represent continuous phenomena such as elevation, temperature, population density or spectral data. @@ -902,9 +901,8 @@ Both uses of raster datasets are illustrated in Figure \@ref(fig:raster-intro-pl Depending on the nature of the application, vector representations of discrete features may be more suitable. ```{r raster-intro-plot2, echo=FALSE, fig.cap="Examples of continuous and categorical rasters.", warning=FALSE, message=FALSE} -# TODO: uncomment when code is fixed: https://github.com/r-tmap/tmap/issues/768 -# source("code/02-raster-intro-plot2.R", print.eval = TRUE) -knitr::include_graphics("https://user-images.githubusercontent.com/1825120/146617327-45919232-a6a3-4d9d-a158-afa87f47381b.png") +source("code/02-raster-intro-plot2.R", print.eval = TRUE) +# knitr::include_graphics("https://user-images.githubusercontent.com/1825120/146617327-45919232-a6a3-4d9d-a158-afa87f47381b.png") ``` ### R packages for working with raster data From cf0ad5377ab7c741fed22f6640a5041b0d8a500e Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Fri, 8 Sep 2023 17:38:49 +0100 Subject: [PATCH 11/12] Improve description of geocomp in other languages --- 01-introduction.Rmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/01-introduction.Rmd b/01-introduction.Rmd index 15f661bd4..f6a5f3465 100644 --- a/01-introduction.Rmd +++ b/01-introduction.Rmd @@ -31,9 +31,9 @@ knitr::kable(x = d, booktabs = TRUE) ``` -R is not the only language providing CLIs for computation. -A range of interactive notebooks and integrated development environments enable powerful and reproducible workflows for geocomputation in other languages, such as Python, Julia, and JavaScript. -Many of the techniques taught in this book can be undertaken in other languages, and we encourage curious readers to give them a try, perhaps with reference to the book Geocomputation with Python. +R is not the only language providing a CLI for geocomputation. +Other command environments with powerful geographic capabilities exist, including Python/IPython, Julia, and JavaScript. +We encourage curious readers to give them a try and to reproduce the examples in this book in other languages, perhaps with reference to the book Geocomputation with Python. However, R has some advantages that make it well-suited to geocomputation. The 'R-spatial stack' is easy to install, has comprehensive and well-maintained core packages meaning less 'context switching' and more focus on techniques rather than getting the code working. R has 'batteries included' with statistical functions as part of the base installation and hundreds of well-mainted packages implementing many cutting edge methods. From e93a67c7678763aef5ef35614946501693912b9c Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Fri, 8 Sep 2023 17:39:53 +0100 Subject: [PATCH 12/12] Add link to geocompy --- 01-introduction.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01-introduction.Rmd b/01-introduction.Rmd index f6a5f3465..2b730fe44 100644 --- a/01-introduction.Rmd +++ b/01-introduction.Rmd @@ -33,7 +33,7 @@ knitr::kable(x = d, R is not the only language providing a CLI for geocomputation. Other command environments with powerful geographic capabilities exist, including Python/IPython, Julia, and JavaScript. -We encourage curious readers to give them a try and to reproduce the examples in this book in other languages, perhaps with reference to the book Geocomputation with Python. +We encourage curious readers to give them a try and to reproduce the examples in this book in other languages, perhaps with reference to the book [Geocomputation with Python](https://py.geocompx.org/), the open access version of which is also hosted on [geocompx.org](https://geocompx.org/). However, R has some advantages that make it well-suited to geocomputation. The 'R-spatial stack' is easy to install, has comprehensive and well-maintained core packages meaning less 'context switching' and more focus on techniques rather than getting the code working. R has 'batteries included' with statistical functions as part of the base installation and hundreds of well-mainted packages implementing many cutting edge methods.