-
Notifications
You must be signed in to change notification settings - Fork 0
/
spatialite_network.c
2910 lines (2860 loc) · 74.1 KB
/
spatialite_network.c
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
/*
/ spatialite_network
/
/ an analysis / validation tool for topological networks
/
/ version 1.0, 2008 October 13
/
/ Author: Sandro Furieri a.furieri@lqt.it
/
/ Copyright (C) 2008 Alessandro Furieri
/
/ This program is free software: you can redistribute it and/or modify
/ it under the terms of the GNU General Public License as published by
/ the Free Software Foundation, either version 3 of the License, or
/ (at your option) any later version.
/
/ This program is distributed in the hope that it will be useful,
/ but WITHOUT ANY WARRANTY; without even the implied warranty of
/ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/ GNU General Public License for more details.
/
/ You should have received a copy of the GNU General Public License
/ along with this program. If not, see <http://www.gnu.org/licenses/>.
/
*/
#if defined(_WIN32) && !defined(__MINGW32__)
/* MSVC strictly requires this include [off_t] */
#include <sys/types.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>
#if defined(_WIN32) && !defined(__MINGW32__)
#include "config-msvc.h"
#else
#include "config.h"
#endif
#ifdef SPATIALITE_AMALGAMATION
#include <spatialite/sqlite3.h>
#else
#include <sqlite3.h>
#endif
#include <spatialite/gaiageo.h>
#include <spatialite.h>
#if defined(_WIN32) && !defined(__MINGW32__)
#define strcasecmp _stricmp
#endif /* not WIN32 */
#if defined(_WIN32) || defined (__MINGW32__)
#define FORMAT_64 "%I64d"
#else
#define FORMAT_64 "%lld"
#endif
#define ARG_NONE 0
#define ARG_DB_PATH 1
#define ARG_TABLE 2
#define ARG_FROM_COLUMN 3
#define ARG_TO_COLUMN 4
#define ARG_COST_COLUMN 5
#define ARG_GEOM_COLUMN 6
#define ARG_NAME_COLUMN 7
#define ARG_ONEWAY_TOFROM 8
#define ARG_ONEWAY_FROMTO 9
#define ARG_OUT_TABLE 10
#define ARG_VIRT_TABLE 11
#define MAX_BLOCK 1048576
struct pre_node
{
/* a preliminary node */
sqlite3_int64 id;
char code[32];
struct pre_node *next;
};
struct node
{
/* a NODE */
int internal_index;
sqlite3_int64 id;
char code[32];
double x;
double y;
struct arc_ref *first_outcoming;
struct arc_ref *last_outcoming;
struct arc_ref *first_incoming;
struct arc_ref *last_incoming;
struct node *next;
};
struct arc
{
/* an ARC */
sqlite3_int64 rowid;
struct node *from;
struct node *to;
double cost;
struct arc *next;
};
struct arc_ref
{
/* a reference to an Arc */
struct arc *reference;
struct arc_ref *next;
};
struct graph
{
struct pre_node *first_pre;
struct pre_node *last_pre;
int n_pre_nodes;
struct pre_node **sorted_pre_nodes;
struct arc *first_arc;
struct arc *last_arc;
struct node *first_node;
struct node *last_node;
int n_nodes;
struct node **sorted_nodes;
int error;
int node_code;
int max_code_length;
};
static struct graph *
graph_init ()
{
/* allocates and initializes the graph structure */
struct graph *p = malloc (sizeof (struct graph));
p->first_pre = NULL;
p->last_pre = NULL;
p->n_pre_nodes = 0;
p->sorted_pre_nodes = NULL;
p->n_pre_nodes = 0;
p->sorted_pre_nodes = NULL;
p->first_arc = NULL;
p->last_arc = NULL;
p->first_node = NULL;
p->last_node = NULL;
p->n_nodes = 0;
p->sorted_nodes = NULL;
p->error = 0;
p->node_code = 0;
p->max_code_length = 0;
return p;
}
static void
graph_free_pre (struct graph *p)
{
/* cleaning up the preliminary Nodes list */
struct pre_node *pP;
struct pre_node *pPn;
if (!p)
return;
pP = p->first_pre;
while (pP)
{
pPn = pP->next;
free (pP);
pP = pPn;
}
p->first_pre = NULL;
p->last_pre = NULL;
p->n_pre_nodes = 0;
if (p->sorted_pre_nodes)
free (p->sorted_pre_nodes);
p->sorted_pre_nodes = NULL;
}
static void
graph_free (struct graph *p)
{
/* cleaning up any memory allocation for the graph structure */
struct arc *pA;
struct arc *pAn;
struct node *pN;
struct node *pNn;
struct arc_ref *pAR;
struct arc_ref *pARn;
if (!p)
return;
graph_free_pre (p);
pA = p->first_arc;
while (pA)
{
pAn = pA->next;
free (pA);
pA = pAn;
}
pN = p->first_node;
while (pN)
{
pNn = pN->next;
pAR = pN->first_incoming;
while (pAR)
{
pARn = pAR->next;
free (pAR);
pAR = pARn;
}
pAR = pN->first_outcoming;
while (pAR)
{
pARn = pAR->next;
free (pAR);
pAR = pARn;
}
free (pN);
pN = pNn;
}
if (p->sorted_nodes)
free (p->sorted_nodes);
free (p);
}
static int
cmp_nodes2_code (const void *p1, const void *p2)
{
/* compares two nodes by CODE [for BSEARCH] */
struct node *pN1 = (struct node *) p1;
struct node *pN2 = *((struct node **) p2);
return strcmp (pN1->code, pN2->code);
}
static int
cmp_nodes2_id (const void *p1, const void *p2)
{
/* compares two nodes by ID [for BSEARCH] */
struct node *pN1 = (struct node *) p1;
struct node *pN2 = *((struct node **) p2);
if (pN1->id == pN2->id)
return 0;
if (pN1->id > pN2->id)
return 1;
return -1;
}
static struct node *
find_node (struct graph *p_graph, sqlite3_int64 id, const char *code)
{
/* searching a Node into the sorted list */
struct node **ret;
struct node pN;
if (!(p_graph->sorted_nodes))
return NULL;
if (p_graph->node_code)
{
/* Nodes are identified by a TEXT code */
int len = strlen (code);
if (len > 31)
{
memcpy (pN.code, code, 31);
*(pN.code + 31) = '\0';
}
else
strcpy (pN.code, code);
ret = bsearch (&pN, p_graph->sorted_nodes, p_graph->n_nodes,
sizeof (struct node *), cmp_nodes2_code);
}
else
{
/* Nodes are identified by an INTEGER id */
pN.id = id;
ret = bsearch (&pN, p_graph->sorted_nodes, p_graph->n_nodes,
sizeof (struct node *), cmp_nodes2_id);
}
if (!ret)
return NULL;
return *ret;
}
static int
cmp_nodes1_code (const void *p1, const void *p2)
{
/* compares two nodes by CODE [for QSORT] */
struct node *pN1 = *((struct node **) p1);
struct node *pN2 = *((struct node **) p2);
return strcmp (pN1->code, pN2->code);
}
static int
cmp_nodes1_id (const void *p1, const void *p2)
{
/* compares two nodes by ID [for QSORT] */
struct node *pN1 = *((struct node **) p1);
struct node *pN2 = *((struct node **) p2);
if (pN1->id == pN2->id)
return 0;
if (pN1->id > pN2->id)
return 1;
return -1;
}
static void
sort_nodes (struct graph *p_graph)
{
/* updating the Nodes sorted list */
int i;
struct node *pN;
p_graph->n_nodes = 0;
if (p_graph->sorted_nodes)
{
/* we must free the already existent sorted list */
free (p_graph->sorted_nodes);
}
p_graph->sorted_nodes = NULL;
pN = p_graph->first_node;
while (pN)
{
(p_graph->n_nodes)++;
pN = pN->next;
}
if (!(p_graph->n_nodes))
return;
p_graph->sorted_nodes = malloc (sizeof (struct node *) * p_graph->n_nodes);
i = 0;
pN = p_graph->first_node;
while (pN)
{
*(p_graph->sorted_nodes + i++) = pN;
pN = pN->next;
}
if (p_graph->node_code)
{
/* Nodes are identified by a TEXT code */
qsort (p_graph->sorted_nodes, p_graph->n_nodes,
sizeof (struct node *), cmp_nodes1_code);
}
else
{
/* Nodes are identified by an INTEGER id */
qsort (p_graph->sorted_nodes, p_graph->n_nodes,
sizeof (struct node *), cmp_nodes1_id);
}
}
static void
insert_node (struct graph *p_graph, sqlite3_int64 id, const char *code,
int node_code)
{
/* inserts a Node into the preliminary list */
struct pre_node *pP = malloc (sizeof (struct pre_node));
if (node_code)
{
/* Node is identified by a TEXT code */
int len = strlen (code);
if (len > 31)
{
memcpy (pP->code, code, 31);
*(pP->code + 31) = '\0';
}
else
strcpy (pP->code, code);
pP->id = -1;
}
else
{
/* Nodes are identified by an INTEGER id */
*(pP->code) = '\0';
pP->id = id;
}
pP->next = NULL;
if (!(p_graph->first_pre))
p_graph->first_pre = pP;
if (p_graph->last_pre)
p_graph->last_pre->next = pP;
p_graph->last_pre = pP;
}
static void
add_node (struct graph *p_graph, sqlite3_int64 id, const char *code)
{
/* inserts a Node into the final list */
int len;
struct node *pN = malloc (sizeof (struct node));
if (p_graph->node_code)
{
/* Nodes are identified by a TEXT code */
len = strlen (code) + 1;
if (len > p_graph->max_code_length)
p_graph->max_code_length = len;
strcpy (pN->code, code);
pN->id = -1;
}
else
{
/* Nodes are identified by an INTEGER id */
*(pN->code) = '\0';
pN->id = id;
}
pN->x = DBL_MAX;
pN->y = DBL_MAX;
pN->first_incoming = NULL;
pN->last_incoming = NULL;
pN->first_outcoming = NULL;
pN->last_outcoming = NULL;
pN->next = NULL;
if (!(p_graph->first_node))
p_graph->first_node = pN;
if (p_graph->last_node)
p_graph->last_node->next = pN;
p_graph->last_node = pN;
}
static struct node *
process_node (struct graph *p_graph, sqlite3_int64 id, const char *code,
double x, double y, struct node **pOther)
{
/* inserts a new node or retrieves an already defined one */
struct node *pN = find_node (p_graph, id, code);
*pOther = NULL;
if (pN)
{
/* this Node already exists into the sorted list */
if (pN->x == DBL_MAX && pN->y == DBL_MAX)
{
pN->x = x;
pN->y = y;
}
else
{
if (pN->x == x && pN->y == y)
;
else
*pOther = pN;
}
return pN;
}
/* unexpected error; undefined Node */
return NULL;
}
static void
add_incoming_arc (struct node *pN, struct arc *pA)
{
/* adds an incoming Arc to a Node */
struct arc_ref *pAR = malloc (sizeof (struct arc_ref));
pAR->reference = pA;
pAR->next = NULL;
if (!(pN->first_incoming))
pN->first_incoming = pAR;
if (pN->last_incoming)
pN->last_incoming->next = pAR;
pN->last_incoming = pAR;
}
static void
add_outcoming_arc (struct node *pN, struct arc *pA)
{
/* adds an outcoming Arc to a Node */
struct arc_ref *pAR = malloc (sizeof (struct arc_ref));
pAR->reference = pA;
pAR->next = NULL;
if (!(pN->first_outcoming))
pN->first_outcoming = pAR;
if (pN->last_outcoming)
pN->last_outcoming->next = pAR;
pN->last_outcoming = pAR;
}
static void
add_arc (struct graph *p_graph, sqlite3_int64 rowid, sqlite3_int64 id_from,
sqlite3_int64 id_to, const char *code_from, const char *code_to,
double node_from_x, double node_from_y, double node_to_x,
double node_to_y, double cost)
{
/* inserting an arc into the memory structures */
struct node *pFrom;
struct node *pTo;
struct node *pN2;
struct arc *pA;
char xRowid[128];
sprintf (xRowid, FORMAT_64, rowid);
pFrom =
process_node (p_graph, id_from, code_from, node_from_x, node_from_y,
&pN2);
if (pN2)
{
printf ("ERROR: arc ROWID=%s; nodeFrom coord inconsistency\n",
xRowid);
printf ("\twas: x=%1.6f y=%1.6f\n", pN2->x, pN2->y);
printf ("\tnow: x=%1.6f y=%1.6f\n", node_from_x, node_from_y);
p_graph->error = 1;
}
pTo = process_node (p_graph, id_to, code_to, node_to_x, node_to_y, &pN2);
if (pN2)
{
printf ("ERROR: arc ROWID=%s; nodeTo coord inconsistency\n", xRowid);
printf ("\twas: x=%1.6f y=%1.6f\n", pN2->x, pN2->y);
printf ("\tnow: x=%1.6f y=%1.6f\n", node_to_x, node_to_y);
p_graph->error = 1;
}
if (!pFrom)
{
printf ("ERROR: arc ROWID=%s internal error: missing NodeFrom\n",
xRowid);
p_graph->error = 1;
}
if (!pTo)
{
printf ("ERROR: arc ROWID=%s internal error: missing NodeTo\n",
xRowid);
p_graph->error = 1;
}
if (pFrom == pTo)
{
printf ("ERROR: arc ROWID=%s is a closed ring\n", xRowid);
p_graph->error = 1;
}
if (p_graph->error)
return;
pA = malloc (sizeof (struct arc));
pA->rowid = rowid;
pA->from = pFrom;
pA->to = pTo;
pA->cost = cost;
pA->next = NULL;
if (!(p_graph->first_arc))
p_graph->first_arc = pA;
if (p_graph->last_arc)
p_graph->last_arc->next = pA;
p_graph->last_arc = pA;
/* updating Node connections */
add_outcoming_arc (pFrom, pA);
add_incoming_arc (pTo, pA);
}
static int
cmp_prenodes_code (const void *p1, const void *p2)
{
/* compares two preliminary nodes by CODE [for QSORT] */
struct pre_node *pP1 = *((struct pre_node **) p1);
struct pre_node *pP2 = *((struct pre_node **) p2);
return strcmp (pP1->code, pP2->code);
}
static int
cmp_prenodes_id (const void *p1, const void *p2)
{
/* compares two preliminary nodes by ID [for QSORT] */
struct pre_node *pP1 = *((struct pre_node **) p1);
struct pre_node *pP2 = *((struct pre_node **) p2);
if (pP1->id == pP2->id)
return 0;
if (pP1->id > pP2->id)
return 1;
return -1;
}
static void
init_nodes (struct graph *p_graph)
{
/* prepares the final Nodes list */
sqlite3_int64 last_id;
char last_code[32];
int i;
struct pre_node *pP;
p_graph->n_pre_nodes = 0;
/* sorting preliminary nodes */
if (p_graph->sorted_pre_nodes)
{
/* we must free the already existent sorted list */
free (p_graph->sorted_pre_nodes);
}
p_graph->sorted_pre_nodes = NULL;
pP = p_graph->first_pre;
while (pP)
{
(p_graph->n_pre_nodes)++;
pP = pP->next;
}
if (!(p_graph->n_pre_nodes))
return;
p_graph->sorted_pre_nodes =
malloc (sizeof (struct pre_node *) * p_graph->n_pre_nodes);
i = 0;
pP = p_graph->first_pre;
while (pP)
{
*(p_graph->sorted_pre_nodes + i++) = pP;
pP = pP->next;
}
if (p_graph->node_code)
{
/* Nodes are identified by a TEXT code */
qsort (p_graph->sorted_pre_nodes, p_graph->n_pre_nodes,
sizeof (struct pre_node *), cmp_prenodes_code);
}
else
{
/* Nodes are identified by an INTEGER id */
qsort (p_graph->sorted_pre_nodes, p_graph->n_pre_nodes,
sizeof (struct pre_node *), cmp_prenodes_id);
}
/* creating the final Nodes linked list */
last_id = -1;
*last_code = '\0';
for (i = 0; i < p_graph->n_pre_nodes; i++)
{
pP = *(p_graph->sorted_pre_nodes + i);
if (p_graph->node_code)
{
/* Nodes are identified by a TEXT code */
if (strcmp (pP->code, last_code) != 0)
add_node (p_graph, pP->id, pP->code);
}
else
{
/* Nodes are identified by an INTEGER id */
if (pP->id != last_id)
add_node (p_graph, pP->id, pP->code);
}
last_id = pP->id;
strcpy (last_code, pP->code);
}
/* sorting the final Nodes list */
sort_nodes (p_graph);
/* cleaning up the preliminary Nodes structs */
graph_free_pre (p_graph);
}
static void
print_report (struct graph *p_graph)
{
/* printing the final report */
int cnt = 0;
int max_in = 0;
int max_out = 0;
int card_in;
int card_out;
int card_1 = 0;
int card_2 = 0;
struct node *pN;
struct arc_ref *pAR;
struct arc *pA = p_graph->first_arc;
while (pA)
{
/* counting the Arcs */
cnt++;
pA = pA->next;
}
pN = p_graph->first_node;
while (pN)
{
card_in = 0;
pAR = pN->first_incoming;
while (pAR)
{
card_in++;
pAR = pAR->next;
}
card_out = 0;
pAR = pN->first_outcoming;
while (pAR)
{
card_out++;
pAR = pAR->next;
}
if (card_in > max_in)
max_in = card_in;
if (card_out > max_out)
max_out = card_out;
if (card_in == 1 && card_out == 1)
card_1++;
if (card_in == 2 && card_out == 2)
card_2++;
pN = pN->next;
}
printf ("\nStatistics\n");
printf
("==================================================================\n");
printf ("\t# Arcs : %d\n", cnt);
printf ("\t# Nodes: %d\n", p_graph->n_nodes);
printf ("\tNode max incoming arcs: %d\n", max_in);
printf ("\tNode max outcoming arcs: %d\n", max_out);
printf ("\t# Nodes cardinality=1: %d [terminal nodes]\n", card_1);
printf ("\t# Nodes cardinality=2: %d [meaningless, pass-through]\n",
card_2);
printf
("==================================================================\n");
}
static struct arc **
prepareOutcomings (struct node *pN, int *count)
{
/* preparing the outcoming arc array */
struct arc **arc_array;
int n = 0;
int i;
int ok;
struct arc_ref *pAR;
struct arc *pA0;
struct arc *pA1;
pAR = pN->first_outcoming;
while (pAR)
{
/* counting how many outcoming arsc are there */
n++;
pAR = pAR->next;
}
if (!n)
{
*count = 0;
return NULL;
}
arc_array = malloc (sizeof (struct arc *) * n);
i = 0;
pAR = pN->first_outcoming;
while (pAR)
{
/* populating the arcs array */
*(arc_array + i++) = pAR->reference;
pAR = pAR->next;
}
ok = 1;
while (ok)
{
/* bubble sorting the arcs by Cost */
ok = 0;
for (i = 1; i < n; i++)
{
pA0 = *(arc_array + i - 1);
pA1 = *(arc_array + i);
if (pA0->cost > pA1->cost)
{
/* swapping the arcs */
*(arc_array + i - 1) = pA1;
*(arc_array + i) = pA0;
ok = 1;
}
}
}
*count = n;
return arc_array;
}
static void
output_node (unsigned char *auxbuf, int *size, int ind, int node_code,
int max_node_length, struct node *pN, int endian_arch,
int a_star_supported)
{
/* exporting a Node into NETWORK-DATA */
int n_star;
int i;
struct arc **arc_array;
struct arc *pA;
unsigned char *out = auxbuf;
*out++ = GAIA_NET_NODE;
gaiaExport32 (out, ind, 1, endian_arch); /* the Node internal index */
out += 4;
if (node_code)
{
/* Nodes are identified by a TEXT Code */
memset (out, '\0', max_node_length);
strcpy ((char *) out, pN->code);
out += max_node_length;
}
else
{
/* Nodes are identified by an INTEGER Id */
gaiaExportI64 (out, pN->id, 1, endian_arch); /* the Node ID */
out += 8;
}
if (a_star_supported)
{
/* in order to support the A* algorithm [X,Y] are required for each node */
gaiaExport64 (out, pN->x, 1, endian_arch);
out += 8;
gaiaExport64 (out, pN->y, 1, endian_arch);
out += 8;
}
arc_array = prepareOutcomings (pN, &n_star);
gaiaExport16 (out, n_star, 1, endian_arch); /* # of outcoming arcs */
out += 2;
for (i = 0; i < n_star; i++)
{
/* exporting the outcoming arcs */
pA = *(arc_array + i);
*out++ = GAIA_NET_ARC;
gaiaExportI64 (out, pA->rowid, 1, endian_arch); /* the Arc rowid */
out += 8;
gaiaExport32 (out, pA->to->internal_index, 1, endian_arch); /* the ToNode internal index */
out += 4;
gaiaExport64 (out, pA->cost, 1, endian_arch); /* the Arc Cost */
out += 8;
*out++ = GAIA_NET_END;
}
if (arc_array)
free (arc_array);
*out++ = GAIA_NET_END;
*size = out - auxbuf;
}
static int
create_network_data (sqlite3 * handle, const char *out_table,
int force_creation, struct graph *p_graph,
const char *table, const char *from_column,
const char *to_column, const char *geom_column,
const char *name_column, int a_star_supported,
double a_star_coeff)
{
/* creates the NETWORK-DATA table */
int ret;
char sql[1024];
char *err_msg = NULL;
unsigned char *auxbuf = malloc (MAX_BLOCK);
unsigned char *buf = malloc (MAX_BLOCK);
unsigned char *out;
sqlite3_stmt *stmt;
int i;
int size;
int endian_arch = gaiaEndianArch ();
struct node *pN;
int pk = 0;
int nodes_cnt;
int len;
for (i = 0; i < p_graph->n_nodes; i++)
{
/* setting the internal index to each Node */
pN = *(p_graph->sorted_nodes + i);
pN->internal_index = i;
}
/* starts a transaction */
strcpy (sql, "BEGIN");
ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
if (ret != SQLITE_OK)
{
printf ("BEGIN error: %s\n", err_msg);
sqlite3_free (err_msg);
goto abort;
}
if (force_creation)
{
sprintf (sql, "DROP TABLE IF EXISTS \"%s\"", out_table);
ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
if (ret != SQLITE_OK)
{
printf ("DROP TABLE error: %s\n", err_msg);
sqlite3_free (err_msg);
goto abort;
}
}
/* creating the NETWORK-DATA table */
sprintf (sql, "CREATE TABLE \"%s\" (", out_table);
strcat (sql, "\"Id\" INTEGER PRIMARY KEY, \"NetworkData\" BLOB NOT NULL)");
ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
if (ret != SQLITE_OK)
{
printf ("CREATE TABLE error: %s\n", err_msg);
sqlite3_free (err_msg);
goto abort;
}
/* preparing the SQL statement */
sprintf (sql, "INSERT INTO \"%s\" (\"Id\", \"NetworkData\") VALUES (?, ?)",
out_table);
ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &stmt, NULL);
if (ret != SQLITE_OK)
{
printf ("INSERT error: %s\n", sqlite3_errmsg (handle));
goto abort;
}
if (pk == 0)
{
/* preparing the HEADER block */
out = buf;
if (a_star_supported)
*out++ = GAIA_NET64_A_STAR_START;
else
*out++ = GAIA_NET64_START;
*out++ = GAIA_NET_HEADER;
gaiaExport32 (out, p_graph->n_nodes, 1, endian_arch); /* how many Nodes are there */
out += 4;
if (p_graph->node_code)
*out++ = GAIA_NET_CODE; /* Nodes are identified by a TEXT code */
else
*out++ = GAIA_NET_ID; /* Nodes are identified by an INTEGER id */
if (p_graph->node_code)
*out++ = p_graph->max_code_length; /* max TEXT code length */
else
*out++ = 0x00;
/* inserting the main Table name */
*out++ = GAIA_NET_TABLE;
len = strlen (table) + 1;
gaiaExport16 (out, len, 1, endian_arch); /* the Table Name length, including last '\0' */
out += 2;
memset (out, '\0', len);
strcpy ((char *) out, table);
out += len;
/* inserting the NodeFrom column name */
*out++ = GAIA_NET_FROM;
len = strlen (from_column) + 1;
gaiaExport16 (out, len, 1, endian_arch); /* the NodeFrom column Name length, including last '\0' */
out += 2;
memset (out, '\0', len);
strcpy ((char *) out, from_column);
out += len;
/* inserting the NodeTo column name */
*out++ = GAIA_NET_TO;
len = strlen (to_column) + 1;
gaiaExport16 (out, len, 1, endian_arch); /* the NodeTo column Name length, including last '\0' */
out += 2;
memset (out, '\0', len);
strcpy ((char *) out, to_column);
out += len;
/* inserting the Geometry column name */
*out++ = GAIA_NET_GEOM;
if (!geom_column)
len = 1;
else
len = strlen (geom_column) + 1;
gaiaExport16 (out, len, 1, endian_arch); /* the Geometry column Name length, including last '\0' */
out += 2;
memset (out, '\0', len);
if (geom_column)
strcpy ((char *) out, geom_column);
out += len;
/* inserting the Name column name - may be empty */
*out++ = GAIA_NET_NAME;
if (!name_column)
len = 1;
else
len = strlen (name_column) + 1;
gaiaExport16 (out, len, 1, endian_arch); /* the Name column Name length, including last '\0' */
out += 2;
memset (out, '\0', len);
if (name_column)
strcpy ((char *) out, name_column);
out += len;
if (a_star_supported)
{
/* inserting the A* Heuristic Coeff */
*out++ = GAIA_NET_A_STAR_COEFF;
gaiaExport64 (out, a_star_coeff, 1, endian_arch);
out += 8;
}
*out++ = GAIA_NET_END;
/* INSERTing the Header block */
sqlite3_reset (stmt);
sqlite3_clear_bindings (stmt);
sqlite3_bind_int64 (stmt, 1, pk);
sqlite3_bind_blob (stmt, 2, buf, out - buf, SQLITE_STATIC);
ret = sqlite3_step (stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
;
else
{
printf ("sqlite3_step() error: %s\n", sqlite3_errmsg (handle));
sqlite3_finalize (stmt);
goto abort;
}
pk++;
/* preparing a new block */
out = buf;
*out++ = GAIA_NET_BLOCK;
gaiaExport16 (out, 0, 1, endian_arch); /* how many Nodes are into this block */
out += 2;
nodes_cnt = 0;
}
for (i = 0; i < p_graph->n_nodes; i++)
{
/* looping on each Node */
pN = *(p_graph->sorted_nodes + i);
output_node (auxbuf, &size, i, p_graph->node_code,
p_graph->max_code_length, pN, endian_arch,
a_star_supported);
if (size >= (MAX_BLOCK - (out - buf)))
{
/* inserting the last block */
gaiaExport16 (buf + 1, nodes_cnt, 1, endian_arch); /* how many Nodes are into this block */
sqlite3_reset (stmt);
sqlite3_clear_bindings (stmt);
sqlite3_bind_int64 (stmt, 1, pk);
sqlite3_bind_blob (stmt, 2, buf, out - buf, SQLITE_STATIC);
ret = sqlite3_step (stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
;
else
{
printf ("sqlite3_step() error: %s\n",
sqlite3_errmsg (handle));
sqlite3_finalize (stmt);
goto abort;
}
pk++;
/* preparing a new block */
out = buf;
*out++ = GAIA_NET_BLOCK;
gaiaExport16 (out, 0, 1, endian_arch); /* how many Nodes are into this block */
out += 2;
nodes_cnt = 0;