-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.qmd
351 lines (293 loc) · 8.19 KB
/
README.qmd
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
---
format: gfm
title: "Notes from a hackathon"
---
Notes from a hackathon.
# Introduction
This is a collection of notes from a hackathon held at the Alan Turing Institute in London on 2023-04-20.
# Setup
## Getting the code repos
The starting point was two code repos:
- Front end (note the use of PMTiles): https://github.com/ADD-William-WaltersDavis/planning_tool
- Backend code in Rust to get scores and routes for each square: https://github.com/adam-jb/rust_connectivity_pt_tiles
These repos were cloned from GitHub as follows:
```{bash}
#| eval: false
gh repo clone ADD-William-WaltersDavis/planning_tool
gh repo clone adam-jb/rust_connectivity_pt_tiles
# add the submodules
git submodule add https://github.com/ADD-William-WaltersDavis/planning_tool planning_tool
git submodule add https://github.com/adam-jb/rust_connectivity_pt_tiles rust_connectivity_pt_tiles
```
## Visualising the data
```{bash}
ls planning_tool
```
Run the front end:
```{bash}
#| eval: false
cd planning_tool
npm install
# global install of vite:
sudo npm install -g vite
npm run dev
```
```{bash}
```
# Working with link data
A starting point was files prepared for the event.
These were copied to the route of the project with bash as follows:
```{bash}
#| eval: false
mkdir data
mv -v ~/Downloads/*.pickle data
```
```{python}
#| eval: false
#| echo: false
# move all pickle files from ~/Downloads to ./data:
import os
import shutil
# Create the data directory if it doesn't exist:
if not os.path.exists('./data'):
os.makedirs('./data')
for file in os.listdir('~/Downloads'):
if file.endswith('.pickle'):
shutil.move(os.path.join('~/Downloads', file), './data')
```
We can list the pickle files as follows:
```{python}
import os
# List pickle files in route directory:
for file in os.listdir('data'):
print(file)
```
```{python}
import pickle
# Read the first pickle file:
with open('data/AA_example_links.pickle', 'rb') as f:
links = pickle.load(f)
```
Show what's in the links object, with output showning first 80 characters:
```{python}
# Find length of links:
len(links)
links.__class__
links.__sizeof__()
links_items = links.items()
links_items.__class__
# links_items[:10]
# Convert dict to list:
links_list = list(links_items)
links_list.__class__
len(links_list)
# Convert list to character string:
links_str = str(links_list)
links_str[:80]
```
We converted the object to json as follows:
```{python}
# Define a function that converts the dict to json and save the output to a file:
import json
def write_json(data, filename='data/AA_example_links.json'):
with open(filename,'w') as f:
json.dump(data, f, indent=4)
write_json(links, 'data/AA_example_links.json')
```
Test reading as a GeoJSON file:
```{python}
#| eval: false
import geopandas as gpd
# The following fails with error:
gdf = gpd.read_file('data/AA_example_links.json')
```
## Read and visualise with R
We'll use the following packages:
```{r}
#| message: false
library(sf)
library(tidyverse)
library(tmap)
tmap_mode("view")
# Install mastermapr dependency:
remotes::install_github("acteng/mastermapr")
```
```{r}
```
```{r}
gdf_list = jsonlite::read_json("data/AA_example_links.json")
str(gdf_list[[1]][[1]])
length(gdf_list)
# show 1st element:
length(gdf_list[[1]])
gdf_list[[1]][[1]]
# create geographic representation of first file:
gdf_origin_coords = c(
gdf_list[[1]][[1]][[8]][[1]],
gdf_list[[1]][[1]][[8]][[2]]
)
gdf_destination_coords = c(
gdf_list[[1]][[1]][[9]][[1]],
gdf_list[[1]][[1]][[9]][[2]]
)
gdf_matrix = rbind(gdf_origin_coords, gdf_destination_coords)
gdf_linestring = sf::st_linestring(gdf_matrix)
sfc_linestring = sf::st_sfc(gdf_linestring)
sf_linestring = sf::st_as_sf(sfc_linestring)
qtm(sf_linestring)
```
## Iterate for all links and visualise network
First we'll generalise the previous code to a function:
```{r}
link_coordinates = function(link) {
# get origin and destination coordinates:
gdf_origin_coords = c(
link[[8]][[1]],
link[[8]][[2]]
)
gdf_destination_coords = c(
link[[9]][[1]],
link[[9]][[2]]
)
# create matrix of coordinates:
gdf_matrix = rbind(gdf_origin_coords, gdf_destination_coords)
# create linestring:
gdf_linestring = sf::st_linestring(gdf_matrix)
# create sfc:
sfc_linestring = sf::st_sfc(gdf_linestring)
# create sf:
sf_linestring = sf::st_as_sf(sfc_linestring)
return(sf_linestring)
}
# Test the function with the first link:
link_coordinates(gdf_list[[1]][[1]])
links = gdf_list[[1]]
links_to_sfc = function(links) {
list_linstrings = lapply(links, link_coordinates)
# class(list_linstrings)
# qtm(list_linstrings[[2]])
# combine the sf linstrings into a single object:
# Note: inefficient implemenation TODO, make more efficient:
# sf_links = do.call(rbind, list_linstrings)
sf_links = mastermapr::fastrbindsf(list_linstrings)
return(sf_links)
}
links_sf = links_to_sfc(gdf_list[[1]])
class(links_sf)
qtm(links_sf)
```
## Get attribute data for links
```{r}
# Function to get link attributes
# For testing:
# link = gdf_list[[1]][[1]]
link_attributes = function(
link,
attribute_names = c(
'Business',
'Education',
'Entertainment',
'Shopping',
'Visit friends',
'start_node',
'end_node',
'start_longlat',
'end_longlat'
)
) {
# get origin and destination coordinates:
link_id = link[[1]]
link_length = link[[2]]
# Subset first 7 elements of list:
link_attributes = link[1:7]
# Create a data frame:
link_df = data.frame(link_attributes)
names(link_df) = attribute_names[1:7]
return(link_df)
}
# Test on a single link:
link_attributes(gdf_list[[1]][[1]])
links_to_df = function(links) {
list_dfs = pbapply::pblapply(links, link_attributes)
data.table::rbindlist(list_dfs)
}
# Function to get link attributes for all links:
links_to_sf = function(links, subset_on = NULL, keep_n = Inf, keep_percent = 100, pt_threshold = 100326060) {
links_df = links_to_df(links)
if(!is.null(subset_on)) {
# indices of top keep_n items:
if(keep_n < nrow(links_df)) {
indices = order(links_df[[subset_on]], decreasing = TRUE)
indices = indices[seq(keep_n)]
links_df = links_df[indices, ]
}
links_sfc = links_to_sfc(links[indices])
} else {
links_sfc = links_to_sfc(links)
}
# Create a spatial data frame:
# class(links_sfc$x)
links_sf = sf::st_sf(geometry = sf::st_geometry(links_sfc), links_df, crs = 4326)
links_sf$Mode = dplyr::case_when(
links_sf$start_node > pt_threshold ~ "Public Transport",
# TODO: add more rules
TRUE ~ "Walking"
)
links_sf
}
links_sf = links_to_sf(gdf_list[[1]], keep_n = 1000, subset_on = "Business")
names(links_sf)
nrow(links_sf)
links_sf |>
select(Business:`Visit friends`) |>
sf::st_drop_geometry() |>
summary()
```
## Style the network
We can now visualise the network with the `tmap` package:
```{r networkvis}
links_sf |>
mutate(across(Business:`Visit friends`, sqrt)) |>
tm_shape() +
tm_lines(lwd = "Business", scale = 15, col = "Mode", palette = "Set1") +
tm_scale_bar()
```
```{r rnetbusiness}
table(links_sf$Mode)
links_sf |>
mutate(across(Business:`Visit friends`, sqrt)) |>
tm_shape() +
tm_lines(lwd = "Visit friends", scale = 15, col = "Mode", palette = "Set1") +
tm_scale_bar()
```
```{r rnetfriends}
links_sf_friends = links_to_sf(gdf_list[[1]], keep_n = 1000, subset_on = "Visit friends")
table(links_sf_friends$Mode)
links_sf_friends |>
mutate(across(Business:`Visit friends`, sqrt)) |>
tm_shape() +
tm_lines(lwd = "Visit friends", scale = 15, col = "Mode", palette = "Set1") +
tm_scale_bar()
```
```{r rneteducation}
links_sf_education = links_to_sf(gdf_list[[1]], keep_n = 1000, subset_on = "Education")
table(links_sf_education$Mode)
links_sf_education |>
mutate(across(Business:`Visit friends`, sqrt)) |>
tm_shape() +
tm_lines(lwd = "Education", scale = 15, col = "Mode", palette = "Set1") +
tm_scale_bar()
```
```{python}
```
```{python}
#| eval: false
#| echo: false
# Print first 10 characters of links:
links[:10]
# Unhashable type: 'slice'
```
```{python}
```
```{python}
```