-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLTS_plot.py
118 lines (92 loc) · 3.61 KB
/
LTS_plot.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
"""
Plotting Level of Traffic Stress
This notebook plots the Level of Traffic Stress map calculated in `LTS_OSM'.
"""
# import os
# import glob
from pathlib import Path
import shutil
import numpy as np
import pandas as pd
import geopandas as gpd
# import shapely.geometry
# import contextily as cx
# from matplotlib import pyplot as plt
# from matplotlib.lines import Line2D
# import plotly.express as px
# import lonboard
# from lonboard.colormap import apply_categorical_cmap
dataFolder = 'data'
queryFolder = 'query'
plotFolder = 'plots'
# create a list of the values we want to assign for each condition
ltsColors = ['grey', 'green', 'deepskyblue', 'orange', 'red']
def load_data(region):
if type(region) is list:
df = pd.DataFrame()
for r in region:
dfr = pd.read_csv(f'{dataFolder}/{r}_4_all_lts.csv', low_memory=False)
df = pd.concat([df, dfr])
print(f'{dfr.shape=} | {df.shape=} | {r}')
else:
df = pd.read_csv(f'{dataFolder}/{region}_4_all_lts.csv', low_memory=False)
# convert to a geodataframe for plotting
geodf = gpd.GeoDataFrame(
df.loc[:, [c for c in df.columns if c != "geometry"]],
geometry=gpd.GeoSeries.from_wkt(df["geometry"]),
crs='wgs84') # projection from graph
# gdf_nodes = pd.read_csv(f"{dataFolder}/{region}_6_gdf_nodes.csv", index_col=0)
# define lts colours for plotting
conditions = [
(geodf['LTS'] == 0),
(geodf['LTS'] == 1),
(geodf['LTS'] == 2),
(geodf['LTS'] == 3),
(geodf['LTS'] == 4),
]
# create a new column and use np.select to assign values to it using our lists as arguments
geodf['color'] = np.select(conditions, ltsColors, default='grey')
return geodf#, gdf_nodes
def plot_lts_geojson(region, all_lts):
lts = all_lts[all_lts['LTS'] > 0]
# Worth about 1.5% of json size (don't know if more effective when getting to mapbox)
# lts = lts.replace({'yes': 1, 'no': 0}).infer_objects(copy=False)
# Worth about 4% of json size
# lts = lts.replace({'Assumed': 'A'})
fields_general = [
'geometry', 'LTS', 'osmid', 'name', 'highway',
'speed', 'speed_rule',
'centerline', 'centerline_rule',
'ADT', 'ADT_rule',
'lane_count', 'oneway',
'street_narrow_wide',
'width_street', 'width_street_rule',
'zoom',
# 'parse',
]
fields_dirs = [
'bike_allowed', 'bike_lane', 'separation',
'parking', 'parking_width',
'buffer', 'buffer_rule', 'bike_width', 'bike_width_rule', 'bike_reach',
'LTS_mixed', 'LTS_bikelane_noparking', 'LTS_bikelane_yesparking',
'LTS_bike_access', 'LTS_separation', 'LTS',
]
fields_dirs = [field + '_fwd' for field in fields_dirs] + [field + '_rev' for field in fields_dirs]
geo_json = lts[fields_general + fields_dirs].to_json()
# Save GeoJson
json_plot_file = f'{plotFolder}/{region}_LTS.json'
with open(json_plot_file, 'w') as f:
f.write(geo_json + '\n')
shutil.copy(json_plot_file, f'{plotFolder}/LTS.json')
return
def main(region, format="json"):
Path(plotFolder).mkdir(exist_ok=True)
all_lts = load_data(region)
# plot_lts_static(region, all_lts)
if format == "json":
plot_lts_geojson(region, all_lts)
if __name__ == '__main__':
# city = 'Cambridge'
city = 'Boston'
# city = 'Somerville'
main(city)