Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?


Failed to load latest commit information.
Latest commit message
Commit time
January 13, 2023 18:36
March 5, 2023 00:37
March 17, 2023 10:05
January 5, 2023 11:13
March 5, 2023 00:37
October 18, 2021 11:44
February 17, 2023 10:09
April 10, 2020 22:12
February 15, 2020 19:41
February 12, 2022 22:38
January 13, 2023 18:36
March 7, 2017 11:41
February 24, 2023 22:28
March 21, 2023 10:20
September 6, 2021 09:09
November 14, 2019 15:59
March 4, 2022 08:27
January 5, 2023 11:33
April 9, 2018 21:10
February 20, 2023 21:48
February 21, 2023 10:32
January 7, 2023 11:34
December 21, 2022 22:02
February 27, 2023 10:31
December 11, 2022 17:29
October 5, 2022 13:17
February 21, 2023 10:15
February 21, 2023 10:11
February 17, 2023 10:42
February 25, 2023 19:45
June 2, 2017 19:42
February 20, 2023 21:48
January 5, 2023 11:33
August 15, 2018 20:34
February 12, 2022 22:38

Geocomputation with R

Binder RstudioCloud Actions Docker discord Open in GitHub Codespaces


This repository hosts the code underlying Geocomputation with R, a book by Robin Lovelace, Jakub Nowosad, and Jannes Muenchow. If you find the contents useful, please cite it as follows:

Lovelace, Robin, Jakub Nowosad and Jannes Muenchow (2019). Geocomputation with R. The R Series. CRC Press.

The first version of the book has been published by CRC Press in the R Series and can be viewed online at Read the latest version at

Note: we are actively working on the Second Edition 🏗

Summary of the changes

