summaryrefslogtreecommitdiff
path: root/navit/maptool/osm_protobufdb.c
blob: 64b6012803ee11541278925492ca7edeb8b11c17 (plain)
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
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2011 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 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 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 <string.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <zlib.h>
#include "maptool.h"
#include "debug.h"
#include "linguistics.h"
#include "file.h"
#include "fileformat.pb-c.h"
#include "osmformat.pb-c.h"

static double latlon_scale=10000000.0;

struct db_config {
    int node_ids_per_file;
    int node_ids_per_blob;
    int node_ids_per_group;
    int way_ids_per_file;
    int way_ids_per_blob;
    int way_ids_per_group;
    int relation_ids_per_file;
    int relation_ids_per_blob;
    int relation_ids_per_group;
} db_config = {
    200000, 30000, 500,
    40000,  1000,  30,
    10000,   500,  20,
};

struct osm_protobufdb_context {
    int current_file, current_block, active_block;
    int in_node, in_way, in_relation;
    OSMPBF__Node n;
    OSMPBF__Way w;
    OSMPBF__Relation r;
    OSMPBF__Info i;
    FILE *f;
    OSMPBF__PrimitiveBlock *pb;
    OSMPBF__PrimitiveGroup *pg;
    GHashTable *string_hash;
    OSMPBF__DenseInfo *di;
    OSMPBF__DenseNodes *dn;
    OSMPBF__StringTable *st;
} context;

static int osm_protobufdb_write_blob(OSMPBF__Blob *blob, FILE *out) {
    unsigned char lenb[4];
    int len,blen;
    unsigned char *buffer;
    OSMPBF__BlobHeader header=OSMPBF__BLOB_HEADER__INIT;



    blen=osmpbf__blob__get_packed_size(blob);
    header.type="OSMData";
    header.datasize=blen;
    len=osmpbf__blob_header__get_packed_size(&header);
    buffer=alloca(len);
    lenb[0]=len>>24;
    lenb[1]=len>>16;
    lenb[2]=len>>8;
    lenb[3]=len;
    osmpbf__blob_header__pack(&header, buffer);
    if (fwrite(lenb, sizeof(lenb), 1, out) != 1)
        return 0;
    if (fwrite(buffer, len, 1, out) != 1)
        return 0;
    buffer=alloca(blen);
    osmpbf__blob__pack(blob, buffer);
    if (fwrite(buffer, blen, 1, out) != 1)
        return 0;
    return 1;
}

#if 0
void dump_block(OSMPBF__PrimitiveBlock *pb) {
    int i,j;
    printf("%d groups\n",pb->n_primitivegroup);
    for (i = 0 ; i < pb->n_primitivegroup ; i++) {
        printf("%d relations\n",pb->primitivegroup[i]->n_relations);
        for (j = 0 ; j < pb->primitivegroup[i]->n_relations ; j++) {
            printf("Info %d\n",pb->primitivegroup[i]->relations[j]->info->version);
        }
    }
}
#endif

static int osm_protobufdb_finish_block(struct osm_protobufdb_context *ctx) {
    OSMPBF__Blob *blob,empty_blob=OSMPBF__BLOB__INIT;
    int len;
    if (!ctx->pb)
        return 0;
    len=osmpbf__primitive_block__get_packed_size(ctx->pb);

    while  (ctx->current_block < ctx->active_block) {
        osm_protobufdb_write_blob(&empty_blob, ctx->f);
        ctx->current_block++;
    }
    blob=g_malloc(sizeof(*blob));
    *blob=empty_blob;
    blob->has_raw=1;
    blob->has_raw_size=1;
    blob->raw.data=g_malloc(len);
    osmpbf__primitive_block__pack(ctx->pb, blob->raw.data);
    blob->raw.len=len;
    blob->raw_size=len;
    osm_protobufdb_write_blob(blob, ctx->f);
    osmpbf__blob__free_unpacked(blob, NULL);
    osmpbf__primitive_block__free_unpacked(ctx->pb, NULL);
    ctx->pb=NULL;
    ctx->current_block++;
    return 1;
}

