-
Notifications
You must be signed in to change notification settings - Fork 0
/
spatialite_osm_overpass.c
4630 lines (4432 loc) · 133 KB
/
spatialite_osm_overpass.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_overpass
/
/ a tool downloading OSM datasets via the Overpass wep API
/
/ version 1.0, 2014 November 13
/
/ Author: Sandro Furieri [email protected]
/
/ Copyright (C) 2014 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/>.
/
*/
#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 ENABLE_LIBXML2 /* only if LIBXML2 is enabled */
#include <libxml/parser.h>
#include <libxml/nanohttp.h>
#include <sqlite3.h>
#include <spatialite/gaiageo.h>
#include <spatialite.h>
#define ARG_NONE 0
#define ARG_OSM_URL 1
#define ARG_MINX 2
#define ARG_MINY 3
#define ARG_MAXX 4
#define ARG_MAXY 5
#define ARG_DB_PATH 6
#define ARG_MODE 7
#define ARG_CACHE_SIZE 8
#define MODE_RAW 1
#define MODE_MAP 2
#define MODE_RAIL 3
#define MODE_ROAD 4
#define OBJ_NODES 1
#define OBJ_WAYS 2
#define OBJ_RELATIONS 3
#if defined(_WIN32)
#define atol_64 _atoi64
#else
#define atol_64 atoll
#endif
struct layers
{
const char *name;
int ok_point;
int ok_linestring;
int ok_polygon;
int ok_multi_linestring;
int ok_multi_polygon;
sqlite3_stmt *ins_point_stmt;
sqlite3_stmt *ins_linestring_stmt;
sqlite3_stmt *ins_polygon_stmt;
sqlite3_stmt *ins_multi_linestring_stmt;
sqlite3_stmt *ins_multi_polygon_stmt;
} base_layers[] =
{
{
"highway", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"junction", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"traffic_calming", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"traffic_sign", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"service", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"barrier", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"cycleway", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"tracktype", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"waterway", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"railway", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"aeroway", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"aerialway", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"power", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"man_made", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"leisure", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"amenity", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"shop", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"tourism", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"historic", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"landuse", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"military", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"natural", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"geological", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"route", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"boundary", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"sport", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"abutters", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"accessories", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"properties", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"restrictions", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"place", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"building", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
"parking", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},
{
NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL},};
struct download_tile
{
/* an helper struct corresponging to a single download tile */
int tile_no;
double minx;
double miny;
double maxx;
double maxy;
struct download_tile *next;
};
struct tiled_download
{
/* a tiled downloader object */
int count;
struct download_tile *first;
struct download_tile *last;
};
struct aux_params
{
/* an auxiliary struct used for XML parsing */
sqlite3 *db_handle;
void *cache;
sqlite3_stmt *ins_nodes_stmt;
sqlite3_stmt *ins_node_tags_stmt;
sqlite3_stmt *ins_ways_stmt;
sqlite3_stmt *ins_way_tags_stmt;
sqlite3_stmt *ins_way_refs_stmt;
sqlite3_stmt *ins_relations_stmt;
sqlite3_stmt *ins_relation_tags_stmt;
sqlite3_stmt *ins_relation_refs_stmt;
int wr_nodes;
int wr_node_tags;
int wr_ways;
int wr_way_tags;
int wr_way_refs;
int wr_relations;
int wr_rel_tags;
int wr_rel_refs;
const char *osm_url;
int mode;
sqlite3_int64 current_node_id;
int current_node_tag_sub;
sqlite3_int64 current_way_id;
int current_way_tag_sub;
int current_way_ref_sub;
sqlite3_int64 current_rel_id;
int current_rel_tag_sub;
int current_rel_ref_sub;
};
struct aux_arc
{
/* an helper struct used to build Road/Rail arcs */
sqlite3_int64 node_from;
sqlite3_int64 node_to;
gaiaGeomCollPtr geom_from;
gaiaGeomCollPtr geom_to;
gaiaGeomCollPtr geom;
struct aux_arc *next;
};
struct aux_arc_container
{
/* container for Road/Rail arcs */
struct aux_arc *first;
struct aux_arc *last;
};
static void
finalize_map_stmts ()
{
/* finalizing all Map statements */
struct layers *layer;
int i = 0;
while (1)
{
layer = &(base_layers[i++]);
if (layer->name == NULL)
break;
if (layer->ins_point_stmt != NULL)
sqlite3_finalize (layer->ins_point_stmt);
if (layer->ins_linestring_stmt != NULL)
sqlite3_finalize (layer->ins_linestring_stmt);
if (layer->ins_polygon_stmt != NULL)
sqlite3_finalize (layer->ins_polygon_stmt);
if (layer->ins_multi_linestring_stmt != NULL)
sqlite3_finalize (layer->ins_multi_linestring_stmt);
if (layer->ins_multi_polygon_stmt != NULL)
sqlite3_finalize (layer->ins_multi_polygon_stmt);
}
}
static void
add_download_tile (struct tiled_download *obj, double minx, double miny,
double maxx, double maxy)
{
/* inserting a further tile into the downloader object */
struct download_tile *tile = malloc (sizeof (struct download_tile));
tile->tile_no = obj->count;
tile->minx = minx;
tile->miny = miny;
tile->maxx = maxx;
tile->maxy = maxy;
tile->next = NULL;
if (obj->first == NULL)
obj->first = tile;
if (obj->last != NULL)
obj->last->next = tile;
obj->last = tile;
obj->count++;
}
static void
downloader_cleanup (struct tiled_download *obj)
{
/* freeing the tiled downloader object */
struct download_tile *tile;
struct download_tile *tile_n;
tile = obj->first;
while (tile != NULL)
{
tile_n = tile->next;
free (tile);
tile = tile_n;
}
}
static int
insert_node_tag (struct aux_params *params, const char *k, const char *v)
{
/* inserting a raw <node><tag> into the DBMS */
int ret;
sqlite3_reset (params->ins_node_tags_stmt);
sqlite3_clear_bindings (params->ins_node_tags_stmt);
sqlite3_bind_int64 (params->ins_node_tags_stmt, 1, params->current_node_id);
sqlite3_bind_int (params->ins_node_tags_stmt, 2,
params->current_node_tag_sub);
sqlite3_bind_text (params->ins_node_tags_stmt, 3, k, strlen (k),
SQLITE_STATIC);
sqlite3_bind_text (params->ins_node_tags_stmt, 4, v, strlen (v),
SQLITE_STATIC);
ret = sqlite3_step (params->ins_node_tags_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
params->wr_node_tags += 1;
params->current_node_tag_sub += 1;
return 1;
}
static int
insert_way_ref (struct aux_params *params, sqlite3_int64 node_id)
{
/* inserting a raw <way><nd> into the DBMS */
int ret;
sqlite3_reset (params->ins_way_refs_stmt);
sqlite3_clear_bindings (params->ins_way_refs_stmt);
sqlite3_bind_int64 (params->ins_way_refs_stmt, 1, params->current_way_id);
sqlite3_bind_int (params->ins_way_refs_stmt, 2,
params->current_way_ref_sub);
sqlite3_bind_int64 (params->ins_way_refs_stmt, 3, node_id);
ret = sqlite3_step (params->ins_way_refs_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
params->wr_way_refs += 1;
params->current_way_ref_sub += 1;
return 1;
}
static int
insert_way_tag (struct aux_params *params, const char *k, const char *v)
{
/* inserting a raw <way><tag> into the DBMS */
int ret;
sqlite3_reset (params->ins_way_tags_stmt);
sqlite3_clear_bindings (params->ins_way_tags_stmt);
sqlite3_bind_int64 (params->ins_way_tags_stmt, 1, params->current_way_id);
sqlite3_bind_int (params->ins_way_tags_stmt, 2,
params->current_way_tag_sub);
sqlite3_bind_text (params->ins_way_tags_stmt, 3, k, strlen (k),
SQLITE_STATIC);
sqlite3_bind_text (params->ins_way_tags_stmt, 4, v, strlen (v),
SQLITE_STATIC);
ret = sqlite3_step (params->ins_way_tags_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
params->wr_way_tags += 1;
params->current_way_tag_sub += 1;
return 1;
}
static int
insert_relation_ref (struct aux_params *params, const char *type,
sqlite3_int64 ref, const char *role)
{
/* inserting a raw <relation><member> into the DBMS */
int ret;
sqlite3_reset (params->ins_relation_refs_stmt);
sqlite3_clear_bindings (params->ins_relation_refs_stmt);
sqlite3_bind_int64 (params->ins_relation_refs_stmt, 1,
params->current_rel_id);
sqlite3_bind_int (params->ins_relation_refs_stmt, 2,
params->current_rel_ref_sub);
sqlite3_bind_text (params->ins_relation_refs_stmt, 3, type, strlen (type),
SQLITE_STATIC);
sqlite3_bind_int64 (params->ins_relation_refs_stmt, 4, ref);
sqlite3_bind_text (params->ins_relation_refs_stmt, 5, role, strlen (role),
SQLITE_STATIC);
ret = sqlite3_step (params->ins_relation_refs_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
params->wr_rel_refs += 1;
params->current_rel_ref_sub += 1;
return 1;
}
static int
insert_relation_tag (struct aux_params *params, const char *k, const char *v)
{
/* inserting a raw <relation><tag> into the DBMS */
int ret;
sqlite3_reset (params->ins_relation_tags_stmt);
sqlite3_clear_bindings (params->ins_relation_tags_stmt);
sqlite3_bind_int64 (params->ins_relation_tags_stmt, 1,
params->current_rel_id);
sqlite3_bind_int (params->ins_relation_tags_stmt, 2,
params->current_rel_tag_sub);
sqlite3_bind_text (params->ins_relation_tags_stmt, 3, k, strlen (k),
SQLITE_STATIC);
sqlite3_bind_text (params->ins_relation_tags_stmt, 4, v, strlen (v),
SQLITE_STATIC);
ret = sqlite3_step (params->ins_relation_tags_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
params->wr_rel_tags += 1;
params->current_rel_tag_sub += 1;
return 1;
}
static int
insert_node (struct aux_params *params, sqlite3_int64 id, double x, double y,
int version, const char *timestamp, int uid, int changeset,
const char *user)
{
/* inserting a raw <node> into the DBMS */
int ret;
unsigned char *blob;
int blob_size;
gaiaGeomCollPtr geom = gaiaAllocGeomColl ();
geom->Srid = 4326;
gaiaAddPointToGeomColl (geom, x, y);
sqlite3_reset (params->ins_nodes_stmt);
sqlite3_clear_bindings (params->ins_nodes_stmt);
if (params->mode == MODE_RAW)
{
sqlite3_bind_int64 (params->ins_nodes_stmt, 1, id);
sqlite3_bind_int (params->ins_nodes_stmt, 2, version);
if (timestamp == NULL)
sqlite3_bind_null (params->ins_nodes_stmt, 3);
else
sqlite3_bind_text (params->ins_nodes_stmt, 3, timestamp,
strlen (timestamp), SQLITE_STATIC);
sqlite3_bind_int (params->ins_nodes_stmt, 4, uid);
if (user == NULL)
sqlite3_bind_null (params->ins_nodes_stmt, 5);
else
sqlite3_bind_text (params->ins_nodes_stmt, 5, user, strlen (user),
SQLITE_STATIC);
sqlite3_bind_int (params->ins_nodes_stmt, 6, changeset);
if (!geom)
sqlite3_bind_null (params->ins_nodes_stmt, 7);
else
{
gaiaToSpatiaLiteBlobWkb (geom, &blob, &blob_size);
gaiaFreeGeomColl (geom);
sqlite3_bind_blob (params->ins_nodes_stmt, 7, blob, blob_size,
free);
}
}
else
{
sqlite3_bind_int64 (params->ins_nodes_stmt, 1, id);
if (!geom)
sqlite3_bind_null (params->ins_nodes_stmt, 2);
else
{
gaiaToSpatiaLiteBlobWkb (geom, &blob, &blob_size);
gaiaFreeGeomColl (geom);
sqlite3_bind_blob (params->ins_nodes_stmt, 2, blob, blob_size,
free);
}
}
ret = sqlite3_step (params->ins_nodes_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
params->wr_nodes += 1;
params->current_node_id = id;
params->current_node_tag_sub = 0;
return 1;
}
static int
insert_way (struct aux_params *params, sqlite3_int64 id)
{
/* inserting a raw <way> into the DBMS */
int ret;
sqlite3_reset (params->ins_ways_stmt);
sqlite3_clear_bindings (params->ins_ways_stmt);
sqlite3_bind_int64 (params->ins_ways_stmt, 1, id);
ret = sqlite3_step (params->ins_ways_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
params->wr_ways += 1;
params->current_way_id = id;
params->current_way_ref_sub = 0;
params->current_way_tag_sub = 0;
return 1;
}
static int
insert_relation (struct aux_params *params, sqlite3_int64 id)
{
/* inserting a raw <relation> into the DBMS */
int ret;
sqlite3_reset (params->ins_relations_stmt);
sqlite3_clear_bindings (params->ins_relations_stmt);
sqlite3_bind_int64 (params->ins_relations_stmt, 1, id);
ret = sqlite3_step (params->ins_relations_stmt);
if (ret == SQLITE_DONE || ret == SQLITE_ROW)
params->wr_relations += 1;
params->current_rel_id = id;
params->current_rel_ref_sub = 0;
params->current_rel_tag_sub = 0;
return 1;
}
const char *
parse_attribute_value (xmlNodePtr node)
{
/* parsing a string argument */
if (node != NULL)
{
if (node->type == XML_TEXT_NODE)
return (const char *) (node->content);
}
return NULL;
}
static int
parse_osm_nd_ref (struct _xmlAttr *attr, sqlite3_int64 * node_id)
{
/* parsing the OSM <node> attributes */
const char *attr_ref = NULL;
while (attr != NULL)
{
if (attr->name != NULL)
{
if (strcmp ((const char *) (attr->name), "ref") == 0)
attr_ref = parse_attribute_value (attr->children);
}
attr = attr->next;
}
if (attr_ref == NULL)
{
fprintf (stderr, "Invalid OSM <nd>: ref=%s\n", attr_ref);
return 0;
}
*node_id = atol_64 (attr_ref);
return 1;
}
static int
parse_osm_member (struct _xmlAttr *attr, const char **type, sqlite3_int64 * ref,
const char **role)
{
/* parsing the OSM <member> attributes */
const char *attr_type = NULL;
const char *attr_ref = NULL;
const char *attr_role = NULL;
while (attr != NULL)
{
if (attr->name != NULL)
{
if (strcmp ((const char *) (attr->name), "type") == 0)
attr_type = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "ref") == 0)
attr_ref = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "role") == 0)
attr_role = parse_attribute_value (attr->children);
}
attr = attr->next;
}
if (attr_type == NULL || attr_ref == NULL || attr_role == NULL)
{
fprintf (stderr, "Invalid OSM <member>: type=%s ref=%s role=%s\n",
attr_type, attr_ref, attr_role);
return 0;
}
*type = attr_type;
*ref = atol_64 (attr_ref);
*role = attr_role;
return 1;
}
static int
parse_osm_tag (struct _xmlAttr *attr, const char **k, const char **v)
{
/* parsing the OSM <tag> attributes */
const char *attr_k = NULL;
const char *attr_v = NULL;
while (attr != NULL)
{
if (attr->name != NULL)
{
if (strcmp ((const char *) (attr->name), "k") == 0)
attr_k = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "v") == 0)
attr_v = parse_attribute_value (attr->children);
}
attr = attr->next;
}
if (attr_k == NULL || attr_v == NULL)
{
fprintf (stderr, "Invalid OSM <tag>: k=%s v=%s\n", attr_k, attr_v);
return 0;
}
*k = attr_k;
*v = attr_v;
return 1;
}
static int
parse_osm_node_attributes (struct _xmlAttr *attr, sqlite3_int64 * id, double *x,
double *y, int *version, const char **timestamp,
int *uid, int *changeset, const char **user)
{
/* parsing the OSM <node> attributes */
const char *attr_id = NULL;
const char *attr_lon = NULL;
const char *attr_lat = NULL;
const char *attr_version = NULL;
const char *attr_timestamp = NULL;
const char *attr_uid = NULL;
const char *attr_changeset = NULL;
const char *attr_user = NULL;
while (attr != NULL)
{
if (attr->name != NULL)
{
if (strcmp ((const char *) (attr->name), "id") == 0)
attr_id = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "lon") == 0)
attr_lon = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "lat") == 0)
attr_lat = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "version") == 0)
attr_version = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "timestamp") == 0)
attr_timestamp = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "uid") == 0)
attr_uid = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "changeset") == 0)
attr_changeset = parse_attribute_value (attr->children);
if (strcmp ((const char *) (attr->name), "user") == 0)
attr_user = parse_attribute_value (attr->children);
}
attr = attr->next;
}
if (attr_id == NULL || attr_lon == NULL || attr_lat == NULL)
{
fprintf (stderr, "Invalid OSM <node>: id=%s lat=%s lon=%s\n", attr_id,
attr_lon, attr_lat);
return 0;
}
*id = atol_64 (attr_id);
*x = atof (attr_lon);
*y = atof (attr_lat);
if (attr_version == NULL)
*version = -1;
else
*version = atoi (attr_version);
*timestamp = attr_timestamp;
if (attr_uid == NULL)
*uid = -1;
else
*uid = atoi (attr_uid);
if (attr_changeset == NULL)
*changeset = -1;
else
*changeset = atoi (attr_changeset);
*user = attr_user;
return 1;
}
static int
parse_osm_way_attributes (struct _xmlAttr *attr, sqlite3_int64 * id)
{
/* parsing the OSM <way> attributes */
const char *attr_id = NULL;
while (attr != NULL)
{
if (attr->name != NULL)
{
if (strcmp ((const char *) (attr->name), "id") == 0)
attr_id = parse_attribute_value (attr->children);
}
attr = attr->next;
}
if (attr_id == NULL)
{
fprintf (stderr, "Invalid OSM <way>: id=%s\n", attr_id);
return 0;
}
*id = atol_64 (attr_id);
return 1;
}
static int
parse_osm_relation_attributes (struct _xmlAttr *attr, sqlite3_int64 * id)
{
/* parsing the OSM <relation> attributes */
const char *attr_id = NULL;
while (attr != NULL)
{
if (attr->name != NULL)
{
if (strcmp ((const char *) (attr->name), "id") == 0)
attr_id = parse_attribute_value (attr->children);
}
attr = attr->next;
}
if (attr_id == NULL)
{
fprintf (stderr, "Invalid OSM <relation>: id=%s\n", attr_id);
return 0;
}
*id = atol_64 (attr_id);
return 1;
}
static int
parse_osm_node_tag (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing an OSM <node><tag> item */
const char *k;
const char *v;
if (!parse_osm_tag (node->properties, &k, &v))
return 0;
if (!insert_node_tag (params, k, v))
return 0;
return 1;
}
static int
parse_osm_way_tag (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing an OSM <way><tag> item */
const char *k;
const char *v;
if (!parse_osm_tag (node->properties, &k, &v))
return 0;
if (!insert_way_tag (params, k, v))
return 0;
return 1;
}
static int
parse_osm_relation_tag (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing an OSM <relation><tag> item */
const char *k;
const char *v;
if (!parse_osm_tag (node->properties, &k, &v))
return 0;
if (!insert_relation_tag (params, k, v))
return 0;
return 1;
}
static int
parse_osm_way_ref (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing an OSM <way><nd> item */
sqlite3_int64 node_id;
if (!parse_osm_nd_ref (node->properties, &node_id))
return 0;
if (!insert_way_ref (params, node_id))
return 0;
return 1;
}
static int
parse_osm_relation_member (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing an OSM <relation><member> item */
const char *type;
sqlite3_int64 ref;
const char *role;
if (!parse_osm_member (node->properties, &type, &ref, &role))
return 0;
if (!insert_relation_ref (params, type, ref, role))
return 0;
return 1;
}
static int
parse_osm_node (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing an OSM <node> item */
xmlNodePtr child;
sqlite3_int64 id;
double x;
double y;
int version;
const char *timestamp;
int uid;
int changeset;
const char *user;
int error = 0;
if (!parse_osm_node_attributes
(node->properties, &id, &x, &y, &version, ×tamp, &uid, &changeset,
&user))
return 0;
if (!insert_node
(params, id, x, y, version, timestamp, uid, changeset, user))
return 0;
for (child = node->children; child; child = child->next)
{
if (child->type == XML_ELEMENT_NODE)
{
const char *name = (const char *) (child->name);
if (name != NULL)
{
int ret = 1;
if (strcmp (name, "tag") == 0)
ret = parse_osm_node_tag (child, params);
if (!ret)
error = 1;
}
}
}
if (error)
return 0;
return 1;
}
static int
parse_osm_way (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing an OSM <way> item */
xmlNodePtr child;
sqlite3_int64 id;
int error = 0;
if (!parse_osm_way_attributes (node->properties, &id))
return 0;
if (!insert_way (params, id))
return 0;
for (child = node->children; child; child = child->next)
{
if (child->type == XML_ELEMENT_NODE)
{
const char *name = (const char *) (child->name);
if (name != NULL)
{
int ret = 1;
if (strcmp (name, "nd") == 0)
ret = parse_osm_way_ref (child, params);
if (strcmp (name, "tag") == 0)
ret = parse_osm_way_tag (child, params);
if (!ret)
error = 1;
}
}
}
if (error)
return 0;
return 1;
}
static int
parse_osm_relation (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing an OSM <relation> item */
xmlNodePtr child;
sqlite3_int64 id;
int error = 0;
if (!parse_osm_relation_attributes (node->properties, &id))
return 0;
if (!insert_relation (params, id))
return 0;
for (child = node->children; child; child = child->next)
{
if (child->type == XML_ELEMENT_NODE)
{
const char *name = (const char *) (child->name);
if (name != NULL)
{
int ret = 1;
if (strcmp (name, "member") == 0)
ret = parse_osm_relation_member (child, params);
if (strcmp (name, "tag") == 0)
ret = parse_osm_relation_tag (child, params);
if (!ret)
error = 1;
}
}
}
if (error)
return 0;
return 1;
}
static int
parse_osm_items (xmlNodePtr node, struct aux_params *params)
{
/* recursively parsing the OSM payload */
int error = 0;
xmlNodePtr cur_node = NULL;
for (cur_node = node; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_ELEMENT_NODE)
{
xmlNodePtr child;
for (child = cur_node->children; child; child = child->next)
{
if (child->type == XML_ELEMENT_NODE)
{
const char *name = (const char *) (child->name);
if (name != NULL)
{
int ret = 1;
if (strcmp (name, "node") == 0)
ret = parse_osm_node (child, params);
if (strcmp (name, "way") == 0)
ret = parse_osm_way (child, params);
if (strcmp (name, "relation") == 0)
ret = parse_osm_relation (child, params);
if (!ret)
error = 1;
}
}
}
}
}
if (error)
return 0;
return 1;
}
static int
osm_parse (struct aux_params *params, struct download_tile *tile, int object)
{
xmlDocPtr xml_doc = NULL;
xmlNodePtr root;
char *url;
int error = 0;
if (params->mode == MODE_ROAD)
{
/* downloading only the ROAD network */
url =
sqlite3_mprintf
("%s/interpreter?data=[timeout:600];(way[highway](%1.12f,%1.12f,%1.12f,%1.12f);>;);out body;",
params->osm_url, tile->miny, tile->minx, tile->maxy, tile->maxx);
}
else if (params->mode == MODE_RAIL)
{
/* downloading only the RAILWAY network */
url =
sqlite3_mprintf
("%s/interpreter?data=[timeout:600];(way[railway](%1.12f,%1.12f,%1.12f,%1.12f);>;);out body;",
params->osm_url, tile->miny, tile->minx, tile->maxy, tile->maxx);
}
else if (params->mode == MODE_MAP)
{
/* downloading a full MAP */
if (object == OBJ_NODES)
url =
sqlite3_mprintf
("%s/interpreter?data=[timeout:600];(node(%1.12f,%1.12f,%1.12f,%1.12f););out body;",
params->osm_url, tile->miny, tile->minx, tile->maxy,
tile->maxx);
else if (object == OBJ_WAYS)
url =
sqlite3_mprintf
("%s/interpreter?data=[timeout:600];(way(%1.12f,%1.12f,%1.12f,%1.12f););out body;",
params->osm_url, tile->miny, tile->minx, tile->maxy,
tile->maxx);
else
url =
sqlite3_mprintf
("%s/interpreter?data=[timeout:600];(relation(%1.12f,%1.12f,%1.12f,%1.12f););out body;",
params->osm_url, tile->miny, tile->minx, tile->maxy,
tile->maxx);
}
else
{
/* downloading a full MAP - raw mode */
if (object == OBJ_NODES)
url =
sqlite3_mprintf
("%s/interpreter?data=[timeout:600];(node(%1.12f,%1.12f,%1.12f,%1.12f););out meta;",
params->osm_url, tile->miny, tile->minx, tile->maxy,
tile->maxx);
else if (object == OBJ_WAYS)
url =
sqlite3_mprintf
("%s/interpreter?data=[timeout:600];(way(%1.12f,%1.12f,%1.12f,%1.12f););out meta;",
params->osm_url, tile->miny, tile->minx, tile->maxy,
tile->maxx);
else
url =
sqlite3_mprintf
("%s/interpreter?data=[timeout:600];(relation(%1.12f,%1.12f,%1.12f,%1.12f););out meta;",
params->osm_url, tile->miny, tile->minx, tile->maxy,
tile->maxx);
}
xml_doc = xmlReadFile (url, NULL, 0);
if (xml_doc == NULL)
{
/* parsing error; not a well-formed XML */
fprintf (stderr, "ERROR: unable to download the OSM dataset\n");
error = 1;
goto end;
}
/* parsing the OSM payload */
root = xmlDocGetRootElement (xml_doc);
if (!parse_osm_items (root, params))
error = 1;
end:
sqlite3_free (url);
if (xml_doc != NULL)
xmlFreeDoc (xml_doc);
if (error)
return 0;
return 1;
}
static void
finalize_sql_stmts (struct aux_params *params)
{
int ret;
char *sql_err = NULL;