diff --git a/instructors/2-vector-slides.html b/instructors/2-vector-slides.html index fa3570cd..076caf95 100644 --- a/instructors/2-vector-slides.html +++ b/instructors/2-vector-slides.html @@ -29,6 +29,70 @@ margin: 0 0.8em 0.2em -1.6em; vertical-align: middle; } + pre > code.sourceCode { white-space: pre; position: relative; } + pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } + pre > code.sourceCode > span:empty { height: 1.2em; } + .sourceCode { overflow: visible; } + code.sourceCode > span { color: inherit; text-decoration: inherit; } + div.sourceCode { margin: 1em 0; } + pre.sourceCode { margin: 0; } + @media screen { + div.sourceCode { overflow: auto; } + } + @media print { + pre > code.sourceCode { white-space: pre-wrap; } + pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } + } + pre.numberSource code + { counter-reset: source-line 0; } + pre.numberSource code > span + { position: relative; left: -4em; counter-increment: source-line; } + pre.numberSource code > span > a:first-child::before + { content: counter(source-line); + position: relative; left: -1em; text-align: right; vertical-align: baseline; + border: none; display: inline-block; + -webkit-touch-callout: none; -webkit-user-select: none; + -khtml-user-select: none; -moz-user-select: none; + -ms-user-select: none; user-select: none; + padding: 0 4px; width: 4em; + color: #aaaaaa; + } + pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } + div.sourceCode + { color: #003b4f; background-color: #f1f3f5; } + @media screen { + pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } + } + code span { color: #003b4f; } /* Normal */ + code span.al { color: #ad0000; } /* Alert */ + code span.an { color: #5e5e5e; } /* Annotation */ + code span.at { color: #657422; } /* Attribute */ + code span.bn { color: #ad0000; } /* BaseN */ + code span.bu { } /* BuiltIn */ + code span.cf { color: #003b4f; } /* ControlFlow */ + code span.ch { color: #20794d; } /* Char */ + code span.cn { color: #8f5902; } /* Constant */ + code span.co { color: #5e5e5e; } /* Comment */ + code span.cv { color: #5e5e5e; font-style: italic; } /* CommentVar */ + code span.do { color: #5e5e5e; font-style: italic; } /* Documentation */ + code span.dt { color: #ad0000; } /* DataType */ + code span.dv { color: #ad0000; } /* DecVal */ + code span.er { color: #ad0000; } /* Error */ + code span.ex { } /* Extension */ + code span.fl { color: #ad0000; } /* Float */ + code span.fu { color: #4758ab; } /* Function */ + code span.im { color: #00769e; } /* Import */ + code span.in { color: #5e5e5e; } /* Information */ + code span.kw { color: #003b4f; } /* Keyword */ + code span.op { color: #5e5e5e; } /* Operator */ + code span.ot { color: #003b4f; } /* Other */ + code span.pp { color: #ad0000; } /* Preprocessor */ + code span.sc { color: #5e5e5e; } /* SpecialChar */ + code span.ss { color: #20794d; } /* SpecialString */ + code span.st { color: #20794d; } /* String */ + code span.va { color: #111111; } /* Variable */ + code span.vs { color: #20794d; } /* VerbatimString */ + code span.wa { color: #5e5e5e; font-style: italic; } /* Warning */ @@ -345,25 +409,35 @@

Intro to Geospatial Vector Data with R

-
-

sf package for spatial vector data

+
+

Intro to Geospatial Concepts

+ +
+ +
+
+

Geospatial Vector Data in R

+ +
+
+

The sf package

-
-

sf package for spatial vector data

+
+

The sf package

  • sf provides simple features access to R
  • -
  • represents simple features as records in a data.frame or tibble with a geometry column
  • +
  • represents simple features as records in a data frame with a geometry column
  • function names start with a prefix st_ (β€œst” stands for spatial type), which is also handy for auto-completion in RStudio
-
+

Open and Plot Shapefiles

-
-

Challenge 1: πŸ• 3 mins

+
+

Challenge 1: πŸ•Ÿ 5 mins

Read in delft-streets.shp and delft-leisure.shp and call them lines_Delft and point_Delft respectively.

Answer the following questions:

    @@ -374,12 +448,27 @@

    Challenge 1: πŸ• 3 mins

-
+
03:00
+
+
+
lines_Delft <- st_read(here("episodes", "data", "delft-streets.shp"))
+point_Delft <- st_read(here("episodes", "data", "delft-leisure.shp"))
+
+st_geometry_type(lines_Delft)
+st_geometry_type(point_Delft)
+
+st_crs(lines_Delft)
+st_crs(point_Delft)
+
+st_bbox(lines_Delft)
+st_bbox(point_Delft)
+
+
@@ -387,7 +476,7 @@

Explore and plot by vector layer attributes

-

Challenge 2: πŸ•™ 3 mins

+

Challenge 2: πŸ• 3 mins

Explore the attributes associated with the point_Delft and boundary_Delft spatial objects.

  1. How many attributes does each have?
  2. @@ -398,7 +487,7 @@

    Challenge 2: πŸ•™ 3 mins

    ⁣        A) location B) leisure C) osm_id

    -
    +
    03:00
    @@ -406,9 +495,22 @@

    Challenge 2: πŸ•™ 3 mins

    +
    +
    +
    ncol(point_Delft)
    +ncol(boundary_Delft)
    +
    +head(point_Delft)
    +head(point_Delft, 10)
    +
    +point_Delft
    +
    +names(point_Delft)
    +
    +