static int osm_protobufdb_start_block(struct osm_protobufdb_context *ctx, int blocknum) {
    OSMPBF__PrimitiveBlock pb=OSMPBF__PRIMITIVE_BLOCK__INIT;
    OSMPBF__StringTable st=OSMPBF__STRING_TABLE__INIT;
    if (ctx->active_block == blocknum)
        return 0;
    osm_protobufdb_finish_block(ctx);
    ctx->active_block=blocknum;
    ctx->pb=g_malloc(sizeof(*ctx->pb));
    *ctx->pb=pb;
    ctx->pb->stringtable=g_malloc(sizeof(*ctx->pb->stringtable));
    *ctx->pb->stringtable=st;
    ctx->st=ctx->pb->stringtable;
    return 1;
}

static int osm_protobufdb_start_group(struct osm_protobufdb_context *ctx, int groupnum) {
    OSMPBF__PrimitiveGroup pg=OSMPBF__PRIMITIVE_GROUP__INIT;
    if (ctx->pb->n_primitivegroup <= groupnum) {
        ctx->pb->primitivegroup=g_realloc(ctx->pb->primitivegroup, (groupnum+1)*sizeof(ctx->pb->primitivegroup[0]));
        while (ctx->pb->n_primitivegroup <= groupnum) {
            ctx->pb->primitivegroup[ctx->pb->n_primitivegroup]=g_malloc(sizeof(*context.pg));
            *ctx->pb->primitivegroup[ctx->pb->n_primitivegroup++]=pg;
        }
        g_hash_table_destroy(ctx->string_hash);
        ctx->string_hash=g_hash_table_new(g_str_hash, g_str_equal);
    }
    ctx->pg=ctx->pb->primitivegroup[groupnum];
    if (!ctx->pg) {
        ctx->pg=g_malloc(sizeof(*context.pg));
        *ctx->pg=pg;
        ctx->pb->primitivegroup[groupnum]=ctx->pg;
    }
    return 1;
}

#if 0
static int osm_protobufdb_start_densenode(struct osm_protobufdb_context *ctx) {
    OSMPBF__DenseInfo di=OSMPBF__DENSE_INFO__INIT;
    OSMPBF__DenseNodes dn=OSMPBF__DENSE_NODES__INIT;

    if (!ctx->pg->dense) {
        ctx->dn=g_malloc(sizeof(*context.dn));
        *ctx->dn=dn;
        ctx->pg->dense=ctx->dn;
    } else
        ctx->dn=ctx->pg->dense;
    if (!ctx->dn->denseinfo) {
        ctx->di=g_malloc(sizeof(*context.di));
        *ctx->di=di;
        ctx->dn->denseinfo=ctx->di;
    } else
        ctx->di=ctx->dn->denseinfo;
    return 1;
}

static void osm_protobufdb_write_primitive_group(OSMPBF__PrimitiveGroup *pg, OSMPBF__PrimitiveBlock *pb) {
    pb->primitivegroup=g_realloc(pb->primitivegroup,(pb->n_primitivegroup+1)*sizeof(OSMPBF__PrimitiveGroup *));
    pb->primitivegroup[pb->n_primitivegroup++]=pg;
}
#endif


#define insert(struct, member, pos) {\
	int n=struct->n_##member; \
	int s=sizeof(struct->member[0]); \
	struct->member=g_realloc(struct->member, (n+1)*s); \
	memmove(&struct->member[n+1], &struct->member[n], (pos-n)*s); \
	memset(&struct->member[n], 0, s); \
	struct->n_##member++;\
}

