-
Notifications
You must be signed in to change notification settings - Fork 7
/
osmconvert.c
12377 lines (11666 loc) · 433 KB
/
osmconvert.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
// osmconvert 2016-02-12 20:30
#define VERSION "0.8.5"
//
// compile this file:
// gcc osmconvert.c -lz -O3 -o osmconvert
//
// (c) 2011..2016 Markus Weber, Nuernberg
// Richard Russo contributed the initiative to --add-bbox-tags option
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public License
// version 3 as published by the Free Software Foundation.
// 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 Affero General Public License for more details.
// You should have received a copy of this license along
// with this program; if not, see http://www.gnu.org/licenses/.
//
// Other licenses are available on request; please ask the author.
#define MAXLOGLEVEL 2
const char* shorthelptext=
"\nosmconvert " VERSION " Parameter Overview\n"
"(Please use --help to get more information.)\n"
"\n"
"<FILE> input file name\n"
"- read from standard input\n"
"-b=<x1>,<y1>,<x2>,<y2> apply a border box\n"
"-B=<border_polygon> apply a border polygon\n"
"--complete-ways do not clip ways at the borders\n"
"--complex-ways do not clip multipolygons at the borders\n"
"--all-to-nodes convert ways and relations to nodes\n"
"--add-bbox-tags add bbox tags to ways and relations\n"
"--add-bboxarea-tags add tags for estimated bbox areas\n"
"--add-bboxweight-tags add tags for log2 of bbox areas\n"
"--add-bboxwidth-tags add tags for estimated bbox widths\n"
"--add-bboxwidthweight-tags add tags for log2 of bbox widths\n"
"--object-type-offset=<id> offset for ways/relations if --all-to-nodes\n"
"--max-objects=<n> space for --all-to-nodes, 1 obj. = 16 bytes\n"
"--drop-broken-refs delete references to excluded nodes\n"
"--drop-author delete changeset and user information\n"
"--drop-version same as before, but delete version as well\n"
"--drop-nodes delete all nodes\n"
"--drop-ways delete all ways\n"
"--drop-relations delete all relations\n"
"--diff calculate differences between two files\n"
"--diff-contents same as before, but compare whole contents\n"
"--subtract subtract objects given by following files\n"
"--pbf-granularity=<val> lon/lat granularity of .pbf input file\n"
"--emulate-osmosis emulate Osmosis XML output format\n"
"--emulate-pbf2osm emulate pbf2osm output format\n"
"--fake-author set changeset to 1 and timestamp to 1970\n"
"--fake-version set version number to 1\n"
"--fake-lonlat set lon to 0 and lat to 0\n"
"-h display this parameter overview\n"
"--help display a more detailed help\n"
"--merge-versions merge versions of each object in a file\n"
"--out-osm write output in .osm format (default)\n"
"--out-osc write output in .osc format (OSMChangefile)\n"
"--out-osh write output in .osh format (visible-tags)\n"
"--out-o5m write output in .o5m format (fast binary)\n"
"--out-o5c write output in .o5c format (bin. Changef.)\n"
"--out-pbf write output in .pbf format (bin. standard)\n"
"--out-csv write output in .csv format (plain table)\n"
"--out-none no standard output (for testing purposes)\n"
"--csv=<column names> choose columns for csv format\n"
"--csv-headline start csv output with a headline\n"
"--csv-separator=<sep> separator character(s) for csv format\n"
"--timestamp=<date_time> add a timestamp to the data\n"
"--timestamp=NOW-<seconds> add a timestamp in seconds before now\n"
"--out-timestamp output the file\'s timestamp, nothing else\n"
"--out-statistics write statistics, nothing else\n"
"--statistics write statistics to stderr\n"
"-o=<outfile> reroute standard output to a file\n"
"-t=<tempfile> define tempfile prefix\n"
"--parameter-file=<file> param. in file, separated by empty lines\n"
"--verbose activate verbose mode\n";
const char* helptext=
"\nosmconvert " VERSION "\n"
"\n"
"This program reads different file formats of the OpenStreetMap\n"
"project and converts the data to the selected output file format.\n"
"These formats can be read:\n"
" .osm .osc .osc.gz .osh .o5m .o5c .pbf\n"
"These formats can be written:\n"
" .osm (default) .osc .osh .o5m .o5c .pbf\n"
"\n"
"Names of input files must be specified as command line parameters.\n"
"Use - to read from standard input. You do not need to specify the\n"
"input formats, osmconvert will recognize them by itself.\n"
"The output format is .osm by default. If you want a different format,\n"
"please specify it using the appropriate command line parameter.\n"
"\n"
"-b=<x1>,<y1>,<x2>,<y2>\n"
" If you want to limit the geographical region, you can define\n"
" a bounding box. To do this, enter the southwestern and the\n"
" northeastern corners of that area. For example:\n"
" -b=-0.5,51,0.5,52\n"
"\n"
"-B=<border_polygon>\n"
" Alternatively to a bounding box you can use a border polygon\n"
" to limit the geographical region.\n"
" The format of a border polygon file can be found in the OSM\n"
" Wiki: http://wiki.openstreetmap.org/wiki/Osmosis/\n"
" Polygon_Filter_File_Format\n"
" You do not need to strictly follow the format description,\n"
" you must ensure that every line of coordinates starts with\n"
" blanks.\n"
"\n"
"--complete-ways\n"
" If applying a border box or a border polygon, all nodes\n"
" the borders are excluded; even then if they belong to a way\n"
" which is not entirely excluded because it has some nodes\n"
" inside the borders.\n"
" This option will ensure that every way stays complete, even\n"
" it it intersects the borders. This will result in slower\n"
" processing, and the program will loose its ability to read\n"
" from standard input. It is recommended to use .o5m format as\n"
" input format to compensate most of the speed disadvantage.\n"
"\n"
"--complex-ways\n"
" Same as before, but multipolygons will not be cut at the\n"
" borders too.\n"
"\n"
"--all-to-nodes\n"
" Some applications do not have the ability to process ways or\n"
" relations, they just accept nodes as input. However, more and\n"
" more complex object are mapped as ways or even relations in\n"
" order to get all their details into the database.\n"
" Apply this option if you want to convert ways and relations\n"
" to nodes and thereby make them available to applications\n"
" which can only deal with nodes.\n"
" For each way a node is created. The way's id is increased by\n"
" 10^15 and taken as id for the new node. The node's longitude\n"
" and latitude are set to the way's geographical center. Same\n"
" applies to relations, however they get 2*10^15 as id offset.\n"
"\n"
"--add-bbox-tags\n"
" This option adds a tag with a bounding box to each object.\n"
" The tag will contain the border coordinates in this order:\n"
" min Longitude, min Latitude, max Longitude, max Latitude.\n"
" e.g.: <tag k=\"bBox\" v=\"-0.5000,51.0000,0.5000,52.0000\"/>\n"
"\n"
"--add-bboxarea-tags\n"
" A tag for an estimated area value for the bbox is added to\n"
" each way and each relation. The unit is square meters.\n"
" For example: <tag k=\"bBoxArea\" v=\"33828002\"/>\n"
"\n"
"--add-bboxweight-tags\n"
" This option will add the binary logarithm of the bbox area\n"
" of each way and each relation.\n"
" For example: <tag k=\"bBoxWeight\" v=\"20\"/>\n"
"\n"
"--add-bboxwidth-tags\n"
" A tag for an estimated width value for the bbox is added to\n"
" each way and each relation. The unit is meters.\n"
" For example: <tag k=\"bBoxWidth\" v=\"825\"/>\n"
"\n"
"--add-bboxwidthweight-tags\n"
" This option will add the binary logarithm of the bbox width\n"
" of each way and each relation.\n"
" For example: <tag k=\"bBoxWidthWeight\" v=\"10\"/>\n"
"\n"
"--object-type-offset=<id offset>\n"
" If applying the --all-to-nodes option as explained above, you\n"
" may adjust the id offset. For example:\n"
" --object-type-offset=4000000000\n"
" By appending \"+1\" to the offset, the program will create\n"
" ids in a sequence with step 1. This might be useful if the\n"
" there is a subsequently running application which cannot\n"
" process large id numbers. Example:\n"
" --object-type-offset=1900000000+1\n"
"\n"
"--drop-broken-refs\n"
" Use this option if you need to delete references to nodes\n"
" which have been excluded because lying outside the borders\n"
" (mandatory for some applications, e.g. Map Composer, JOSM).\n"
"\n"
"--drop-author\n"
" For most applications the author tags are not needed. If you\n"
" specify this option, no author information will be written:\n"
" no changeset, user or timestamp.\n"
"\n"
"--drop-version\n"
" If you want to exclude not only the author information but\n"
" also the version number, specify this option.\n"
"\n"
"--drop-nodes\n"
"--drop-ways\n"
"--drop-relations\n"
" According to the combination of these parameters, no members\n"
" of the referred section will be written.\n"
"\n"
"--diff\n"
" Calculate difference between two files and create a new .osc\n"
" or .o5c file.\n"
" There must be TWO input files and borders cannot be applied.\n"
" Both files must be sorted by object type and id. Created\n"
" objects will appear in the output file as \"modified\", unless\n"
" having version number 1.\n"
"\n"
"--diff-contents\n"
" Similar to --diff, this option calculates differences between\n"
" two OSM files. Here, to determine the differences complete\n"
" OSM objects are consulted, not only the version numbers.\n"
" Unfortunately, this option strictly requires both input files\n"
" to have .o5m format.\n"
"\n"
"--subtract\n"
" The output file will not contain any object which exists in\n"
" one of the input files following this directive. For example:\n"
" osmconvert input.o5m --subtract minus.o5m -o=output.o5m\n"
"\n"
"--pbf-granularity=<val>\n"
" Rarely .pbf files come with non-standard granularity.\n"
" osmconvert will recognize this and suggest to specify the\n"
" abnormal lon/lat granularity using this command line option.\n"
" Allowed values are: 100 (default), 1000, 10000, ..., 10000000.\n"
"\n"
"--emulate-osmosis\n"
"--emulate-pbf2osm\n"
" In case of .osm output format, the program will try to use\n"
" the same data syntax as Osmosis, resp. pbf2osm.\n"
"\n"
"--fake-author\n"
" If you have dropped author information (--drop-author) that\n"
" data will be lost, of course. Some programs however require\n"
" author information on input although they do not need that\n"
" data. For this purpose, you can fake the author information.\n"
" osmconvert will write changeset 1, timestamp 1970.\n"
"\n"
"--fake-version\n"
" Same as --fake-author, but - if .osm xml is used as output\n"
" format - only the version number will be written (version 1).\n"
" This is useful if you want to inspect the data with JOSM.\n"
"\n"
"--fake-lonlat\n"
" Some programs depend on getting longitude/latitude values,\n"
" even when the object in question shall be deleted. With this\n"
" option you can have osmconvert to fake these values:\n"
" ... lat=\"0\" lon=\"0\" ...\n"
" Note that this is for XML files only (.osc and .osh).\n"
"\n"
"-h\n"
" Display a short parameter overview.\n"
"\n"
"--help\n"
" Display this help.\n"
"\n"
"--merge-versions\n"
" Some .osc files contain different versions of one object.\n"
" Use this option to accept such duplicates on input.\n"
"\n"
"--out-osm\n"
" Data will be written in .osm format. This is the default\n"
" output format.\n"
"\n"
"--out-osc\n"
" The OSM Change format will be used for output. Please note\n"
" that OSM objects which are to be deleted will be represented\n"
" by their ids only.\n"
"\n"
"--out-osh\n"
" For every OSM object, the appropriate \'visible\' tag will be\n"
" added to meet \'full planet history\' specification.\n"
"\n"
"--out-o5m\n"
" The .o5m format will be used. This format has the same\n"
" structure as the conventional .osm format, but the data are\n"
" stored as binary numbers and are therefore much more compact\n"
" than in .osm format. No packing is used, so you can pack .o5m\n"
" files using every file packer you want, e.g. lzo, bz2, etc.\n"
"\n"
"--out-o5c\n"
" This is the change file format of .o5m data format. All\n"
" <delete> tags will not be performed as delete actions but\n"
" converted into .o5c data format.\n"
"\n"
"--out-pbf\n"
" For output, PBF format will be used.\n"
"\n"
"--out-csv\n"
" A character separated list will be written to output.\n"
" The default separator is Tab, the default columns are:\n"
" type, id, name. You can change both by using the options\n"
" --csv-separator= and --csv=\n"
"\n"
"--csv-headline\n"
" Choose this option to print a headline to csv output.\n"
"\n"
"--csv-separator=<sep>\n"
" You may change the default separator (Tab) to a different\n"
" character or character sequence. For example:\n"
" --csv-separator=\"; \"\n"
"\n"
"--csv=<columns>\n"
" If you want to have certain columns in your csv list, please \n"
" specify their names as shown in this example:\n"
" --csv=\"@id name ref description\"\n"
" There are a few special column names for header data:\n"
" @otype (object type 0..2), @oname (object type name), @id\n"
" @lon, @lat, @version, @timestamp, @changeset, @uid, @user\n"
"\n"
"--out-none\n"
" This will be no standard output. This option is for testing\n"
" purposes only.\n"
"\n"
"--timestamp=<date_and_time>\n"
"--timestamp=NOW<seconds_relative_to_now>\n"
" If you want to set the OSM timestamp of your output file,\n"
" supply it with this option. Date and time must be formatted\n"
" according OSM date/time specifications. For example:\n"
" --timestamp=2011-01-31T23:59:30Z\n"
" You also can supply a relative time in seconds, e.g. 24h ago:\n"
" --timestamp=NOW-86400\n"
"\n"
"--out-timestamp\n"
" With this option set, osmconvert prints just the time stamp\n"
" of the input file, nothing else.\n"
"\n"
"--statistics\n"
" This option activates a statistics counter. The program will\n"
" print statistical data to stderr.\n"
"\n"
"--out-statistics\n"
" Same as --statistics, but the statistical data will be\n"
" written to standard output.\n"
"\n"
"-o=<outfile>\n"
" Standard output will be rerouted to the specified file.\n"
" If no output format has been specified, the program will\n"
" rely on the file name\'s extension.\n"
"\n"
"-t=<tempfile>\n"
" If borders are to be applied or broken references to be\n"
" eliminated, osmconvert creates and uses two temporary files.\n"
" This parameter defines their name prefix. The default value\n"
" is \"osmconvert_tempfile\".\n"
"\n"
"--parameter-file=FILE\n"
" If you want to supply one ore more command line arguments\n"
" by a parameter file, please use this option and specify the\n"
" file name. Within the parameter file, parameters must be\n"
" separated by empty lines. Line feeds inside a parameter will\n"
" be converted to spaces.\n"
" Lines starting with \"// \" will be treated as comments.\n"
"\n"
"-v\n"
"--verbose\n"
" With activated \'verbose\' mode, some statistical data and\n"
" diagnosis data will be displayed.\n"
" If -v resp. --verbose is the first parameter in the line,\n"
" osmconvert will display all input parameters.\n"
"\n"
"Examples\n"
"\n"
"./osmconvert europe.pbf --drop-author >europe.osm\n"
"./osmconvert europe.pbf |gzip >europe.osm.gz\n"
"bzcat europe.osm.bz2 |./osmconvert --out-pbf >europe.pbf\n"
"./osmconvert europe.pbf -B=ch.poly >switzerland.osm\n"
"./osmconvert switzerland.osm --out-o5m >switzerland.o5m\n"
"./osmconvert june_july.osc --out-o5c >june_july.o5c\n"
"./osmconvert june.o5m june_july.o5c.gz --out-o5m >july.o5m\n"
"./osmconvert sep.osm sep_oct.osc oct_nov.osc >nov.osm\n"
"./osmconvert northamerica.osm southamerica.osm >americas.osm\n"
"\n"
"Tuning\n"
"\n"
"To speed-up the process, the program uses some main memory for a\n"
"hash table. By default, it uses 900 MB for storing a flag for every\n"
"possible node, 90 for the way flags, and 10 relation flags.\n"
"Every byte holds the flags for 8 ID numbers, i.e., in 900 MB the\n"
"program can store 7200 million flags. As there are less than 3200\n"
"million IDs for nodes at present (Oct 2014), 400 MB would suffice.\n"
"So, for example, you can decrease the hash sizes to e.g. 400, 50 and\n"
"2 MB using this option:\n"
"\n"
" --hash-memory=400-50-2\n"
"\n"
"But keep in mind that the OSM database is continuously expanding. For\n"
"this reason the program-own default value is higher than shown in the\n"
"example, and it may be appropriate to increase it in the future.\n"
"If you do not want to bother with the details, you can enter the\n"
"amount of memory as a sum, and the program will divide it by itself.\n"
"For example:\n"
"\n"
" --hash-memory=1500\n"
"\n"
"These 1500 MB will be split in three parts: 1350 for nodes, 135 for\n"
"ways, and 15 for relations.\n"
"\n"
"Because we are taking hashes, it is not necessary to provide all the\n"
"suggested memory; the program will operate with less hash memory too.\n"
"But, in this case, the border filter will be less effective, i.e.,\n"
"some ways and some relations will be left in the output file although\n"
"they should have been excluded.\n"
"The maximum value the program accepts for the hash size is 4000 MiB;\n"
"If you exceed the maximum amount of memory available on your system,\n"
"the program will try to reduce this amount and display a warning\n"
"message.\n"
"\n"
"There is another temporary memory space which is used only for the\n"
"conversion of ways and relations to nodes (option --all-to-nodes).\n"
"This space is sufficient for up to 25 Mio. OSM objects, 400 MB of\n"
"main memory are needed for this purpose, 800 MB if extended option\n"
"--add-bbox-tags has been invoked. If this is not sufficient or\n"
"if you want to save memory, you can configure the maximum number of\n"
"OSM objects by yourself. For example:\n"
"\n"
" --max-objects=35000000\n"
"\n"
"The number of references per object is limited to 100,000. This will\n"
"be sufficient for all OSM files. If you are going to create your own\n"
"OSM files by converting shapefiles or other files to OSM format, this\n"
"might result in way objects with more than 100,000 nodes. For this\n"
"reason you will need to increase the maximum accordingly. Example:\n"
"\n"
" --max-refs=400000\n"
"\n"
"Limitations\n"
"\n"
"When extracting a geographical region (using -b or -B), the input\n"
"file must contain the objects ordered by their type: first, all\n"
"nodes, next, all ways, followed by all relations. Within each of\n"
"these sections, the objects section must be sorted by their id in\n"
"ascending order.\n"
"\n"
"Usual .osm, .osc, .o5m, o5c and .pbf files adhere to this condition.\n"
"This means that you do not have to worry about this limitation.\n"
"osmconvert will display an error message if this sequence is broken.\n"
"\n"
"If a polygon file for borders is supplied, the maximum number of\n"
"polygon points is about 40,000.\n"
"\n"
"This program is for experimental use. Expect malfunctions and data\n"
"loss. Do not use the program in productive or commercial systems.\n"
"\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"Please send any bug reports to [email protected]\n\n";
#define _FILE_OFFSET_BITS 64
#include <zlib.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
typedef enum {false= 0,true= 1} bool;
typedef uint8_t byte;
typedef unsigned int uint;
#define isdig(x) isdigit((unsigned char)(x))
static int loglevel= 0; // logging to stderr;
// 0: no logging; 1: small logging; 2: normal logging;
// 3: extended logging;
#define DP(f) fprintf(stderr,"Debug: " #f "\n");
#define DPv(f,...) fprintf(stderr,"Debug: " #f "\n",__VA_ARGS__);
#define DPM(f,p,m) { byte* pp; int i,mm; static int msgn= 3; \
if(--msgn>=0) { fprintf(stderr,"Debug memory: " #f); \
pp= (byte*)(p); mm= (m); if(pp==NULL) fprintf(stderr,"\n (null)"); \
else for(i= 0; i<mm; i++) { \
if((i%16)==0) fprintf(stderr,"\n "); \
fprintf(stderr," %02x",*pp++); } \
fprintf(stderr,"\n"); } }
#define UR(x) if(x){} // result value intentionally ignored
#if __WIN32__
#define NL "\r\n" // use CR/LF as new-line sequence
#define off_t off64_t
#define lseek lseek64
z_off64_t gzseek64(gzFile,z_off64_t,int);
#define gzseek gzseek64
#else
#define NL "\n" // use LF as new-line sequence
#define O_BINARY 0
#endif
//------------------------------------------------------------
// Module Global global variables for this program
//------------------------------------------------------------
// to distinguish global variable from local or module global
// variables, they are preceded by 'global_';
static bool global_diff= false; // calculate diff between two files
static bool global_diffcontents= false;
// calculate physical diff between two files; 'physical' means
// that not only the version number is consulted to determine
// object differences, the whole object contents is;
static bool global_subtract= false; // any file which is opened
// via read_open() resp. oo_open() while global_subtract==true
// will be subtracted, i.e. the delete flags will be inverted:
// <delete> works as non-delete and no-<delete> works as "stay";
// be sure to have set this variable back to false before starting
// processing, to exclude unwanted effects on temporary files;
static bool global_mergeversions= false; // accept duplicate versions
static bool global_dropversion= false; // exclude version
static bool global_dropauthor= false; // exclude author information
static bool global_fakeauthor= false; // fake author information
static bool global_fakeversion= false; // fake just the version number
static bool global_fakelonlat= false;
// fake longitude and latitude in case of delete actions (.osc);
static bool global_dropbrokenrefs= false; // exclude broken references
static bool global_dropnodes= false; // exclude nodes section
static bool global_dropways= false; // exclude ways section
static bool global_droprelations= false; // exclude relations section
static bool global_outo5m= false; // output shall have .o5m format
static bool global_outo5c= false; // output shall have .o5c format
static bool global_outosm= false; // output shall have .osm format
static bool global_outosc= false; // output shall have .osc format
static bool global_outosh= false; // output shall have .osh format
static bool global_outpbf= false; // output shall have .pbf format
static bool global_outcsv= false; // output shall have .csv format
static bool global_outnone= false; // no standard output at all
static int32_t global_pbfgranularity= 100;
// granularity of lon/lat in .pbf files; unit: 1 nanodegree;
static int32_t global_pbfgranularity100= 0;
// granularity of lon/lat in .pbf files; unit: 100 nanodegrees;
// 0: default: 100 nanodegrees;
static bool global_emulatepbf2osm= false;
// emulate pbf2osm compatible output
static bool global_emulateosmosis= false;
// emulate Osmosis compatible output
static bool global_emulateosmium= false;
// emulate Osmium compatible output
static int64_t global_timestamp= 0;
// manually chosen file timestamp; ==0: no file timestamp given;
static bool global_outtimestamp= false;
// print only the file timestamp, nothing else
static bool global_statistics= false; // print statistics to stderr
static bool global_outstatistics= false; // print statistics to stdout
static bool global_csvheadline= false; // headline for csv
static char global_csvseparator[16]= "\t"; // separator for csv
static bool global_completeways= false; // when applying borders,
// do not clip ways but include them as whole if at least a single
// of its nodes lies inside the borders;
static bool global_complexways= false; // same as global_completeways,
// but multipolygons are included completely (with all ways and their
// nodes), even when only a single nodes lies inside the borders;
static int global_calccoords= 0;
// calculate coordinates for all objects;
// 0: no coordinates to calculate; 1: calculate coordinates;
// -1: calculate coordinates and bbox;
static bool global_alltonodes= false;
// convert all ways and all relations to nodes
static bool global_add= false;
// add at least one tag shall be added;
// global_add == global_addbbox || global_addbboxarea ||
// global_addbboxweight ||
// global_addbboxwidth || global_addbboxwidthweight
static bool global_addbbox= false;
// add bBox tags to ways and relations
static bool global_addbboxarea= false;
// add bBoxArea tags to ways and relations
static bool global_addbboxweight= false;
// add bBoxWeight tags to ways and relations
static bool global_addbboxwidth= false;
// add bBoxWidth tags to ways and relations
static bool global_addbboxwidthweight= false;
// add bBoxWidthWeight tags to ways and relations
static int64_t global_maxobjects= 25000000;
static int64_t global_otypeoffset10= INT64_C(1000000000000000);
// if global_calccoords!=0:
// id offset for ways; *2: id offset for relations;
static int64_t global_otypeoffset05,
global_otypeoffset15,global_otypeoffset20;
// (just to save CPU time for calculating the offset of relations)
static int64_t global_otypeoffsetstep= 0;
// if !=0, the program will not create the new id by adding
// global_otypeoffset but by starting at global_otypeoffset
// and adding 1 for every new way, resp. relation:
static char global_tempfilename[350]= "osmconvert_tempfile";
// prefix of names for temporary files
static int64_t global_maxrefs= 100000;
#define PERR(f) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmconvert Error: " f "\n"); }
// print error message
#define PERRv(f,...) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmconvert Error: " f "\n",__VA_ARGS__); }
// print error message with value(s)
#define WARN(f) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmconvert Warning: " f "\n"); }
// print a warning message, do it maximal 3 times
#define WARNv(f,...) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmconvert Warning: " f "\n",__VA_ARGS__); }
// print a warning message with value(s), do it maximal 3 times
#define PINFO(f) \
fprintf(stderr,"osmconvert: " f "\n"); // print info message
#define PINFOv(f,...) \
fprintf(stderr,"osmconvert: " f "\n",__VA_ARGS__);
#define ONAME(i) \
(i==0? "node": i==1? "way": i==2? "relation": "unknown object")
#define global_fileM 1002 // maximum number of input files
//------------------------------------------------------------
// end Module Global global variables for this program
//------------------------------------------------------------
static inline char* uint32toa(uint32_t v,char* s) {
// convert uint32_t integer into string;
// v: long integer value to convert;
// return: s;
// s[]: digit string;
char* s1,*s2;
char c;
s1= s;
if(v==0)
*s1++= '0';
s2= s1;
while(v>0)
{ *s2++= "0123456789"[v%10]; v/= 10; }
*s2--= 0;
while(s2>s1)
{ c= *s1; *s1= *s2; *s2= c; s1++; s2--; }
return s;
} // end uint32toa()
static inline char* int64toa(int64_t v,char* s) {
// convert int64_t integer into string;
// v: long integer value to convert;
// return: s;
// s[]: digit string;
char* s1,*s2;
char c;
s1= s;
if(v<0)
{ *s1++= '-'; v= -v; }
else if(v==0)
*s1++= '0';
s2= s1;
while(v>0)
{ *s2++= "0123456789"[v%10]; v/= 10; }
*s2--= 0;
while(s2>s1)
{ c= *s1; *s1= *s2; *s2= c; s1++; s2--; }
return s;
} // end int64toa()
static inline char *stpcpy0(char *dest, const char *src) {
// redefinition of C99's stpcpy() because it's missing in MinGW,
// and declaration in Linux seems to be wrong;
while(*src!=0)
*dest++= *src++;
*dest= 0;
return dest;
} // end stpcpy0()
static inline char *strmcpy(char *dest, const char *src, size_t maxlen) {
// similar to strcpy(), this procedure copies a character string;
// here, the length is cared about, i.e. the target string will
// be limited in case it is too long;
// src[]: source string which is to be copied;
// maxlen: maximum length of the destination string
// (including terminator null);
// return:
// dest[]: destination string of the copy; this is the
// function's return value too;
char* d;
if(maxlen==0)
return dest;
d= dest;
while(--maxlen>0 && *src!=0)
*d++= *src++;
*d= 0;
return dest;
} // end strmcpy()
#define strMcpy(d,s) strmcpy((d),(s),sizeof(d))
static inline char *stpmcpy(char *dest, const char *src, size_t maxlen) {
// similar to strmcpy(), this procedure copies a character string;
// however, it returns the address of the destination string's
// terminating zero character;
// this makes it easier to concatenate strings;
char* d;
if(maxlen==0)
return dest;
d= dest;
while(--maxlen>0 && *src!=0)
*d++= *src++;
*d= 0;
return d;
} // end stpmcpy()
#define stpMcpy(d,s) stpmcpy(d,s,sizeof(d))
static inline int strzcmp(const char* s1,const char* s2) {
// similar to strcmp(), this procedure compares two character strings;
// here, the number of characters which are to be compared is limited
// to the length of the second string;
// i.e., this procedure can be used to identify a short string s2
// within a long string s1;
// s1[]: first string;
// s2[]: string to compare with the first string;
// return:
// 0: both strings are identical; the first string may be longer than
// the second;
// -1: the first string is alphabetical smaller than the second;
// 1: the first string is alphabetical greater than the second;
while(*s1==*s2 && *s1!=0) { s1++; s2++; }
if(*s2==0)
return 0;
return *(unsigned char*)s1 < *(unsigned char*)s2? -1: 1;
} // end strzcmp()
static inline int strzlcmp(const char* s1,const char* s2) {
// similar to strzcmp(), this procedure compares two character strings;
// and accepts the first string to be longer than the second;
// other than strzcmp(), this procedure returns the length of s2[] in
// case both string contents are identical, and returns 0 otherwise;
// s1[]: first string;
// s2[]: string to compare with the first string;
// return:
// >0: both strings are identical, the length of the second string is
// returned; the first string may be longer than the second;
// 0: the string contents are not identical;
const char* s2a;
s2a= s2;
while(*s1==*s2 && *s1!=0) { s1++; s2++; }
if(*s2==0)
return s2-s2a;
return 0;
} // end strzlcmp()
static inline int strycmp(const char* s1,const char* s2) {
// similar to strcmp(), this procedure compares two character strings;
// here, both strings are end-aligned;
// not more characters will be compared than are existing in string s2;
// i.e., this procedure can be used to identify a file name extension;
const char* s1e;
int l;
l= strchr(s2,0)-s2;
s1e= strchr(s1,0);
if(s1e-s1<l)
return 1;
s1= s1e-l;
while(*s1==*s2 && *s1!=0) { s1++; s2++; }
if(*s2==0)
return 0;
return *(unsigned char*)s1 < *(unsigned char*)s2? -1: 1;
} // end strycmp()
static inline bool file_exists(const char* file_name) {
// query if a file exists;
// file_name[]: name of the file in question;
// return: the file exists;
return access(file_name,R_OK)==0;
} // file_exists()
static inline int32_t msbit(int64_t v) {
// gets the most significant 1-bit of a 64 bit integer value;
int32_t msb;
msb= 0;
if(v>=0x100000000LL) {
v/= 0x100000000LL;
msb+= 32;
}
if(v>=0x10000L) {
v/= 0x10000L;
msb+= 16;
}
if(v>=0x100) {
v/= 0x100;
msb+= 8;
}
if(v>=0x10) {
v/= 0x10;
msb+= 4;
}
if(v>=0x4) {
v/= 0x4;
msb+= 2;
}
if(v>=0x2) {
v/= 0x2;
msb+= 1;
}
if(v!=0) {
msb+= 1;
}
return msb;
} // msbit()
static inline int64_t cosrk(int32_t lat) {
// this procedure calculates the Cosinus of the given latitude,
// multiplies it with 40000k/(360*10^7)==0.00012345679,
// and takes the reciprocal value of it;
// lat: latitude in 100 nano degrees;
// return: constant k needed to approximate the area of a
// coordinte-defined bbox:
// (lonmax-lonmin)*(latmax-latmin)/k
static const int32_t cosrktab[901]= {
8100,8100,8100,8100,8100,8100,8100,8100,
8100,8100,8101,8101,8101,8102,8102,8102,
8103,8103,8103,8104,8104,8105,8105,8106,
8107,8107,8108,8109,8109,8110,8111,8111,
8112,8113,8114,8115,8116,8116,8117,8118,
8119,8120,8121,8122,8123,8125,8126,8127,
8128,8129,8130,8132,8133,8134,8136,8137,
8138,8140,8141,8143,8144,8146,8147,8149,
8150,8152,8154,8155,8157,8159,8160,8162,
8164,8166,8168,8169,8171,8173,8175,8177,
8179,8181,8183,8185,8187,8189,8192,8194,
8196,8198,8200,8203,8205,8207,8210,8212,
8215,8217,8219,8222,8224,8227,8230,8232,
8235,8237,8240,8243,8246,8248,8251,8254,
8257,8260,8263,8265,8268,8271,8274,8277,
8280,8284,8287,8290,8293,8296,8299,8303,
8306,8309,8313,8316,8319,8323,8326,8330,
8333,8337,8340,8344,8347,8351,8355,8358,
8362,8366,8370,8374,8377,8381,8385,8389,
8393,8397,8401,8405,8409,8413,8418,8422,
8426,8430,8434,8439,8443,8447,8452,8456,
8461,8465,8470,8474,8479,8483,8488,8493,
8497,8502,8507,8512,8516,8521,8526,8531,
8536,8541,8546,8551,8556,8561,8566,8571,
8577,8582,8587,8592,8598,8603,8608,8614,
8619,8625,8630,8636,8642,8647,8653,8658,
8664,8670,8676,8682,8687,8693,8699,8705,
8711,8717,8723,8729,8736,8742,8748,8754,
8761,8767,8773,8780,8786,8793,8799,8806,
8812,8819,8825,8832,8839,8846,8852,8859,
8866,8873,8880,8887,8894,8901,8908,8915,
8922,8930,8937,8944,8951,8959,8966,8974,
8981,8989,8996,9004,9012,9019,9027,9035,
9043,9050,9058,9066,9074,9082,9090,9098,
9107,9115,9123,9131,9140,9148,9156,9165,
9173,9182,9190,9199,9208,9216,9225,9234,
9243,9252,9261,9270,9279,9288,9297,9306,
9315,9325,9334,9343,9353,9362,9372,9381,
9391,9400,9410,9420,9430,9439,9449,9459,
9469,9479,9489,9499,9510,9520,9530,9540,
9551,9561,9572,9582,9593,9604,9614,9625,
9636,9647,9658,9669,9680,9691,9702,9713,
9724,9736,9747,9758,9770,9781,9793,9805,
9816,9828,9840,9852,9864,9876,9888,9900,
9912,9924,9937,9949,9961,9974,9986,9999,
10012,10024,10037,10050,10063,10076,10089,10102,
10115,10128,10142,10155,10169,10182,10196,10209,
10223,10237,10251,10265,10279,10293,10307,10321,
10335,10350,10364,10378,10393,10408,10422,10437,
10452,10467,10482,10497,10512,10527,10542,10558,
10573,10589,10604,10620,10636,10652,10668,10684,
10700,10716,10732,10748,10765,10781,10798,10815,
10831,10848,10865,10882,10899,10916,10934,10951,
10968,10986,11003,11021,11039,11057,11075,11093,
11111,11129,11148,11166,11185,11203,11222,11241,
11260,11279,11298,11317,11337,11356,11375,11395,
11415,11435,11455,11475,11495,11515,11535,11556,
11576,11597,11618,11639,11660,11681,11702,11724,
11745,11767,11788,11810,11832,11854,11876,11899,
11921,11944,11966,11989,12012,12035,12058,12081,
12105,12128,12152,12176,12200,12224,12248,12272,
12297,12321,12346,12371,12396,12421,12446,12472,
12497,12523,12549,12575,12601,12627,12654,12680,
12707,12734,12761,12788,12815,12843,12871,12898,
12926,12954,12983,13011,13040,13069,13098,13127,
13156,13186,13215,13245,13275,13305,13336,13366,
13397,13428,13459,13490,13522,13553,13585,13617,
13649,13682,13714,13747,13780,13813,13847,13880,
13914,13948,13982,14017,14051,14086,14121,14157,
14192,14228,14264,14300,14337,14373,14410,14447,
14485,14522,14560,14598,14637,14675,14714,14753,
14792,14832,14872,14912,14952,14993,15034,15075,
15116,15158,15200,15242,15285,15328,15371,15414,
15458,15502,15546,15591,15636,15681,15726,15772,
15818,15865,15912,15959,16006,16054,16102,16151,
16200,16249,16298,16348,16398,16449,16500,16551,
16603,16655,16707,16760,16813,16867,16921,16975,
17030,17085,17141,17197,17253,17310,17367,17425,
17483,17542,17601,17660,17720,17780,17841,17903,
17964,18027,18090,18153,18217,18281,18346,18411,
18477,18543,18610,18678,18746,18814,18883,18953,
19023,19094,19166,19238,19310,19384,19458,19532,
19607,19683,19759,19836,19914,19993,20072,20151,
20232,20313,20395,20478,20561,20645,20730,20815,
20902,20989,21077,21166,21255,21346,21437,21529,
21622,21716,21811,21906,22003,22100,22199,22298,
22398,22500,22602,22705,22810,22915,23021,23129,
23237,23347,23457,23569,23682,23796,23912,24028,
24146,24265,24385,24507,24630,24754,24879,25006,
25134,25264,25395,25527,25661,25796,25933,26072,
26212,26353,26496,26641,26788,26936,27086,27238,
27391,27547,27704,27863,28024,28187,28352,28519,
28688,28859,29033,29208,29386,29566,29748,29933,
30120,30310,30502,30696,30893,31093,31295,31501,
31709,31920,32134,32350,32570,32793,33019,33249,
33481,33717,33957,34200,34447,34697,34951,35209,
35471,35737,36007,36282,36560,36843,37131,37423,
37720,38022,38329,38641,38958,39281,39609,39943,
40282,40628,40980,41337,41702,42073,42450,42835,
43227,43626,44033,44447,44870,45301,45740,46188,
46646,47112,47588,48074,48570,49076,49594,50122,
50662,51214,51778,52355,52946,53549,54167,54800,
55447,56111,56790,57487,58200,58932,59683,60453,
61244,62056,62890,63747,64627,65533,66464,67423,
68409,69426,70473,71552,72665,73814,75000,76225,
77490,78799,80153,81554,83006,84510,86071,87690,
89371,91119,92937,94828,96799,98854,100998,103238,
105580,108030,110598,113290,116118,119090,122220,125518,
129000,132681,136578,140712,145105,149781,154769,160101,
165814,171950,178559,185697,193429,201834,211004,221047,
232095,244305,257873,273037,290097,309432,331529,357027,
386774,421931,464119,515683,580138,663010,773507,928203,
1160248,1546993,2320483,4640960,
2147483647 }; // cosrk values for 10th degrees from 0 to 90
lat/= 1000000;
// transform unit 100 nano degree into unit 10th degree
if(lat<0) lat= -lat; // make it positive
if(lat>900) lat= 900; // set maximum of 90 degree
return cosrktab[lat];
} // cosrk()
// the table in the previous procedure has been generated by this
// program:
#if 0 // file cosrk.c, run it with: gcc cosrk.c -lm -o cosrk && ./cosrk
#include <stdio.h>
#include <math.h>
#include <inttypes.h>
int main() {
int i;
printf(" static const int32_t cosrktab[901]= {");
i= 0;
for(i= 0;i<900;i++) {
if(i%8==0)
printf("\n ");
printf("%"PRIi32",",(int32_t)(
1/( cos(i/1800.0*3.14159265359) *0.00012345679)
));
}
printf("\n 2147483647");
printf(" }; // cosrk values for 10th degrees from 0 to 90\n");
return 0; }
#endif
static int32_t lonadapt(int32_t londiff,int32_t lat) {
// takes a West-East distance given in longitude difference,
// and calculates the adjusted distance in degrees,
// i.e., it takes the latitude into account;
// all units: 100 nano degrees;
// londiff: West-East distance between two points;
// lat: latitude at which the distance is to be calculated;
// return: West-East distance in Equator degrees;
// this adjusted longitude difference is then comparable
// to latitude differences;
static const uint32_t cosrtab[901]= {
UINT32_C(4294967295),UINT32_C(4294960754),UINT32_C(4294941129),
UINT32_C(4294908421),UINT32_C(4294862630),UINT32_C(4294803756),
UINT32_C(4294731800),UINT32_C(4294646761),UINT32_C(4294548639),
UINT32_C(4294437436),UINT32_C(4294313151),UINT32_C(4294175785),
UINT32_C(4294025338),UINT32_C(4293861811),UINT32_C(4293685204),
UINT32_C(4293495517),UINT32_C(4293292752),UINT32_C(4293076909),
UINT32_C(4292847988),UINT32_C(4292605991),UINT32_C(4292350917),
UINT32_C(4292082769),UINT32_C(4291801546),UINT32_C(4291507249),
UINT32_C(4291199879),UINT32_C(4290879438),UINT32_C(4290545926),
UINT32_C(4290199345),UINT32_C(4289839694),UINT32_C(4289466976),
UINT32_C(4289081192),UINT32_C(4288682342),UINT32_C(4288270429),
UINT32_C(4287845452),UINT32_C(4287407414),UINT32_C(4286956316),
UINT32_C(4286492159),UINT32_C(4286014944),UINT32_C(4285524674),
UINT32_C(4285021349),UINT32_C(4284504971),UINT32_C(4283975542),
UINT32_C(4283433063),UINT32_C(4282877536),UINT32_C(4282308963),
UINT32_C(4281727345),UINT32_C(4281132684),UINT32_C(4280524982),
UINT32_C(4279904241),UINT32_C(4279270462),UINT32_C(4278623648),
UINT32_C(4277963801),UINT32_C(4277290922),UINT32_C(4276605014),
UINT32_C(4275906079),UINT32_C(4275194118),UINT32_C(4274469135),
UINT32_C(4273731130),UINT32_C(4272980107),UINT32_C(4272216068),
UINT32_C(4271439015),UINT32_C(4270648951),UINT32_C(4269845877),
UINT32_C(4269029797),UINT32_C(4268200712),UINT32_C(4267358626),
UINT32_C(4266503540),UINT32_C(4265635459),UINT32_C(4264754383),
UINT32_C(4263860316),UINT32_C(4262953261),UINT32_C(4262033219),
UINT32_C(4261100196),UINT32_C(4260154192),UINT32_C(4259195210),
UINT32_C(4258223255),UINT32_C(4257238328),UINT32_C(4256240433),
UINT32_C(4255229573),UINT32_C(4254205750),UINT32_C(4253168969),
UINT32_C(4252119232),UINT32_C(4251056542),UINT32_C(4249980902),
UINT32_C(4248892316),UINT32_C(4247790788),UINT32_C(4246676320),
UINT32_C(4245548916),UINT32_C(4244408579),UINT32_C(4243255313),
UINT32_C(4242089121),UINT32_C(4240910007),UINT32_C(4239717975),
UINT32_C(4238513027),UINT32_C(4237295169),UINT32_C(4236064403),
UINT32_C(4234820733),UINT32_C(4233564163),UINT32_C(4232294697),
UINT32_C(4231012338),UINT32_C(4229717092),UINT32_C(4228408960),
UINT32_C(4227087949),UINT32_C(4225754060),UINT32_C(4224407300),
UINT32_C(4223047671),UINT32_C(4221675178),UINT32_C(4220289825),
UINT32_C(4218891617),UINT32_C(4217480557),UINT32_C(4216056649),
UINT32_C(4214619899),UINT32_C(4213170311),UINT32_C(4211707888),
UINT32_C(4210232636),UINT32_C(4208744558),UINT32_C(4207243661),
UINT32_C(4205729947),UINT32_C(4204203421),UINT32_C(4202664089),