-

Challenge 3: πŸ•“ 5 mins

+

Challenge 3: πŸ•’ 5 mins

  1. Create a new object that only contains the motorways in Delft.
  2. How many features does the new object have?
  3. @@ -418,15 +520,35 @@

    Challenge 3: πŸ•“ 5 mins

-
+
05:00
+
+
+
levels(factor(lines_Delft$highway))
+
+motorway_Delft <- lines_Delft %>% 
+  filter(highway == "motorway")
+
+motorway_Delft %>% 
+  mutate(length = st_length(.)) %>% 
+  select(everything(), geometry) %>%
+  summarise(total_length = sum(length))
+
+nrow(motorway_Delft)
+
+ggplot(data = motorway_Delft) +
+  geom_sf(size = 1.5) +
+  ggtitle("Mobility network of Delft", subtitle = "Motorways") +
+  coord_sf()
+
+
-

Challenge 4: ⏰ 3 mins

+

Challenge 4: πŸ•š 3 mins

In the previous example, we set the line widths to be 1, 0.75, 0.5, and 0.25. In our case line thicknesses are consistent with the hierarchy of the selected road types, but in some cases we might want to show a different hierarchy.

Let’s create another plot where we show the different line types with the following thicknesses:

    @@ -437,32 +559,60 @@

    Challenge 4: ⏰ 3 mins

-
+
03:00
+
+
+
levels(factor(lines_Delft$highway))
+
+line_widths <- c(0.25, 0.75, 0.5, 1)
+
+ggplot(data = lines_Delft_selection) +
+  geom_sf(aes(size = highway)) +
+  scale_size_manual(values = line_widths) +
+  labs(size = "Road Size") +
+  ggtitle("Mobility network of Delft", subtitle = "Roads & Cycleways - Line width varies") +
+  coord_sf()
+
+
-

Challenge 5: πŸ•° 5 mins

+

Challenge 5: πŸ•‘ 5 mins

Create a plot that emphasizes only roads where bicycles are allowed, as follows:

    -
  • make the lines where bicycles are not allowed THINNER than the roads where bicycles are allowed.
  • +
  • Make the lines where bicycles are not allowed THINNER than the roads where bicycles are allowed.
  • Be sure to add a title and legend to your map.
  • You might consider a color palette that has all bike-friendly roads displayed in a bright color. All other lines can be black.
-
+
05:00
+
+
+
levels(factor(lines_Delft_selection$highway))
+
+lines_Delft_bicycle <- lines_Delft %>% 
+  filter(highway == "cycleway")
+
+ggplot() +
+  geom_sf(data = lines_Delft) +
+  geom_sf(data = lines_Delft_bicycle, color = "magenta", size = 2) +
+  ggtitle("Mobility network of Delft", subtitle = "Roads dedicated to bikes") +
+  coord_sf()
+
+
-

Challenge 6: πŸ•— 3 mins

+

Challenge 6: πŸ•œ 3 mins

Create a map of the municipal boundaries in the Netherlands, as follows:

  • Use the data located in your data folder: nl-gemeenten.shp.
  • @@ -471,12 +621,24 @@

    Challenge 6: πŸ•— 3 mins

-
+
03:00
+
+
+
municipal_boundaries_NL <- st_read(here("episodes", "data", "nl-gemeenten.shp"))
+str(municipal_boundaries_NL)
+levels(factor(municipal_boundaries_NL$ligtInPr_1))
+
+ggplot(data = municipal_boundaries_NL) +
+  geom_sf(aes(color = ligtInPr_1), size = 1) +
+  ggtitle("Contiguous NL Municipal Boundaries") +
+  coord_sf()
+
+
@@ -484,30 +646,52 @@

Plot multiple shapefiles

-

Challenge 7: πŸ•” 5 mins

+

Challenge 7: πŸ•˜ 5 mins

Create a map of leisure locations only including playground and picnic_table:

    -
  • color each point by the leisure type.

  • +
  • Color each point by the leisure type.

  • Overlay this layer on top of the lines_Delft layer (the streets).

  • Create a custom legend that applies line symbols to lines and point symbols to the points.

  • Extra: Modify the previous plot. Tell R to plot each point, using a different symbol of shape value.

