pbf_parser 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/pbf_parser/extconf.rb +2 -2
- data/ext/pbf_parser/fileformat.pb-c.c +89 -88
- data/ext/pbf_parser/fileformat.pb-c.h +53 -46
- data/ext/pbf_parser/osmformat.pb-c.c +584 -583
- data/ext/pbf_parser/osmformat.pb-c.h +272 -264
- data/ext/pbf_parser/pbf_parser.c +33 -33
- data/lib/pbf_parser/version.rb +3 -0
- data/lib/pbf_parser.rb +1 -0
- metadata +4 -2
data/ext/pbf_parser/pbf_parser.c
CHANGED
@@ -33,11 +33,11 @@ static char *parse_binary_str(ProtobufCBinaryData bstr)
|
|
33
33
|
return str;
|
34
34
|
}
|
35
35
|
|
36
|
-
static
|
36
|
+
static OSMPBF__BlobHeader *read_blob_header(FILE *input)
|
37
37
|
{
|
38
38
|
void *buffer;
|
39
39
|
size_t length = get_header_size(input);
|
40
|
-
|
40
|
+
OSMPBF__BlobHeader *header = NULL;
|
41
41
|
|
42
42
|
if(length < 1 || length > MAX_BLOB_HEADER_SIZE)
|
43
43
|
{
|
@@ -56,7 +56,7 @@ static BlobHeader *read_blob_header(FILE *input)
|
|
56
56
|
rb_raise(rb_eIOError, "Unable to read the blob header");
|
57
57
|
}
|
58
58
|
|
59
|
-
header =
|
59
|
+
header = osmpbf__blob_header__unpack(NULL, length, buffer);
|
60
60
|
|
61
61
|
free(buffer);
|
62
62
|
|
@@ -70,7 +70,7 @@ static void *read_blob(FILE *input, size_t length, size_t *raw_length)
|
|
70
70
|
{
|
71
71
|
VALUE exc = Qnil;
|
72
72
|
void *buffer = NULL;
|
73
|
-
|
73
|
+
OSMPBF__Blob *blob = NULL;
|
74
74
|
|
75
75
|
if(length < 1 || length > MAX_BLOB_SIZE)
|
76
76
|
rb_raise(rb_eIOError, "Invalid blob size");
|
@@ -79,7 +79,7 @@ static void *read_blob(FILE *input, size_t length, size_t *raw_length)
|
|
79
79
|
rb_raise(rb_eNoMemError, "Unable to allocate memory for the blob");
|
80
80
|
|
81
81
|
if(fread(buffer, length, 1, input))
|
82
|
-
blob =
|
82
|
+
blob = osmpbf__blob__unpack(NULL, length, buffer);
|
83
83
|
|
84
84
|
free(buffer);
|
85
85
|
|
@@ -150,7 +150,7 @@ static void *read_blob(FILE *input, size_t length, size_t *raw_length)
|
|
150
150
|
}
|
151
151
|
|
152
152
|
exit_nicely:
|
153
|
-
if(blob)
|
153
|
+
if(blob) osmpbf__blob__free_unpacked(blob, NULL);
|
154
154
|
if(!data) free(data);
|
155
155
|
if(exc != Qnil) rb_exc_raise(exc);
|
156
156
|
|
@@ -168,7 +168,7 @@ static VALUE init_data_arr()
|
|
168
168
|
return data;
|
169
169
|
}
|
170
170
|
|
171
|
-
static void add_info(VALUE hash,
|
171
|
+
static void add_info(VALUE hash, OSMPBF__Info *info, OSMPBF__StringTable *string_table, double ts_granularity)
|
172
172
|
{
|
173
173
|
VALUE version, timestamp, changeset, uid, user;
|
174
174
|
|
@@ -195,7 +195,7 @@ static void add_info(VALUE hash, Info *info, StringTable *string_table, double t
|
|
195
195
|
|
196
196
|
static int parse_osm_header(VALUE obj, FILE *input)
|
197
197
|
{
|
198
|
-
|
198
|
+
OSMPBF__BlobHeader *header = read_blob_header(input);
|
199
199
|
|
200
200
|
// EOF reached
|
201
201
|
if(header == NULL)
|
@@ -206,12 +206,12 @@ static int parse_osm_header(VALUE obj, FILE *input)
|
|
206
206
|
|
207
207
|
void *blob = NULL;
|
208
208
|
size_t blob_length = 0, datasize = header->datasize;
|
209
|
-
|
209
|
+
OSMPBF__HeaderBlock *header_block = NULL;
|
210
210
|
|
211
|
-
|
211
|
+
osmpbf__blob_header__free_unpacked(header, NULL);
|
212
212
|
|
213
213
|
blob = read_blob(input, datasize, &blob_length);
|
214
|
-
header_block =
|
214
|
+
header_block = osmpbf__header_block__unpack(NULL, blob_length, blob);
|
215
215
|
|
216
216
|
free(blob);
|
217
217
|
|
@@ -282,12 +282,12 @@ static int parse_osm_header(VALUE obj, FILE *input)
|
|
282
282
|
|
283
283
|
rb_iv_set(obj, "@header", header_hash);
|
284
284
|
|
285
|
-
|
285
|
+
osmpbf__header_block__free_unpacked(header_block, NULL);
|
286
286
|
|
287
287
|
return 1;
|
288
288
|
}
|
289
289
|
|
290
|
-
static void process_nodes(VALUE out,
|
290
|
+
static void process_nodes(VALUE out, OSMPBF__PrimitiveGroup *group, OSMPBF__StringTable *string_table, int64_t lat_offset, int64_t lon_offset, int64_t granularity, int32_t ts_granularity)
|
291
291
|
{
|
292
292
|
double lat = 0;
|
293
293
|
double lon = 0;
|
@@ -296,7 +296,7 @@ static void process_nodes(VALUE out, PrimitiveGroup *group, StringTable *string_
|
|
296
296
|
|
297
297
|
for(i = 0; i < group->n_nodes; i++)
|
298
298
|
{
|
299
|
-
|
299
|
+
OSMPBF__Node *node = group->nodes[i];
|
300
300
|
VALUE node_out = rb_hash_new();
|
301
301
|
|
302
302
|
lat = NANO_DEGREE * (lat_offset + (node->lat * granularity));
|
@@ -327,7 +327,7 @@ static void process_nodes(VALUE out, PrimitiveGroup *group, StringTable *string_
|
|
327
327
|
}
|
328
328
|
}
|
329
329
|
|
330
|
-
static void process_dense_nodes(VALUE out,
|
330
|
+
static void process_dense_nodes(VALUE out, OSMPBF__DenseNodes *dense_nodes, OSMPBF__StringTable *string_table, int64_t lat_offset, int64_t lon_offset, int64_t granularity, int32_t ts_granularity)
|
331
331
|
{
|
332
332
|
uint64_t node_id = 0;
|
333
333
|
int64_t delta_lat = 0;
|
@@ -366,7 +366,7 @@ static void process_dense_nodes(VALUE out, DenseNodes *dense_nodes, StringTable
|
|
366
366
|
delta_user_sid += dense_nodes->denseinfo->user_sid[i];
|
367
367
|
delta_uid += dense_nodes->denseinfo->uid[i];
|
368
368
|
|
369
|
-
|
369
|
+
OSMPBF__Info info = {
|
370
370
|
.version = dense_nodes->denseinfo->version[i],
|
371
371
|
.timestamp = delta_timestamp,
|
372
372
|
.changeset = delta_changeset,
|
@@ -402,14 +402,14 @@ static void process_dense_nodes(VALUE out, DenseNodes *dense_nodes, StringTable
|
|
402
402
|
}
|
403
403
|
}
|
404
404
|
|
405
|
-
static void process_ways(VALUE out,
|
405
|
+
static void process_ways(VALUE out, OSMPBF__PrimitiveGroup *group, OSMPBF__StringTable *string_table, int32_t ts_granularity)
|
406
406
|
{
|
407
407
|
unsigned j, k;
|
408
408
|
size_t i = 0;
|
409
409
|
|
410
410
|
for(i = 0; i < group->n_ways; i++)
|
411
411
|
{
|
412
|
-
|
412
|
+
OSMPBF__Way *way = group->ways[i];
|
413
413
|
int64_t delta_refs = 0;
|
414
414
|
|
415
415
|
VALUE way_out = rb_hash_new();
|
@@ -449,14 +449,14 @@ static void process_ways(VALUE out, PrimitiveGroup *group, StringTable *string_t
|
|
449
449
|
}
|
450
450
|
}
|
451
451
|
|
452
|
-
static void process_relations(VALUE out,
|
452
|
+
static void process_relations(VALUE out, OSMPBF__PrimitiveGroup *group, OSMPBF__StringTable *string_table, int32_t ts_granularity)
|
453
453
|
{
|
454
454
|
unsigned j, k;
|
455
455
|
size_t i = 0;
|
456
456
|
|
457
457
|
for(i = 0; i < group->n_relations; i++)
|
458
458
|
{
|
459
|
-
|
459
|
+
OSMPBF__Relation *relation = group->relations[i];
|
460
460
|
VALUE relation_out = rb_hash_new();
|
461
461
|
|
462
462
|
rb_hash_aset(relation_out, STR2SYM("id"), LL2NUM(relation->id));
|
@@ -501,13 +501,13 @@ static void process_relations(VALUE out, PrimitiveGroup *group, StringTable *str
|
|
501
501
|
|
502
502
|
switch(relation->types[k])
|
503
503
|
{
|
504
|
-
case
|
504
|
+
case OSMPBF__RELATION__MEMBER_TYPE__NODE:
|
505
505
|
rb_ary_push(nodes, member);
|
506
506
|
break;
|
507
|
-
case
|
507
|
+
case OSMPBF__RELATION__MEMBER_TYPE__WAY:
|
508
508
|
rb_ary_push(ways, member);
|
509
509
|
break;
|
510
|
-
case
|
510
|
+
case OSMPBF__RELATION__MEMBER_TYPE__RELATION:
|
511
511
|
rb_ary_push(relations, member);
|
512
512
|
break;
|
513
513
|
}
|
@@ -530,7 +530,7 @@ static void process_relations(VALUE out, PrimitiveGroup *group, StringTable *str
|
|
530
530
|
static VALUE parse_osm_data(VALUE obj)
|
531
531
|
{
|
532
532
|
FILE *input = DATA_PTR(obj);
|
533
|
-
|
533
|
+
OSMPBF__BlobHeader *header = read_blob_header(input);
|
534
534
|
|
535
535
|
if(header == NULL)
|
536
536
|
return Qfalse;
|
@@ -540,12 +540,12 @@ static VALUE parse_osm_data(VALUE obj)
|
|
540
540
|
|
541
541
|
void *blob = NULL;
|
542
542
|
size_t blob_length = 0, datasize = header->datasize;
|
543
|
-
|
543
|
+
OSMPBF__PrimitiveBlock *primitive_block = NULL;
|
544
544
|
|
545
|
-
|
545
|
+
osmpbf__blob_header__free_unpacked(header, NULL);
|
546
546
|
|
547
547
|
blob = read_blob(input, datasize, &blob_length);
|
548
|
-
primitive_block =
|
548
|
+
primitive_block = osmpbf__primitive_block__unpack(NULL, blob_length, blob);
|
549
549
|
|
550
550
|
free(blob);
|
551
551
|
|
@@ -560,7 +560,7 @@ static VALUE parse_osm_data(VALUE obj)
|
|
560
560
|
granularity = primitive_block->granularity;
|
561
561
|
ts_granularity = primitive_block->date_granularity;
|
562
562
|
|
563
|
-
|
563
|
+
OSMPBF__StringTable *string_table = primitive_block->stringtable;
|
564
564
|
|
565
565
|
VALUE data = init_data_arr();
|
566
566
|
VALUE nodes = rb_hash_aref(data, STR2SYM("nodes"));
|
@@ -571,7 +571,7 @@ static VALUE parse_osm_data(VALUE obj)
|
|
571
571
|
|
572
572
|
for(i = 0; i < primitive_block->n_primitivegroup; i++)
|
573
573
|
{
|
574
|
-
|
574
|
+
OSMPBF__PrimitiveGroup *primitive_group = primitive_block->primitivegroup[i];
|
575
575
|
|
576
576
|
if(primitive_group->nodes)
|
577
577
|
process_nodes(nodes, primitive_group, string_table, lat_offset, lon_offset, granularity, ts_granularity);
|
@@ -588,7 +588,7 @@ static VALUE parse_osm_data(VALUE obj)
|
|
588
588
|
|
589
589
|
rb_iv_set(obj, "@data", data);
|
590
590
|
|
591
|
-
|
591
|
+
osmpbf__primitive_block__free_unpacked(primitive_block, NULL);
|
592
592
|
|
593
593
|
// Increment position
|
594
594
|
rb_iv_set(obj, "@pos", INT2NUM(NUM2INT(rb_iv_get(obj, "@pos")) + 1));
|
@@ -606,7 +606,7 @@ static VALUE find_all_blobs(VALUE obj)
|
|
606
606
|
rb_raise(rb_eIOError, "Unable to seek to beginning of file");
|
607
607
|
}
|
608
608
|
|
609
|
-
|
609
|
+
OSMPBF__BlobHeader *header;
|
610
610
|
|
611
611
|
VALUE blobs = rb_ary_new();
|
612
612
|
rb_iv_set(obj, "@blobs", blobs);
|
@@ -638,7 +638,7 @@ static VALUE find_all_blobs(VALUE obj)
|
|
638
638
|
rb_ary_push(blobs, blob_info);
|
639
639
|
}
|
640
640
|
|
641
|
-
|
641
|
+
osmpbf__blob_header__free_unpacked(header, NULL);
|
642
642
|
|
643
643
|
if (0 != fseek(input, datasize, SEEK_CUR)) {
|
644
644
|
break; // cut losses
|
@@ -731,7 +731,7 @@ static VALUE seek_to_osm_data(VALUE obj, VALUE index)
|
|
731
731
|
if (0 != fseek(input, pos, SEEK_SET)) {
|
732
732
|
rb_raise(rb_eIOError, "Unable to seek to file position");
|
733
733
|
}
|
734
|
-
|
734
|
+
|
735
735
|
// Set position - incremented by parse_osm_data
|
736
736
|
rb_iv_set(obj, "@pos", INT2NUM(index_raw - 1));
|
737
737
|
|
data/lib/pbf_parser.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'pbf_parser/pbf_parser'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pbf_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrià Planas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,6 +53,8 @@ files:
|
|
53
53
|
- ext/pbf_parser/osmformat.pb-c.h
|
54
54
|
- ext/pbf_parser/pbf_parser.c
|
55
55
|
- ext/pbf_parser/pbf_parser.h
|
56
|
+
- lib/pbf_parser.rb
|
57
|
+
- lib/pbf_parser/version.rb
|
56
58
|
homepage: https://github.com/planas/pbf_parser
|
57
59
|
licenses:
|
58
60
|
- MIT
|