-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
2_spatial_data.Rmd
153 lines (122 loc) · 6.79 KB
/
2_spatial_data.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
---
title: "Space Syntax analysis in R"
subtitle: "2. Spatial Data"
author:
- name: "Petros Koutsolampros"
- name: "Kimon Krenz"
output: html_notebook
---
This document is the second part of a workshop that presents a workflow for working with spatial data common to the space syntax field in the R programming language. Find all parts here:
1. [Basic functionality of R](./1_r_basics.Rmd)
2. [Spatial data and its forms](./2_spatial_data.Rmd)
3. [Space Syntax Analysis using the alcyon package](./3_space_syntax_analysis.Rmd)
4. [Advanced: Graphs and J-Graphs](./4_advanced_j_graphs.Rmd)
5. [Advanced: Isovist networks](./5_advanced_isovist_network.Rmd)
## Loading spatial data
First let's load some libraries required later. These should be installed using install.package(<library>) i.e. install.package(sf), or if using RSTudio from Tools -> Install Packages..
```{r setup, echo = FALSE}
library(sf)
library(stars)
```
A quick example: Read and plot the classic barnsbury axial map from a pair of MID/MIF files. Adding "quiet = TRUE" to st_read will stop it from displaying diagnostic information while reading the MID/MIF files. The size of the plot can be changed by setting fig.width and fig.height in the chunk options as below.
```{r, fig.width = 5, fig.height = 5}
barnsburySmall = st_read("data/barnsbury/barnsbury_small_axial.mif",
geometry_column = 1L, quiet = TRUE)
plot(barnsburySmall)
```
Reading spatial data produces a spatial data.frame (in this case a sf data.frame). These spatial data.frames can be examined to see the data or the coordinates. To get the data as a data.frame, call the as.data.frame() function which will convert the map to a data.frame.
```{r}
as.data.frame(barnsburySmall)
```
#### Exercise: Print the coordinates of the barnsbury map using the st_coordinates() function:
```{r}
```
To see the columns of the data.frame invoke names()
```{r}
names(barnsburySmall)
```
A simple scatterplot between two of the columns can be achieved with the function plot() and by providing the two columns. The label of the x or y axis can also be provided. To see more plotting parameters type ?plot in the console.
```{r}
plot(barnsburySmall$Integration_HH_,
barnsburySmall$Choice,
xlab = "Integration (HH)")
```
### Spatial Point Data
Spatial point data such as gatecounts can be loaded in the same way using st_read().
```{r, fig.width = 5, fig.height = 5}
gateCounts = st_read("data/barnsbury/barnsbury_ped_gatecounts.mid",
geometry_column = 1L, quiet = TRUE)
plot(gateCounts)
```
#### Exercise: Plot the gatecounts showing only pedestrian movement (mov_ped_ph column)
```{r}
```
We can plot a boxplot of pedestrian movement (mov_ped_ph column) grouped by land use (landuse column)
```{r}
boxplot(mov_ped_ph ~ landuse, gateCounts)
```
An interesting function to apply to point data is the convex hull, a polygon that includes all points and is convex. This can be done using the command st_convex_hull (but first the points need to be converted to multipoints using st_union())
```{r, fig.width = 5, fig.height = 5}
chull = st_convex_hull(st_union(gateCounts))
plot(chull)
```
### Spatial Line Data
Spatial line data such as axial and segment maps can be loaded in the same way using st_read() which produces an sf data.frame with lines Here let's load and plot a larger axial map of the Barnsbury area, only plotting the already calculated Integration.
```{r, fig.width=6, fig.height=6}
barnsburyAxial = st_read("data/barnsbury/barnsbury_axial.mid",
geometry_column = 1L, quiet = TRUE)
plot(barnsburyAxial[, "Integration"])
```
We can provide a palette to the plot function to change the colours:
```{r, fig.width=6, fig.height=6}
plot(barnsburyAxial[,'Integration'], pal = heat.colors)
```
An interesting function that relates to the sf lines data.frame is st_length() we can then use to get the sum of lengths of all lines:
```{r}
sum(st_length(barnsburyAxial))
```
Here we make a histogram of all line lengths:
```{r}
lineLengths = st_length(barnsburyAxial)
hist(lineLengths)
```
Two plots may also be overlaid by calling them one after the other and providing "add = TRUE" for all plots apart from the first. For the first plot we need to provide "reset = FALSE" to keep the plot in a mode that allows adding further map elements. The first plot defines the plot limits. Here let's plot the large barnsbury map and add the gatecounts loaded previously:
```{r, fig.width=6, fig.height=6}
plot(barnsburyAxial[, 'Integration'], pal = heat.colors, reset = FALSE)
plot(gateCounts[,'mov_ped_ph'], add = TRUE)
```
If the gatecounts are plotted first, then the plot limits are around them:
```{r, fig.width=6, fig.height=6}
plot(gateCounts[,'mov_ped_ph'], pch = 22, reset = FALSE)
plot(barnsburyAxial[, 'Integration'], pal = heat.colors, add = TRUE)
```
### Spatial Polygon Data
Spatial polygon data such as urban plots and building rooms can be loaded in the same way using st_read() which produces another sf data.frame with polygons Here let's load and plot some generated urban plots of the Barnsbury area.
```{r, fig.width=6, fig.height=6}
barnsburyPlots = st_read("data/barnsbury/barnsbury_plots.mid",
geometry_column = 1L, quiet = TRUE)
plot(barnsburyPlots, col = "red")
```
Two useful functions for polygon data are st_centroid() which provides a new sf data.frame with the centroids of the polygons and st_area() which gives the area of the polygons. Here we generate the centroid and area and plot the polygons coloured by area with the centroids on top.
Comments that will not be interpreted as code may be placed inside the chunk by starting a line with #
```{r, fig.width=6, fig.height=6}
plotCentroids = st_centroid(st_geometry(barnsburyPlots))
barnsburyPlots[,'area'] = st_area(barnsburyPlots)
cols = rev(heat.colors(100))
plot(barnsburyPlots[,'area'], col = cols[cut(barnsburyPlots[['area']], 100)], reset = FALSE)
# pch is the shape of the points
# cex is the size of the points
plot(plotCentroids, add = TRUE, pch = 15, cex = 0.5, col = "black")
```
### Spatial Pixel Data
Spatial pixel data such as data from Visibility Graph Analysis (VGA) can be loaded in various ways to produce a stars data.frame. Spatial pixels are essentially spatial points that are placed on a grid. We will load the point data from a CSV as plain tabular data, then convert the plain data.frame to an sf data.frame using "st_as_sf()" and then convert that to a stars data.frame using st_sfc2xy(). Here let's load the classic National Gallery VGA.
```{r, fig.width=8}
galleryCSV = read.csv("data/gallery/gallery_vga.csv")
galleryPts = st_as_sf(galleryCSV, coords = c("x","y"))
galleryVGA = st_sfc2xy(galleryPts, )
names(galleryVGA) # print the column names of galleryVGA
```
We can now plot the data.frame "Visual Mean Depth" column
```{r, fig.width=8}
plot(galleryVGA["Visual.Mean.Depth"])
```