-
+
05:00
+
+
+
leisure_locations_selection <- st_read(here("episodes", "data", "delft-leisure.shp")) %>% 
+  filter(leisure %in% c("playground", "picnic_table"))
+
+blue_orange <- c("cornflowerblue", "darkorange")
+
+p <- ggplot() + 
+  geom_sf(data = lines_Delft_selection, aes(color = highway)) + 
+  scale_color_manual(name = "Line Type", values = road_colors) +
+  ggtitle("Road network and leisure")
+
+p +
+  geom_sf(data = leisure_locations_selection, aes(fill = leisure), shape = 21) +
+  scale_fill_manual(name = "Leisure Type", values = blue_orange)
+
+p + 
+  geom_sf(data = leisure_locations_selection, aes(fill = leisure, shape = leisure), size = 3) +
+  scale_fill_manual(name = "Leisure Type", values = blue_orange) +
+  scale_shape_manual(name = "Leisure Type", values = c(21, 22))
+
+
-
-

Handle spatial projections

+
+

Handling spatial projections

-

Challenge 8: πŸ•“ 3 mins

+

Challenge 8: πŸ•° 3 mins

Create a map of the South Holland, as follows:

  1. Import nl-gemeenten.shp and filter only the municipalities in South Holland.
  2. @@ -518,13 +702,26 @@

    Challenge 8: πŸ•“ 3 mins

