-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture_work.qmd
1989 lines (1750 loc) · 55.5 KB
/
future_work.qmd
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Future work & suggestions
## Point of interest (POI)
## Data
```{r}
data.frame(
analysis = c("Vulnerability index"),
problem =c("Results and conclussions based on aggregated data can be limited and biased"),
suggestion =c("Find non-spatial data that can be added using municipality, neighbor or sector ID")
)
```
### Bairros
```{sql}
#| eval: false
# import the data : shp2pgsql -D -I -s 4674 -W "LATIN1" '43SEE250GC_SIR.shp' barrios_brasil | psql -p 25432 -U docker -d gis -h localhost
CREATE TABLE brasil_porto_barrios AS
SELECT
nm_bairro,
st_union(brasil_barrios_4326.geom_4326)
FROM
brasil_barrios_4326,
ghs_aoi_porto_alegre
WHERE
brasil_barrios_4326.nm_bairro IS NOT NULL
AND
st_intersects(brasil_barrios_4326.geom_4326,
ghs_aoi_porto_alegre.geom)
GROUP BY
nm_bairro;
```
## Perforamance
### Flood mask
Using the dilate and erode method described in the slide 95/187 ([link](https://cdn2.hubspot.net/hubfs/2283855/PostGIS%20Day%202019%20-%20Overview.pdf)) could remove small islands that increase the computational costs. A use case of this technique is observed when simplifying coastlines ([link](https://github.com/rruiz-s/heigit-gima/wiki/2.-Data-preparation#post-scenario))
### Buildings
#### Download from Overture
```{r}
#| eval: false
#| code-summary: Import buildings and roads from Overture
## Import data
library(overturemapsr)
ghs <- st_read(eisenberg_connection, "ghs_aoi_porto_alegre")
overture_roads <- record_batch_reader(overture_type = 'segment', bbox = sf::st_bbox(ghs))
overture_buildings <- record_batch_reader(overture_type = 'building', bbox = sf::st_bbox(ghs))
## Subset data
overture_roads_subset <- overture_roads |> dplyr::select(c(id, geometry,class,update_time, subtype))
## Export data
### Local file
library(arrow)
library(sfarrow)
sfarrow::st_write_parquet(overture_roads, "overture_roads.parquet")
### Database
library(DBI)
library(nanoarrow)
DBI::dbWriteTable(eisenberg_connection, "overture_roads_subset", overture_roads_subset)
```
#### Processing in duckdb
```{sql}
#| eval: false
--- Using Duckdb
INSTALL spatial;
LOAD spatial;
----- Importing buildings & flood extent from file to duckdb
---- Buildings
CREATE TABLE porto_buildings AS
SELECT
id,
st_geomfromwkb(geometry) as geom
FROM
'porto_buildings_overture.parquet';
--- Floodig
CREATE TABLE flood AS
SELECT
*
FROM
st_read('flooding_porto_cleaned.geojson');
---- Filtering building in flood extent
CREATE TABLE flood_building AS
SELECT
buildings.*
FROM
porto_buildings AS buildings,
flood
WHERE
st_intersects(buildings.geom, flood.geom);
--- Export data
COPY flood_building TO
'flood_building_v2.gpkg' WITH (FORMAT GDAL, DRIVER 'GPKG');
COPY porto_buildings TO
'porto_buildings.gpkg' WITH (FORMAT GDAL, DRIVER 'GPKG');
```
## Alternative route
```{sql}
#| connection: eisenberg_connection
#| eval: false
CREATE TABLE second_path_net AS
WITH first_path AS (
SELECT * FROM pgr_dijkstra(
'SELECT id,
source,
target,
cost
FROM porto_alegre_net_pre_component_one',
7249, 7268,
true))
SELECT
*
FROM
porto_alegre_net_pre_component_one
WHERE
porto_alegre_net_pre_component_one.id NOT IN (select edge from first_path);
SELECT * FROM pgr_dijkstra(
'SELECT id,
source,
target,
cost
FROM second_path_net',
7249, 7268,
true)
-----2
CREATE TABLE pkt_2000 AS
WITH hospital AS (SELECT
h.ds_cnes,
net.id,
h.geom
FROM hospital_rs_geoportal as h
LEFT JOIN LATERAL
(SELECT
id,
the_geom
FROM
porto_alegre_net_pre_component_one_vertices_pgr as net
ORDER BY h.geom <-> net.the_geom
LIMIT 1) AS net ON TRUE
WHERE
h.ds_cnes = 'IRMANDADE DA SANTA CASA DE MISERICORDIA DE PORTO ALEGRE'),
origin AS(SELECT * FROM porto_alegre_net_pre_component_one_vertices_pgr
WHERE id = 7268)
SELECT j.path_id, j.node, j.edge, b.cost, b.the_geom FROM pgr_KSP(
'SELECT id,
source,
target,
cost
FROM porto_alegre_net_pre_component_one',
(SELECT id FROM hospital),
(SELECT id FROM origin),
2000) as j
left JOIN porto_alegre_net_pre_component_one AS b
ON j.edge = b.id;
CREATE TABLE road_without_first AS
SELECT
*
FROM
porto_alegre_net_pre_component_one
WHERE
id NOT IN (SELECT edge FROM pkt_2000 WHERE path_id = 1);
-----3
WITH paths AS (
SELECT
path_id,
edge,
agg_cost,
SUM(agg_cost) OVER (PARTITION BY path_id ORDER BY seq ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cum_sum,
SUM(agg_cost) OVER (PARTITION BY path_id) AS total_cost
FROM
your_table
),
filtered_paths AS (
SELECT
path_id,
edge,
agg_cost,
cum_sum,
total_cost
FROM
paths
WHERE
cum_sum > 0.25 * total_cost -- remove the first 25%
AND cum_sum < 0.75 * total_cost -- remove the last 25%
)
SELECT
path_id,
edge,
agg_cost
FROM
filtered_paths;
----5
WITH hospital AS (SELECT
h.ds_cnes,
net.id,
h.geom
FROM hospital_rs_geoportal as h
LEFT JOIN LATERAL
(SELECT
id,
the_geom
FROM
porto_alegre_net_pre_component_one_vertices_pgr as net
ORDER BY h.geom <-> net.the_geom
LIMIT 1) AS net ON TRUE
WHERE
h.ds_cnes = 'IRMANDADE DA SANTA CASA DE MISERICORDIA DE PORTO ALEGRE'),
origin AS(SELECT
*
FROM
porto_alegre_net_pre_component_one_vertices_pgr
WHERE id = 7268),
table_ksp AS (
SELECT j.seq, j.path_id, j.path_seq, j.node, j.edge, j.agg_cost,j.cost,
b.the_geom, b.target, b.source, b.id FROM pgr_KSP(
'SELECT id,
source,
target,
cost
FROM porto_alegre_net_pre_component_one',
(SELECT id FROM hospital),
(SELECT
id
FROM
origin,
extract_ksp_segments
WHERE
origin.id NOT IN (
SELECT
id
FROM
extract_ksp_segments
WHERE
path_id = 1)),
3) as j
left JOIN porto_alegre_net_pre_component_one AS b
ON j.edge = b.id)
SELECT * FROM table_ksp;
```
This second one does work without creating two segments of networks. It just increased the costs dinamically. This is inspired by [post](https://gis-ops.com/pgrouting-customize-routing-pleasant-hiking/) and [post](https://gis.stackexchange.com/questions/432741/creating-two-costs-in-pgrouting)
```{sql}
#| eval: false
---- Obtain the shortest path as usual
CREATE TABLE first_path_dijkstra AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
9372,
1084) AS path
LEFT JOIN porto_alegre_net_pre_component_one AS net
ON path.edge = net.id;
---- detected the shortest path and then increased cost two fold
CREATE TABLE dijkstra_second_route AS
WITH pgr_ksp_table AS (
SELECT
*
FROM pgr_ksp('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
9372,
1084,
3,
true)), ----node 9372, network = source, target)
second_route AS (
SELECT
net.*,
CASE
WHEN route.node = net.source THEN net.cost * 2
ELSE net.cost
END AS cost_updated
FROM
porto_alegre_net_largest AS net
LEFT JOIN
pgr_ksp_table AS route
ON
route.edge= net.id)
SELECT * FROM second_route;
---- Create spatial index
CREATE INDEX idx_ ON dijkstra_second_route USING gist(the_geom);
CREATE INDEX dijkstra_second_route_source_source ON dijkstra_second_route USING btree(source);
CREATE INDEX dijkstra_second_route_source_target ON dijkstra_second_route USING btree(target);
---- obtain the second shortest path, since the first-path has now higher
CREATE TABLE second_path_dikstra AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom
FROM
pgr_Dijkstra('
SELECT
id,
source,
target,
cost_updated AS cost
FROM
dijkstra_second_route',
9372,
1084) AS path
LEFT JOIN
dijkstra_second_route AS net ON
path.edge = net.id
```
Using this strategy, the following code create 3 alternative routes obtaining the final table "paths_linestring_table".
```{sql}
#| eval: false
CREATE TABLE first_path_6211 AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom,
1 AS path
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
6211,
ARRAY(SELECT id FROM hospital_rs_node_v2),
directed := TRUE) AS path
LEFT JOIN porto_alegre_net_largest AS net
ON path.edge = net.id;
---- detected the shortest path and then increased cost two fold
DROP TABLE dijkstra_second_route_6211;
CREATE TABLE dijkstra_second_route_6211 AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
6211,
ARRAY(SELECT id FROM hospital_rs_node_v2),
directed:=true)), ----node 9372, network = source, target)
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;
---- Create spatial index
CREATE INDEX idx_second_6211 ON dijkstra_second_route_6211 USING gist(the_geom);
CREATE INDEX idx_second_6211_source ON dijkstra_second_route_6211 USING btree(source);
CREATE INDEX idx_second_6211_target ON dijkstra_second_route_6211 USING btree(target);
---- obtain the second shortest path, since the first-path has now higher
DROP TABLE second_path_dikstra_6211;
CREATE TABLE second_path_dikstra_6211 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
dijkstra_second_route_6211',
6211,
ARRAY(SELECT id FROM hospital_rs_node_v2)) AS path
LEFT JOIN
dijkstra_second_route_6211 AS net ON
path.edge = net.id;
select * from second_path_dikstra_6211;
----- 3 path
SELECT * FROM dijkstra_second_route_6211;
DROP TABLE dijkstra_third_route_6211;
CREATE TABLE dijkstra_third_route_6211 AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost_updated AS cost
FROM dijkstra_second_route_6211',
6211,
ARRAY(SELECT id FROM hospital_rs_node_v2),
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
dijkstra_second_route_6211 AS net
LEFT JOIN
pgr_doubled_dijkstra AS route
ON
route.edge= net.id)
SELECT * FROM second_route;
----
DROP TABLE third_path_dikstra_6211 ;
CREATE TABLE third_path_dikstra_6211 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
dijkstra_third_route_6211',
6211,
ARRAY(SELECT id FROM hospital_rs_node_v2)) AS path
LEFT JOIN
dijkstra_third_route_6211 AS net ON
path.edge = net.id;
select * from dijkstra_third_route_6211;
CREATE TABLE three_alternative_paths AS
SELECT * FROM first_path_6211
UNION
SELECT * FROM second_path_dikstra_6211
UNION
SELECT * FROM third_path_dikstra_6211
CREATE TABLE three_alternative_paths_20 AS
SELECT * FROM first_path_6211
UNION
SELECT * FROM second_path_dikstra_6211
UNION
SELECT * FROM third_path_dikstra_6211
CREATE TABLE ernesto_three_alternatives AS
SELECT
alternative_paths.*,
hospitals.ds_cnes,
hospitals.cd_cnes
FROM
three_alternative_paths AS alternative_paths
JOIN hospital_rs_node_v2 AS hospitals
ON hospitals.id = alternative_paths.end_vid
WHERE end_vid = 1743;
CREATE TABLE ernesto_three_alternatives_20 AS
SELECT
alternative_paths.*,
hospitals.ds_cnes,
hospitals.cd_cnes
FROM
three_alternative_paths_20 AS alternative_paths
JOIN hospital_rs_node_v2 AS hospitals
ON hospitals.id = alternative_paths.end_vid
WHERE end_vid = 1743;
CREATE TABLE paths_linestring_table AS
WITH paths_linestring AS (
SELECT
start_vid,
end_vid,
path,
st_union(the_geom) AS line_geom
FROM
three_alternative_paths_20
GROUP BY path, start_vid, end_vid)
SELECT
paths_linestring.*,
hospitals.ds_cnes,
round(st_length(line_geom::geography)::numeric/1000,2) AS distance
FROM
paths_linestring
JOIN hospital_rs_node_v2 AS hospitals
ON hospitals.id = paths_linestring.end_vid
```
This code created the table to represent these values
```{r}
#| eval: false
alternative_routes <- st_read(eisenberg_connection ,"paths_linestring_table")
library(gtExtras)
alternative_routes |> arrange(distance) |>
st_drop_geometry() |>
select(c(ds_cnes, path, distance)) |>
mutate(ds_cnes = as_factor(str_to_title(ds_cnes))) |>
group_by(ds_cnes) |>
mutate(sd = sd(distance)) |>
gt(groupname_col="ds_cnes")
```
### Porto Alegre
```{sql}
#| eval: false
--- Before flooding
CREATE TABLE centroid_cbd_porto AS
SELECT
vertices.id,
vertices.the_geom
FROM
central_business_district AS cbd
LEFT JOIN LATERAL
(SELECT
id,
the_geom
FROM
porto_alegre_net_pre_vertices_pgr AS vertices
ORDER BY
st_centroid(cbd.st_union) <-> vertices.the_geom
LIMIT 1) AS vertices ON TRUE
WHERE
nm_bairro
LIKE
'Centro Histórico';
CREATE TABLE first_path_porto_alegre_cbd AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom,
1 AS path
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospital_rs_node_v2),
directed := TRUE) AS path
LEFT JOIN porto_alegre_net_largest AS net
ON path.edge = net.id;
CREATE TABLE dijkstra_second_route_cbd_porto AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM porto_alegre_net_largest',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospital_rs_node_v2),
directed:=true)),
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;
--- Create spatial index
CREATE INDEX idx_second_cdb_porto ON dijkstra_second_route_cbd_porto USING gist(the_geom);
CREATE INDEX idx_second_cdb_porto_source ON dijkstra_second_route_cbd_porto USING btree(source);
CREATE INDEX idx_second_cdb_porto_target ON dijkstra_second_route_cbd_porto USING btree(target);
---- obtain the second shortest path, since the first-path has now higher
DROP TABLE second_path_dikstra_cdb_porto;
CREATE TABLE second_path_dikstra_cdb_porto 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
dijkstra_second_route_cbd_porto',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospital_rs_node_v2)) AS path
LEFT JOIN
dijkstra_second_route_cbd_porto AS net ON
path.edge = net.id;
select * from second_path_dikstra_cdb_porto;
CREATE TABLE dijkstra_third_route_cdb_porto AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost_updated AS cost
FROM dijkstra_second_route_cbd_porto',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospital_rs_node_v2),
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
dijkstra_second_route_cbd_porto AS net
LEFT JOIN
pgr_doubled_dijkstra AS route
ON
route.edge= net.id)
SELECT * FROM second_route;
----
CREATE TABLE third_path_dikstra_cdb_porto 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
dijkstra_third_route_cdb_porto',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospital_rs_node_v2)) AS path
LEFT JOIN
dijkstra_third_route_cdb_porto AS net ON
path.edge = net.id;
select * from third_path_dikstra_cdb_porto;
CREATE TABLE three_alternative_paths_porto AS
SELECT * FROM first_path_porto_alegre_cbd
UNION
SELECT * FROM second_path_dikstra_cdb_porto
UNION
SELECT * FROM third_path_dikstra_cdb_porto
CREATE TABLE paths_linestring_table_cdb_porto AS
WITH paths_linestring AS (
SELECT
start_vid,
end_vid,
path,
st_union(the_geom) AS line_geom
FROM
three_alternative_paths_porto
GROUP BY path, start_vid, end_vid)
SELECT
paths_linestring.*,
hospitals.ds_cnes,
round(st_length(line_geom::geography)::numeric/1000,2) AS distance
FROM
paths_linestring
JOIN hospitales AS hospitals
ON hospitals.id = paths_linestring.end_vid
SELECT * FROM paths_linestring_table_cdb_porto;
---- after flooding
---- Postflooding
---- hospitals
CREATE TABLE hospitals_after_flooding AS
SELECT
hospitals.id
FROM
hospital_rs_node_v2 AS hospitals,
flooding_cleaned_porto_union AS flood
WHERE st_disjoint(hospitals.geom_node,
flood.geom);
-----
DROP TABLE first_path_porto_alegre_cbd_after;
CREATE TABLE first_path_porto_alegre_cbd_after AS
SELECT
seq,
path_seq,
start_vid,
end_vid,
node,
edge,
net.the_geom,
1 AS path
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM prueba_largest_network_post',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospitals_after_flooding),
directed := TRUE) AS path
LEFT JOIN prueba_largest_network_post AS net
ON path.edge = net.id;
----------------
DROP TABLE dijkstra_second_route_cbd_porto_after;
CREATE TABLE dijkstra_second_route_cbd_porto_after AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost
FROM prueba_largest_network_post',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospitals_after_flooding),
directed:=true)),
second_route AS (
SELECT
net.*,
CASE
WHEN route.node = net.source THEN net.cost * 20
ELSE net.cost
END AS cost_updated
FROM
prueba_largest_network_post AS net
LEFT JOIN
pgr_doubled_dijkstra AS route
ON
route.edge= net.id)
SELECT * FROM second_route;
-----
SELECT DISTINCT(end_vid) FROM first_path_porto_alegre_cbd_after; ---15 hospitals
SELECT DISTINCT (cd_cnes) FROM hospital_rs_node_v2 ; --- 22 hospital totals
-------------
DROP TABLE second_path_dikstra_cdb_porto_after;
CREATE TABLE second_path_dikstra_cdb_porto_after 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
dijkstra_second_route_cbd_porto_after',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospital_rs_node_v2)) AS path
LEFT JOIN
dijkstra_second_route_cbd_porto_after AS net ON
path.edge = net.id;
DROP TABLE dijkstra_third_route_cdb_porto_after;
CREATE TABLE dijkstra_third_route_cdb_porto_after AS
WITH pgr_doubled_dijkstra AS (
SELECT
*
FROM pgr_dijkstra('
SELECT
id,
source,
target,
cost_updated AS cost
FROM dijkstra_second_route_cbd_porto_after',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospital_rs_node_v2),
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
dijkstra_second_route_cbd_porto_after AS net
LEFT JOIN
pgr_doubled_dijkstra AS route
ON
route.edge= net.id)
SELECT * FROM second_route;
-----
DROP TABLE third_path_dikstra_cdb_porto_after;
CREATE TABLE third_path_dikstra_cdb_porto_after 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
dijkstra_third_route_cdb_porto_after',
(SELECT id FROM centroid_cbd_porto),
ARRAY(SELECT id FROM hospital_rs_node_v2)) AS path
LEFT JOIN
dijkstra_third_route_cdb_porto_after AS net ON
path.edge = net.id;
CREATE TABLE three_alternative_paths_porto_after AS
SELECT * FROM first_path_porto_alegre_cbd_after
UNION
SELECT * FROM second_path_dikstra_cdb_porto_after
UNION
SELECT * FROM third_path_dikstra_cdb_porto_after
CREATE TABLE paths_linestring_table_cdb_porto_after AS
WITH paths_linestring AS (
SELECT
start_vid,
end_vid,
path,
st_union(the_geom) AS line_geom
FROM
three_alternative_paths_porto_after
GROUP BY path, start_vid, end_vid)
SELECT
paths_linestring.*,
hospitals.ds_cnes,
round(st_length(line_geom::geography)::numeric/1000,2) AS distance
FROM
paths_linestring
JOIN hospital_rs_node_v2 AS hospitals
ON hospitals.id = paths_linestring.end_vid
```
### Canoas
```{sql}
#| eval: false
CREATE TABLE centroid_cbd_canoas AS
SELECT
vertices.id,
vertices.the_geom
FROM
central_business_district AS cbd
LEFT JOIN LATERAL
(SELECT
id,
the_geom
FROM