-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
235 lines (206 loc) · 7.28 KB
/
main.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import argparse
import json
import logging
import sys
from pathlib import Path
from roadgraphtool.config import parse_config_file
import roadgraphtool.db
import roadgraphtool.log
from roadgraphtool.db import db
from roadgraphtool.process_osm import import_osm_to_db
def get_area_for_demand(
srid_plain: int,
dataset_ids: list,
zone_types: list,
buffer_meters: int,
min_requests_in_zone: int,
datetime_min: str,
datetime_max: str,
center_point: tuple,
max_distance_from_center_point_meters: int,
) -> list:
sql_query = """
select * from get_area_for_demand(
srid_plain := :srid_plain,
dataset_ids := (:dataset_ids)::smallint[],
zone_types := (:zone_types)::smallint[],
buffer_meters := (:buffer_meters)::smallint,
min_requests_in_zone := (:min_requests_in_zone)::smallint,
datetime_min := :datetime_min,
datetime_max := :datetime_max,
center_point := st_makepoint(:center_x, :center_y),
max_distance_from_center_point_meters := (:max_distance_from_center_point_meters)::smallint
);"""
params = {
"srid_plain": srid_plain,
"dataset_ids": dataset_ids,
"zone_types": zone_types,
"buffer_meters": buffer_meters,
"min_requests_in_zone": min_requests_in_zone,
"datetime_min": datetime_min,
"datetime_max": datetime_max,
"center_x": center_point[0],
"center_y": center_point[1],
"max_distance_from_center_point_meters": max_distance_from_center_point_meters,
}
return db.execute_sql_and_fetch_all_rows(sql_query, params)
def insert_area(name: str, coordinates: list):
geom_json = {"type": "MultiPolygon", "coordinates": coordinates}
params = {"name": name, "json_data": json.dumps(geom_json)}
sql_query = """insert into areas (name, geom) values (:name, st_geomfromgeojson(:json_data))"""
db.execute_sql(sql_query, params)
def contract_graph_in_area(
target_area_id: int, target_area_srid: int, fill_speed: bool = True
):
sql_query = f'call public.contract_graph_in_area({target_area_id}::smallint, {target_area_srid}::int{", FALSE" if not fill_speed else ""})'
db.execute_sql(sql_query)
def select_network_nodes_in_area(target_area_id: int) -> list:
sql_query = (
f"select * from select_network_nodes_in_area({target_area_id}::smallint)"
)
return db.execute_sql_and_fetch_all_rows(sql_query)
def assign_average_speed_to_all_segments_in_area(
target_area_id: int, target_area_srid: int
):
sql_query = (
f"call public.assign_average_speed_to_all_segments_in_area({target_area_id}::smallint, "
f"{target_area_srid}::int)"
)
db.execute_sql(sql_query)
def compute_strong_components(target_area_id: int):
sql_query = f"call public.compute_strong_components({target_area_id}::smallint)"
db.execute_sql(sql_query)
def compute_speeds_for_segments(
target_area_id: int, speed_records_dataset: int, hour: int, day_of_week: int
):
sql_query = (
f"call public.compute_speeds_for_segments({target_area_id}::smallint, "
f"{speed_records_dataset}::smallint, {hour}::smallint, {day_of_week}::smallint)"
)
db.execute_sql(sql_query)
def compute_speeds_from_neighborhood_segments(
target_area_id: int, target_area_srid: int
):
sql_query = (
f"call public.compute_speeds_from_neighborhood_segments({target_area_id}::smallint, "
f"{target_area_srid}::int)"
)
db.execute_sql(sql_query)
def configure_arg_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="File containing the main flow of your application"
)
parser.add_argument(
"-a", "--area_id", type=int, help="Id of the area.", required=True
)
parser.add_argument(
"-s",
"--area_srid",
type=int,
help="Postgis srid. Default is set to 4326.",
default=4326,
required=False,
)
parser.add_argument(
"-f",
"--fill-speed",
type=bool,
choices=[True, False],
help="An option indicating if specific functions should process speed data. Default is set to False.",
default=False,
required=False,
)
parser.add_argument(
'-i',
'--import',
dest='importing',
action="store_true",
help='Import OSM data to database specified in config.ini'
)
parser.add_argument(
'-if',
'--input-file',
dest='input_file',
required=True,
help='Input OSM file path for -i/--import.'
)
parser.add_argument(
'-sf', '--style-file',
dest='style_file',
help=f"Optional style file path for -i/--import. Default is 'pipeline.lua' otherwise.",
required=False
)
parser.add_argument(
'-sch', '--schema',
dest='schema',
help="Optional schema argument for -i/--import. Default is 'public' otherwise.",
required=False
)
parser.add_argument(
'--force',
dest='force',
action="store_true",
help="Force overwrite of data in existing tables in schema.",
required=False
)
parser.add_argument(
"-W",
dest="password",
action="store_true",
help="Force password prompt instead of using pgpass file.")
return parser
def main():
args = sys.argv
if len(args) < 2:
logging.error("You have to provide a path to the config file as an argument.")
return -1
config = parse_config_file(Path(args[1]))
roadgraphtool.db.init_db(config)
if config.importer.activated:
import_osm_to_db(config)
# area_id = args.area_id
# area_srid = args.area_srid
# fill_speed = args.fill_speed
# logging.info("selecting nodes")
# nodes = select_network_nodes_in_area(area_id)
# logging.info("selected network nodes in area_id = {}".format(area_id))
# print(nodes)
#
# logging.info("contracting graph")
# contract_graph_in_area(area_id, area_srid, fill_speed)
#
# logging.info("computing strong components for area_id = {}".format(area_id))
# compute_strong_components(area_id)
# logging.info("storing the results in the component_data table")
#
# insert_area("test1", [])
#
# area = get_area_for_demand(
# 4326,
# [1, 2, 3],
# [1, 2, 3],
# 1000,
# 5,
# "2023-01-01 00:00:00",
# "2023-12-31 23:59:59",
# (50.0, 10.0),
# 5000,
# )
# print(area)
#
# logging.info("Execution of assign_average_speeds_to_all_segments_in_area")
# try:
# assign_average_speed_to_all_segments_in_area(area_id, area_srid)
# except psycopg2.errors.InvalidParameterValue as e:
# logging.info("Expected Error: ", e)
#
# nodes = get_map_nodes_from_db(area_id)
# print(nodes)
#
# logging.info("Execution of compute_speeds_for_segments")
# compute_speeds_for_segments(area_id, 1, 12, 1)
#
# logging.info("Execution of compute_speeds_from_neighborhood_segments")
# compute_speeds_from_neighborhood_segments(area_id, area_srid)
if __name__ == '__main__':
main()