#if 0
static int osm_protobufdb_insert_densenode(long long id, OSMPBF__Node *offset, OSMPBF__Info *offseti,
        OSMPBF__DenseNodes *dn) {
    int i,l,p;
    memset(offset, 0, sizeof(*offset));
    offseti->timestamp=0;
    offseti->changeset=0;
    offseti->user_sid=0;
    offseti->uid=0;
    l=dn->n_id;
    for (i = 0 ; i < l ; i++) {
        offset->id+=dn->id[i];
        offset->lat+=dn->lat[i];
        offset->lon+=dn->lon[i];
        offseti->timestamp+=dn->denseinfo->timestamp[i];
        offseti->changeset+=dn->denseinfo->changeset[i];
        offseti->user_sid+=dn->denseinfo->user_sid[i];
        offseti->uid+=dn->denseinfo->uid[i];
    }
    p=l;
    insert(dn, id, p);
    insert(dn, lat, p);
    insert(dn, lon, p);
    insert(dn->denseinfo, version, p);
    insert(dn->denseinfo, timestamp, p);
    insert(dn->denseinfo, changeset, p);
    insert(dn->denseinfo, user_sid, p);
    insert(dn->denseinfo, uid, p);
    return p;
}

static void osm_protobufdb_modify_densenode(OSMPBF__Node *node, OSMPBF__Info *info, OSMPBF__Node *offset,
        OSMPBF__Info *offseti,
        int pos, OSMPBF__DenseNodes *dn) {
    int i;
    if (pos+1 < dn->n_id) {
        dn->id[pos+1]+=dn->id[pos]-node->id;
        dn->lat[pos+1]+=dn->lat[pos]-node->lat;
        dn->lon[pos+1]+=dn->lon[pos]-node->lon;
        dn->denseinfo->timestamp[pos+1]+=dn->denseinfo->timestamp[pos]-info->timestamp;
        dn->denseinfo->changeset[pos+1]+=dn->denseinfo->changeset[pos]-info->changeset;
        dn->denseinfo->user_sid[pos+1]+=dn->denseinfo->user_sid[pos]-info->user_sid;
        dn->denseinfo->uid[pos+1]+=dn->denseinfo->uid[pos]-info->uid;
    }
    dn->id[pos]=node->id-offset->id;
    dn->lat[pos]=node->lat-offset->lat;
    dn->lon[pos]=node->lon-offset->lon;
    dn->keys_vals=g_realloc(dn->keys_vals, (dn->n_keys_vals+node->n_keys+node->n_vals+1)*sizeof(dn->keys_vals[0]));
    for (i = 0 ; i < node->n_keys ; i++) {
        dn->keys_vals[dn->n_keys_vals++]=node->keys[i];
        dn->keys_vals[dn->n_keys_vals++]=node->vals[i];
    }
    dn->keys_vals[dn->n_keys_vals++]=0;
    dn->denseinfo->version[pos]=info->version;
    dn->denseinfo->timestamp[pos]=info->timestamp-offseti->timestamp;
    dn->denseinfo->changeset[pos]=info->changeset-offseti->changeset;
    dn->denseinfo->user_sid[pos]=info->user_sid-offseti->user_sid;
    dn->denseinfo->uid[pos]=info->uid-offseti->uid;
}
#endif

static int osm_protobufdb_insert_node(long long id, OSMPBF__PrimitiveGroup *pg) {
    int l,p;
    OSMPBF__Node node=OSMPBF__NODE__INIT;
    l=pg->n_nodes;
    p=l;
    insert(pg, nodes, p);
    pg->nodes[p]=g_malloc(sizeof(*pg->nodes[0]));
    *pg->nodes[p]=node;
    return p;
}

static void osm_protobufdb_modify_node(OSMPBF__Node *node, OSMPBF__Info *info, int pos, OSMPBF__PrimitiveGroup *pg) {
    OSMPBF__Node *n=pg->nodes[pos];
    OSMPBF__Info *old_info;

    g_free(n->keys);
    g_free(n->vals);
    old_info=n->info;
    *n=*node;
    if (!info) {
        if (old_info)
            osmpbf__info__free_unpacked(old_info, NULL);
        n->info=NULL;
    } else {
        if (old_info)
            n->info=old_info;
        else
            n->info=g_malloc(sizeof(*info));
        *n->info=*info;
    }

}

