-
Notifications
You must be signed in to change notification settings - Fork 0
/
spatialite_osm_net.c
2775 lines (2660 loc) · 77.7 KB
/
spatialite_osm_net.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_osm_net
/
/ a tool loading OSM-XML roads into a SpatiaLite DB
/
/ version 1.0, 2009 August 1
/
/ Author: Sandro Furieri a.furieri@lqt.it
/
/ Copyright (C) 2009 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>
#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>
#include <readosm.h>
#ifdef _WIN32
#define strcasecmp _stricmp
#endif /* not WIN32 */
#define ARG_NONE 0
#define ARG_OSM_PATH 1
#define ARG_DB_PATH 2
#define ARG_TABLE 3
#define ARG_CACHE_SIZE 4
#define ARG_TEMPLATE_PATH 5
#define NODE_STRAT_NONE 0
#define NODE_STRAT_ALL 1
#define NODE_STRAT_ENDS 2
#define ONEWAY_STRAT_NONE 0
#define ONEWAY_STRAT_FULL 1
#define ONEWAY_STRAT_NO_ROUND 2
#define ONEWAY_STRAT_NO_MOTOR 3
#define ONEWAY_STRAT_NO_BOTH 4
struct aux_speed
{
/* an auxiliary struct for Speeds */
char *class_name;
double speed;
struct aux_speed *next;
};
struct aux_class
{
/* an auxiliary struct for road Classes */
char *class_name;
char *sub_class;
struct aux_class *next;
};
struct aux_params
{
/* an auxiliary struct used for OSM parsing */
sqlite3 *db_handle;
const char *table;
int double_arcs;
int noding_strategy;
int oneway_strategy;
struct aux_speed *first_speed;
struct aux_speed *last_speed;
double default_speed;
struct aux_class *first_include;
struct aux_class *last_include;
struct aux_class *first_ignore;
struct aux_class *last_ignore;
sqlite3_stmt *ins_tmp_nodes_stmt;
sqlite3_stmt *upd_tmp_nodes_stmt;
sqlite3_stmt *rd_tmp_nodes_stmt;
sqlite3_stmt *ins_arcs_stmt;
};
static void
save_current_line (gaiaGeomCollPtr geom, gaiaDynamicLinePtr dyn)
{
/* inserting a GraphArc from a splitted Way */
gaiaPointPtr pt;
gaiaLinestringPtr ln;
int iv;
int points = 0;
pt = dyn->First;
while (pt)
{
/* counting how many points are there */
points++;
pt = pt->Next;
}
if (points < 2)
return;
ln = gaiaAddLinestringToGeomColl (geom, points);
iv = 0;
pt = dyn->First;
while (pt)
{
gaiaSetPoint (ln->Coords, iv, pt->X, pt->Y);
iv++;
pt = pt->Next;
}
}
static gaiaGeomCollPtr
build_linestrings (struct aux_params *params, const readosm_way * way)
{
/* building the Arcs of the Graph [may be, splitting a Way in more Arcs] */
int i_ref;
int ret;
gaiaGeomCollPtr geom;
gaiaDynamicLinePtr dyn = gaiaAllocDynamicLine ();
gaiaPointPtr pt;
geom = gaiaAllocGeomColl ();
geom->Srid = 4326;
for (i_ref = 0; i_ref < way->node_ref_count; i_ref++)
{
/* fetching point coords */
sqlite3_int64 id = *(way->node_refs + i_ref);
double x;
double y;
int ref_count;
sqlite3_reset (params->rd_tmp_nodes_stmt);
sqlite3_clear_bindings (params->rd_tmp_nodes_stmt);
sqlite3_bind_int64 (params->rd_tmp_nodes_stmt, 1, id);
/* scrolling the result set */
ret = sqlite3_step (params->rd_tmp_nodes_stmt);
if (ret == SQLITE_DONE)
{
/* empty resultset - no coords !!! */
#if defined(_WIN32) || defined(__MINGW32__)
/* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
fprintf (stderr, "UNRESOLVED-NODE %I64d\n", id);
#else
fprintf (stderr, "UNRESOLVED-NODE %lld\n", id);
#endif
gaiaFreeDynamicLine (dyn);
gaiaFreeGeomColl (geom);
return NULL;
}
else if (ret == SQLITE_ROW)
{
/* ok, we've just fetched a valid row */
x = sqlite3_column_double (params->rd_tmp_nodes_stmt, 0);
y = sqlite3_column_double (params->rd_tmp_nodes_stmt, 1);
ref_count = sqlite3_column_int (params->rd_tmp_nodes_stmt, 2);
pt = dyn->Last;
if (pt)
{
if (pt->X == x && pt->Y == y)
{
/* skipping any repeated point */
continue;
}
}
/* appending the point to the current line anyway */
gaiaAppendPointToDynamicLine (dyn, x, y);
if (params->noding_strategy != NODE_STRAT_NONE)
{
/* attempting to renode the Graph */
int limit = 1;
if (params->noding_strategy == NODE_STRAT_ENDS)
{
/* renoding each Way terminal point */
limit = 0;
}
if ((ref_count > limit)
&& (i_ref > 0 && i_ref < (way->node_ref_count - 1)))
{
/* found an internal Node: saving the current line */
save_current_line (geom, dyn);
/* starting a further line */
gaiaFreeDynamicLine (dyn);
dyn = gaiaAllocDynamicLine ();
/* inserting the current point in the new line */
gaiaAppendPointToDynamicLine (dyn, x, y);
}
}
}
else
{
/* some unexpected error occurred */
fprintf (stderr, "sqlite3_step() error: %s\n",
sqlite3_errmsg (params->db_handle));
sqlite3_finalize (params->rd_tmp_nodes_stmt);
gaiaFreeGeomColl (geom);
gaiaFreeDynamicLine (dyn);
return NULL;
}
}
/* saving the last line */
save_current_line (geom, dyn);
gaiaFreeDynamicLine (dyn);
if (geom->FirstLinestring == NULL)
{
/* invalid geometry: no lines */
gaiaFreeGeomColl (geom);
return NULL;
}
return geom;
}
static int
arcs_insert_straight (struct aux_params *params, sqlite3_int64 id,
const char *class, const char *name, unsigned char *blob,
int blob_size)
{
/* Inserts a uniderectional Arc into the Graph - straight direction */
int ret;
if (params->ins_arcs_stmt == NULL)
return 1;
sqlite3_reset (params->ins_arcs_stmt);
sqlite3_clear_bindings (params->ins_arcs_stmt);
sqlite3_bind_int64 (params->ins_arcs_stmt, 1, id);
sqlite3_bind_text (params->ins_arcs_stmt, 2, class, strlen (class),
SQLITE_STATIC);
sqlite3_bind_text (params->ins_arcs_stmt, 3, name, strlen (name),
SQLITE_STATIC);
sqlite3_bind_blob (params->ins_arcs_stmt, 4, blob, blob_size,
SQLITE_STATIC);
ret = sqlite3_step (params->ins_arcs_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
return 1;
fprintf (stderr, "sqlite3_step() error: INS_ARCS\n");
sqlite3_finalize (params->ins_arcs_stmt);
params->ins_arcs_stmt = NULL;
return 0;
}
static int
arcs_insert_reverse (struct aux_params *params, sqlite3_int64 id,
const char *class, const char *name, unsigned char *blob,
int blob_size)
{
/* Inserts a unidirectional Arc into the Graph - reverse direction */
int ret;
gaiaGeomCollPtr g1;
gaiaGeomCollPtr g2;
gaiaLinestringPtr ln1;
gaiaLinestringPtr ln2;
unsigned char *blob2;
int blob_size2;
int iv1;
int iv2;
double x;
double y;
double z;
double m;
if (params->ins_arcs_stmt == NULL)
return 1;
/* creating a geometry in reverse order */
g1 = gaiaFromSpatiaLiteBlobWkb (blob, blob_size);
if (!g1)
return 0;
ln1 = g1->FirstLinestring;
if (!ln1)
return 0;
if (g1->DimensionModel == GAIA_XY_Z)
g2 = gaiaAllocGeomCollXYZ ();
else if (g1->DimensionModel == GAIA_XY_M)
g2 = gaiaAllocGeomCollXYM ();
else if (g1->DimensionModel == GAIA_XY_Z_M)
g2 = gaiaAllocGeomCollXYZM ();
else
g2 = gaiaAllocGeomColl ();
g2->Srid = g1->Srid;
ln2 = gaiaAddLinestringToGeomColl (g2, ln1->Points);
iv2 = ln1->Points - 1;
for (iv1 = 0; iv1 < ln1->Points; iv1++)
{
/* copying points in reverse order */
if (g1->DimensionModel == GAIA_XY_Z)
{
gaiaGetPointXYZ (ln1->Coords, iv1, &x, &y, &z);
}
else if (g1->DimensionModel == GAIA_XY_M)
{
gaiaGetPointXYM (ln1->Coords, iv1, &x, &y, &m);
}
else if (g1->DimensionModel == GAIA_XY_Z_M)
{
gaiaGetPointXYZM (ln1->Coords, iv1, &x, &y, &z, &m);
}
else
{
gaiaGetPoint (ln1->Coords, iv1, &x, &y);
}
if (g2->DimensionModel == GAIA_XY_Z)
{
gaiaSetPointXYZ (ln2->Coords, iv2, x, y, z);
}
else if (g2->DimensionModel == GAIA_XY_M)
{
gaiaSetPointXYM (ln2->Coords, iv2, x, y, m);
}
else if (g2->DimensionModel == GAIA_XY_Z_M)
{
gaiaSetPointXYZM (ln2->Coords, iv2, x, y, z, m);
}
else
{
gaiaSetPoint (ln2->Coords, iv2, x, y);
}
iv2--;
}
gaiaToSpatiaLiteBlobWkb (g2, &blob2, &blob_size2);
gaiaFreeGeomColl (g1);
gaiaFreeGeomColl (g2);
sqlite3_reset (params->ins_arcs_stmt);
sqlite3_clear_bindings (params->ins_arcs_stmt);
sqlite3_bind_int64 (params->ins_arcs_stmt, 1, id);
sqlite3_bind_text (params->ins_arcs_stmt, 2, class, strlen (class),
SQLITE_STATIC);
sqlite3_bind_text (params->ins_arcs_stmt, 3, name, strlen (name),
SQLITE_STATIC);
sqlite3_bind_blob (params->ins_arcs_stmt, 4, blob2, blob_size2, free);
ret = sqlite3_step (params->ins_arcs_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
return 1;
fprintf (stderr, "sqlite3_step() error: INS_ARCS\n");
sqlite3_finalize (params->ins_arcs_stmt);
params->ins_arcs_stmt = NULL;
return 0;
}
static int
arcs_insert_double (struct aux_params *params, sqlite3_int64 id,
const char *class, const char *name, int oneway,
unsigned char *blob, int blob_size)
{
/* Inserts a unidirectional Arc into the Graph */
if (oneway >= 0)
{
/* inserting the straight direction */
if (!arcs_insert_straight (params, id, class, name, blob, blob_size))
goto stop;
}
if (oneway <= 0)
{
/* inserting the reverse direction */
if (!arcs_insert_reverse (params, id, class, name, blob, blob_size))
goto stop;
}
if (blob)
free (blob);
return 1;
stop:
if (blob)
free (blob);
return 0;
}
static int
arcs_insert (struct aux_params *params, sqlite3_int64 id,
const char *class, const char *name, int oneway,
unsigned char *blob, int blob_size)
{
/* Inserts a bi-directional Arc into the Graph */
int ret;
if (params->ins_arcs_stmt == NULL)
return 1;
sqlite3_reset (params->ins_arcs_stmt);
sqlite3_clear_bindings (params->ins_arcs_stmt);
sqlite3_bind_int64 (params->ins_arcs_stmt, 1, id);
sqlite3_bind_text (params->ins_arcs_stmt, 2, class, strlen (class),
SQLITE_STATIC);
sqlite3_bind_text (params->ins_arcs_stmt, 3, name, strlen (name),
SQLITE_STATIC);
if (oneway > 0)
{
sqlite3_bind_int (params->ins_arcs_stmt, 4, 1);
sqlite3_bind_int (params->ins_arcs_stmt, 5, 0);
}
else if (oneway < 0)
{
sqlite3_bind_int (params->ins_arcs_stmt, 4, 0);
sqlite3_bind_int (params->ins_arcs_stmt, 5, 1);
}
else
{
sqlite3_bind_int (params->ins_arcs_stmt, 4, 1);
sqlite3_bind_int (params->ins_arcs_stmt, 5, 1);
}
sqlite3_bind_blob (params->ins_arcs_stmt, 6, blob, blob_size, free);
ret = sqlite3_step (params->ins_arcs_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
return 1;
fprintf (stderr, "sqlite3_step() error: INS_ARCS\n");
sqlite3_finalize (params->ins_arcs_stmt);
params->ins_arcs_stmt = NULL;
return 0;
}
static int
find_include_class (struct aux_params *params, const char *class,
const char *sub_class)
{
/* testing if this Way belongs to some class to be Included */
struct aux_class *pc = params->first_include;
while (pc)
{
if (strcmp (pc->class_name, class) == 0)
{
if (pc->sub_class == NULL)
return 1;
if (strcmp (pc->sub_class, sub_class) == 0)
return 1;
}
pc = pc->next;
}
return 0;
}
static int
find_ignore_class (struct aux_params *params, const char *class,
const char *sub_class)
{
/* testing if this Way belongs to some class to be Ignored */
struct aux_class *pc = params->first_ignore;
while (pc)
{
if (strcmp (pc->class_name, class) == 0)
{
if (strcmp (pc->sub_class, sub_class) == 0)
return 1;
}
pc = pc->next;
}
return 0;
}
static int
consume_way_1 (const void *user_data, const readosm_way * way)
{
/* processing an OSM Way - Pass#1 (ReadOSM callback function) */
struct aux_params *params = (struct aux_params *) user_data;
const readosm_tag *p_tag;
int i_tag;
int i_ref;
int ret;
sqlite3_int64 id;
int include = 0;
int ignore = 0;
if (params->noding_strategy == NODE_STRAT_NONE)
{
/* renoding the graph isn't required, we can skip all this */
return READOSM_OK;
}
for (i_tag = 0; i_tag < way->tag_count; i_tag++)
{
p_tag = way->tags + i_tag;
if (find_include_class (params, p_tag->key, p_tag->value))
include = 1;
if (find_ignore_class (params, p_tag->key, p_tag->value))
ignore = 1;
}
if (!include || ignore)
return READOSM_OK;
for (i_ref = 0; i_ref < way->node_ref_count; i_ref++)
{
if (params->noding_strategy == NODE_STRAT_ENDS)
{
/* checking only the Way extreme points */
if (i_ref == 0 || i_ref == (way->node_ref_count - 1));
else
continue;
}
id = *(way->node_refs + i_ref);
sqlite3_reset (params->upd_tmp_nodes_stmt);
sqlite3_clear_bindings (params->upd_tmp_nodes_stmt);
sqlite3_bind_int64 (params->upd_tmp_nodes_stmt, 1, id);
ret = sqlite3_step (params->upd_tmp_nodes_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW);
else
{
fprintf (stderr, "sqlite3_step() error: %s\n",
sqlite3_errmsg (params->db_handle));
sqlite3_finalize (params->upd_tmp_nodes_stmt);
return READOSM_ABORT;
}
}
return READOSM_OK;
}
static int
consume_way_2 (const void *user_data, const readosm_way * way)
{
/* processing an OSM Way - Pass#2 (ReadOSM callback function) */
struct aux_params *params = (struct aux_params *) user_data;
const readosm_tag *p_tag;
int i_tag;
unsigned char *blob;
int blob_size;
const char *class = NULL;
const char *name = "*** Unknown ****";
int oneway = 0;
int ret;
int include = 0;
int ignore = 0;
gaiaGeomCollPtr geom;
for (i_tag = 0; i_tag < way->tag_count; i_tag++)
{
p_tag = way->tags + i_tag;
if (find_include_class (params, p_tag->key, p_tag->value))
{
include = 1;
class = p_tag->value;
}
if (find_ignore_class (params, p_tag->key, p_tag->value))
ignore = 1;
}
if (!include || ignore)
return READOSM_OK;
for (i_tag = 0; i_tag < way->tag_count; i_tag++)
{
/* retrieving the road name */
p_tag = way->tags + i_tag;
if (strcmp (p_tag->key, "name") == 0)
{
name = p_tag->value;
break;
}
}
if (params->oneway_strategy != ONEWAY_STRAT_NONE)
{
/* checking for Oneeays */
for (i_tag = 0; i_tag < way->tag_count; i_tag++)
{
/* checking for one-ways */
p_tag = way->tags + i_tag;
if (strcmp (p_tag->key, "oneway") == 0)
{
if (strcmp (p_tag->value, "yes") == 0
|| strcmp (p_tag->value, "true") == 0
|| strcmp (p_tag->value, "1") == 0)
oneway = 1;
if (strcmp (p_tag->value, "-1") == 0
|| strcmp (p_tag->value, "reverse") == 0)
oneway = -1;
}
if (params->oneway_strategy != ONEWAY_STRAT_NO_BOTH
&& params->oneway_strategy != ONEWAY_STRAT_NO_ROUND)
{
/* testing for junction:roundabout */
if (strcmp (p_tag->key, "junction") == 0)
{
if (strcmp (p_tag->value, "roundabout") == 0)
oneway = 1;
}
}
if (params->oneway_strategy != ONEWAY_STRAT_NO_BOTH
&& params->oneway_strategy != ONEWAY_STRAT_NO_MOTOR)
{
/* testing for highway_motorway or highway_motorway_link */
if (strcmp (p_tag->key, "highway") == 0)
{
if (strcmp (p_tag->value, "motorway") == 0
|| strcmp (p_tag->value, "motorway_link") == 0)
oneway = 1;
}
}
}
}
geom = build_linestrings (params, way);
if (geom)
{
gaiaLinestringPtr ln = geom->FirstLinestring;
while (ln)
{
/* inserting any splitted Arc */
gaiaGeomCollPtr g;
gaiaLinestringPtr ln2;
int iv;
if (gaiaIsClosed (ln))
{
ln = ln->Next;
continue;
}
/* building a new Geometry - simple line */
g = gaiaAllocGeomColl ();
g->Srid = 4326;
ln2 = gaiaAddLinestringToGeomColl (g, ln->Points);
for (iv = 0; iv < ln->Points; iv++)
{
/* copying line's points */
double x;
double y;
gaiaGetPoint (ln->Coords, iv, &x, &y);
gaiaSetPoint (ln2->Coords, iv, x, y);
}
gaiaToSpatiaLiteBlobWkb (g, &blob, &blob_size);
if (params->double_arcs)
ret =
arcs_insert_double (params, way->id, class, name,
oneway, blob, blob_size);
else
ret =
arcs_insert (params, way->id, class, name, oneway, blob,
blob_size);
gaiaFreeGeomColl (g);
if (!ret)
return READOSM_ABORT;
ln = ln->Next;
}
gaiaFreeGeomColl (geom);
}
return READOSM_OK;
}
static int
tmp_nodes_insert (struct aux_params *params, const readosm_node * node)
{
/* inserts a node into the corresponding temporary table */
int ret;
if (params->ins_tmp_nodes_stmt == NULL)
return 1;
sqlite3_reset (params->ins_tmp_nodes_stmt);
sqlite3_clear_bindings (params->ins_tmp_nodes_stmt);
sqlite3_bind_int64 (params->ins_tmp_nodes_stmt, 1, node->id);
sqlite3_bind_double (params->ins_tmp_nodes_stmt, 2, node->latitude);
sqlite3_bind_double (params->ins_tmp_nodes_stmt, 3, node->longitude);
ret = sqlite3_step (params->ins_tmp_nodes_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
return 1;
fprintf (stderr, "sqlite3_step() error: INS_TMP_NODES\n");
sqlite3_finalize (params->ins_tmp_nodes_stmt);
params->ins_tmp_nodes_stmt = NULL;
return 0;
}
static int
consume_node (const void *user_data, const readosm_node * node)
{
/* processing an OSM Node (ReadOSM callback function) */
struct aux_params *params = (struct aux_params *) user_data;
if (!tmp_nodes_insert (params, node))
return READOSM_ABORT;
return READOSM_OK;
}
static int
populate_graph_nodes (sqlite3 * handle, const char *table)
{
int ret;
char *sql_err = NULL;
char sql[8192];
char sql2[1024];
sqlite3_stmt *query_stmt = NULL;
sqlite3_stmt *update_stmt = NULL;
/* populating GRAPH_NODES */
strcpy (sql, "INSERT OR IGNORE INTO graph_nodes (lon, lat) ");
strcat (sql, "SELECT ST_X(ST_StartPoint(Geometry)), ");
strcat (sql, "ST_Y(ST_StartPoint(Geometry)) ");
sprintf (sql2, "FROM \"%s\" ", table);
strcat (sql, sql2);
strcat (sql, "UNION ");
strcat (sql, "SELECT ST_X(ST_EndPoint(Geometry)), ");
strcat (sql, "ST_Y(ST_EndPoint(Geometry)) ");
sprintf (sql2, "FROM \"%s\"", table);
strcat (sql, sql2);
ret = sqlite3_exec (handle, sql, NULL, NULL, &sql_err);
if (ret != SQLITE_OK)
{
fprintf (stderr, "GraphNodes SQL error: %s\n", sql_err);
sqlite3_free (sql_err);
return 0;
}
/* setting OSM-IDs to graph-nodes */
/* the complete operation is handled as a unique SQL Transaction */
ret = sqlite3_exec (handle, "BEGIN", NULL, NULL, &sql_err);
if (ret != SQLITE_OK)
{
fprintf (stderr, "BEGIN TRANSACTION error: %s\n", sql_err);
sqlite3_free (sql_err);
return 0;
}
strcpy (sql, "SELECT n.ROWID, t.id FROM osm_tmp_nodes AS t ");
strcat (sql, "JOIN graph_nodes AS n ON (t.lon = n.lon AND t.lat = n.lat)");
ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &query_stmt, NULL);
if (ret != SQLITE_OK)
{
fprintf (stderr, "SQL error: %s\n%s\n", sql, sqlite3_errmsg (handle));
goto error;
}
strcpy (sql, "UPDATE graph_nodes SET osm_id = ? ");
strcat (sql, "WHERE ROWID = ?");
ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &update_stmt, NULL);
if (ret != SQLITE_OK)
{
fprintf (stderr, "SQL error: %s\n%s\n", sql, sqlite3_errmsg (handle));
goto error;
}
while (1)
{
ret = sqlite3_step (query_stmt);
if (ret == SQLITE_DONE)
break;
if (ret == SQLITE_ROW)
{
sqlite3_int64 id = sqlite3_column_int64 (query_stmt, 0);
sqlite3_int64 osm_id = sqlite3_column_int64 (query_stmt, 1);
/* udating the GraphNote */
sqlite3_reset (update_stmt);
sqlite3_clear_bindings (update_stmt);
sqlite3_bind_int64 (update_stmt, 1, osm_id);
sqlite3_bind_int64 (update_stmt, 2, id);
ret = sqlite3_step (update_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW);
else
{
printf ("sqlite3_step() error: %s\n",
sqlite3_errmsg (handle));
goto error;
}
}
else
{
printf ("sqlite3_step() error: %s\n", sqlite3_errmsg (handle));
goto error;
}
}
if (query_stmt != NULL)
sqlite3_finalize (query_stmt);
if (update_stmt != NULL)
sqlite3_finalize (update_stmt);
/* committing the still pending SQL Transaction */
ret = sqlite3_exec (handle, "COMMIT", NULL, NULL, &sql_err);
if (ret != SQLITE_OK)
{
fprintf (stderr, "COMMIT TRANSACTION error: %s\n", sql_err);
sqlite3_free (sql_err);
}
return 1;
error:
if (query_stmt != NULL)
sqlite3_finalize (query_stmt);
if (update_stmt != NULL)
sqlite3_finalize (update_stmt);
ret = sqlite3_exec (handle, "ROLLBACK", NULL, NULL, &sql_err);
if (ret != SQLITE_OK)
{
fprintf (stderr, "ROLLBACK TRANSACTION error: %s\n", sql_err);
sqlite3_free (sql_err);
}
return 0;
}
static int
set_node_ids (sqlite3 * handle, const char *table)
{
/* assigning IDs to Nodes of the Graph */
int ret;
char *sql_err = NULL;
char sql[8192];
char sql2[1024];
sqlite3_stmt *query_stmt = NULL;
sqlite3_stmt *update_stmt = NULL;
/* the complete operation is handled as a unique SQL Transaction */
ret = sqlite3_exec (handle, "BEGIN", NULL, NULL, &sql_err);
if (ret != SQLITE_OK)
{
fprintf (stderr, "BEGIN TRANSACTION error: %s\n", sql_err);
sqlite3_free (sql_err);
return 0;
}
/* querying NodeIds */
strcpy (sql, "SELECT w.id, n1.ROWID, n2.ROWID ");
sprintf (sql2, "FROM \"%s\" AS w, ", table);
strcat (sql, sql2);
strcat (sql, "graph_nodes AS n1, graph_nodes AS n2 ");
strcat (sql, "WHERE n1.lon = ST_X(ST_StartPoint(w.Geometry)) ");
strcat (sql, "AND n1.lat = ST_Y(ST_StartPoint(w.Geometry)) ");
strcat (sql, "AND n2.lon = ST_X(ST_EndPoint(w.Geometry)) ");
strcat (sql, "AND n2.lat = ST_Y(ST_EndPoint(w.Geometry))");
ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &query_stmt, NULL);
if (ret != SQLITE_OK)
{
fprintf (stderr, "SQL error: %s\n%s\n", sql, sqlite3_errmsg (handle));
goto error;
}
/* updating Arcs */
sprintf (sql, "UPDATE \"%s\" SET node_from = ?, node_to = ? ", table);
strcat (sql, "WHERE id = ?");
ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &update_stmt, NULL);
if (ret != SQLITE_OK)
{
fprintf (stderr, "SQL error: %s\n%s\n", sql, sqlite3_errmsg (handle));
goto error;
}
while (1)
{
ret = sqlite3_step (query_stmt);
if (ret == SQLITE_DONE)
break;
if (ret == SQLITE_ROW)
{
sqlite3_int64 id = sqlite3_column_int64 (query_stmt, 0);
sqlite3_int64 node_from = sqlite3_column_int64 (query_stmt, 1);
sqlite3_int64 node_to = sqlite3_column_int64 (query_stmt, 2);
/* udating the Arc */
sqlite3_reset (update_stmt);
sqlite3_clear_bindings (update_stmt);
sqlite3_bind_int64 (update_stmt, 1, node_from);
sqlite3_bind_int64 (update_stmt, 2, node_to);
sqlite3_bind_int64 (update_stmt, 3, id);
ret = sqlite3_step (update_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW);
else
{
printf ("sqlite3_step() error: %s\n",
sqlite3_errmsg (handle));
goto error;
}
}
else
{
printf ("sqlite3_step() error: %s\n", sqlite3_errmsg (handle));
goto error;
}
}
if (query_stmt != NULL)
sqlite3_finalize (query_stmt);
if (update_stmt != NULL)
sqlite3_finalize (update_stmt);
/* committing the still pending SQL Transaction */
ret = sqlite3_exec (handle, "COMMIT", NULL, NULL, &sql_err);
if (ret != SQLITE_OK)
{
fprintf (stderr, "COMMIT TRANSACTION error: %s\n", sql_err);
sqlite3_free (sql_err);
}
return 1;
error:
if (query_stmt != NULL)
sqlite3_finalize (query_stmt);
if (update_stmt != NULL)
sqlite3_finalize (update_stmt);
ret = sqlite3_exec (handle, "ROLLBACK", NULL, NULL, &sql_err);
if (ret != SQLITE_OK)
{
fprintf (stderr, "ROLLBACK TRANSACTION error: %s\n", sql_err);
sqlite3_free (sql_err);
}
return 0;
}
static double
compute_cost (struct aux_params *params, const char *class, double length)
{
/* computing the travel time [cost] */
double speed = params->default_speed; /* speed, in Km/h */
double msec;
if (class != NULL)
{
struct aux_speed *ps = params->first_speed;
while (ps)
{
if (strcmp (ps->class_name, class) == 0)
{
speed = ps->speed;
break;
}
ps = ps->next;
}
}
msec = speed * 1000.0 / 3600.0; /* transforming speed in m/sec */
return length / msec;
}
static int
set_lengths_costs (struct aux_params *params, const char *table)
{
/* assigning lengths and costs to each Arc of the Graph */
int ret;
char *sql_err = NULL;
char sql[8192];
char sql2[1024];
sqlite3_stmt *query_stmt = NULL;
sqlite3_stmt *update_stmt = NULL;
/* the complete operation is handled as a unique SQL Transaction */
ret = sqlite3_exec (params->db_handle, "BEGIN", NULL, NULL, &sql_err);
if (ret != SQLITE_OK)
{
fprintf (stderr, "BEGIN TRANSACTION error: %s\n", sql_err);
sqlite3_free (sql_err);
return 0;
}
/* querying Arcs */
strcpy (sql, "SELECT id, class, GreatCircleLength(Geometry) ");
sprintf (sql2, "FROM \"%s\"", table);
strcat (sql, sql2);
ret = sqlite3_prepare_v2 (params->db_handle, sql, strlen (sql),
&query_stmt, NULL);
if (ret != SQLITE_OK)
{
fprintf (stderr, "SQL error: %s\n%s\n", sql,
sqlite3_errmsg (params->db_handle));
goto error;
}
/* updating Arcs */
sprintf (sql, "UPDATE \"%s\" SET length = ?, cost = ? ", table);
strcat (sql, "WHERE id = ?");
ret = sqlite3_prepare_v2 (params->db_handle, sql, strlen (sql),
&update_stmt, NULL);
if (ret != SQLITE_OK)
{
fprintf (stderr, "SQL error: %s\n%s\n", sql,
sqlite3_errmsg (params->db_handle));
goto error;
}
while (1)
{
ret = sqlite3_step (query_stmt);
if (ret == SQLITE_DONE)
break;
if (ret == SQLITE_ROW)
{
sqlite3_int64 id = sqlite3_column_int64 (query_stmt, 0);
const char *class =
(const char *) sqlite3_column_text (query_stmt, 1);
double length = sqlite3_column_double (query_stmt, 2);
double cost = compute_cost (params, class, length);
/* udating the Arc */
sqlite3_reset (update_stmt);
sqlite3_clear_bindings (update_stmt);
sqlite3_bind_double (update_stmt, 1, length);
sqlite3_bind_double (update_stmt, 2, cost);
sqlite3_bind_int64 (update_stmt, 3, id);
ret = sqlite3_step (update_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW);
else
{
printf ("sqlite3_step() error: %s\n",
sqlite3_errmsg (params->db_handle));
goto error;
}
}
else