Skip to content

Commit 042ca69

Browse files
committed
Updated attraction and mode choice
1 parent 73e253b commit 042ca69

File tree

3 files changed

+86
-42
lines changed

3 files changed

+86
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.RData
44
.Ruserdata
55
/data
6+
/result/local

script/Manchester/destinationChoice/observedAttraction.R

+43-19
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,57 @@ test <- trips %>% count(EndPurpose, purpose) %>% arrange(desc(n))
4040
# Remove home/work/business destinations
4141
trips <- filter(trips, purpose != "work" & purpose != "home" & purpose != "education" & purpose != "business")
4242

43+
# Add MSOA/LSOA
44+
OA_lookup <- readr::read_csv(paste0("data/Manchester/OA_lookup.csv"), col_select = c("OA11CD","LSOA11CD","MSOA11CD"))
45+
46+
trips <- left_join(trips,OA_lookup,by = c("EndOutputArea" = "OA11CD")) %>% rename(EndMSOA = MSOA11CD, EndLSOA = LSOA11CD)
47+
4348
# Get Attraction of each OA
44-
rawAttractions <- trips %>%
45-
count(EndOutputArea,EndPurpose) %>%
46-
pivot_wider(names_from = EndPurpose,
47-
values_from = n) %>%
48-
filter(EndOutputArea != " ")
4949

50-
zoneAttractions <- trips %>%
51-
count(EndOutputArea,purpose) %>%
52-
pivot_wider(names_from = purpose,
53-
values_from = n) %>%
54-
filter(EndOutputArea != " ")
50+
getAttractions <- function(regionType) {
51+
rawAttractions <- trips %>%
52+
count(!!enquo(regionType),EndPurpose) %>%
53+
pivot_wider(names_from = EndPurpose,
54+
values_from = n) %>%
55+
filter(!!enquo(regionType) != " ")
56+
57+
zoneAttractions <- trips %>%
58+
count(!!enquo(regionType),purpose) %>%
59+
pivot_wider(names_from = purpose,
60+
values_from = n) %>%
61+
filter(!!enquo(regionType) != " ")
62+
63+
return(full_join(rawAttractions,zoneAttractions))
64+
}
65+
66+
attractions_OA <- getAttractions(EndOutputArea)
67+
attractions_LSOA <- getAttractions(EndLSOA)
68+
attractions_MSOA <- getAttractions(EndMSOA)
69+
5570

56-
attractions <- full_join(rawAttractions,zoneAttractions)
5771

5872
# Attach attractions to OAs
5973

60-
all_OAs <- sf::st_read("~/Documents/TfGM/destinationChoice/gm_oa.gpkg")
61-
OA_lookup <- readr::read_csv(paste0("data/Manchester/OA_lookup.csv"), col_select = c("OA11CD","LAD17CD","LAD17NM"))
74+
OAs <- sf::st_read("~/Documents/manchester/zones/gm_oa.shp")
75+
LSOAs <- sf::st_read("~/Documents/manchester/zones/LSOA_studyArea.shp")
76+
MSOAs <- sf::st_read("~/Documents/manchester/zones/MSOA_studyArea.shp")
6277

6378

64-
gm_districts <- c("Bolton","Bury","Manchester","Oldham","Rochdale","Salford","Stockport","Tameside","Trafford","Wigan")
65-
gm_oa_list <- OA_lookup %>% filter(LAD17NM %in% gm_districts) %>% pull(OA11CD)
79+
attr_OAs <- OAs %>% select(-fid) %>%
80+
left_join(attractions_OA, by = c("OA11CD" = "EndOutputArea")) %>%
81+
mutate(across(where(is.integer),function(x) replace_na(x,0)))
82+
83+
attr_LSOAs <- LSOAs %>%
84+
left_join(attractions_LSOA, by = c("LSOA11CD" = "EndLSOA")) %>%
85+
mutate(across(where(is.integer),function(x) replace_na(x,0)))
6686

67-
gm_OAs <- all_OAs %>%
68-
filter(geo_code %in% gm_oa_list) %>%
69-
left_join(attractions, by = c("geo_code" = "EndOutputArea")) %>%
87+
attr_MSOAs <- MSOAs %>%
88+
left_join(attractions_MSOA, by = c("MSOA11CD" = "EndMSOA")) %>%
7089
mutate(across(where(is.integer),function(x) replace_na(x,0)))
7190

72-
sf::st_write(gm_OAs,"result/attractions.gpkg",delete_layer = TRUE)
91+
92+
# Write
93+
sf::st_write(attr_OAs,"result/attractions_OA.gpkg",delete_layer = TRUE)
94+
sf::st_write(attr_LSOAs,"result/attractions_LSOA.gpkg",delete_layer = TRUE)
95+
sf::st_write(attr_MSOAs,"result/attractions_MSOA.gpkg",delete_layer = TRUE)
96+
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
setwd("~/Documents/JIBE/travelDiaryProcessing")
22
source("script/Manchester/read.R")
33