Since commencing work on the Second Edition in September 2021 much has changed, including:

  • Replacement of raster with terra in Chapters 1 to 7 (see commits related to this update here)
  • Update of Chapter 7 to include mention alternative ways or reading-in OSM data in #656
  • Refactor build settings so the book builds on Docker images in the geocompr/docker repo
  • Improve the experience of using the book in Binder (ideal for trying out the code before installing or updating the necessary R packages), as documented in issue #691 (thanks to yuvipanda)
  • Improved communication of binary spatial predicates in Chapter 4 (see #675)
  • New section on the links between subsetting and clipping (see #698) in Chapter 5
  • New section on the dimensionally extended 9 intersection model (DE-9IM)
  • New chapter on raster-vector interactions split out from Chapter 5
  • New section on the sfheaders package
  • New section in Chapter 2 on spherical geometry engines and the s2 package
  • Replacement of code based on the old mlr package with code based on the new mlr3 package, as described in a huge pull request

See…main for a continuously updated summary of the changes to date. At the time of writing (April 2022) there have been more than 10k lines of code/prose added, lots of refactoring!

Contributions at this stage are very welcome.


We encourage contributions on any part of the book, including:

  • improvements to the text, e.g. clarifying unclear sentences, fixing typos (see guidance from Yihui Xie);
  • changes to the code, e.g. to do things in a more efficient way;
  • suggestions on content (see the project’s issue tracker);
  • improvements to and alternative approaches in the Geocompr solutions booklet hosted at (see a blog post on how to update solutions in files such as _01-ex.Rmd here)

See for the book’s style.

Many thanks to all contributors to the book so far via GitHub (this list will update automatically): prosoitos, florisvdh, katygregg, Lvulis, rsbivand, iod-ine, babayoshihiko, KiranmayiV, cuixueqin, defuneste, zmbc, erstearns, FlorentBedecarratsNM, dcooley, darrellcarvalho, marcosci, appelmar, MikeJohnPage, eyesofbambi, krystof236, nickbearman, tyluRp, giocomai, KHwong12, LaurieLBaker, MarHer90, mdsumner, pat-s, sdesabbata, e-clin, gisma, ateucher, annakrystalli, andtheWings, kant, gavinsimpson, Himanshuteli, yutannihilation, jimr1603, jbixon13, olyerickson, yvkschaefer, katiejolly, kwhkim, layik, mpaulacaldas, mtennekes, mvl22, ganes1410, richfitz, wdearden, yihui, adambhouston, chihinl, cshancock, ec-nebi, gregor-d, jasongrahn, p-kono, pokyah, schuetzingit, tim-salabim, tszberkowitz.

During the project we aim to contribute ‘upstream’ to the packages that make geocomputation with R possible. This impact is recorded in our-impact.csv.

Downloading the source code

The recommended way to get the source code underlying Geocomputation with R on your computer is by cloning the repo. You can can that on any computer with Git installed with the following command:

git clone

An alternative approach, which we recommend for people who want to contribute to open source projects hosted on GitHub, is to install the gh CLI tool. From there cloning a fork of the source code, that you can change and share (including with Pull Requests to improve the book), can be done with the following command:

gh repo fork geocompx/geocompr # (gh repo clone geocompx/geocompr # also works)

Both of those methods require you to have Git installed. If not, you can download the book’s source code from the URL . Download/unzip the source code from the R command line to increase reproducibility and reduce time spent clicking around:

u = ""
f = basename(u)
download.file(u, f)        # download the file
unzip(f)                   # unzip it
file.rename(f, "geocompr") # rename the directory
rstudioapi::openProject("geococompr") # or open the folder in vscode / other IDE

Reproducing the book in R/RStudio/VS Code

To ease reproducibility, we created the geocompkg package. Install it with the following commands:

# To reproduce the first Part (chapters 1 to 8):

Installing geocompkg will also install core packages required for reproducing Part 1 of the book (chapters 1 to 8). Note: you may also need to install system dependencies if you’re running Linux (recommended) or Mac operating systems. You also need to have the remotes package installed:

To reproduce book in its entirety, run the following command (which installs additional ‘Suggests’ packages, this may take some time to run!):

# To reproduce all chapters (install lots of packages, may take some time!)
remotes::install_github("geocompr/geocompkg", dependencies = TRUE)

You need a recent version of the GDAL, GEOS, PROJ and udunits libraries installed for this to work on Mac and Linux. See the sf package’s README for information on that. After the dependencies have been installed you should be able to build and view a local version the book with:

# Change this depending on where you have the book code stored:
 # or code /location/of/geocompr in the system terminal
 # or cd /location/of/geocompr then R in the system terminal, then:
bookdown::render_book("index.Rmd") # to build the book
browseURL("_book/index.html")      # to view it
# Or, to serve a live preview the book and observe impact of changes:

Geocompr in a devcontainer

A great feature of VS Code is devcontainers, which allow you to develop in an isolated Docker container. If you have VS Code and the necessary dependencies installed on your computer, you can build Geocomputation with R in a devcontainer as shown below (see #873 for details):

Geocompr in Binder

For many people the quickest way to get started with Geocomputation with R is in your web browser via Binder. To see an interactive RStudio Server instance click on the following button, which will open with an R installation that has all the dependencies needed to reproduce the book:

Launch Rstudio Binder

You can also have a play with the repo in RStudio Cloud by clicking on this link (requires log-in):

Launch Rstudio Cloud

Geocomputation with R in a Docker container

To ease reproducibility we have made Docker images available, at geocompr/geocompr on DockerHub. These images allow you to explore Geocomputation with R in a virtual machine that has up-to-date dependencies.

After you have installed docker and set-it up on your computer you can start RStudio Server without a password (see the Rocker project for info on how to add a password and other security steps for public-facing servers):

docker run -p 8787:8787 -e DISABLE_AUTH=TRUE geocompr/geocompr

If it worked you should be able to open-up RStudio server by opening a browser and navigating to http://localhost:8787/ resulting in an up-to-date version of R and RStudio running in a container.

Start a plain R session running:

docker run -it geocompr/geocompr R

If you see something like this after following the steps above, congratulations: it worked! See for more info.

If you want to call QGIS from R, you can use the qgis tag, by running the following command for example (which also shows how to set a password and use a different port on localhost):

docker run -d -p 8799:8787 -e USERID=$UID -e PASSWORD=strongpass -v $(pwd):/home/rstudio/geocompr geocompx/geocompr:qgis

From this point to build the book you can open projects in the geocompr directory from the project box in the top-right hand corner, and knit index.Rmd with the little knit button above the the RStudio script panel (Ctl+Shift+B should do the same job).

See the geocompr/docker repo for details, including how to share volumes between your computer and the Docker image, for using geographic R packages on your own data and for information on available tags.

Reproducing this README

To reduce the book’s dependencies, scripts to be run infrequently to generate input for the book are run on creation of this README.

The additional packages required for this can be installed as follows:


With these additional dependencies installed, you should be able to run the following scripts, which create content for the book, that we’ve removed from the main book build to reduce package dependencies and the book’s build time:


Note: the .Rproj file is configured to build a website not a single page. To reproduce this README use the following command:

rmarkdown::render("README.Rmd", output_format = "github_document", output_file = "")


To cite packages used in this book we use code from Efficient R Programming:

# geocompkg:::generate_citations()

This generates .bib and .csv files containing the packages. The current of packages used can be read-in as follows:

pkg_df = readr::read_csv("extdata/package_list.csv")

Other citations are stored online using Zotero.

If you would like to add to the references, please use Zotero, join the open group add your citation to the open geocompr library.

We use the following citation key format:


This can be set from inside Zotero desktop with the Better Bibtex plugin installed (see by selecting the following menu options (with the shortcut Alt+E followed by N), and as illustrated in the figure below:

Edit > Preferences > Better Bibtex

Zotero settings: these are useful if you want to add references.

We use Zotero because it is a powerful open source reference manager that integrates well with the citr package. As described in the GitHub repo Robinlovelace/rmarkdown-citr-demo.