forked from npct/pct-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_compute_commute_densities.py
executable file
·220 lines (174 loc) · 7.51 KB
/
04_compute_commute_densities.py
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
#!/usr/bin/python3
import os
import glob
import numpy as np
import pandas as pd
import geopandas as gpd
from stplanpy import od
from stplanpy import geo
from stplanpy import dist
from stplanpy import cycle
from stplanpy import route
in_dir = os.path.expanduser("../pct-inputs/02_intermediate/")
out_dir = os.path.expanduser("../pct-outputs/static/commute/")
os.makedirs(out_dir, exist_ok=True)
################################################################################
if os.path.isfile("flow_data.pkl"):
print("Read flow_data pickle")
# Read pickle
flow_data = pd.read_pickle("flow_data.pkl")
# Read taz data
taz = pd.read_pickle(in_dir + "01_geographies/bayArea_taz.pkl")
else:
print("Compute flow_data")
# Read flow data
flow_data = pd.read_pickle(in_dir + "02_travel_data/commute/flow_data.pkl")
# Read taz data
taz = pd.read_pickle(in_dir + "01_geographies/bayArea_taz.pkl")
taz_cent = pd.read_pickle(in_dir + "01_geographies/bayArea_taz_cent.pkl")
# Add county and place codes to data frame. This data is used to compute mode
# share in counties and places
flow_data = flow_data.orig_dest(taz)
# Compute origin-destination lines
flow_data["geometry"] = flow_data.od_lines(taz_cent)
flow_data["od_lines"] = flow_data["geometry"]
# Read the Cycle Streets API key
cyclestreets_key = cycle.read_key()
# Compute routes
flow_data["geometry"] = flow_data.routes(api_key=cyclestreets_key)
flow_data["routes"] = flow_data["geometry"]
flow_data.find_cent()
# Compute distances, gradients, and directness
flow_data["distance"] = flow_data.distances()
flow_data["gradient"] = flow_data.gradient(taz_cent)
flow_data["directness"] = flow_data.directness()
# Compute go_dutch and ebike scenarios
flow_data["go_dutch"] = flow_data.go_dutch()
flow_data["ebike"] = flow_data.ebike()
flow_data = gpd.GeoDataFrame(flow_data)
flow_data = flow_data.set_crs("EPSG:6933")
flow_data.to_pickle("flow_data.pkl")
#stp.plot_dist(flow_data, out_dir)
################################################################################
if os.path.isfile("taz.pkl"):
print("Read taz pickle")
# Read pickle
taz = pd.read_pickle("taz.pkl")
else:
print("Compute taz")
# Compute mode share
taz[["bike", "go_dutch", "ebike", "all"]] = taz.mode_share(
flow_data, modes=["bike", "go_dutch", "ebike"])
# Compute mode share for trips shorter than 7.5km (4.5 miles)
taz[["bike75", "go_dutch75", "ebike75", "all75"]] = taz.mode_share(
flow_data.loc[flow_data["distance"] <= 7500], modes=["bike", "go_dutch", "ebike"])
taz.to_pickle("taz.pkl")
################################################################################
# Read place data
place = pd.read_pickle(in_dir + "01_geographies/bayArea_place.pkl")
# Compute mode share
place[["bike", "go_dutch", "ebike", "all"]] = place.mode_share(
flow_data, modes=["bike", "go_dutch", "ebike"])
# Compute mode share for trips shorter than 7.5km (4.5 miles)
place[["bike75", "go_dutch75", "ebike75", "all75"]] = place.mode_share(
flow_data.loc[flow_data["distance"] <= 7500], modes=["bike", "go_dutch", "ebike"])
# Write to disk
place.to_geojson(out_dir + "bayArea_place.GeoJson")
place.to_pickle(out_dir + "bayArea_place.pkl")
# Organize per place
for row in place.iterrows():
placefp = place.loc[row[0], "placefp"]
tz = taz.loc[(taz["placefp"] == placefp)].copy()
if not tz.empty:
name = place.loc[row[0], "name"]
print(name)
# filter out place
pc = place.loc[place["name"] != name]
name = name.replace(" ","")
path = out_dir + "place/" + name
if not os.path.isdir(path):
os.mkdir(path)
tz["name"] = tz.index
# Remove NaN (countyfp)
tz = tz[["name", "all", "bike", "go_dutch", "ebike", "geometry"]]
pc = pc[["name", "all", "bike", "go_dutch", "ebike", "geometry"]]
tz["geometry"] = tz["geometry"].simplify(5.0)
pc["geometry"] = pc["geometry"].simplify(25.0)
tz.to_geojson(path + "/taz.GeoJson")
pc.to_geojson(path + "/place.GeoJson")
fd = flow_data.to_frm(placefp)
name = place.loc[row[0], "name"]
name = name.replace(" ","")
path = out_dir + "place/" + name
if not os.path.isdir(path):
os.mkdir(path)
fd = fd[["all", "bike", "go_dutch", "ebike", "geometry", "od_lines", "routes", "distance", "gradient", "directness"]]
fd = fd.loc[fd["distance"] > 0]
fd = fd.loc[fd["distance"] < 30000]
if not fd.empty:
network = fd.network(modes=["bike", "go_dutch", "ebike"])
fd = fd.nlargest(100, "bike")
fd.set_geometry("od_lines", crs=flow_data.crs).to_geojson(path + "/line.GeoJson")
fd.set_geometry("routes", crs=flow_data.crs).to_geojson(path + "/route.GeoJson")
network.to_geojson(path + "/network.GeoJson")
else:
ar = np.empty([1,10])
ar[:,:] = np.nan
fd = pd.DataFrame(data=ar, columns=fd.columns)
fd.to_pickle(path + "/line.pkl")
network.to_pickle(path + "/network.pkl")
################################################################################
# Read county data
county = pd.read_pickle(in_dir + "01_geographies/bayArea_county.pkl")
# Compute mode share
county[["bike", "go_dutch", "ebike", "all"]] = county.mode_share(
flow_data, modes=["bike", "go_dutch", "ebike"])
# Compute mode share for trips shorter than 7.5km (4.5 miles)
county[["bike75", "go_dutch75", "ebike75", "all75"]] = county.mode_share(
flow_data.loc[flow_data["distance"] <= 7500], modes=["bike", "go_dutch", "ebike"])
# Write to disk
county.to_geojson(out_dir + "bayArea_county.GeoJson")
county.to_pickle(out_dir + "bayArea_county.pkl")
# Organize per county
for row in county.iterrows():
countyfp = county.loc[row[0], "countyfp"]
tz = taz.loc[(taz["countyfp"] == countyfp)].copy()
if not tz.empty:
name = county.loc[row[0], "name"]
print(name)
# filter out county
ct = county.loc[county["name"] != name]
name = name.replace(" ","")
path = out_dir + "county/" + name
if not os.path.isdir(path):
os.mkdir(path)
tz["name"] = tz.index
# Remove NaN (placefp)
tz = tz[["name", "all", "bike", "go_dutch", "ebike", "geometry"]]
ct = ct[["name", "all", "bike", "go_dutch", "ebike", "geometry"]]
tz["geometry"] = tz["geometry"].simplify(5.0)
ct["geometry"] = ct["geometry"].simplify(25.0)
tz.to_geojson(path + "/taz.GeoJson")
ct.to_geojson(path + "/county.GeoJson")
fd = flow_data.to_frm(countyfp)
name = county.loc[row[0], "name"]
name = name.replace(" ","")
path = out_dir + "county/" + name
if not os.path.isdir(path):
os.mkdir(path)
fd = fd[["all", "bike", "go_dutch", "ebike", "geometry", "od_lines", "routes", "distance", "gradient", "directness"]]
fd = fd.loc[fd["distance"] > 0]
fd = fd.loc[fd["distance"] < 30000]
if not fd.empty:
network = fd.network(modes=["bike", "go_dutch", "ebike"])
fd = fd.nlargest(100, "bike")
fd.set_geometry("od_lines", crs=flow_data.crs).to_geojson(path + "/line.GeoJson")
fd.set_geometry("routes", crs=flow_data.crs).to_geojson(path + "/route.GeoJson")
network.to_geojson(path + "/network.GeoJson")
else:
ar = np.empty([1,10])
ar[:,:] = np.nan
fd = pd.DataFrame(data=ar, columns=fd.columns)
fd.to_pickle(path + "/line.pkl")
network.to_pickle(path + "/network.pkl")
################################################################################