-
+
03:00
+
+
boundary_ZH <- municipal_boundary_NL %>% 
+  filter(ligtInPr_1 == "Zuid-Holland")
+
+ggplot() +
+  geom_sf(data = boundary_ZH, aes(color ="color"), show.legend = "line") +
+  scale_color_manual(name = "", labels = "Municipal Boundaries", values = c("color" = "gray18")) +
+  geom_sf(data = boundary_Delft, aes(shape = "shape"), color = "purple", fill = "purple") +
+  scale_shape_manual(name = "", labels = "Municipality of Delft", values = c("shape" = 19)) +
+  ggtitle("Delft location in South Holland") +
+  theme(legend.background = element_rect(color = NA)) +
+  coord_sf()
+
diff --git a/instructors/2-vector-slides.qmd b/instructors/2-vector-slides.qmd index 11224511..bf668f22 100644 --- a/instructors/2-vector-slides.qmd +++ b/instructors/2-vector-slides.qmd @@ -46,19 +46,23 @@ knitr::opts_chunk$set( ``` -## `sf` package for spatial vector data +# Intro to Geospatial Concepts + +# Geospatial Vector Data in R + +## The `sf` package ![](https://user-images.githubusercontent.com/520851/50280460-e35c1880-044c-11e9-9ed7-cc46754e49db.jpg){fig-align="center"} -## `sf` package for spatial vector data +## The `sf` package - `sf` provides [simple features access](https://en.wikipedia.org/wiki/Simple_Features) to R -- represents simple features as records in a `data.frame` or `tibble` with a `geometry` column +- represents simple features as records in a data frame with a `geometry` column - function names start with a prefix `st_` ("st" stands for spatial type), which is also handy for auto-completion in RStudio # Open and Plot Shapefiles -## Challenge 1: `r emo::ji("clock")` **3 mins** +## Challenge 1: `r emo::ji("clock")` **5 mins** Read in `delft-streets.shp` and `delft-leisure.shp` and call them `lines_Delft` and `point_Delft` respectively. @@ -80,13 +84,13 @@ countdown::countdown(minutes = 3) ```{r} #| label: ch-1 #| eval: false -#| include: false +#| include: true lines_Delft <- st_read(here("episodes", "data", "delft-streets.shp")) point_Delft <- st_read(here("episodes", "data", "delft-leisure.shp")) -class(lines_Delft) -class(point_Delft) +st_geometry_type(lines_Delft) +st_geometry_type(point_Delft) st_crs(lines_Delft) st_crs(point_Delft) @@ -121,7 +125,7 @@ countdown::countdown(minutes = 3) ```{r} #| label: ch-2 #| eval: false -#| include: false +#| include: true ncol(point_Delft) ncol(boundary_Delft) @@ -152,7 +156,7 @@ countdown::countdown(minutes = 5) ```{r} #| label: ch-3 #| eval: false -#| include: false +#| include: true levels(factor(lines_Delft$highway)) @@ -170,16 +174,6 @@ ggplot(data = motorway_Delft) + geom_sf(size = 1.5) + ggtitle("Mobility network of Delft", subtitle = "Motorways") + coord_sf() - -pedestrian_Delft <- lines_Delft %>% - filter(highway == "pedestrian") - -nrow(pedestrian_Delft) - -ggplot(data = pedestrian_Delft) + - geom_sf() + - ggtitle("Slow mobility network of Delft", subtitle = "Pedestrian") + - coord_sf() ``` ## Challenge 4: `r emo::ji("clock")` **3 mins** @@ -203,7 +197,7 @@ countdown::countdown(minutes = 3) ```{r} #| label: ch-4 #| eval: false -#| include: false +#| include: true levels(factor(lines_Delft$highway)) @@ -221,7 +215,7 @@ ggplot(data = lines_Delft_selection) + Create a plot that emphasizes only roads where bicycles are allowed, as follows: -- make the lines where bicycles are not allowed THINNER than the roads where bicycles are allowed. +- Make the lines where bicycles are not allowed THINNER than the roads where bicycles are allowed. - Be sure to add a title and legend to your map. - You might consider a color palette that has all bike-friendly roads displayed in a bright color. All other lines can be black. @@ -235,9 +229,7 @@ countdown::countdown(minutes = 5) ```{r} #| label: ch-5 #| eval: false -#| include: false - -class(lines_Delft_selection$highway) +#| include: true levels(factor(lines_Delft_selection$highway)) @@ -267,9 +259,9 @@ countdown::countdown(minutes = 3) . . . ```{r} -#| label: ch-6 +#| label: ch-6-1 #| eval: false -#| include: false +#| include: true municipal_boundaries_NL <- st_read(here("episodes", "data", "nl-gemeenten.shp")) str(municipal_boundaries_NL) @@ -287,7 +279,7 @@ ggplot(data = municipal_boundaries_NL) + Create a map of leisure locations only including `playground` and `picnic_table`: -- color each point by the leisure type. +- Color each point by the leisure type. - Overlay this layer on top of the `lines_Delft` layer (the streets). - Create a custom legend that applies line symbols to lines and point symbols to the points. @@ -303,39 +295,29 @@ countdown::countdown(minutes = 5) ```{r} #| label: ch-7 #| eval: false -#| include: false +#| include: true -leisure_locations_selection <- st_read(here("data", "delft-leisure.shp")) %>% +leisure_locations_selection <- st_read(here("episodes", "data", "delft-leisure.shp")) %>% filter(leisure %in% c("playground", "picnic_table")) -levels(factor(leisure_locations_selection$leisure)) - blue_orange <- c("cornflowerblue", "darkorange") -ggplot() + +p <- ggplot() + geom_sf(data = lines_Delft_selection, aes(color = highway)) + - geom_sf(data = leisure_locations_selection, aes(fill = leisure), - shape = 21, show.legend = 'point') + - scale_color_manual(name = "Line Type", values = road_colors, - guide = guide_legend(override.aes = list(linetype = "solid", shape = NA))) + - scale_fill_manual(name = "Soil Type", values = blue_orange, - guide = guide_legend(override.aes = list(linetype = "blank", shape = 21, colour = NA))) + - ggtitle("Traffic and leisure") + - coord_sf() + scale_color_manual(name = "Line Type", values = road_colors) + + ggtitle("Road network and leisure") -ggplot() + - geom_sf(data = lines_Delft_selection, aes(color = highway), size = 1) + - geom_sf(data = leisure_locations_selection, aes(fill = leisure, shape = leisure), size = 3) + - scale_shape_manual(name = "Leisure Type", values = c(21, 22)) + - scale_color_manual(name = "Line Type", values = road_colors) + - scale_fill_manual(name = "Leisure Type", values = rainbow(15), - guide = guide_legend(override.aes = list(linetype = "blank", shape = c(21, 22), - color = "black"))) + - ggtitle("Road network and leisure") + - coord_sf() +p + + geom_sf(data = leisure_locations_selection, aes(fill = leisure), shape = 21) + + scale_fill_manual(name = "Leisure Type", values = blue_orange) + +p + + geom_sf(data = leisure_locations_selection, aes(fill = leisure, shape = leisure), size = 3) + + scale_fill_manual(name = "Leisure Type", values = blue_orange) + + scale_shape_manual(name = "Leisure Type", values = c(21, 22)) ``` -# Handle spatial projections +# Handling spatial projections ## Challenge 8: `r emo::ji("clock")` **3 mins** @@ -357,7 +339,7 @@ countdown::countdown(minutes = 3) ```{r} #| label: ch-8 #| eval: false -#| include: false +#| include: true boundary_ZH <- municipal_boundary_NL %>% filter(ligtInPr_1 == "Zuid-Holland") diff --git a/instructors/2-vector-slides_files/figure-revealjs/ch-6-2-1.png b/instructors/2-vector-slides_files/figure-revealjs/ch-6-2-1.png new file mode 100644 index 00000000..7777697c Binary files /dev/null and b/instructors/2-vector-slides_files/figure-revealjs/ch-6-2-1.png differ