-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_points_isochrone.R
545 lines (489 loc) · 17.4 KB
/
loop_points_isochrone.R
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
eisenberg_connection <- DBI::dbConnect(RPostgres::Postgres(),
user= "docker",
password = "docker",
host = "localhost",
dbname="gis",
port = 25432)
table_end <- glue_sql("CREATE TABLE table_end
(start_vid integer,
end_vid integer,
path smallint,
length float,
cost float)")
#idx <- 1
to <- sf::read_sf(eisenberg_connection,
"hospital_rs_node_v2") |> st_as_sf()
tic.clear()
table_end_db <- dbGetQuery(conn=eisenberg_connection,table_end )
library(tictoc)
for (idx in 1:length(from$id)) {
tic(glue("run {idx}/{length(from$id)}"))
hospital_name <- from$ds_cnes[idx]
origin_id <- from$id[idx]
table_name_1 <- glue("first_path_point_{as.character(idx)}")
table_name_2 <- glue("second_path_point_{as.character(idx)}")
table_name_3 <- glue("second_route_point_{as.character(idx)}")
table_name_4 <- glue("third_path_point_{as.character(idx)}")
table_name_5 <- glue("third_route_point_{as.character(idx)}")
table_name_6 <- glue("three_alternative_routes_point_{as.character(idx)}")
table_name_7 <- glue("table_end_point_{as.character(idx)}")
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_1`}", .con=eisenberg_connection),
conn=eisenberg_connection)
# Assuming you have an open PostgreSQL connection in con
query_1 <- glue_sql("
CREATE TABLE {`table_name_1`} AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
net.the_geom,
node,
edge,
1 AS path
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
ARRAY(SELECT id FROM isochrones_sampling_points WHERE id ={origin_id}),
ARRAY(SELECT id AS end_id FROM hospital_rs_node_v2 WHERE cd_cnes = {hospital_name}),
directed:=TRUE) AS path
LEFT JOIN porto_alegre_net_largest AS net
ON Path.edge = net.id", .con = eisenberg_connection)
# Send the query
result_q1 <- dbGetQuery(conn= eisenberg_connection, query_1)
# dijkstra_second_route
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_2`}", .con=eisenberg_connection),
conn=eisenberg_connection)
query_2 <- glue_sql("
CREATE TABLE {`table_name_2`} AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
ARRAY(SELECT id FROM points_cardiologia WHERE id = {origin_id}),
ARRAY(SELECT id FROM hospital_rs_node_v2 WHERE ds_cnes = {hospital_name}),
directed := TRUE -- This clause should be inside the pgr_dijkstra function
)
), -- Corrected closing of pgr_dijkstra CTE
second_route AS (
SELECT
net.*,
CASE
WHEN route.node = net.source THEN net.cost * 20
ELSE net.cost
END AS cost_updated
FROM
porto_alegre_net_largest AS net
LEFT JOIN
pgr_doubled_dijkstra AS route
ON
route.edge = net.id
)
SELECT *
FROM second_route", .con = eisenberg_connection)
result_2 <- dbGetQuery(conn= eisenberg_connection, query_2)
## Calculate teh second route
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_3`}", .con=eisenberg_connection),
conn=eisenberg_connection)
query_3 <- glue_sql("
CREATE TABLE {`table_name_3`} AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom,
2 AS path
FROM
pgr_dijkstra('
SELECT
id,
source,
target,
cost_updated AS cost
FROM
second_path_point_1',
ARRAY(SELECT id FROM points_cardiologia WHERE id = {origin_id}),
ARRAY(SELECT id FROM hospital_rs_node_v2 WHERE ds_cnes = {hospital_name}), directed:=TRUE
) AS path
LEFT JOIN
second_path_point_1 AS net
ON path.edge = net.id;
", .con = eisenberg_connection)
# Run the CREATE TABLE query
dbExecute(conn = eisenberg_connection, query_3)
select_query <- "SELECT * FROM second_route_point_1;"
# Fetch the data
result_3 <- dbGetQuery(conn = eisenberg_connection, select_query)
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_4`}", .con=eisenberg_connection),
conn=eisenberg_connection)
# Third route
query_4 <- glue_sql("
CREATE TABLE {`table_name_4`} AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost_updated AS cost
FROM second_path_point_1',
ARRAY(SELECT id FROM points_cardiologia WHERE id = {origin_id}),
ARRAY(SELECT id FROM hospital_rs_node_v2 WHERE ds_cnes = {hospital_name}),
directed:=TRUE)), ----node 9372, network = source, target)
second_route AS (
SELECT
net.*,
CASE
WHEN route.node = net.source THEN net.cost_updated * 20
ELSE net.cost_updated
END AS cost_updated_nd
FROM
second_path_point_1 AS net
LEFT JOIN
pgr_doubled_dijkstra AS route
ON
route.edge= net.id)
SELECT * FROM second_route", .con=eisenberg_connection);
result_4 <- dbGetQuery(conn = eisenberg_connection, query_4)
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_5`}", .con=eisenberg_connection),
conn=eisenberg_connection)
# third route
query_5 <- glue_sql("
CREATE TABLE {`table_name_5`} AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom,
3 AS path
FROM
pgr_Dijkstra('
SELECT
id,
source,
target,
cost_updated_nd AS cost
FROM
third_path_point_1',
ARRAY(SELECT id FROM points_cardiologia WHERE id = {origin_id}),
ARRAY(SELECT id FROM hospital_rs_node_v2 WHERE ds_cnes = {hospital_name})) AS path
LEFT JOIN
third_path_point_1 AS net ON
path.edge = net.id;", .con= eisenberg_connection)
result <- dbGetQuery(conn = eisenberg_connection, query_5)
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_6`}", .con=eisenberg_connection),
conn=eisenberg_connection)
alterantive_point_1 <- glue_sql("
CREATE TABLE {`table_name_6`} AS
SELECT start_vid, end_vid,path, the_geom, edge FROM {`table_name_1`}
UNION
SELECT start_vid, end_vid,path, the_geom, edge FROM {`table_name_3`}
UNION
SELECT start_vid, end_vid,path, the_geom, edge FROM {`table_name_5`}", .con = eisenberg_connection)
result_6 <- dbGetQuery(conn = eisenberg_connection, alterantive_point_1)
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_7`}", .con=eisenberg_connection),
conn=eisenberg_connection)
table_end_point_1 <- glue_sql("
CREATE TABLE {`table_name_7`} AS
SELECT
alternative.*,
ceil(st_length(alternative.the_geom::geography)/1000) As length,
original.cost AS cost
FROM
{`table_name_6`} AS alternative
LEFT JOIN
porto_alegre_net_largest AS original
ON
alternative.edge = original.id
WHERE
edge!= -1", .con=eisenberg_connection)
result_7 <- dbGetQuery(conn=eisenberg_connection,table_end_point_1 )
calculations <- glue_sql("
INSERT INTO table_end (start_vid, end_vid, path, length, cost)
SELECT
start_vid,
end_vid,
path,
st_union(the_geom),
sum(length) AS length,
sum(cost) AS cost
FROM
{`table_name_7`}
GROUP BY
start_vid,
end_vid,
path
ORDER BY path ASC", .con=eisenberg_connection)
dbExecute(con=eisenberg_connection, calculations)
toc()
}
## Test 2
to <- sf::read_sf(eisenberg_connection,
"hospital_rs_node_v2") |> st_as_sf()
table_end_v2 <- glue_sql("CREATE TABLE table_end_v2
(start_vid integer,
end_vid integer,
path smallint,
length float,
cost float,
the_geom geometry)")
table_end_db <- dbGetQuery(conn=eisenberg_connection,table_end_v2 )
isochrones_sampling_points <-st_read(eisenberg_connection, "isochrones_snapped")
df_isochrone_points <- isochrones_sampling_points
DBI::dbWriteTable(eisenberg_connection,"isochrones_sampling_points",isochrones_sampling_points)
# we have the id of the hospital, then we nned all the points that are in the isochrone of the hospital. How do we get this?
idx_hospital <- 15
for (idx_hospital in 1:length(unique(isochrones_sampling_points$cd_cnes))){
tic(glue("hospital run {idx_hospital}/{length(unique(isochrones_sampling_points$cd_cnes))}"))
code_hospital<-unique(isochrones_sampling_points$cd_cnes)[idx_hospital]
points_for_isochrone <- filter(isochrones_sampling_points,
cd_cnes == code_hospital)
alternative_routes(
df_isochrone_points=points_for_isochrone)
toc()
}
#idx <- 1
tic.clear()
library(tictoc)
## data= isochrones_sampling_points
alternative_routes <- function(df_isochrone_points){
for (idx in 1:length(df_isochrone_points$id)) {
tic(glue("run {idx}/{length(df_isochrone_points$id)}"))
hospital_name <- df_isochrone_points$cd_cnes[idx]
origin_id <- df_isochrone_points$id[idx]
table_name_1 <- glue("first_path_point_{as.character(idx)}")
table_name_2 <- glue("second_path_point_{as.character(idx)}")
table_name_3 <- glue("second_route_point_{as.character(idx)}")
table_name_4 <- glue("third_path_point_{as.character(idx)}")
table_name_5 <- glue("third_route_point_{as.character(idx)}")
table_name_6 <- glue("three_alternative_routes_point_{as.character(idx)}")
table_name_7 <- glue("table_end_point_{as.character(idx)}")
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_1`}", .con=eisenberg_connection),
conn=eisenberg_connection)
# Assuming you have an open PostgreSQL connection in con
query_1 <- glue_sql("
CREATE TABLE {`table_name_1`} AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
net.the_geom,
node,
edge,
1 AS path
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
ARRAY(SELECT id FROM isochrones_snapped WHERE id ={origin_id}),
ARRAY(SELECT id AS end_id FROM hospital_rs_node_v2 WHERE cd_cnes = {hospital_name}),
directed:=TRUE) AS path
LEFT JOIN porto_alegre_net_largest AS net
ON path.edge = net.id", .con = eisenberg_connection)
# Send the query
result_q1 <- dbGetQuery(conn= eisenberg_connection, query_1)
# dijkstra_second_route
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_2`}", .con=eisenberg_connection),
conn=eisenberg_connection)
query_2 <- glue_sql("
CREATE TABLE {`table_name_2`} AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
ARRAY(SELECT id FROM isochrones_snapped WHERE id = {origin_id}),
ARRAY(SELECT id FROM hospital_rs_node_v2 WHERE cd_cnes = {hospital_name}),
directed := TRUE -- This clause should be inside the pgr_dijkstra function
)
), -- Corrected closing of pgr_dijkstra CTE
second_route AS (
SELECT
net.*,
CASE
WHEN route.node = net.source THEN net.cost * 20
ELSE net.cost
END AS cost_updated
FROM
porto_alegre_net_largest AS net
LEFT JOIN
pgr_doubled_dijkstra AS route
ON
route.edge = net.id
)
SELECT *
FROM second_route", .con = eisenberg_connection)
result_2 <- dbGetQuery(conn= eisenberg_connection, query_2)
## Calculate teh second route
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_3`}", .con=eisenberg_connection),
conn=eisenberg_connection)
query_3 <- glue_sql("
CREATE TABLE {`table_name_3`} AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom,
2 AS path
FROM
pgr_dijkstra('
SELECT
id,
source,
target,
cost_updated AS cost
FROM
{`table_name_2`}',
ARRAY(SELECT id FROM isochrones_snapped WHERE id = {origin_id}),
ARRAY(SELECT id FROM hospital_rs_node_v2 WHERE cd_cnes = {hospital_name}),
directed:=TRUE
) AS path
LEFT JOIN
{`table_name_2`} AS net
ON path.edge = net.id;
", .con = eisenberg_connection)
# Run the CREATE TABLE query
dbExecute(conn = eisenberg_connection, query_3)
select_query <- glue_sql("SELECT * FROM {`table_name_3`} ;",.con=eisenberg_connection)
# Fetch the data
result_3 <- dbGetQuery(conn = eisenberg_connection, select_query)
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_4`}", .con=eisenberg_connection),
conn=eisenberg_connection)
# Third route
query_4 <- glue_sql("
CREATE TABLE {`table_name_4`} AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost_updated AS cost
FROM {`table_name_2`}',
ARRAY(SELECT id FROM isochrones_snapped WHERE id = {origin_id}),
ARRAY(SELECT id FROM hospital_rs_node_v2 WHERE cd_cnes = {hospital_name}),
directed:=TRUE)), ----node 9372, network = source, target)
second_route AS (
SELECT
net.*,
CASE
WHEN route.node = net.source THEN net.cost_updated * 20
ELSE net.cost_updated
END AS cost_updated_nd
FROM
{`table_name_2`} AS net
LEFT JOIN
pgr_doubled_dijkstra AS route
ON
route.edge= net.id)
SELECT * FROM second_route", .con=eisenberg_connection);
result_4 <- dbGetQuery(conn = eisenberg_connection, query_4)
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_5`}", .con=eisenberg_connection),
conn=eisenberg_connection)
# third route
query_5 <- glue_sql("
CREATE TABLE {`table_name_5`} AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom,
3 AS path
FROM
pgr_Dijkstra('
SELECT
id,
source,
target,
cost_updated_nd AS cost
FROM
{`table_name_4`}',
ARRAY(SELECT id FROM isochrones_snapped WHERE id = {origin_id}),
ARRAY(SELECT id FROM hospital_rs_node_v2 WHERE cd_cnes = {hospital_name}),
directed:=TRUE) AS path
LEFT JOIN
{`table_name_4`} AS net ON
path.edge = net.id;", .con= eisenberg_connection)
result <- dbGetQuery(conn = eisenberg_connection, query_5)
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_6`}", .con=eisenberg_connection),
conn=eisenberg_connection)
alterantive_point_1 <- glue_sql("
CREATE TABLE {`table_name_6`} AS
SELECT start_vid, end_vid,path, the_geom, edge FROM {`table_name_1`}
UNION
SELECT start_vid, end_vid,path, the_geom, edge FROM {`table_name_3`}
UNION
SELECT start_vid, end_vid,path, the_geom, edge FROM {`table_name_5`}", .con = eisenberg_connection)
result_6 <- dbGetQuery(conn = eisenberg_connection, alterantive_point_1)
dbExecute(glue_sql("DROP TABLE IF EXISTS {`table_name_7`}", .con=eisenberg_connection),
conn=eisenberg_connection)
table_end_point_1 <- glue_sql("
CREATE TABLE {`table_name_7`} AS
SELECT
alternative.*,
ceil(st_length(alternative.the_geom::geography)/1000) As length,
original.cost AS cost
FROM
{`table_name_6`} AS alternative
LEFT JOIN
porto_alegre_net_largest AS original
ON
alternative.edge = original.id
WHERE
edge!= -1", .con=eisenberg_connection)
result_7 <- dbGetQuery(conn=eisenberg_connection,table_end_point_1 )
calculations <- glue_sql("
INSERT INTO table_end_v2 (start_vid, end_vid, path, length, cost, the_geom)
SELECT
start_vid,
end_vid,
path,
sum(length) AS length,
sum(cost) AS cost,
st_union(the_geom) as the_geom
FROM
{`table_name_7`}
GROUP BY
start_vid,
end_vid,
path
ORDER BY path ASC", .con=eisenberg_connection)
dbExecute(conn=eisenberg_connection, calculations)
toc()
}
}