forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse-pbf.c
606 lines (481 loc) · 16.5 KB
/
parse-pbf.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
/*
#-----------------------------------------------------------------------------
# osm2pgsql - converts planet.osm file into PostgreSQL
# compatible output suitable to be rendered by mapnik
#-----------------------------------------------------------------------------
# Original Python implementation by Artem Pavlenko
# Re-implementation by Jon Burgess, Copyright 2006
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-----------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <time.h>
#include <zlib.h>
#include "osmtypes.h"
#include "output.h"
#include "reprojection.h"
#include "fileformat.pb-c.h"
#include "osmformat.pb-c.h"
#define UNUSED __attribute__ ((unused))
#define MAX_BLOCK_HEADER_SIZE 64*1024
#define MAX_BLOB_SIZE 32*1024*1024
#define NANO_DEGREE .000000001
static uint32_t get_length(FILE *input)
{
char buf[4];
if (1 != fread(buf, sizeof(buf), 1, input))
return 0;
return ntohl(*((size_t *)buf));
}
#if 0
static void *realloc_or_free(void *p, size_t len)
{
void *new = realloc(p, len);
if (new == NULL) {
free(p);
}
return new;
}
#endif
static BlockHeader *read_header(FILE *input, void *buf)
{
BlockHeader *header_msg;
size_t read, length = get_length(input);
if (length < 1 || length > MAX_BLOCK_HEADER_SIZE) {
if (!feof(input)) {
fprintf(stderr, "Invalid blocksize %lu\n", (unsigned long)length);
}
return NULL;
}
read = fread(buf, length, 1, input);
if (!read) {
perror("parse-pbf: error while reading header data");
return NULL;
}
header_msg = block_header__unpack (NULL, length, buf);
if (header_msg == NULL) {
fprintf(stderr, "Error unpacking BlockHeader message\n");
return NULL;
}
return header_msg;
}
static Blob *read_blob(FILE *input, void *buf, int32_t length)
{
Blob *blob_msg;
if (length < 1 || length > MAX_BLOB_SIZE) {
fprintf(stderr, "Blob isn't present or exceeds minimum/maximum size\n");
return NULL;
}
if(1 != fread(buf, length, 1, input)) {
fprintf(stderr, "error reading blob content\n");
return NULL;
}
blob_msg = blob__unpack (NULL, length, buf);
if (blob_msg == NULL) {
fprintf(stderr, "Error unpacking Blob message\n");
return NULL;
}
return blob_msg;
}
static size_t uncompress_blob(Blob *bmsg, void *buf, int32_t max_size)
{
if (bmsg->raw_size > max_size) {
fprintf(stderr, "blob raw size too large\n");
return 0;
}
if (bmsg->has_raw) {
memcpy(buf, bmsg->raw.data, bmsg->raw.len);
return bmsg->raw.len;
} else if (bmsg->has_zlib_data) {
int ret;
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = bmsg->zlib_data.len;
strm.next_in = bmsg->zlib_data.data;
strm.avail_out = bmsg->raw_size;
strm.next_out = buf;
ret = inflateInit(&strm);
if (ret != Z_OK) {
fprintf(stderr, "Zlib init failed\n");
return 0;
}
ret = inflate(&strm, Z_NO_FLUSH);
(void)inflateEnd(&strm);
if (ret != Z_STREAM_END) {
fprintf(stderr, "Zlib compression failed (code %d, %s)\n", ret, strm.msg);
return 0;
}
return bmsg->raw_size;
} else if (bmsg->has_bzip2_data) {
fprintf(stderr, "Can't uncompress bz2 data\n");
return 0;
} else if (bmsg->has_lzma_data) {
fprintf(stderr, "Can't uncompress LZMA data\n");
return 0;
} else {
fprintf(stderr, "We cannot handle the %d non-raw bytes yet...\n", bmsg->raw_size);
return 0;
}
return 0;
}
int addProtobufItem(struct keyval *head, ProtobufCBinaryData key, ProtobufCBinaryData val, int noDupe)
{
char *keystr, *valstr;
int retval;
keystr = calloc(key.len + 1, 1);
memcpy(keystr, key.data, key.len);
/* drop certain keys (matching parse-xml2) */
if ((strcmp(keystr, "created_by") == 0) || (strcmp(keystr, "source") == 0)) {
free(keystr);
return 0;
}
valstr = calloc(val.len + 1, 1);
memcpy(valstr, val.data, val.len);
retval = addItem(head, keystr, valstr, noDupe);
free(keystr);
free(valstr);
return retval;
}
int addIntItem(struct keyval *head, const char *key, int val, int noDupe)
{
char buf[100];
sprintf(buf, "%d", val);
return addItem(head, key, buf, noDupe);
}
int addInfoItems(struct keyval *head, Info *info, StringTable *string_table)
{
if (info->has_version) {
addIntItem(head, "osm_version", info->version, 0);
}
if (info->has_changeset) {
addIntItem(head, "osm_changeset", info->changeset, 0);
}
if (info->has_uid) {
addIntItem(head, "osm_uid", info->uid, 0);
}
if (info->has_user_sid) {
ProtobufCBinaryData user = string_table->s[info->user_sid];
char *username;
username = calloc(user.len + 1, 1);
memcpy(username, user.data, user.len);
addItem(head, "osm_user", username, 0);
free(username);
}
/* TODO timestamp */
return 0;
}
int processOsmHeader(void *data, size_t length)
{
HeaderBlock *hmsg = header_block__unpack (NULL, length, data);
if (hmsg == NULL) {
fprintf(stderr, "Error unpacking HeaderBlock message\n");
return 0;
}
header_block__free_unpacked (hmsg, &protobuf_c_default_allocator);
return 1;
}
int processOsmDataNodes(struct osmdata_t *osmdata, PrimitiveGroup *group, StringTable *string_table, double lat_offset, double lon_offset, double granularity)
{
unsigned node_id, key_id;
for (node_id = 0; node_id < group->n_nodes; node_id++) {
Node *node = group->nodes[node_id];
double lat, lon;
resetList(&(osmdata->tags));
if (node->info && osmdata->extra_attributes) {
addInfoItems(&(osmdata->tags), node->info, string_table);
}
for (key_id = 0; key_id < node->n_keys; key_id++) {
addProtobufItem(&(osmdata->tags),
string_table->s[node->keys[key_id]],
string_table->s[node->vals[key_id]],
0);
}
lat = lat_offset + (node->lat * granularity);
lon = lon_offset + (node->lon * granularity);
if (node_wanted(osmdata, lat, lon)) {
reproject(&lat, &lon);
osmdata->out->node_add(node->id, lat, lon, &(osmdata->tags));
if (node->id > osmdata->max_node) {
osmdata->max_node = node->id;
}
if (osmdata->count_node == 0) {
time(&osmdata->start_node);
}
osmdata->count_node++;
if (osmdata->count_node%10000 == 0)
printStatus(osmdata);
}
}
return 1;
}
int processOsmDataDenseNodes(struct osmdata_t *osmdata, PrimitiveGroup *group, StringTable *string_table, double lat_offset, double lon_offset, double granularity)
{
unsigned node_id;
if (group->dense) {
unsigned l = 0;
osmid_t deltaid = 0;
long int deltalat = 0;
long int deltalon = 0;
unsigned long int deltatimestamp = 0;
unsigned long int deltachangeset = 0;
long int deltauid = 0;
unsigned long int deltauser_sid = 0;
double lat, lon;
DenseNodes *dense = group->dense;
for (node_id = 0; node_id < dense->n_id; node_id++) {
resetList(&(osmdata->tags));
deltaid += dense->id[node_id];
deltalat += dense->lat[node_id];
deltalon += dense->lon[node_id];
if (dense->denseinfo && osmdata->extra_attributes) {
DenseInfo *denseinfo = dense->denseinfo;
deltatimestamp += denseinfo->timestamp[node_id];
deltachangeset += denseinfo->changeset[node_id];
deltauid += denseinfo->uid[node_id];
deltauser_sid += denseinfo->user_sid[node_id];
addIntItem(&(osmdata->tags), "osm_version", denseinfo->version[node_id], 0);
addIntItem(&(osmdata->tags), "osm_changeset", deltachangeset, 0);
if (deltauid != -1) { /* osmosis devs failed to read the specs */
char * valstr;
addIntItem(&(osmdata->tags), "osm_uid", deltauid, 0);
valstr = calloc(string_table->s[deltauser_sid].len + 1, 1);
memcpy(valstr, string_table->s[deltauser_sid].data, string_table->s[deltauser_sid].len);
addItem(&(osmdata->tags), "osm_user", valstr, 0);
free(valstr);
}
}
if (l < dense->n_keys_vals) {
while (dense->keys_vals[l] != 0 && l < dense->n_keys_vals) {
addProtobufItem(&(osmdata->tags),
string_table->s[dense->keys_vals[l]],
string_table->s[dense->keys_vals[l+1]],
0);
l += 2;
}
l += 1;
}
lat = lat_offset + (deltalat * granularity);
lon = lon_offset + (deltalon * granularity);
if (node_wanted(osmdata, lat, lon)) {
reproject(&lat, &lon);
osmdata->out->node_add(deltaid, lat, lon, &(osmdata->tags));
if (deltaid > osmdata->max_node) {
osmdata->max_node = deltaid;
}
if (osmdata->count_node == 0) {
time(&osmdata->start_node);
}
osmdata->count_node++;
if (osmdata->count_node%10000 == 0)
printStatus(osmdata);
}
}
}
return 1;
}
int processOsmDataWays(struct osmdata_t *osmdata, PrimitiveGroup *group, StringTable *string_table)
{
unsigned way_id, key_id, ref_id;
for (way_id = 0; way_id < group->n_ways; way_id++) {
Way *way = group->ways[way_id];
osmid_t deltaref = 0;
resetList(&(osmdata->tags));
if (way->info && osmdata->extra_attributes) {
addInfoItems(&(osmdata->tags), way->info, string_table);
}
osmdata->nd_count = 0;
for (ref_id = 0; ref_id < way->n_refs; ref_id++) {
deltaref += way->refs[ref_id];
osmdata->nds[osmdata->nd_count++] = deltaref;
if( osmdata->nd_count >= osmdata->nd_max )
realloc_nodes(osmdata);
}
for (key_id = 0; key_id < way->n_keys; key_id++) {
addProtobufItem(&(osmdata->tags),
string_table->s[way->keys[key_id]],
string_table->s[way->vals[key_id]],
0);
}
osmdata->out->way_add(way->id,
osmdata->nds,
osmdata->nd_count,
&(osmdata->tags) );
if (way->id > osmdata->max_way) {
osmdata->max_way = way->id;
}
if (osmdata->count_way == 0) {
time(&osmdata->start_way);
}
osmdata->count_way++;
if (osmdata->count_way%1000 == 0)
printStatus(osmdata);
}
return 1;
}
int processOsmDataRelations(struct osmdata_t *osmdata, PrimitiveGroup *group, StringTable *string_table)
{
unsigned rel_id, member_id, key_id;
for (rel_id = 0; rel_id < group->n_relations; rel_id++) {
Relation *relation = group->relations[rel_id];
osmid_t deltamemids = 0;
resetList(&(osmdata->tags));
osmdata->member_count = 0;
if (relation->info && osmdata->extra_attributes) {
addInfoItems(&(osmdata->tags), relation->info, string_table);
}
for (member_id = 0; member_id < relation->n_memids; member_id++) {
ProtobufCBinaryData role = string_table->s[relation->roles_sid[member_id]];
char *rolestr;
deltamemids += relation->memids[member_id];
osmdata->members[osmdata->member_count].id = deltamemids;
rolestr = calloc(role.len + 1, 1);
memcpy(rolestr, role.data, role.len);
osmdata->members[osmdata->member_count].role = rolestr;
switch (relation->types[member_id]) {
case RELATION__MEMBER_TYPE__NODE:
osmdata->members[osmdata->member_count].type = OSMTYPE_NODE;
break;
case RELATION__MEMBER_TYPE__WAY:
osmdata->members[osmdata->member_count].type = OSMTYPE_WAY;
break;
case RELATION__MEMBER_TYPE__RELATION:
osmdata->members[osmdata->member_count].type = OSMTYPE_RELATION;
break;
default:
fprintf(stderr, "Unsupported type: %u""\n", relation->types[member_id]);
return 0;
}
osmdata->member_count++;
if( osmdata->member_count >= osmdata->member_max ) {
realloc_members(osmdata);
}
}
for (key_id = 0; key_id < relation->n_keys; key_id++) {
addProtobufItem(&(osmdata->tags),
string_table->s[relation->keys[key_id]],
string_table->s[relation->vals[key_id]],
0);
}
osmdata->out->relation_add(relation->id,
osmdata->members,
osmdata->member_count,
&(osmdata->tags));
for (member_id = 0; member_id < osmdata->member_count; member_id++) {
free(osmdata->members[member_id].role);
}
if (relation->id > osmdata->max_rel) {
osmdata->max_rel = relation->id;
}
if (osmdata->count_rel == 0) {
time(&osmdata->start_rel);
}
osmdata->count_rel++;
if (osmdata->count_rel%10 == 0)
printStatus(osmdata);
}
return 1;
}
int processOsmData(struct osmdata_t *osmdata, void *data, size_t length)
{
unsigned int j;
double lat_offset, lon_offset, granularity;
PrimitiveBlock *pmsg = primitive_block__unpack (NULL, length, data);
if (pmsg == NULL) {
fprintf(stderr, "Error unpacking PrimitiveBlock message\n");
return 0;
}
lat_offset = NANO_DEGREE * pmsg->lat_offset;
lon_offset = NANO_DEGREE * pmsg->lon_offset;
granularity = NANO_DEGREE * pmsg->granularity;
for (j = 0; j < pmsg->n_primitivegroup; j++) {
PrimitiveGroup *group = pmsg->primitivegroup[j];
StringTable *string_table = pmsg->stringtable;
if (!processOsmDataNodes(osmdata, group, string_table, lat_offset, lon_offset, granularity)) return 0;
if (!processOsmDataDenseNodes(osmdata, group, string_table, lat_offset, lon_offset, granularity)) return 0;
if (!processOsmDataWays(osmdata, group, string_table)) return 0;
if (!processOsmDataRelations(osmdata, group, string_table)) return 0;
}
primitive_block__free_unpacked (pmsg, &protobuf_c_default_allocator);
return 1;
}
int streamFilePbf(char *filename, int sanitize UNUSED, struct osmdata_t *osmdata)
{
void *header = NULL;
void *blob = NULL;
char *data = NULL;
FILE *input = NULL;
BlockHeader *header_msg = NULL;
Blob *blob_msg = NULL;
size_t length;
int exit_status = EXIT_FAILURE;
header = malloc(MAX_BLOCK_HEADER_SIZE);
if (!header) {
fprintf(stderr, "parse-pbf: out of memory allocating header buffer\n");
goto err;
}
blob = malloc(MAX_BLOB_SIZE);
if (!blob) {
fprintf(stderr, "parse-pbf: out of memory allocating blob buffer\n");
goto err;
}
data = malloc(MAX_BLOB_SIZE);
if (!data) {
fprintf(stderr, "parse-pbf: out of memory allocating data buffer\n");
goto err;
}
input = fopen(filename, "rb");
if (!input) {
fprintf(stderr, "Unable to open %s\n", filename);
goto err;
}
do {
header_msg = read_header(input, header);
if (header_msg == NULL) {
break;
}
blob_msg = read_blob(input, blob, header_msg->datasize);
length = uncompress_blob(blob_msg, data, MAX_BLOB_SIZE);
if (!length) {
goto err;
}
if (strcmp(header_msg->type, "OSMHeader") == 0) {
if (!processOsmHeader(data, length)) {
goto err;
}
} else if (strcmp(header_msg->type, "OSMData") == 0) {
if (!processOsmData(osmdata, data, length)) {
goto err;
}
}
blob__free_unpacked (blob_msg, &protobuf_c_default_allocator);
block_header__free_unpacked (header_msg, &protobuf_c_default_allocator);
} while (!feof(input));
if (!feof(input)) {
goto err;
}
exit_status = EXIT_SUCCESS;
err:
if (input) fclose(input);
if (header) free(header);
if (blob) free(blob);
if (data) free(data);
return exit_status;
}