Repository navigation
Expand file tree
/
Copy pathfinal_flows.R
More file actions
104 lines (86 loc) · 3.21 KB
/
Copy pathfinal_flows.R
File metadata and controls
104 lines (86 loc) · 3.21 KB
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
# # # # # # # # #
# Flows to plot #
# # # # # # # # #
# # # # # # # # # # # # # # # # #
# Stage 1: manipulate the data #
# # # # # # # # # # # # # # # # #
f <- read.csv("data/msoa-leeds-all-with-route.csv")
nrow(f)
# which cols do we want from f?
names(f) # id and last 4 are new
fl <- read.csv("data/msoa-flow-leeds-all.csv")
fl$lat_dest <- f$lat_dest
head(fl)
names(fl)
# flows to present
library("dplyr")
fl_present <- select(fl, Area.of.residence, Area.of.workplace, All.categories..Method.of.travel.to.work, Bicycle, lon_origin, lat_origin, lon_dest, lat_dest, ecp, p_cycle, pc, dist) # fl dataset to present
fl <- rename(fl_present, Origin = Area.of.residence, Destination = Area.of.workplace, Total = All.categories..Method.of.travel.to.work, clc = p_cycle, plc = pc)
names(fl_present)
# Test the datasets join-up
fl$lon_dest[1:5] == f$lon_dest[1:5]
# summary(fl$lon_dest == f$lon_dest)
# summary(fl$lon_origin == f$lon_origin)
# summary(fl$lat_origin == f$lat_origin)
# summary(fl$lat_origin == f$lat_origin)
f <- f[c(1, 6:9)]
names(f)
ids <- f$id
f$id <- NULL
flows <- cbind(ids, fl, f)
# Is this the final df?
summary(flows$dist) # na - we kanye be doing with 0 dists
flows <- flows[flows$dist > 10, ]
summary(flows$dist)
head(flows) # yay we have the final data frame
flows <- flows[ flows$dist < 8000, ]
# write.csv(flows, "R/fixMyPath/flows.csv")
# points to lines
l <- vector("list", nrow(flows))
for(i in 1:nrow(flows)){
x <- c(flows$lon_origin[i], flows$lon_dest[i])
y <- c(flows$lat_origin[i], flows$lat_dest[i])
l[[i]] <- Lines(list(Line(rbind(x, y))), as.character(i))
}
l <- SpatialLines(l)
l <- SpatialLinesDataFrame(l, data = flows, match.ID = F)
object.size(l) / 1000000
saveRDS(l, "R/fixMyPath/l.Rds")
# # # # # # # # # # # # # #
# Extract the ones to plot #
# # # # # # # # # # # # # #
# Intersting routes (top and bottom ecp, plc and clc)
head_tail <- rbind(flows[head(order(flows$ecp),10),], flows[tail(order(flows$ecp),10),])
head_tail <- rbind(head_tail, flows[head(order(flows$clc),10),], flows[head(order(flows$clc),10),])
head_tail <- rbind(head_tail, flows[head(order(flows$plc),10),], flows[head(order(flows$plc),10),])
head_tail <- unique(head_tail)
# How good is the estimate of cycling potential?
plot(flows$plc, flows$Bicycle)
cor(flows$plc, flows$Bicycle)
# Saving the routes
library(rgdal)
library("maptools")
al <- NULL
for(i in 1:nrow(head_tail)){
rfile <- list.files(path = "local-data/", pattern = paste0("^", head_tail[i,]$ids, "-"), full.names = T)
for(j in rfile){
l <- readOGR(j, layer = "OGRGeoJSON")
if(grepl("fast", j)){
da <- rename(head_tail[i,], d = fastest_distance_in_m, t = fastest_time_in_s)
l <- SpatialLinesDataFrame(l, data = select(da, d, t, ecp, clc, plc), match.ID = F)
l$route <- "fast"
}else{
da <- rename(head_tail[i,], d = quietest_distance_in_m, t = quietest_time_in_s)
l <- SpatialLinesDataFrame(l, data = select(da, d, t, ecp, clc, plc), match.ID = F)
l$route <- "quiet"
}
row.names(l) = toString(j)
if(is.null(al)) {
al <- l
row.names(al) = toString(i)
}
else al <- spRbind(al, l)
}
}
# writeOGR(al, layer = "shape1", dsn = "/tmp/", driver = "ESRI Shapefile")
saveRDS(al, "R/fixMyPath/al.Rds")