2021-01-19
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.
To setup the R environment:
sf
package:install.packages("sf")
You should now be able to load the sf
package as follows:
library(sf)
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).
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")
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")
Let’s try to plot the layers (Figures 1, 2, and 3):
plot(nafot, key.width = lcm(4))
plot(rail)
plot(stat)
## Warning: plotting the first 10 out of 34 attributes; use max.plot = 34 to plot
## all
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)
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:
To follow the code examples in the lecture, you should be familiar with the following topics and aspects of the R language:
matrix
and data.frame
+
, -
, …)<
, ==
, …)[
, $
)