static int osm_protobufdb_insert_way(long long id, OSMPBF__PrimitiveGroup *pg) {
    int l,p;
    OSMPBF__Way way=OSMPBF__WAY__INIT;
    l=pg->n_ways;
    p=l;
    insert(pg, ways, p);
    pg->ways[p]=g_malloc(sizeof(*pg->ways[0]));
    *pg->ways[p]=way;
    return p;
}

static void osm_protobufdb_modify_way(OSMPBF__Way *way, OSMPBF__Info *info, int pos, OSMPBF__PrimitiveGroup *pg) {
    OSMPBF__Way *w=pg->ways[pos];
    OSMPBF__Info *old_info;
    int i;
    long long ref=0;

    g_free(w->keys);
    g_free(w->vals);
    g_free(w->refs);
    old_info=w->info;
    *w=*way;
    for (i = 0 ; i < w->n_refs ; i++) {
        w->refs[i]-=ref;
        ref+=w->refs[i];
    }
    if (!info) {
        if (old_info)
            osmpbf__info__free_unpacked(old_info, NULL);
        w->info=NULL;
    } else {
        if (old_info)
            w->info=old_info;
        else
            w->info=g_malloc(sizeof(*info));
        *w->info=*info;
    }

}

static int osm_protobufdb_insert_relation(long long id, OSMPBF__PrimitiveGroup *pg) {
    int l,p;
    OSMPBF__Relation relation=OSMPBF__RELATION__INIT;
    l=pg->n_relations;
    p=l;
    insert(pg, relations, p);
    pg->relations[p]=g_malloc(sizeof(*pg->relations[0]));
    *pg->relations[p]=relation;
    return p;
}

static void osm_protobufdb_modify_relation(OSMPBF__Relation *relation, OSMPBF__Info *info, int pos,
        OSMPBF__PrimitiveGroup *pg) {
    OSMPBF__Relation *r=pg->relations[pos];
    OSMPBF__Info *old_info;
    int i;
    long long ref=0;

    g_free(r->keys);
    g_free(r->vals);
    g_free(r->roles_sid);
    g_free(r->memids);
    g_free(r->types);
    old_info=r->info;
    *r=*relation;
    for (i = 0 ; i < r->n_memids ; i++) {
        r->memids[i]-=ref;
        ref+=r->memids[i];
    }
    if (!info) {
        if (old_info)
            osmpbf__info__free_unpacked(old_info, NULL);
        r->info=NULL;
    } else {
        if (old_info)
            r->info=old_info;
        else
            r->info=g_malloc(sizeof(*info));
        *r->info=*info;
    }

}

static int osm_protobufdb_string(struct osm_protobufdb_context *ctx, char *str) {
    char *strd;
    OSMPBF__StringTable *st=ctx->st;

    gpointer value;
    assert(ctx->string_hash != NULL);
    if (g_hash_table_lookup_extended(ctx->string_hash, str, NULL, &value)) {
        return (long)value;
    }
    if (!st->n_s) {
        st->n_s++;
    }
    strd=g_strdup(str);
    st->s=g_realloc(st->s, sizeof(st->s[0])*(st->n_s+1));
    if (st->n_s == 1) {
        st->s[0].data=NULL;
        st->s[0].len=0;
    }
    st->s[st->n_s].data=(unsigned char *)strd;
    st->s[st->n_s].len=strlen(strd);
    g_hash_table_insert(ctx->string_hash, strd, (gpointer)st->n_s);
    return st->n_s++;
}


static int osm_protobufdb_finish_file(struct osm_protobufdb_context *ctx) {
    osm_protobufdb_finish_block(ctx);
    if (ctx->f) {
        fclose(ctx->f);
        ctx->f=NULL;
    }
    ctx->current_file=-1;
    return 1;
}


static int osm_protobufdb_start_file(struct osm_protobufdb_context *ctx, int type, int num) {
    char name[1024];
    if (ctx->current_file == num)
        return 0;
    osm_protobufdb_finish_file(ctx);
    sprintf(name,"tst/%d-%08d",type,num);
    ctx->f=fopen(name,"w");
    ctx->current_file=num;
    ctx->current_block=0;
    ctx->active_block=-1;
    return 1;
}