4-
# Spatial variation based on origin
5-
cycleTripsByStartOA <- TRADS$trips %>%
6-
filter(t.startOA != " ") %>%
7-
group_by(t.startOA) %>%
8-
summarise(cycleStart = sum(t.m_main == "Bicycle"),
9-
cycleStartShare = cycleStart / n())
10-
11-
cycleTripsByEndOA <- TRADS$trips %>%
12-
filter(t.endOA != " ") %>%
13-
group_by(t.endOA) %>%
14-
summarise(cycleEnd = sum(t.m_main == "Bicycle"),
15-
cycleEndShare = cycleEnd / n())
4+
OAs <- sf::st_read("~/Documents/manchester/zones/gm_oa.shp")
5+
LSOAs <- sf::st_read("~/Documents/manchester/zones/LSOA_studyArea.shp")
6+
MSOAs <- sf::st_read("~/Documents/manchester/zones/MSOA_studyArea.shp")
167

17-
cycleTripsByHomeOA <- TRADS$trips %>%
18-
left_join(TRADS$households) %>%
19-
group_by(hh.OA) %>%
20-
summarise(cycleHome = sum(t.m_main == "Bicycle"),
21-
cycleHomeShare = cycleHome / n())
8+
trips <- TRADS$trips %>% left_join(select(TRADS$households,hh.id,hh.OA))
229

23-
OAs <- sf::read_sf("~/Documents/manchester/JIBE/accessibility/OAs/gm_oa.shp")
10+
# Spatial variation based on origin
11+
getTripsAndShares <- function(var,suffix,areaType) {
12+
lookup <- OA_lookup[,c("OA11CD",areaType)]
13+
colnames(lookup)[1] <- var
14+
15+
result <- trips %>%
16+
left_join(lookup) %>%
17+
group_by_(areaType) %>%
18+
summarise(cycleCount = sum(t.m_main == "Bicycle"),
19+
walkCount = sum(t.m_main == "Walk"),
20+
trips = n(),
21+
cycleShare = cycleCount / n(),
22+
walkShare = walkCount / n())
23+
24+
colnames(result)[2:6] <- paste(colnames(result)[2:6],suffix,sep = "_")
25+
26+
return(result)
27+
}
2428

2529
OAs <- OAs %>%
26-
left_join(cycleTripsByStartOA,by = c("geo_code" = "t.startOA")) %>%
27-
left_join(cycleTripsByEndOA,by = c("geo_code" = "t.endOA")) %>%
28-
left_join(cycleTripsByHomeOA,by = c("geo_code" = "hh.OA")) %>%
29-
mutate(across(starts_with("cycle"),function(x) replace_na(x,0)))
30+
left_join(getTripsAndShares("t.startOA","start","OA11CD"),by = c("geo_code" = "OA11CD")) %>%
31+
left_join(getTripsAndShares("t.endOA","end","OA11CD"),by = c("geo_code" = "OA11CD")) %>%
32+
left_join(getTripsAndShares("hh.OA","hh","OA11CD"),by = c("geo_code" = "OA11CD")) %>%
33+
mutate(across(starts_with("cycle") | starts_with("walk") | starts_with("trip"),function(x) replace_na(x,0)))
34+
35+
MSOAs <- MSOAs %>%
36+
left_join(getTripsAndShares("t.startOA","start","MSOA11CD")) %>%
37+
left_join(getTripsAndShares("t.endOA","end","MSOA11CD")) %>%
38+
left_join(getTripsAndShares("hh.OA","hh","MSOA11CD")) %>%
39+
mutate(across(starts_with("cycle") | starts_with("walk") | starts_with("trip"),function(x) replace_na(x,0)))
40+
41+
LSOAs <- LSOAs %>%
42+
left_join(getTripsAndShares("t.startOA","start","LSOA11CD")) %>%
43+
left_join(getTripsAndShares("t.endOA","end","LSOA11CD")) %>%
44+
left_join(getTripsAndShares("hh.OA","hh","LSOA11CD")) %>%
45+
mutate(across(starts_with("cycle") | starts_with("walk") | starts_with("trip"),function(x) replace_na(x,0)))
46+
3047

31-
sf::write_sf(OAs,"result/bikeShares.shp")
48+
sf::st_write(OAs,"result/activeSharesOA.gpkg",append = FALSE)
49+
sf::st_write(MSOAs,"result/activeSharesMSOA.gpkg",append = FALSE)
50+
sf::st_write(LSOAs,"result/activeSharesLSOA.gpkg",append = FALSE)

0 commit comments

Comments
 (0)