forked from mapsme/osmctools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osmfilter.c
7020 lines (6621 loc) · 234 KB
/
osmfilter.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
// osmfilter 2017-09-22 14:00
#define VERSION "1.4.3"
//
// compile this file:
// gcc osmfilter.c -O3 -o osmfilter
//
// (c) 2011..2017 Markus Weber, Nuernberg
//
// 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=
"\nosmfilter " VERSION " Parameter Overview\n"
"(Please use --help to get more information.)\n"
"\n"
"<file> file to filter; (.o5m faster than .osm)\n"
"--keep= define which objects are to be kept\n"
"--keep-nodes= same as above, but applies to nodes only,\n"
"--keep-ways= etc.\n"
"--keep-relations= Examples:\n"
"--keep-nodes-ways= --keep=\"amenity=pub =bar\"\n"
"--keep-nodes-relations= --keep=\"tunnel=yes and lit=yes\"\n"
"--keep-ways-relations=\n"
"--drop= define which objects are to be dropped\n"
"--drop-...(see above)= similar to --keep-...= (see above)\n"
"--keep-tags= define which tags are to be kept\n"
"--keep-node-tags= same as above, but applies to nodes only,\n"
"--keep-way-tags= etc.\n"
"--keep-relation-tags=\n"
"--keep-node-way-tags=\n"
"--keep-node-relation-tags=\n"
"--keep-way-relation-tags=\n"
"--drop-tags= define which tags are to be dropped\n"
"--drop-...-tags= similar to --keep-...-tags= (see above)\n"
"--modify-tags= define which tags are to be modified\n"
"--modify-...-tags= similar to --keep-...-tags= (see above)\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"
"--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"
"--ignore-dependencies ignore dependencies between OSM objects\n"
"--out-key= write statistics (for the key, if supplied)\n"
"--out-count= same as before, but sorted by occurrence\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"
"-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=
"\nosmfilter " VERSION "\n"
"\n"
"THIS PROGRAM IS FOR EXPERIMENTAL USE ONLY.\n"
"PLEASE EXPECT MALFUNCTION AND DATA LOSS.\n"
"SAVE YOUR DATA BEFORE STARTING THIS PROGRAM.\n"
"\n"
"This program filters OpenStreetMap data.\n"
"\n"
"The input file name must be supplied as command line argument. The\n"
"file must not be a stream. Redirections from standard input will not\n"
"work because the program needs random access to the file. You do not\n"
"need to specify the input format, osmfilter will recognize these\n"
"formats: .osm (XML), .osc (OSM Change File), .osh (OSM Full History),\n"
".o5m (speed-optimized) and .o5c (speed-optimized Change File).\n"
"\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"
"--keep=OBJECT_FILTER\n"
" All object types (nodes, ways and relations) will be kept\n"
" if they meet the filter criteria. Same applies to dependent\n"
" objects, e.g. nodes in ways, ways in relations, relations in\n"
" other relations.\n"
" Please look below for a syntax description of OBJECT_FILTER.\n"
"\n"
"--keep-nodes=OBJECT_FILTER\n"
"--keep-ways=OBJECT_FILTER\n"
"--keep-relations=OBJECT_FILTER\n"
"--keep-nodes-ways=OBJECT_FILTER\n"
"--keep-nodes-relations=OBJECT_FILTER\n"
"--keep-ways-relations=OBJECT_FILTER\n"
" Same as above, but just for the specified object types.\n"
"\n"
"--drop=OBJECT_FILTER\n"
" All object types (nodes, ways and relations) which meet the\n"
" supplied filter criteria will be dropped, regardless of\n"
" meeting the criteria of a keep filter (see above).\n"
" Please look below for a syntax description of OBJECT_FILTER.\n"
"\n"
"--drop-nodes=OBJECT_FILTER\n"
"--drop-ways=OBJECT_FILTER\n"
"--drop-relations=OBJECT_FILTER\n"
"--drop-nodes-ways=OBJECT_FILTER\n"
"--drop-nodes-relations=OBJECT_FILTER\n"
"--drop-ways-relations=OBJECT_FILTER\n"
" Same as above, but just for the specified object types.\n"
"\n"
"--keep-tags=TAG_FILTER\n"
" The in TAG_FILTER specified tags will be allowed on output.\n"
" Please look below for a syntax description of TAG_FILTER.\n"
"\n"
"--keep-node-tags=TAG_FILTER\n"
"--keep-way-tags=TAG_FILTER\n"
"--keep-relation-tags=TAG_FILTER\n"
"--keep-node-way-tags=TAG_FILTER\n"
"--keep-node-relation-tags=TAG_FILTER\n"
"--keep-way-relation-tags=TAG_FILTER\n"
" Same as above, but just for the specified object types.\n"
"\n"
"--drop-tags=TAG_FILTER\n"
" The specified tags will be dropped. This overrules the\n"
" previously described parameter --keep-tags.\n"
" Please look below for a syntax description of TAG_FILTER.\n"
"\n"
"--drop-node-tags=TAG_FILTER\n"
"--drop-way-tags=TAG_FILTER\n"
"--drop-relation-tags=TAG_FILTER\n"
"--drop-node-way-tags=TAG_FILTER\n"
"--drop-node-relation-tags=TAG_FILTER\n"
"--drop-way-relation-tags=TAG_FILTER\n"
" Same as above, but just for the specified object types.\n"
"\n"
"--modify-tags=TAG_MODIFICATION_LIST\n"
" The specified tags will be modified. This is done after any\n"
" filtering (see --keep, --keep-tags, --drop, --drop-tags).\n"
" Please look below for a description of TAG_MODIFICATION_LIST.\n"
"\n"
"--modify-node-tags=TAG_MODIFICATION_LIST\n"
"--modify-way-tags=TAG_MODIFICATION_LIST\n"
"--modify-relation-tags=TAG_MODIFICATION_LIST\n"
"--modify-node-way-tags=TAG_MODIFICATION_LIST\n"
"--modify-node-relation-tags=TAG_MODIFICATION_LIST\n"
"--modify-way-relation-tags=TAG_MODIFICATION_LIST\n"
" Same as above, but just for the specified object types.\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"
"--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"
" o5mfiler 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 osmfilter 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"
"--ignore-dependencies\n"
" Usually, all member nodes of a way which meets the filter\n"
" criteria will be included as well. Same applies to members of\n"
" included relations. If you activate this option, all these\n"
" dependencies between OSM objects will be ignored.\n"
"\n"
"--out-key=KEYNAME\n"
" The output will contain no regular OSM data but only\n"
" statistics: a list of all used keys is assembled. Left to\n"
" each key, the number of occurrences is printed.\n"
" If KEYNAME is given, the program will list all values which\n"
" are used in connections with this key.\n"
" You may use wildcard characters for KEYNAME, but only at the\n"
" beginning and/or at the end. For example: --out-key=addr:*\n"
"\n"
"--out-count=KEYNAME\n"
" Same as --out-key=, but the list is sorted by the number of\n"
" occurrences of the keys resp. values.\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 are represented by\n"
" 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"
"-o=<outfile>\n"
" Standard output will be rerouted to the specified file.\n"
" If no output format has been specified, the program will\n"
" proceed according to the file name extension.\n"
"\n"
"-t=<tempfile>\n"
" osmfilter uses a temporary file to process interrelational\n"
" dependencies. This parameter defines the name prefix. The\n"
" default value is \"osmfilter_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"
" osmfilter will display all input parameters.\n"
"\n"
"OBJECT_FILTER\n"
" Some of the command line arguments need a filter to be\n"
" specified. This filter definition consists of key/val pairs\n"
" and uses the following syntax:\n"
" \"KEY1=VAL1 OP KEY2=VAL2 OP KEY3=VAL3 ...\"\n"
" OP is the Boolean operator, it must be either \"and\" or \"or\".\n"
" As usual, \"and\" will be processed prior to \"or\". If you\n"
" want to influence the sequence of processing, you may use\n"
" brackets to do so. Please note that brackets always must be\n"
" padded by spaces. Example: lit=yes and ( note=a or source=b )\n"
" Instead of each \"=\" you may enter one of these comparison\n"
" operators: != (not equal), <, >, <=, >=\n"
" The program will use ASCII-alphabetic comparison unless you\n" " compare against a value which is starting with a digit.\n"
" If there are different possible values for the same key, you\n"
" need to write the key only once. For example:\n"
" \"amenity=restaurant =pub =bar\"\n"
" It is allowed to omit the value. In this case, the program\n"
" will accept every value for the defined key. For example:\n"
" \"highway= and lit=yes\"\n"
" You may use wildcard characters for key or value, but only at\n"
" the beginning and/or at the end. For example:\n"
" wikipedia:*= highway=*ary ref_name=*central*\n"
" Please be careful with wildcards in keys since only the first\n"
" key which meets the pattern will be processed.\n"
" There are three special keys which represent object id, user\n"
" id and user name: @id, @uid and @user. They allow you to\n"
" search for certain objects or for edits of specific users.\n"
"\n"
"TAG_FILTER\n"
" The tag filter determines which tags will be kept and which\n"
" will be not. The example\n"
" --keep-tags=\"highway=motorway =primary\"\n"
" will not accept \"highway\" tags other than \"motorway\" or\n"
" \"primary\". Note that neither the object itself will be\n"
" deleted, nor the remaining tags. If you want to drop every\n"
" tag which is not mentioned in a list, use this example:\n"
" all highway= amenity= name=\n"
"\n"
"TAG_MODIFICATION_LIST\n"
" The tag modification list determines which tags will be\n"
" modified. The example\n"
" --modify-tags=\"highway=primary to =secondary\"\n"
" will change every \"primary\" highway into \"secondary\".\n"
" You can also use comparisons or add additional tags:\n"
" --modify-way-tags=\"maxspeed>200 add highspeed=yes\"\n"
"\n"
"Examples\n"
"\n"
"./osmfilter europe.o5m --keep=amenity=bar -o=new.o5m\n"
"./osmfilter a.osm --keep-nodes=lit=yes --drop-ways -o=light.osm\n"
"./osmfilter a.osm --keep=\"\n"
" place=city or ( place=town and population>=10000 )\" -o=b.osm\n"
"./osmfilter region.o5m --keep=\"bridge=yes and layer>=2\" -o=r.o5m\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 (for relations, 2 flags are needed each) 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"
"Limitations\n"
"\n"
"When filtering whole OSM objects (--keep...=, --drop...=), the input\n"
"file must contain the objects ordered by their type: first, all nodes\n"
"nodes, next, all ways, followed by all relations.\n"
"\n"
"Usual .osm, .osc, .o5m and o5c files adhere to this condition. This\n"
"means that you do not have to worry about this limitation. osmfilter\n"
"will display an error message if this sequence is broken.\n"
"\n"
"The number of key/val pairs in each filter parameter is limited to\n"
"1000, the length of each key or val is limited to 100.\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 <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.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 byte isdigi_tab[]= {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
#define isdigi(c) (isdigi_tab[(c)]) // digit
static byte digival_tab[]= {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,2,3,4,5,6,7,8,9,10,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
#define digival(c) (digival_tab[(c)])
// value of a digit, starting with 1, for comparisons only
static int loglevel= 0; // logging to stderr;
// 0: no logging; 1: small logging; 2: normal logging;
// 3: extended logging;
#define UR(x) if(x){} // result value intentionally ignored
#define DP(f) fprintf(stderr,"- Debug: " #f "\n");
#define DPv(f,...) fprintf(stderr,"- Debug: " #f "\n",__VA_ARGS__);
#if __WIN32__
#define NL "\r\n" // use CR/LF as new-line sequence
#define off_t off64_t
#define lseek lseek64
#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_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_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 const char* global_outkey= NULL;
// =="": do not write osm data, write a list of keys instead;
// !=NULL && !="": write a list of vals to the key this variable
// points to;
static bool global_outsort= false; // sort item list by count;
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 char global_tempfilename[350]= "osmfilter_tempfile";
// prefix of names for temporary files
static bool global_recursive= false; // recursive processing necessary
static bool global_ignoredependencies= false;
// user wants interobject dependencies to be ignored
#define PERR(f) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmfilter Error: " f "\n"); }
// print error message
#define PERRv(f,...) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmfilter Error: " f "\n",__VA_ARGS__); }
// print error message with value(s)
#define WARN(f) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmfilter Warning: " f "\n"); }
// print a warning message, do it maximal 3 times
#define WARNv(f,...) { static int msgn= 3; if(--msgn>=0) \
fprintf(stderr,"osmfilter Warning: " f "\n",__VA_ARGS__); }
// print a warning message with value(s), do it maximal 3 times
#define PINFO(f) \
fprintf(stderr,"osmfilter: " f "\n"); // print info message
#define PINFOv(f,...) \
fprintf(stderr,"osmfilter: " f "\n",__VA_ARGS__);
#define ONAME(i) \
(i==0? "node": i==1? "way": i==2? "relation": "unknown object")
#define global_fileM 1 // maximum number of input files
//------------------------------------------------------------
// end Module Global global variables for this program
//------------------------------------------------------------
static inline char* int32toa(int32_t v,char* s) {
// convert int32_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 int32toa()
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 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()
//------------------------------------------------------------
// Module pbf_ protobuf conversions module
//------------------------------------------------------------
// this module provides procedures for conversions from
// protobuf formats to regular numbers;
// as usual, all identifiers of a module have the same prefix,
// in this case 'pbf'; one underline will follow in case of a
// global accessible object, two underlines in case of objects
// which are not meant to be accessed from outside this module;
// the sections of private and public definitions are separated
// by a horizontal line: ----
// many procedures have a parameter 'pp'; here, the address of
// a buffer pointer is expected; this pointer will be incremented
// by the number of bytes the converted protobuf element consumes;
//------------------------------------------------------------
static inline uint32_t pbf_uint32(byte** pp) {
// get the value of an unsigned integer;
// pp: see module header;
byte* p;
uint32_t i;
uint32_t fac;
p= *pp;
i= *p;
if((*p & 0x80)==0) { // just one byte
(*pp)++;
return i;
}
i&= 0x7f;
fac= 0x80;
while(*++p & 0x80) { // more byte(s) will follow
i+= (*p & 0x7f)*fac;
fac<<= 7;
}
i+= *p++ *fac;
*pp= p;
return i;
} // end pbf_uint32()
static inline int32_t pbf_sint32(byte** pp) {
// get the value of an unsigned integer;
// pp: see module header;
byte* p;
int32_t i;
int32_t fac;
int sig;
p= *pp;
i= *p;
if((*p & 0x80)==0) { // just one byte
(*pp)++;
if(i & 1) // negative
return -1-(i>>1);
else
return i>>1;
}
sig= i & 1;
i= (i & 0x7e)>>1;
fac= 0x40;
while(*++p & 0x80) { // more byte(s) will follow
i+= (*p & 0x7f)*fac;
fac<<= 7;
}
i+= *p++ *fac;
*pp= p;
if(sig) // negative
return -1-i;
else
return i;
} // end pbf_sint32()
static inline uint64_t pbf_uint64(byte** pp) {
// get the value of an unsigned integer;
// pp: see module header;
byte* p;
uint64_t i;
uint64_t fac;
p= *pp;
i= *p;
if((*p & 0x80)==0) { // just one byte
(*pp)++;
return i;
}
i&= 0x7f;
fac= 0x80;
while(*++p & 0x80) { // more byte(s) will follow
i+= (*p & 0x7f)*fac;
fac<<= 7;
}
i+= *p++ *fac;
*pp= p;
return i;
} // end pbf_uint64()
static inline int64_t pbf_sint64(byte** pp) {
// get the value of a signed integer;
// pp: see module header;
byte* p;
int64_t i;
int64_t fac;
int sig;
p= *pp;
i= *p;
if((*p & 0x80)==0) { // just one byte
(*pp)++;
if(i & 1) // negative
return -1-(i>>1);
else
return i>>1;
}
sig= i & 1;
i= (i & 0x7e)>>1;
fac= 0x40;
while(*++p & 0x80) { // more byte(s) will follow
i+= (*p & 0x7f)*fac;
fac<<= 7;
}
i+= *p++ *fac;
*pp= p;
if(sig) // negative
return -1-i;
else
return i;
} // end pbf_sint64()
#if 0 // not used at present
static inline void pbf_intjump(byte** pp) {
// jump over a protobuf formatted integer;
// pp: see module header;
// we do not care about a possibly existing identifier,
// therefore as the start address *pp the address of the
// integer value is expected;
byte* p;
p= *pp;
while(*p & 0x80) p++; p++;
*pp= p;
} // end pbf_intjump()
#endif
//------------------------------------------------------------
// end Module pbf_ protobuf conversions module
//------------------------------------------------------------
//------------------------------------------------------------
// Module hash_ OSM hash module
//------------------------------------------------------------
// this module provides three hash tables with default sizes
// of 320, 60 and 20 MB;
// the procedures hash_seti() and hash_geti() allow bitwise
// access to these tables;
// as usual, all identifiers of a module have the same prefix,
// in this case 'hash'; one underline will follow in case of a
// global accessible object, two underlines in case of objects
// which are not meant to be accessed from outside this module;
// the sections of private and public definitions are separated
// by a horizontal line: ----
static bool hash__initialized= false;
#define hash__M 4
static unsigned char* hash__mem[hash__M]= {NULL,NULL,NULL,NULL};
// start of the hash fields for each object type (node, way, relation);
static uint32_t hash__max[hash__M]= {0,0,0,0};
// size of the hash fields for each object type
// (node, way, positive relation, negative relation);
static int hash__errornumber= 0;
// 1: object too large
static void hash__end() {
// clean-up for hash module;
// will be called at program's end;
int o; // object type
for(o= 0;o<hash__M;o++) {
hash__max[o]= 0;
if(hash__mem[o]!=NULL) {
free(hash__mem[o]); hash__mem[o]= NULL; }
}
hash__initialized= false;
} // end hash__end()
//------------------------------------------------------------
static int hash_ini(int n,int w,int r) {
// initializes the hash module;
// n: amount of memory which is to be allocated for nodes;
// w: amount of memory which is to be allocated for ways;
// r: amount of memory which is to be allocated for relations;
// this will be divided for the two relation has fields:
// one field for positive relations and one for negative relations;
// range for all input parameters: 1..4000, unit: MiB;
// the second and any further call of this procedure will be ignored;
// return: 0: initialization has been successful (enough memory);
// 1: memory request had to been reduced to fit the system's
// resources (warning);
// 2: memory request was unsuccessful (error);
// general note concerning OSM database:
// number of objects at Oct 2010: 950M nodes, 82M ways, 1.3M relations;
// number of objects at May 2011: 1.3G nodes, 114M ways, 1.6M relations;
int o; // object type
bool warning,error;
warning= error= false;
if(hash__initialized) // already initialized
return 0; // ignore the call of this procedure
// check parameters and store the values
#define D(x,o) if(x<1) x= 1; else if(x>4000) x= 4000; \
hash__max[o]= x*(1024*1024);
D(n,0) D(w,1) D(r,2) D(r,3)
#undef D
// allocate memory for each hash table
for(o= 0;o<hash__M;o++) { // for each hash table
do {
hash__mem[o]= (unsigned char*)malloc(hash__max[o]);
if(hash__mem[o]!=NULL) { // allocation successful
memset(hash__mem[o],0,hash__max[o]); // clear all flags
break;
}
// here: allocation unsuccessful
// reduce amount by 50%
hash__max[o]/=2;
warning= true;
// memorize that the user should be warned about this reduction
// try to allocate the reduced amount of memory
} while(hash__max[o]>=1024);
if(hash__mem[o]==NULL) // allocation unsuccessful at all
error= true; // memorize that the program should be aborted
} // end for each hash table
atexit(hash__end); // chain-in the clean-up procedure
if(!error) hash__initialized= true;
return error? 2: warning? 1: 0;
} // end hash_ini()
static void hash_seti(int o,int64_t idi) {
// set a flag for a specific object type and ID;
// o: object type; 0: node; 1: way; 2: relation;
// caution: due to performance reasons the boundaries
// are not checked;
// id: id of the object;
unsigned char* mem; // address of byte in hash table
unsigned int ido; // bit offset to idi;
if(!hash__initialized) return; // ignore this call
idi+= ((int64_t)hash__max[o])<<3; // consider small negative numbers
ido= idi&0x7; // extract bit number (0..7)
idi>>=3; // calculate byte offset
idi%= hash__max[o]; // consider length of hash table
mem= hash__mem[o]; // get start address of hash table
mem+= idi; // calculate address of the byte
*mem|= (1<<ido); // set bit
} // end hash_seti()
static bool hash_relseti(int64_t idi) {
// set the status of a flag for a relation of a specific ID;
// the flag is set only if this relations does not have a set flag
// in the 'negative relations' hash field;
// id: id of the object;
// return: the flag has been set by this call of this procedure;
// this procedure assumes that both, the hash field for positive
// relations and the hash field for negative relations, have the
// same size;
unsigned char* mem;
unsigned int ido; // bit offset to idi;
unsigned char bitmask;
bool r;
if(!hash__initialized) return true; // ignore this call
idi+= ((int64_t)hash__max[2])<<3; // consider small negative numbers
ido= idi&0x7; // extract bit number (0..7)
idi>>=3; // calculate byte offset
idi%= hash__max[2]; // consider length of hash table
mem= hash__mem[3]; // get start address of negative hash table
mem+= idi; // calculate address of the byte
if((*mem&(1<<ido))!=0) // the relation's negative flag is set
return false; // end processing here because we do not want to
// set the relation's positive flag
mem= hash__mem[2]; // get start address of positive hash table
mem+= idi; // calculate address of the byte
bitmask= 1<<ido; // determine the bitmask
r= (*mem&bitmask)==0; // the addressed bit has not been set until now
*mem|= bitmask; // set bit
return r;
} // end hash_relseti();
static bool hash_geti(int o,int64_t idi) {
// get the status of a flag for a specific object type and ID;
// (same as previous procedure, but id must be given as number);
// o: object type; 0: node; 1: way; 2: relation; caution:
// due to performance reasons the boundaries are not checked;
// id: id of the object; the id is given as a string of decimal digits;
// a specific string terminator is not necessary, it is assumed
// that the id number ends with the first non-digit character;
unsigned char* mem;
unsigned int ido; // bit offset to idi;
bool flag;
if(!hash__initialized) return false;
idi+= ((int64_t)hash__max[o])<<3; // consider small negative numbers
ido= idi&0x7; // extract bit number (0..7)
idi>>=3; // calculate byte offset
idi%= hash__max[o]; // consider length of hash table
mem= hash__mem[o]; // get start address of hash table
mem+= idi; // calculate address of the byte
flag= (*mem&(1<<ido))!=0; // get status of the addressed bit
return flag;
} // end hash_geti();
static int hash_queryerror() {
// determine if an error has occurred;
return hash__errornumber;
} // end hash_queryerror()
//------------------------------------------------------------