static void test(void) {
    context.current_file=-1;
}

static void finish(void) {
    osm_protobufdb_finish_file(&context);
}

static long long osm_protobufdb_timestamp(char *str) {
    struct tm tm;
    int res=sscanf(str,"%d-%d-%dT%d:%d:%dZ",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
    if (res != 6)
        return 0;
    tm.tm_year-=1900;
    tm.tm_mon-=1;
#if defined(HAVE_API_WIN32_BASE) || defined(ANDROID)
    return 0;
#else
    return timegm(&tm);
#endif
}

static void osm_protobufdb_parse_info(struct osm_protobufdb_context *ctx, char *str) {
    char version[1024];
    char changeset[1024];
    char user[1024];
    char uid[1024];
    char timestamp[1024];

    OSMPBF__Info *i=&ctx->i, ii=OSMPBF__INFO__INIT;
    *i=ii;
    if (osm_xml_get_attribute(str, "version", version, sizeof(version))) {
        i->version=atoi(version);
        i->has_version=1;
    }
    if (osm_xml_get_attribute(str, "changeset", changeset, sizeof(changeset))) {
        i->changeset=atoll(changeset);
        i->has_changeset=1;
    }
    if (osm_xml_get_attribute(str, "user", user, sizeof(user))) {
        osm_xml_decode_entities(user);
        i->user_sid=osm_protobufdb_string(ctx, user);
        i->has_user_sid=1;
    }
    if (osm_xml_get_attribute(str, "uid", uid, sizeof(uid))) {
        i->uid=atoi(uid);
        i->has_uid=1;
    }
    if (osm_xml_get_attribute(str, "timestamp", timestamp, sizeof(timestamp))) {
        i->timestamp=osm_protobufdb_timestamp(timestamp);
        i->has_timestamp=1;
    }
}

static int osm_protobufdb_parse_node(struct osm_protobufdb_context *ctx, char *str) {
    char id[1024];
    char lat[1024];
    char lon[1024];
    OSMPBF__Node *n=&ctx->n, ni=OSMPBF__NODE__INIT;

    *n=ni;
    if (!osm_xml_get_attribute(str, "id", id, sizeof(id)))
        return 0;
    if (!osm_xml_get_attribute(str, "lat", lat, sizeof(lat)))
        return 0;
    if (!osm_xml_get_attribute(str, "lon", lon, sizeof(lon)))
        return 0;
    n->id=atoll(id);
    n->lat=atof(lat)*latlon_scale+0.5;
    n->lon=atof(lon)*latlon_scale+0.5;
    int file=n->id/db_config.node_ids_per_file;
    int fileo=n->id%db_config.node_ids_per_file;
    int blob=fileo/db_config.node_ids_per_blob;
    int blobo=fileo%db_config.node_ids_per_blob;
    int group=blobo/db_config.node_ids_per_group;

    osm_protobufdb_start_file(ctx, 1, file);
    osm_protobufdb_start_block(ctx, blob);
    osm_protobufdb_start_group(ctx, group);
    osm_protobufdb_parse_info(ctx, str);
    ctx->in_node=1;
    return 1;
}

static int osm_protobufdb_end_node(struct osm_protobufdb_context *ctx) {
    int p;
    p=osm_protobufdb_insert_node(ctx->n.id, ctx->pg);
    osm_protobufdb_modify_node(&ctx->n, &ctx->i, p, ctx->pg);
    ctx->in_node=0;
    return 1;
}


static int osm_protobufdb_parse_way(struct osm_protobufdb_context *ctx, char *str) {
    char id[1024];
    OSMPBF__Way *w=&ctx->w, wi=OSMPBF__WAY__INIT;

    *w=wi;
    if (!osm_xml_get_attribute(str, "id", id, sizeof(id)))
        return 0;
    w->id=atoll(id);
    int file=w->id/db_config.way_ids_per_file;
    int fileo=w->id%db_config.way_ids_per_file;
    int blob=fileo/db_config.way_ids_per_blob;
    int blobo=fileo%db_config.way_ids_per_blob;
    int group=blobo/db_config.way_ids_per_group;

    osm_protobufdb_start_file(ctx, 2, file);
    osm_protobufdb_start_block(ctx, blob);
    osm_protobufdb_start_group(ctx, group);
    osm_protobufdb_parse_info(ctx, str);
    ctx->in_way=1;
    return 1;
}

static int osm_protobufdb_end_way(struct osm_protobufdb_context *ctx) {
    int p;
    p=osm_protobufdb_insert_way(ctx->w.id, ctx->pg);
    osm_protobufdb_modify_way(&ctx->w, &ctx->i, p, ctx->pg);
    ctx->in_way=0;
    return 1;
}

static int osm_protobufdb_parse_relation(struct osm_protobufdb_context *ctx, char *str) {
    char id[1024];
    OSMPBF__Relation *r=&ctx->r, ri=OSMPBF__RELATION__INIT;

    *r=ri;
    if (!osm_xml_get_attribute(str, "id", id, sizeof(id)))
        return 0;
    r->id=atoll(id);
    int file=r->id/db_config.relation_ids_per_file;
    int fileo=r->id%db_config.relation_ids_per_file;
    int blob=fileo/db_config.relation_ids_per_blob;
    int blobo=fileo%db_config.relation_ids_per_blob;
    int group=blobo/db_config.relation_ids_per_group;

    osm_protobufdb_start_file(ctx, 3, file);
    osm_protobufdb_start_block(ctx, blob);
    osm_protobufdb_start_group(ctx, group);
    osm_protobufdb_parse_info(ctx, str);
    ctx->in_relation=1;
    return 1;
}

static int osm_protobufdb_end_relation(struct osm_protobufdb_context *ctx) {
    int p;
    p=osm_protobufdb_insert_relation(ctx->r.id, ctx->pg);
    osm_protobufdb_modify_relation(&ctx->r, &ctx->i, p, ctx->pg);
    ctx->in_node=0;
    return 1;
}

static int osm_protobufdb_parse_tag(struct osm_protobufdb_context *ctx, char *str) {
    OSMPBF__Node *n=&ctx->n;
    OSMPBF__Way *w=&ctx->w;
    OSMPBF__Relation *r=&ctx->r;
    char k_buffer[BUFFER_SIZE];
    char v_buffer[BUFFER_SIZE];
    if (!osm_xml_get_attribute(str, "k", k_buffer, BUFFER_SIZE))
        return 0;
    if (!osm_xml_get_attribute(str, "v", v_buffer, BUFFER_SIZE))
        return 0;
    osm_xml_decode_entities(v_buffer);
    if (ctx->in_node) {
        n->keys=g_realloc(n->keys, (n->n_keys+1)*sizeof(n->keys[0]));
        n->vals=g_realloc(n->vals, (n->n_vals+1)*sizeof(n->vals[0]));
        n->keys[n->n_keys++]=osm_protobufdb_string(ctx, k_buffer);
        n->vals[n->n_vals++]=osm_protobufdb_string(ctx, v_buffer);
    }
    if (ctx->in_way) {
        w->keys=g_realloc(w->keys, (w->n_keys+1)*sizeof(w->keys[0]));
        w->vals=g_realloc(w->vals, (w->n_vals+1)*sizeof(w->vals[0]));
        w->keys[w->n_keys++]=osm_protobufdb_string(ctx, k_buffer);
        w->vals[w->n_vals++]=osm_protobufdb_string(ctx, v_buffer);
    }
    if (ctx->in_relation) {
        r->keys=g_realloc(r->keys, (r->n_keys+1)*sizeof(r->keys[0]));
        r->vals=g_realloc(r->vals, (r->n_vals+1)*sizeof(r->vals[0]));
        r->keys[r->n_keys++]=osm_protobufdb_string(ctx, k_buffer);
        r->vals[r->n_vals++]=osm_protobufdb_string(ctx, v_buffer);
    }
    return 1;
}

static int osm_protobufdb_parse_nd(struct osm_protobufdb_context *ctx, char *str) {
    OSMPBF__Way *w=&ctx->w;
    char ref_buffer[BUFFER_SIZE];
    if (!osm_xml_get_attribute(str, "ref", ref_buffer, BUFFER_SIZE))
        return 0;
    if (ctx->in_way) {
        w->refs=g_realloc(w->refs, (w->n_refs+1)*sizeof(w->refs[0]));
        w->refs[w->n_refs++]=atoll(ref_buffer);
    }
    return 1;
}

static int osm_protobufdb_parse_member(struct osm_protobufdb_context *ctx, char *str) {
    OSMPBF__Relation *r=&ctx->r;
    char type_buffer[BUFFER_SIZE];
    char ref_buffer[BUFFER_SIZE];
    char role_buffer[BUFFER_SIZE];
    int type=0;
    if (!osm_xml_get_attribute(str, "type", type_buffer, BUFFER_SIZE))
        return 0;
    if (!osm_xml_get_attribute(str, "ref", ref_buffer, BUFFER_SIZE))
        return 0;
    if (!osm_xml_get_attribute(str, "role", role_buffer, BUFFER_SIZE))
        return 0;
    if (!g_strcmp0(type_buffer,"node"))
        type=0;
    else if (!g_strcmp0(type_buffer,"way"))
        type=1;
    else if (!g_strcmp0(type_buffer,"relation"))
        type=2;
    if (ctx->in_relation) {
        r->roles_sid=g_realloc(r->roles_sid, (r->n_roles_sid+1)*sizeof(r->roles_sid[0]));
        r->roles_sid[r->n_roles_sid++]=osm_protobufdb_string(ctx, role_buffer);
        r->memids=g_realloc(r->memids, (r->n_memids+1)*sizeof(r->memids[0]));
        r->memids[r->n_memids++]=atoll(ref_buffer);
        r->types=g_realloc(r->types, (r->n_types+1)*sizeof(r->types[0]));
        r->types[r->n_types++]=type;
    }
    return 1;
}



int osm_protobufdb_load(FILE *in, char *dir) {
    int size=BUFFER_SIZE;
    char buffer[size];
    char *p;
    sig_alrm(0);
    test();
    while (fgets(buffer, size, in)) {
        int closed=strstr(buffer,"/>")?1:0;
        p=strchr(buffer,'<');
        if (! p) {
            fprintf(stderr,"WARNING: wrong line %s\n", buffer);
            continue;
        }
        if (!strncmp(p, "<?xml ",6)) {
        } else if (!strncmp(p, "<osm ",5)) {
        } else if (!strncmp(p, "<bound ",7)) {
        } else if (!strncmp(p, "<node ",6)) {
            if (!osm_protobufdb_parse_node(&context, p))
                fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
            if (closed)
                osm_protobufdb_end_node(&context);
            processed_nodes++;
        } else if (!strncmp(p, "<tag ",5)) {
            if (!osm_protobufdb_parse_tag(&context, p))
                fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
        } else if (!strncmp(p, "<way ",5)) {
            if (!osm_protobufdb_parse_way(&context, p))
                fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
            if (closed)
                osm_protobufdb_end_way(&context);
            processed_ways++;
        } else if (!strncmp(p, "<nd ",4)) {
            if (!osm_protobufdb_parse_nd(&context, p))
                fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
        } else if (!strncmp(p, "<relation ",10)) {
            if (!osm_protobufdb_parse_relation(&context, p))
                fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
            processed_relations++;
        } else if (!strncmp(p, "<member ",8)) {
            if (!osm_protobufdb_parse_member(&context, p))
                fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
        } else if (!strncmp(p, "</node>",7)) {
            osm_protobufdb_end_node(&context);
        } else if (!strncmp(p, "</way>",6)) {
            osm_protobufdb_end_way(&context);
        } else if (!strncmp(p, "</relation>",11)) {
            osm_protobufdb_end_relation(&context);
        } else if (!strncmp(p, "</osm>",6)) {
        } else {
            fprintf(stderr,"WARNING: unknown tag in %s\n", buffer);
        }
    }
    finish();
    return 1;
}