Skip to content

Commit c41fe3a

Browse files
committed
Backup
1 parent 3f2e90b commit c41fe3a

File tree

3 files changed

+79
-46
lines changed

3 files changed

+79
-46
lines changed

01_convert_flow_data.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import os
77
import glob
88
import pandas as pd
9-
import numpy as np
109
from stplanpy import acs
1110

1211
################################################################################
@@ -36,7 +35,8 @@
3635
print(county)
3736

3837
file_list = glob.glob(
39-
in_dir + "02_travel_data/commute/taz/acs2011-2015/" + county + "*.csv")
38+
# in_dir + "02_travel_data/commute/taz/acs2011-2015/" + county + "*.csv")
39+
in_dir + "02_travel_data/commute/taz/acs2012-2016/" + county + "*.csv")
4040
for i, file_path in enumerate(file_list):
4141
print(file_path)
4242
if (init):

02_prepare_geographies.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# the Bay Area from shapefile data and converts it to the geojson format
55

66
import os
7-
import numpy as np
87
import pandas as pd
98
import geopandas as gpd
109
from stplanpy import geo
@@ -61,8 +60,6 @@
6160

6261
# Read taz data
6362
taz = geo.read_shp(in_dir + "taz_boundaries/tl_2011_06_taz10.zip")
64-
#taz["placefp"] = np.nan
65-
#taz["area"] = np.nan
6663

6764
# Rename columns for consistency
6865
taz.rename(columns = {"countyfp10":"countyfp", "tazce10":"tazce"}, inplace = True)
@@ -73,38 +70,40 @@
7370
# Compute centroids
7471
taz_cent = taz.cent()
7572

76-
# Correct centroid locations
73+
# Correct centroid locations so they are close to a road
7774
# Google plex
78-
taz_cent.corr_cent("00101155", -122.07805259936053, 37.42332894065777)
75+
taz_cent.corr_cent("00101155", -122.078052, 37.423328)
7976
# Stanford research park
80-
taz_cent.corr_cent("00100480", -122.14512495139151, 37.407136806684605)
77+
taz_cent.corr_cent("00100480", -122.145124, 37.407136)
8178
# Facebook
82-
taz_cent.corr_cent("00102130", -122.1487037864525, 37.48492337393505)
83-
# Alviso
84-
taz_cent.corr_cent("00101013", -121.96984835449749, 37.42903692638468)
85-
# Newark
86-
taz_cent.corr_cent("00103259", -122.04629989984038, 37.52303561234163)
79+
taz_cent.corr_cent("00102130", -122.148703, 37.484923)
80+
# San Antonio watershed
81+
taz_cent.corr_cent("00103321", -121.845271, 37.602841)
82+
# Tesla
83+
taz_cent.corr_cent("00103023", -121.949050, 37.502547)
84+
# Hayward
85+
taz_cent.corr_cent("00103112", -122.122513, 37.622984)
86+
# Pacifica
87+
taz_cent.corr_cent("00102160", -122.483845, 37.611063)
88+
# Gilroy
89+
taz_cent.corr_cent("00101057", -121.5119226,37.025154)
8790

8891
# Compute which taz lay inside a place and which part
8992
taz = taz.in_place(place)
9093

9194
# Write to disk
92-
#county.set_index("countyfp", inplace=True)
9395
county.to_geojson(out_dir + "bayArea_county.GeoJson")
9496
county.to_pickle(out_dir + "bayArea_county.pkl")
9597

9698
# Write to disk
97-
#place.set_index("placefp", inplace=True)
9899
place.to_geojson(out_dir + "bayArea_place.GeoJson")
99100
place.to_pickle(out_dir + "bayArea_place.pkl")
100101

101102
# Write to disk
102-
#taz.set_index("tazce", inplace=True)
103103
taz.to_geojson(out_dir + "bayArea_taz.GeoJson")
104104
taz.to_pickle(out_dir + "bayArea_taz.pkl")
105105

106106
# Write to disk
107-
#taz_cent.set_index("tazce", inplace=True)
108107
taz_cent.to_geojson(out_dir + "bayArea_taz_cent.GeoJson")
109108
taz_cent.to_pickle(out_dir + "bayArea_taz_cent.pkl")
110109

04_compute_commute_densities.py

+63-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import geopandas as gpd
88
from stplanpy import od
99
from stplanpy import dist
10+
from stplanpy import cycle
11+
from stplanpy import route
1012

1113
in_dir = os.path.expanduser("../pct-inputs/02_intermediate/")
1214
out_dir = os.path.expanduser("../pct-outputs/static/commute/")
@@ -35,13 +37,26 @@
3537
# share in counties and places
3638
flow_data = flow_data.orig_dest(taz)
3739

38-
# Compute origin destination lines, distances, and gradient
40+
# Compute origin-destination lines
3941
flow_data["geometry"] = flow_data.od_lines(taz_cent)
42+
flow_data["od_lines"] = flow_data["geometry"]
43+
44+
# Read the Cycle Streets API key
45+
cyclestreets_key = cycle.read_key()
46+
47+
# Compute routes
48+
flow_data["geometry"] = flow_data.routes(api_key=cyclestreets_key)
49+
flow_data["routes"] = flow_data["geometry"]
50+
flow_data.find_cent()
51+
52+
# Compute distances, gradients, and directness
4053
flow_data["distance"] = flow_data.distances()
4154
flow_data["gradient"] = flow_data.gradient(taz_cent)
55+
flow_data["directness"] = flow_data.directness()
4256

