Introduction

This is an exercise and checklist to make sure that your system is correctly set up for the Using R for Spatial Data Analysis lecture.

A script with the R code of this document is available here:

https://github.com/michaeldorman/R-Spatial-Workshop-at-CBS-2021/raw/main/preparation.R

The Rmarkdown file is available here:

https://github.com/michaeldorman/R-Spatial-Workshop-at-CBS-2021/raw/main/preparation.Rmd

All of the materials are also available on GitHub.

Setting up R environemnt

To setup the R environment:

  • Install R
  • Install RStudio
  • In R, install the sf package:
install.packages("sf")

You should now be able to load the sf package as follows:

library(sf)

Sample data

Now, download the ZIP file with sample data from:

https://github.com/michaeldorman/R-Spatial-Workshop-at-CBS-2021/raw/main/data.zip

The file contains two Shapefiles and one Geodatabase (Table 1).

Table 1: Sample data
Data File(s) Format Source
“Nafot” nafot.shp (+7) Shapefile https://www.gov.il/he/Departments/Guides/info-gis
Railways RAIL_STRATEGIC.shp (+7) Shapefile https://data.gov.il/dataset/rail_strategic
Statistical areas statisticalareas_demography2018.gdb GDB https://www.cbs.gov.il/he/Pages/geo-layers.aspx

Extract the contents of the ZIP file and set the working directory to that location using an expression such as (use your own path):

setwd("C:\\Data\\CSB")

Alternatively, run the following expressions to automatically download and unzip the sample data into your current working directory:

url = "https://github.com/michaeldorman/R-Spatial-Workshop-at-CBS-2021/raw/main/data.zip"
download.file(url, "data.zip")
unzip("data.zip")

Reading the layers

If all worked well, the following expressions will import the three layers into R.

Note that the file names are prefixed with data/ when the layers are in a sub-directory named data, inside the working directory.

nafot = st_read("data/nafot.shp")
## Reading layer `nafot' from data source `/home/michael/Sync/Presentations/p_2021_01_LMS_Intro_to_Spatial_R/data/nafot.shp' using driver `ESRI Shapefile'
## Simple feature collection with 15 features and 1 field
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 620662.1 ymin: 3263494 xmax: 770624.4 ymax: 3691834
## projected CRS:  WGS 84 / UTM zone 36N
rail = st_read("data/RAIL_STRATEGIC.shp", options = "ENCODING=UTF-8")
## options:        ENCODING=UTF-8 
## Reading layer `RAIL_STRATEGIC' from data source `/home/michael/Sync/Presentations/p_2021_01_LMS_Intro_to_Spatial_R/data/RAIL_STRATEGIC.shp' using driver `ESRI Shapefile'
## Simple feature collection with 217 features and 6 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: 157822.2 ymin: 385552.2 xmax: 255113.7 ymax: 790663.5
## projected CRS:  Israel 1993 / Israeli TM Grid
stat = st_read("data/statisticalareas_demography2018.gdb", options = "ENCODING=UTF-8")
## options:        ENCODING=UTF-8 
## Reading layer `statisticalareas_demography2018' from data source `/home/michael/Sync/Presentations/p_2021_01_LMS_Intro_to_Spatial_R/data/statisticalareas_demography2018.gdb' using driver `OpenFileGDB'
## Simple feature collection with 3194 features and 34 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: 130175.2 ymin: 378139.7 xmax: 284068.5 ymax: 804530.3
## projected CRS:  Israel 1993 / Israeli TM Grid

If the files are in the working directory, use just the file name, as follows:

nafot = st_read("nafot.shp")
rail = st_read("RAIL_STRATEGIC.shp")
stat = st_read("statisticalareas_demography2018.gdb")

Plotting the layers

Let’s try to plot the layers (Figures 1, 2, and 3):

plot(nafot, key.width = lcm(4))
The `nafot` layer

Figure 1: The nafot layer

plot(rail)
The `rail` layer

Figure 2: The rail layer

plot(stat)
## Warning: plotting the first 10 out of 34 attributes; use max.plot = 34 to plot
## all
The `stat` layer

Figure 3: The stat layer

Using Hebrew text in R

Finally, to make sure that we can work with Hebrew in R, try to subset the active railway lines with the following expression:

rail = rail[rail$ISACTIVE == "פעיל", ]

and then plot the subset (Figure 4):

plot(rail)
The `rail` layer, subset of active railway lines

Figure 4: The rail layer, subset of active railway lines

In case you are working with the R script file or the Rmarkdown file, to properly display the Hebrew text the file needs to be opened with the UTF-8 encoding. To do that:

  • Open the file in RStudio, by double-click or through File→Open File…
  • Click on File→Reopen with Encoding
  • Select UTF-8 from the list, and click OK (Figure 5)
Reopening an R script with the UTF-8 encoding in RStudio

Figure 5: Reopening an R script with the UTF-8 encoding in RStudio

Prerequisites

To follow the code examples in the lecture, you should be familiar with the following topics and aspects of the R language:

  • Installing R and RStudio
  • Installing and loading packages
  • Working with vectors, matrix and data.frame
  • Arithmetic operators (+, -, …)
  • Conditional operators (<, ==, …)
  • Subsetting ([, $)