-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutingeotopo.old
1952 lines (1378 loc) · 50.9 KB
/
routingeotopo.old
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
<?php
header('Content-Type: application/json');
//check
//error_reporting(E_ALL); ini_set('display_errors', 1);
error_reporting(E_ERROR | E_PARSE);
/*
ingresando un fr ( fraccionradio, ej:0108) devuelve:
- las manzanas ordenadas del radio por adyacencia
por manzana:
- el id de fraccionradio
- la manzana actual
- la manzana siguiente
- el linestring de adyacencia entre manzactual - manzsig
- el punto de interseccion entre:
- manzactual y manzsig
- manzactual y boundary del radio
- el id del punto tras stdump de manzactual
- el id del punto tras stdump de manzasig
*/
// php recuperaManzanas01.php --fr=0108
// localhost/test01.php?fr=0106
if ( isset( $_GET["fr"]) ) {
$fr = htmlspecialchars($_GET["fr"]);
} else {
$fr = getopt(null, ["fr:"]);
$fr = $fr['fr'];
}
//pdcl
if ( isset( $_GET["pdcl"]) ) {
$pdcl = htmlspecialchars($_GET["pdcl"]);
} else {
$pdcl = getopt(null, ["pdcl:"]);
$pdcl = $pdcl['pdcl'];
}
// Arrays
$arrayMZAOK = array();
$arrayMZAMIDJOIN = array();
$arrayMZAINNER = array();
$data =array();
$dataInner =array();
try {
//localhost
$mbd = new PDO('pgsql:host=localhost;dbname=gisdata', 'indec', 'indec');
foreach($mbd->query( "
with mzas as
(
WITH const (pdclfr,fr) as ( VALUES ('".$pdcl.$fr."','".$fr."') )
SELECT
pdclfr,
fr,
pol.mzatxt mzaid,
ST_GeometryType(
st_intersection(
pol.geom,
(select ST_Boundary(st_union(st_setsrid(geom,4326))) from indec_e0211poligono where prov||depto||codloc||frac||radio = pdclfr)
)
) in ('ST_LineString', 'ST_MultiLineString', 'ST_GeometryCollection')
as tipo,
(
select
array_to_json(array_agg(polNext.mzatxt))
from public.indec_e0211poligono as polNext
where
prov||depto||codloc||frac||radio = pdclfr and
st_intersects(pol.geom,polNext.geom)
and ST_GeometryType(st_intersection(pol.geom,polNext.geom)) in ('ST_LineString', 'ST_MultiLineString')
) as mza_touches,
(
select
array_to_json(array_agg(ST_Distance(st_centroid(pol.geom),ST_Centroid(polNext.geom))))
from public.indec_e0211poligono as polNext
where
prov||depto||codloc||frac||radio = pdclfr and
st_touches(pol.geom,polNext.geom)
and ST_GeometryType(st_intersection(pol.geom,polNext.geom)) in ('ST_LineString', 'ST_MultiLineString')
) as mza_touchescentdist
,
ST_GeometryType(
st_intersection(
pol.geom,
(select ST_Boundary(st_union(st_setsrid(geom,4326))) from indec_e0211poligono where prov||depto||codloc||frac||radio = pdclfr)
)
) geotype,
st_astext(ST_CollectionExtract(
st_intersection(
pol.geom,
(select ST_Boundary(st_union(st_setsrid(geom,4326))) from indec_e0211poligono where prov||depto||codloc||frac||radio = pdclfr)
),1)) colex
FROM
const,
public.indec_e0211poligono as pol
WHERE
prov||depto||codloc||frac||radio = pdclfr and
ST_GeometryType(
st_intersection(
pol.geom,
(select ST_Boundary(st_union(st_setsrid(geom,4326))) from indec_e0211poligono where prov||depto||codloc||frac||radio = pdclfr)
)
) in ('ST_LineString', 'ST_MultiLineString', 'ST_GeometryCollection')
)
select mzaid, tipo, mza_touches ,mza_touchescentdist , geotype
from mzas
where colex in ('POINT EMPTY','MULTIPOINT EMPTY')
ORDER BY length(mza_touches::text)"
) as $fila) {
//data : manzana id y manzanas intersectadas
array_push($data, array('mzaid'=>json_decode($fila['mzaid']) , 'touches' => json_decode($fila['mza_touches'])));
}
for($i = 0; $i < count($data); ++$i) {
//manzanas que tocan
foreach ( $data[$i]['touches'] as $valtouch) {
if (empty($filacandidata)) {
array_push($arrayMZAOK,$data[$i]['mzaid']);
foreach($mbd->query("
SELECT
mzatxt
FROM public.indec_e0211poligono
WHERE
prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and
st_intersects(
geom,
(
SELECT
ST_CollectionExtract(
st_intersection(
geom, (select ST_Boundary(st_union(st_setsrid(geom,4326))) from indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."')
)
,2)
FROM public.indec_e0211poligono
WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$valtouch."'
)
)
and mzatxt not in (".implode(",", $arrayMZAOK).",".$valtouch.") limit 1" ) as $filaposible) {
if (empty($filaposible)) {} else {
$filacandidata = $filaposible['mzatxt'];
if (!in_array($valtouch, $arrayMZAOK)) {
array_push($arrayMZAOK,$valtouch);
}
if (!in_array($filaposible['mzatxt'], $arrayMZAOK)) {
array_push($arrayMZAOK,$filacandidata);
}
$arrayMZAOK = array_unique($arrayMZAOK);
}
}
} else {
foreach(
$mbd->query("
SELECT
mzatxt
FROM public.indec_e0211poligono
WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and
st_intersects(
geom,
(
SELECT
ST_CollectionExtract(
st_intersection(
geom, (select ST_Boundary(st_union(st_setsrid(geom,4326))) from indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."' )
)
,2)
FROM public.indec_e0211poligono
WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$filacandidata."'
)
)
and mzatxt not in (".implode(",", $arrayMZAOK).",".$filacandidata.")"
) as $filaposible) {
if (empty($filaposible)) {} else {
$filacandidata = $filaposible['mzatxt'];
if (!in_array($filaposible['mzatxt'], $arrayMZAOK)) {
array_push($arrayMZAOK,$filacandidata);
}
$arrayMZAOK = array_unique($arrayMZAOK);
}
}
} //else
} //touches
} //for
$arrayMZAOK = array_unique($arrayMZAOK);
/*
Generar un subradio de manzanas no boundaries repitiendo el proceso excluyendo las manzanas boundaries.
Teniendo como secuencial la ultima del array boundaries y la primera adyacente de las manzanas no boundaries.
*/
foreach($mbd->query( "
with mzasinner as
(
WITH const (pdclfr,fr) as (
values ('".$pdcl.$fr."','".$fr."')
)
SELECT
pdclfr,
fr,
pol.mzatxt mzaid,
ST_GeometryType(
st_intersection(
pol.geom,
(select ST_Boundary(st_union(st_setsrid(geom,4326)))
from indec_e0211poligono
where prov||depto||codloc||frac||radio = pdclfr
and mzatxt not in (".implode(",", $arrayMZAOK).") )
)
) in ('ST_LineString', 'ST_MultiLineString', 'ST_GeometryCollection')
and mzatxt not in (".implode(",", $arrayMZAOK).")
as tipo,
(
select
array_to_json(array_agg(polNext.mzatxt))
from public.indec_e0211poligono as polNext
where
prov||depto||codloc||frac||radio = pdclfr and
st_intersects(pol.geom,polNext.geom)
and ST_GeometryType(st_intersection(pol.geom,polNext.geom)) in ('ST_LineString', 'ST_MultiLineString')
and mzatxt not in (".implode(",", $arrayMZAOK).")
) as mza_touches,
(
select
array_to_json(array_agg(ST_Distance(st_centroid(pol.geom),ST_Centroid(polNext.geom))))
from public.indec_e0211poligono as polNext
where
prov||depto||codloc||frac||radio = pdclfr and
st_touches(pol.geom,polNext.geom)
and ST_GeometryType(st_intersection(pol.geom,polNext.geom)) in ('ST_LineString', 'ST_MultiLineString')
and mzatxt not in (".implode(",", $arrayMZAOK).")
) as mza_touchescentdist
,
ST_GeometryType(
st_intersection(
pol.geom,
(select ST_Boundary(st_union(st_setsrid(geom,4326)))
from indec_e0211poligono
where prov||depto||codloc||frac||radio = pdclfr and
mzatxt not in (".implode(",", $arrayMZAOK).") )
)
) geotype
FROM
const,
public.indec_e0211poligono as pol
WHERE
prov||depto||codloc||frac||radio = pdclfr and
ST_GeometryType(
st_intersection(
pol.geom,
(select ST_Boundary(st_union(st_setsrid(geom,4326)))
from indec_e0211poligono
where prov||depto||codloc||frac||radio = pdclfr and
mzatxt not in (".implode(",", $arrayMZAOK).") )
)
) in ('ST_LineString', 'ST_MultiLineString', 'ST_GeometryCollection')
and mzatxt not in (".implode(",", $arrayMZAOK).")
)
select mzaid, tipo, mza_touches ,mza_touchescentdist , geotype
from mzasinner
where mzaid not in (".implode(",", $arrayMZAOK).")
ORDER BY length(mza_touches::text)"
) as $fila) {
array_push($dataInner, array('mzaid'=>json_decode($fila['mzaid']), 'touches' => json_decode($fila['mza_touches'])));
}
for($i = 0; $i < count($dataInner); ++$i) {
foreach ( $dataInner[$i]['touches'] as $valtouchInner) {
if (empty($filacandidataInner)) {
array_push($arrayMZAINNER,$dataInner[$i]['mzaid']);
foreach($mbd->query("
SELECT
mzatxt
FROM public.indec_e0211poligono
WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and
st_intersects(
geom,
(
SELECT
ST_CollectionExtract(
st_intersection(
geom, (select ST_Boundary(st_union(st_setsrid(geom,4326))) from indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."'
and mzatxt not in (".implode(",", $arrayMZAOK).") )
)
,2)
FROM public.indec_e0211poligono
WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$valtouchInner."'
and mzatxt not in (".implode(",", $arrayMZAOK).")
)
)
and mzatxt not in (".implode(",", $arrayMZAOK).",".implode(",", $arrayMZAINNER).",".$valtouchInner.")" ) as $filaposibleInner) {
if (empty($filaposibleInner)) {} else {
$filacandidataInner = $filaposibleInner['mzatxt'];
if (!in_array($valtouchInner, $arrayMZAINNER)) {
array_push($arrayMZAINNER,$valtouchInner);
}
if (!in_array($filaposibleInner['mzatxt'], $arrayMZAINNER)) {
array_push($arrayMZAINNER,$filacandidataInner);
}
$arrayMZAINNER = array_unique($arrayMZAINNER);
}
}
} else {
foreach($mbd->query("
SELECT
mzatxt
FROM public.indec_e0211poligono
WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and
st_intersects(
geom,
(
SELECT
ST_CollectionExtract(
st_intersection(
geom, (select ST_Boundary(st_union(st_setsrid(geom,4326))) from indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."'
and mzatxt not in (".implode(",", $arrayMZAOK).") )
)
,2)
FROM public.indec_e0211poligono
WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$filacandidataInner."'
and mzatxt not in (".implode(",", $arrayMZAOK).")
)
)
and mzatxt not in (".implode(",", $arrayMZAOK).",".implode(",", $arrayMZAINNER).",".$filacandidataInner.")" ) as $filaposibleInner) {
if (empty($filaposibleInner)) {} else {
$filacandidataInner = $filaposibleInner['mzatxt'];
if (!in_array($filaposibleInner['mzatxt'], $arrayMZAINNER)) {
array_push($arrayMZAINNER,$filacandidataInner);
}
$arrayMZAINNER = array_unique($arrayMZAINNER);
}
}
}
}
}
$arrayMZAINNER = array_unique($arrayMZAINNER);
array_push($arrayMZAMIDJOIN, end($arrayMZAOK));
array_push($arrayMZAMIDJOIN, $arrayMZAINNER[0]);
if (!empty($arrayMZAOK) ) {
$arrayMZAOKLine = "ST_AsText(ST_MakeLine( ST_GeomFromText('MULTIPOINT(";
foreach ($arrayMZAOK as $mzaid) {
foreach($mbd->query( "
SELECT st_X(ST_Centroid(geom)) x, st_Y(ST_Centroid(geom)) y, st_astext(ST_Centroid(geom)) centro FROM public.indec_e0211poligono WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = ".$mzaid. " limit 1"
) as $fila) {
$arrayMZAOKLine .= $fila['x'].' ';
$arrayMZAOKLine .= $fila['y'].", ";
}
}
$arrayMZAOKLine = rtrim($arrayMZAOKLine, ', ');
}
if (!empty($arrayMZAMIDJOIN) ) {
$arrayMZAMIDJOINLine = "ST_AsText(ST_MakeLine( ST_GeomFromText('MULTIPOINT(";
foreach ($arrayMZAMIDJOIN as $mzaid) {
foreach($mbd->query( "
SELECT st_X(ST_Centroid(geom)) x, st_Y(ST_Centroid(geom)) y, st_astext(ST_Centroid(geom)) centro FROM public.indec_e0211poligono WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = ".$mzaid. " limit 1"
) as $fila) {
$arrayMZAMIDJOINLine .= $fila['x'].' ';
$arrayMZAMIDJOINLine .= $fila['y'].", ";
}
}
$arrayMZAMIDJOINLine = rtrim($arrayMZAMIDJOINLine, ', ');
}
if (!empty($arrayMZAINNER) ) {
$arrayMZAINNERLine = "ST_AsText(ST_MakeLine( ST_GeomFromText('MULTIPOINT(";
foreach ($arrayMZAINNER as $mzaid) {
foreach($mbd->query( "
SELECT st_X(ST_Centroid(geom)) x, st_Y(ST_Centroid(geom)) y, st_astext(ST_Centroid(geom)) centro FROM public.indec_e0211poligono WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = ".$mzaid. " limit 1"
) as $fila) {
$arrayMZAINNERLine .= $fila['x'].' ';
$arrayMZAINNERLine .= $fila['y'].", ";
}
}
$arrayMZAINNERLine = rtrim($arrayMZAINNERLine, ', ');
}
//start
$respuestamza = [];
for ($x = 0; $x < count($arrayMZAOK); $x++) {
$y = $x+1;
$z = $y+1;
// manzana actual (1), manzana siguiente (2), manzana tercera (3)
$mzaAct = $arrayMZAOK[$x];
$mzaSig = $arrayMZAOK[$y];
$mzaTerc = $arrayMZAOK[$z];
//if $mzaTipo:
//inicial : 1 ruteo desde-hasta : mismo vertice de interseccion adyacente.
//medio : 2 ruteos desde adyacencia inicial a adyacencia siguiente
//final : 1 ruteo desde-hasta : mismo vertice de interseccion adyacente.
if ( $x+1 == count($arrayMZAOK) ) {
$mzaCantRutas = 1;
$mzaTipoPath = 'end';
} elseif ( $x+1 == 1 ) {
$mzaCantRutas = 1;
$mzaTipoPath = 'start';
} else {
$mzaCantRutas = 2;
$mzaTipoPath ='mid';
}
//posee 3ra manz
if ( !empty($mzaTerc ) ) {
foreach($mbd->query(
"
WITH orderpunto as (
WITH linderasmza as (
SELECT
distinct
st_astext(
ST_LineMerge(
st_intersection(
(select distinct geom from public.indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$mzaAct."' ),
(select distinct geom from public.indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$mzaSig."' )
)
)
) intersect_lineamza,
st_astext(
ST_CollectionHomogenize(
st_intersection(
(
st_intersection(
(select distinct geom from public.indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$mzaAct."' ),
(select distinct geom from public.indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$mzaSig."' )
)
),
(
select ST_Boundary(st_union(geom)) from indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."'
)
)
)
) intersect_lineabound
FROM public.indec_e0211poligono
WHERE prov||depto||codloc||frac||radio = '".$pdcl.$fr."'
limit 1
)
SELECT
intersect_lineamza,
intersect_lineabound,
st_area(
st_intersection(
ST_SetSRID(ST_Buffer((dp).geom ,10),4326),
( select distinct st_union(geom) from public.indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."' )
)
) areaorder,
(dp).path[1] As index,
ST_AsText((dp).geom) As wktnode,
(dp).geom <#>
ST_CollectionHomogenize(
st_intersection(
(
st_intersection(
(select distinct geom from public.indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$mzaSig."' ),
(select distinct geom from public.indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."' and mzatxt = '".$mzaTerc."' )
)
),
(
select ST_Boundary(st_union(geom)) from indec_e0211poligono where prov||depto||codloc||frac||radio = '".$pdcl.$fr."'
)
)
) dist
FROM (
SELECT
ST_DumpPoints(intersect_lineabound) AS dp,
intersect_lineamza,
intersect_lineabound
from linderasmza
) As foo
)
select * from orderpunto
order by areaorder asc, dist asc
limit 1
"
) as $fila) {
// punto de interseccion de manzana act y sig.
$wkt = $fila['wktnode'];
$pgr_verticeid_old = '';
if (isset($pgr_vertix_res['pgr_vertix_id'])) {
$pgr_verticeid_old = $pgr_vertix_res['pgr_vertix_id'];
}
// id del vertice segun la geometria del punto de adyacencia entre manzanas
$pgr_vertix_sql = "
select id as pgr_vertix_id
from public.indec_e0211linea_vertices_pgr
where st_astext(the_geom) = '".$wkt."'
limit 1
";
$pgr_vertix = $mbd->prepare($pgr_vertix_sql);
$pgr_vertix->execute();
$pgr_vertix_res = $pgr_vertix->fetch(PDO::FETCH_ASSOC);
$linestring_ruteo_res = array();
$linestring_ruteo_res_order_true = array();
$linestring_ruteo_res_order_false = array();
if ( $mzaCantRutas == 2) {
// pgrouting pgr_ksp para 2 vertices diferentes de inicio : fin
$pgr_ruteo_sql = "
with ruteo3 as (
with ruteo2 as (
with ruteo as ( SELECT * FROM pgr_ksp (
'SELECT
id,
source, target,
st_length(geomline::geography, true)/100000 as cost,
st_length(geomline::geography, true)/100000 as reverse_cost
FROM
public.indec_e0211linea
WHERE
(
mzai like ''%".$fr.'0'.$mzaAct."'' or
mzad like ''%".$fr.'0'.$mzaAct."'' )',
".$pgr_verticeid_old.",
".$pgr_vertix_res['pgr_vertix_id'].",
2,
true
) where edge > 0
)
select
".$mzaAct." as mzaid,
linea.id as lineaid,
linea.tipo,
linea.nombre,
case
when linea.mzad like '%".$mzaAct."' then linea.desded
else linea.desdei
end desde,
case
when linea.mzad like '%".$mzaAct."' then linea.hastad
else linea.hastai
end hasta,
(
select hn from public.indec_geocoding_viviendas_indec geocode where ref_id = ruteo.edge
order by st_distance ( geocode.geom , ( select distinct the_geom from public.indec_e0211linea_vertices_pgr where id = ruteo.node limit 1) ) limit 1
) as hn,
ruteo.*
from ruteo
join public.indec_e0211linea linea on ruteo.edge = linea.id
)
select
(select st_astext(ST_CollectionHomogenize(geom)) from public.indec_e0211linea l where l.id = edge),
(
select ST_AsText(ST_CollectionHomogenize(ST_Boundary(ST_Union(geom)))) boundary_geom_astext FROM indec_e0211poligono
where prov||depto||codloc||frac||radio in (
'".$pdcl.$fr."'
)
)
,
ST_IsEmpty(
ST_CollectionHomogenize(
st_intersection(
(
select ST_CollectionHomogenize(geom) from public.indec_e0211linea l where l.id = edge
),
(
select ST_CollectionHomogenize(ST_Boundary(ST_Union(geom))) FROM indec_e0211poligono
where prov||depto||codloc||frac||radio in ('".$pdcl.$fr."')
)
)
)
) = 'f'
and
ST_GeometryType(
st_intersection(
(
select ST_CollectionHomogenize(geom) from public.indec_e0211linea l where l.id = edge
),
(
select ST_CollectionHomogenize(ST_Boundary(ST_Union(geom))) FROM indec_e0211poligono
where prov||depto||codloc||frac||radio in (
'".$pdcl.$fr."'
)
) )
) in ('ST_LineString', 'ST_MultiLineString', 'ST_GeometryCollection') boundaryradio_intersecta
,
edge as edge2,
*,
CASE when ABS(hn - desde) < ABS(hn - hasta) then desde else hasta end altura_start,
CASE when ABS(hn - desde) < ABS(hn - hasta) then 'DESDE' else 'HASTA' end altura_orderby,
CASE when MOD (desde::integer, 2) = 0 then 'PAR' else 'IMPAR' end as paridad
from ruteo2
)
select
ROW_NUMBER () OVER (PARTITION BY geoc.ref_id ORDER BY seq,
cnombre,
case when altura_orderby = 'HASTA' then geoc.hn end desc,
case when altura_orderby = 'DESDE' then geoc.hn end asc,
h4,hp ,hd) seqid_por_segmentolinea,
case when geoc.ref_id is null then ruteo3.edge2 else geoc.ref_id end as geocref_id,
geoc.id as geocid,
case when geoc.hn is not null then geoc.hn else ruteo3.altura_start end as geochn,
ruteo3.nombre as geoccnombre,
geoc.h4 as geoch4,
geoc.hp as geochp,
geoc.hd as geocdh,
case when geoc.geom is null then ST_GeomFromText(ruteo3.st_astext) else geoc.geom end as geocgeom,
case when geoc.geom is null then ruteo3.st_astext else st_astext(geoc.geom) end as geocgeomtext,
ruteo3.*
from ruteo3
left join public.indec_geocoding_viviendas_indec geoc on geoc.ref_id = ruteo3.edge
and MOD (desde::integer, 2) = MOD (geoc.hn::integer, 2)
order by
seq,
cnombre,
case when altura_orderby = 'HASTA' then geoc.hn end desc,
case when altura_orderby = 'DESDE' then geoc.hn end asc,
h4,hp ,hd
";
$linestring_ruteo = $mbd->prepare($pgr_ruteo_sql);
$linestring_ruteo->execute();
while ($fila = $linestring_ruteo->fetch(PDO::FETCH_ASSOC)) {
if ($fila['boundaryradio_intersecta'] == true) {
array_push(
$linestring_ruteo_res_order_true,
[
'geocref_id' => $fila['geocref_id'],
'boundaryradio_intersecta' => $fila['boundaryradio_intersecta'],
'geoccnombre' => $fila['geoccnombre'],
'geochn' => $fila['geochn'],
'geocgeomtext' => $fila['geocgeomtext']
]
);
}
else {
array_push(
$linestring_ruteo_res_order_false,
[
'geocref_id' => $fila['geocref_id'],
'boundaryradio_intersecta' => $fila['boundaryradio_intersecta'],
'geoccnombre' => $fila['geoccnombre'],
'geochn' => $fila['geochn'],
'geocgeomtext' => $fila['geocgeomtext']
]
);
}
}
$pgr_ruteo = $mbd->prepare($pgr_ruteo_sql);
$pgr_ruteo->execute();
$linestring_ruteo_res = $pgr_ruteo->fetchAll(PDO::FETCH_ASSOC);
} elseif ( $mzaCantRutas == 1) {
$pgr_ruteo_sql = "
with ruteo as (
SELECT * FROM pgr_TSP(
$$
SELECT * FROM pgr_dijkstraCostMatrix
(
'
SELECT
id,
source, target,
st_length(geomline::geography, true)/100000 as cost,
st_length(geomline::geography, true)/100000 as reverse_cost
FROM
public.indec_e0211linea
WHERE
(
mzai like ''%".$fr."0%'' or
mzad like ''%".$fr."0%''
)
AND
(
mzad like ''%".$mzaAct."'' or
mzai like ''%".$mzaAct."''
)
',
( SELECT array_agg(id) FROM indec_e0211linea_vertices_pgr ),
directed := false
)
$$,
start_id := ".$pgr_vertix_res['pgr_vertix_id'].",
randomize := false
)
)
select
*,
( select distinct st_astext(ST_CollectionHomogenize(the_geom)) from public.indec_e0211linea_vertices_pgr where id = ruteo.node limit 1) as node_geom
from ruteo
";
$pgr_ruteo = $mbd->prepare($pgr_ruteo_sql);
$pgr_ruteo->execute();
$pgr_ruteo_res1 = $pgr_ruteo->fetchAll(PDO::FETCH_ASSOC);
for ($nodo = 0; $nodo <= count($pgr_ruteo_res1)-2; $nodo++) {
$nodosig = $nodo+1;
$nodo1 = $pgr_ruteo_res1[$nodo]['node'];
$nodo1geom = $pgr_ruteo_res1[$nodo]['node_geom'];