43-
# Compute go_dutch scenario
57+
# Compute go_dutch and ebike scenarios
4458
flow_data["go_dutch"] = flow_data.go_dutch()
59+
flow_data["ebike"] = flow_data.ebike()
4560

4661
flow_data = gpd.GeoDataFrame(flow_data)
4762
flow_data = flow_data.set_crs("EPSG:6933")
@@ -59,26 +74,36 @@
5974
else:
6075
print("Compute taz")
6176

62-
taz = taz.mode_share(flow_data)
77+
# Compute mode share
78+
taz[["bike", "go_dutch", "ebike", "all"]] = taz.mode_share(
79+
flow_data, modes=["bike", "go_dutch", "ebike"])
80+
# Compute mode share for trips shorter than 7.5km (4.5 miles)
81+
taz[["bike75", "go_dutch75", "ebike75", "all75"]] = taz.mode_share(
82+
flow_data.loc[flow_data["distance"] <= 7500], modes=["bike", "go_dutch", "ebike"])
6383
taz.to_pickle("taz.pkl")
6484

6585
################################################################################
6686

6787
# Read place data
6888
place = pd.read_pickle(in_dir + "01_geographies/bayArea_place.pkl")
6989

70-
# Compute mode shares
71-
place = place.mode_share(flow_data)
90+
# Compute mode share
91+
place[["bike", "go_dutch", "ebike", "all"]] = place.mode_share(
92+
flow_data, modes=["bike", "go_dutch", "ebike"])
93+
# Compute mode share for trips shorter than 7.5km (4.5 miles)
94+
place[["bike75", "go_dutch75", "ebike75", "all75"]] = place.mode_share(
95+
flow_data.loc[flow_data["distance"] <= 7500], modes=["bike", "go_dutch", "ebike"])
7296

7397
# Write to disk
7498
place.to_crs("EPSG:4326").to_file(out_dir + "bayArea_place.GeoJson", driver="GeoJSON")
7599
place.to_pickle(out_dir + "bayArea_place.pkl")
76100

77101
# Organize per place
78-
for placefp in place.iterrows():
79-
tz = taz.loc[(taz["placefp"] == placefp[0])].copy()
102+
for row in place.iterrows():
103+
placefp = place.loc[row[0], "placefp"]
104+
tz = taz.loc[(taz["placefp"] == placefp)].copy()
80105
if not tz.empty:
81-
name = place.loc[placefp[0], "name"]
106+
name = place.loc[row[0], "name"]
82107
print(name)
83108
# filter out place
84109
pc = place.loc[place["name"] != name]
@@ -90,55 +115,62 @@
90115
tz["name"] = tz.index
91116

92117
# Remove NaN (countyfp)
93-
tz = tz[["name", "all", "sov", "bike", "go_dutch", "geometry"]]
94-
pc = pc[["name", "all", "sov", "bike", "go_dutch", "geometry"]]
118+
tz = tz[["name", "all", "bike", "go_dutch", "ebike", "geometry"]]
119+
pc = pc[["name", "all", "bike", "go_dutch", "ebike", "geometry"]]
95120

96121
tz["geometry"] = tz["geometry"].simplify(5.0)
97122
pc["geometry"] = pc["geometry"].simplify(25.0)
98123

99124
tz.to_crs("EPSG:4326").to_file(path + "/taz.GeoJson", driver="GeoJSON")
100125
pc.to_crs("EPSG:4326").to_file(path + "/place.GeoJson", driver="GeoJSON")
101126

102-
fd = flow_data.loc[(flow_data["orig_plc"] == placefp[0]) | (flow_data["dest_plc"] == placefp[0])]
127+
fd = flow_data.to_frm(placefp)
103128

104-
name = place.loc[placefp[0], "name"]
129+
name = place.loc[row[0], "name"]
105130
name = name.replace(" ","")
106131
path = out_dir + "place/" + name
107132
if not os.path.isdir(path):
108133
os.mkdir(path)
109134

110-
# stp.plot_dist(fd, path)
111-
112-
fd = fd[["all", "bike", "go_dutch", "geometry", "distance", "gradient"]]
135+
fd = fd[["all", "bike", "go_dutch", "ebike", "geometry", "od_lines", "routes", "distance", "gradient", "directness"]]
113136
if not fd.empty:
114137
fd = fd.loc[fd["distance"] > 0]
115138
fd = fd.loc[fd["distance"] < 30000]
139+
network = fd.network()
116140
fd = fd.nlargest(100, "bike")
117-
fd.to_crs("EPSG:4326").to_file(path + "/line.GeoJson", driver="GeoJSON")
141+
fd.set_geometry("od_lines", crs=flow_data.crs).to_crs("EPSG:4326").drop(columns=["geometry","routes"]).to_file(path + "/line.GeoJson", driver="GeoJSON")
142+
fd.set_geometry("routes", crs=flow_data.crs).to_crs("EPSG:4326").drop(columns=["geometry","od_lines"]).to_file(path + "/route.GeoJson", driver="GeoJSON")
143+
network.to_crs("EPSG:4326").to_file(path + "/network.GeoJson", driver="GeoJSON")
118144
else:
119145
ar = np.empty([1,6])
120146
ar[:,:] = np.nan
121147
fd = pd.DataFrame(data=ar, columns=fd.columns)
122148

123149
fd.to_pickle(path + "/line.pkl")
150+
network.to_pickle(path + "/network.pkl")
124151

125152
################################################################################
126153

127154
# Read county data
128155
county = pd.read_pickle(in_dir + "01_geographies/bayArea_county.pkl")
129156

130-
# Compute mode shares
131-
county = county.mode_share(flow_data)
157+
# Compute mode share
158+
county[["bike", "go_dutch", "ebike", "all"]] = county.mode_share(
159+
flow_data, modes=["bike", "go_dutch", "ebike"])
160+
# Compute mode share for trips shorter than 7.5km (4.5 miles)
161+
county[["bike75", "go_dutch75", "ebike75", "all75"]] = county.mode_share(
162+
flow_data.loc[flow_data["distance"] <= 7500], modes=["bike", "go_dutch", "ebike"])
132163

133164
# Write to disk
134165
county.to_crs("EPSG:4326").to_file(out_dir + "bayArea_county.GeoJson", driver="GeoJSON")
135166
county.to_pickle(out_dir + "bayArea_county.pkl")
136167

137168
# Organize per county
138-
for countyfp in county.iterrows():
139-
tz = taz.loc[(taz["countyfp"] == countyfp[0])].copy()
169+
for row in county.iterrows():
170+
countyfp = county.loc[row[0], "countyfp"]
171+
tz = taz.loc[(taz["countyfp"] == countyfp)].copy()
140172
if not tz.empty:
141-
name = county.loc[countyfp[0], "name"]
173+
name = county.loc[row[0], "name"]
142174
print(name)
143175
# filter out county
144176
ct = county.loc[county["name"] != name]
@@ -150,36 +182,38 @@
150182
tz["name"] = tz.index
151183

152184
# Remove NaN (placefp)
153-
tz = tz[["name", "all", "sov", "bike", "go_dutch", "geometry"]]
154-
ct = ct[["name", "all", "sov", "bike", "go_dutch", "geometry"]]
185+
tz = tz[["name", "all", "bike", "go_dutch", "ebike", "geometry"]]
186+
ct = ct[["name", "all", "bike", "go_dutch", "ebike", "geometry"]]
155187

156188
tz["geometry"] = tz["geometry"].simplify(5.0)
157189
ct["geometry"] = ct["geometry"].simplify(25.0)
158190

159191
tz.to_crs("EPSG:4326").to_file(path + "/taz.GeoJson", driver="GeoJSON")
160192
ct.to_crs("EPSG:4326").to_file(path + "/county.GeoJson", driver="GeoJSON")
161193

162-
fd = flow_data.loc[(flow_data["orig_cnt"] == countyfp[0]) | (flow_data["dest_cnt"] == countyfp[0])]
194+
fd = flow_data.to_frm(countyfp)
163195

164-
name = county.loc[countyfp[0], "name"]
196+
name = county.loc[row[0], "name"]
165197
name = name.replace(" ","")
166198
path = out_dir + "county/" + name
167199
if not os.path.isdir(path):
168200
os.mkdir(path)
169201

170-
# stp.plot_dist(fd, path)
171-
172-
fd = fd[["all", "bike", "go_dutch", "geometry", "distance", "gradient"]]
202+
fd = fd[["all", "bike", "go_dutch", "ebike", "geometry", "od_lines", "routes", "distance", "gradient", "directness"]]
173203
if not fd.empty:
174204
fd = fd.loc[fd["distance"] > 0]
175205
fd = fd.loc[fd["distance"] < 30000]
206+
network = fd.network()
176207
fd = fd.nlargest(100, "bike")
177-
fd.to_crs("EPSG:4326").to_file(path + "/line.GeoJson", driver="GeoJSON")
208+
fd.set_geometry("od_lines", crs=flow_data.crs).to_crs("EPSG:4326").drop(columns=["geometry","routes"]).to_file(path + "/line.GeoJson", driver="GeoJSON")
209+
fd.set_geometry("routes", crs=flow_data.crs).to_crs("EPSG:4326").drop(columns=["geometry","od_lines"]).to_file(path + "/route.GeoJson", driver="GeoJSON")
210+
network.to_crs("EPSG:4326").to_file(path + "/network.GeoJson", driver="GeoJSON")
178211
else:
179212
ar = np.empty([1,6])
180213
ar[:,:] = np.nan
181214
fd = pd.DataFrame(data=ar, columns=fd.columns)
182215

183216
fd.to_pickle(path + "/line.pkl")
217+
network.to_pickle(path + "/network.pkl")
184218

185219
################################################################################

0 commit comments

Comments
 (0)