Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/osmpbfparse.c
Parent Directory
|
Revision Log
Revision 2170 -
(hide annotations)
(download)
(as text)
Thu Jul 13 18:14:49 2023 UTC (20 months ago) by amb
File MIME type: text/x-csrc
File size: 30989 byte(s)
Thu Jul 13 18:14:49 2023 UTC (20 months ago) by amb
File MIME type: text/x-csrc
File size: 30989 byte(s)
Refactor the OSM file parsing to move code from individual file format specific parsers to the shared parser back-end.
1 | amb | 1221 | /*************************************** |
2 | A simple osm-specific PBF parser where the structure is hard-coded. | ||
3 | |||
4 | Part of the Routino routing software. | ||
5 | ******************/ /****************** | ||
6 | amb | 2170 | This file Copyright 2012-2015, 2023 Andrew M. Bishop |
7 | amb | 1221 | |
8 | This program is free software: you can redistribute it and/or modify | ||
9 | it under the terms of the GNU Affero General Public License as published by | ||
10 | the Free Software Foundation, either version 3 of the License, or | ||
11 | (at your option) any later version. | ||
12 | |||
13 | This program is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | GNU Affero General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Affero General Public License | ||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | ***************************************/ | ||
21 | |||
22 | |||
23 | #include <stdio.h> | ||
24 | amb | 1680 | |
25 | #if defined(_MSC_VER) | ||
26 | #include <io.h> | ||
27 | amb | 1726 | #include <basetsd.h> |
28 | amb | 1680 | #define read(fd,address,length) _read(fd,address,(unsigned int)(length)) |
29 | amb | 1726 | #define ssize_t SSIZE_T |
30 | amb | 1680 | #else |
31 | amb | 1221 | #include <unistd.h> |
32 | amb | 1680 | #endif |
33 | |||
34 | amb | 1221 | #include <stdlib.h> |
35 | #include <stdint.h> | ||
36 | #include <string.h> | ||
37 | |||
38 | #if defined(USE_GZIP) && USE_GZIP | ||
39 | #include <zlib.h> | ||
40 | #endif | ||
41 | |||
42 | #include "osmparser.h" | ||
43 | #include "tagging.h" | ||
44 | #include "logging.h" | ||
45 | |||
46 | |||
47 | /* Inside a BlobHeader message */ | ||
48 | |||
49 | #define PBF_VAL_BLOBHEADER_TYPE 1 | ||
50 | #define PBF_VAL_BLOBHEADER_SIZE 3 | ||
51 | |||
52 | /* Inside a Blob message */ | ||
53 | |||
54 | #define PBF_VAL_BLOB_RAW_DATA 1 | ||
55 | #define PBF_VAL_BLOB_RAW_SIZE 2 | ||
56 | #define PBF_VAL_BLOB_ZLIB_DATA 3 | ||
57 | |||
58 | /* Inside a HeaderBlock message */ | ||
59 | |||
60 | #define PBF_VAL_REQUIRED_FEATURES 4 | ||
61 | #define PBF_VAL_OPTIONAL_FEATURES 5 | ||
62 | |||
63 | /* Inside a PrimitiveBlock message */ | ||
64 | |||
65 | #define PBF_VAL_STRING_TABLE 1 | ||
66 | #define PBF_VAL_PRIMITIVE_GROUP 2 | ||
67 | #define PBF_VAL_GRANULARITY 17 | ||
68 | #define PBF_VAL_LAT_OFFSET 19 | ||
69 | #define PBF_VAL_LON_OFFSET 20 | ||
70 | |||
71 | /* Inside a PrimitiveGroup message */ | ||
72 | |||
73 | #define PBF_VAL_NODES 1 | ||
74 | #define PBF_VAL_DENSE_NODES 2 | ||
75 | #define PBF_VAL_WAYS 3 | ||
76 | #define PBF_VAL_RELATIONS 4 | ||
77 | |||
78 | /* Inside a StringTable message */ | ||
79 | |||
80 | #define PBF_VAL_STRING 1 | ||
81 | |||
82 | /* Inside a Node message */ | ||
83 | |||
84 | #define PBF_VAL_NODE_ID 1 | ||
85 | #define PBF_VAL_NODE_KEYS 2 | ||
86 | #define PBF_VAL_NODE_VALS 3 | ||
87 | #define PBF_VAL_NODE_LAT 8 | ||
88 | #define PBF_VAL_NODE_LON 9 | ||
89 | |||
90 | /* Inside a DenseNode message */ | ||
91 | |||
92 | #define PBF_VAL_DENSE_NODE_ID 1 | ||
93 | #define PBF_VAL_DENSE_NODE_LAT 8 | ||
94 | #define PBF_VAL_DENSE_NODE_LON 9 | ||
95 | #define PBF_VAL_DENSE_NODE_KEYS_VALS 10 | ||
96 | |||
97 | /* Inside a Way message */ | ||
98 | |||
99 | #define PBF_VAL_WAY_ID 1 | ||
100 | #define PBF_VAL_WAY_KEYS 2 | ||
101 | #define PBF_VAL_WAY_VALS 3 | ||
102 | #define PBF_VAL_WAY_REFS 8 | ||
103 | |||
104 | /* Inside a Relation message */ | ||
105 | |||
106 | #define PBF_VAL_RELATION_ID 1 | ||
107 | #define PBF_VAL_RELATION_KEYS 2 | ||
108 | #define PBF_VAL_RELATION_VALS 3 | ||
109 | #define PBF_VAL_RELATION_ROLES 8 | ||
110 | #define PBF_VAL_RELATION_MEMIDS 9 | ||
111 | #define PBF_VAL_RELATION_TYPES 10 | ||
112 | |||
113 | /* Errors */ | ||
114 | |||
115 | #define PBF_EOF 0 | ||
116 | |||
117 | #define PBF_ERROR_UNEXP_EOF 100 | ||
118 | #define PBF_ERROR_BLOB_HEADER_LEN 101 | ||
119 | #define PBF_ERROR_BLOB_LEN 102 | ||
120 | #define PBF_ERROR_NOT_OSM 103 | ||
121 | #define PBF_ERROR_BLOB_BOTH 104 | ||
122 | #define PBF_ERROR_BLOB_NEITHER 105 | ||
123 | #define PBF_ERROR_NO_GZIP 106 | ||
124 | #define PBF_ERROR_GZIP_INIT 107 | ||
125 | #define PBF_ERROR_GZIP_INFLATE 108 | ||
126 | #define PBF_ERROR_GZIP_WRONG_LEN 109 | ||
127 | #define PBF_ERROR_GZIP_END 110 | ||
128 | #define PBF_ERROR_UNSUPPORTED 111 | ||
129 | #define PBF_ERROR_TOO_MANY_GROUPS 112 | ||
130 | |||
131 | |||
132 | amb | 1784 | /* Local parsing variables (re-initialised for each file) */ |
133 | amb | 1221 | |
134 | amb | 1784 | static uint64_t byteno; |
135 | static uint64_t nnodes,nways,nrelations; | ||
136 | amb | 1221 | |
137 | amb | 1301 | static uint32_t buffer_allocated,zbuffer_allocated; |
138 | amb | 1221 | static unsigned char *buffer=NULL,*zbuffer=NULL; |
139 | static unsigned char *buffer_ptr,*buffer_end; | ||
140 | |||
141 | static int string_table_length=0,string_table_allocated=0; | ||
142 | static unsigned char **string_table=NULL; | ||
143 | static uint32_t *string_table_string_lengths=NULL; | ||
144 | |||
145 | static int32_t granularity=100; | ||
146 | static int64_t lat_offset=0,lon_offset=0; | ||
147 | |||
148 | #define LENGTH_32M (32*1024*1024) | ||
149 | |||
150 | |||
151 | /*++++++++++++++++++++++++++++++++++++++ | ||
152 | Refill the data buffer and set the pointers. | ||
153 | |||
154 | int buffer_refill Return 0 if everything is OK or 1 for EOF. | ||
155 | |||
156 | int fd The file descriptor to read from. | ||
157 | |||
158 | uint32_t bytes The number of bytes to read. | ||
159 | ++++++++++++++++++++++++++++++++++++++*/ | ||
160 | |||
161 | static inline int buffer_refill(int fd,uint32_t bytes) | ||
162 | { | ||
163 | ssize_t n; | ||
164 | |||
165 | if(bytes>buffer_allocated) | ||
166 | buffer=(unsigned char *)realloc(buffer,buffer_allocated=bytes); | ||
167 | |||
168 | amb | 1230 | byteno+=bytes; |
169 | |||
170 | buffer_ptr=buffer; | ||
171 | amb | 1221 | buffer_end=buffer; |
172 | |||
173 | do | ||
174 | { | ||
175 | n=read(fd,buffer_end,bytes); | ||
176 | |||
177 | if(n<=0) | ||
178 | return(1); | ||
179 | |||
180 | buffer_end+=n; | ||
181 | bytes-=n; | ||
182 | } | ||
183 | while(bytes>0); | ||
184 | |||
185 | return(0); | ||
186 | } | ||
187 | |||
188 | #if defined(USE_GZIP) && USE_GZIP | ||
189 | static int uncompress_pbf(unsigned char *data,uint32_t compressed,uint32_t uncompressed); | ||
190 | #endif /* USE_GZIP */ | ||
191 | |||
192 | static void process_string_table(unsigned char *data,uint32_t length); | ||
193 | static void process_primitive_group(unsigned char *data,uint32_t length); | ||
194 | static void process_nodes(unsigned char *data,uint32_t length); | ||
195 | static void process_dense_nodes(unsigned char *data,uint32_t length); | ||
196 | static void process_ways(unsigned char *data,uint32_t length); | ||
197 | static void process_relations(unsigned char *data,uint32_t length); | ||
198 | |||
199 | |||
200 | /* Macros to simplify the parser (and make it look more like the XML parser) */ | ||
201 | |||
202 | #define BEGIN(xx) do{ state=(xx); goto finish_parsing; } while(0) | ||
203 | |||
204 | #define BUFFER_CHARS_EOF(xx) do{ if(buffer_refill(fd,(xx))) BEGIN(PBF_EOF); } while(0) | ||
205 | |||
206 | #define BUFFER_CHARS(xx) do{ if(buffer_refill(fd,(xx))) BEGIN(PBF_ERROR_UNEXP_EOF); } while(0) | ||
207 | |||
208 | |||
209 | /* PBF decoding */ | ||
210 | |||
211 | #define PBF_FIELD(xx) (int)(((xx)&0xFFF8)>>3) | ||
212 | #define PBF_TYPE(xx) (int)((xx)&0x0007) | ||
213 | |||
214 | #define PBF_LATITUDE(xx) (double)(1E-9*(granularity*(xx)+lat_offset)) | ||
215 | #define PBF_LONGITUDE(xx) (double)(1E-9*(granularity*(xx)+lon_offset)) | ||
216 | |||
217 | |||
218 | /*++++++++++++++++++++++++++++++++++++++ | ||
219 | Parse a PBF int32 data value. | ||
220 | |||
221 | uint32_t pbf_int32 Returns the integer value. | ||
222 | |||
223 | unsigned char **ptr The pointer to read the data from. | ||
224 | ++++++++++++++++++++++++++++++++++++++*/ | ||
225 | |||
226 | static inline uint32_t pbf_int32(unsigned char **ptr) | ||
227 | { | ||
228 | uint32_t result=(**ptr)&0x7F; | ||
229 | |||
230 | if((**ptr)&0x80) result+=((*++(*ptr))&0x7F)<<7; | ||
231 | if((**ptr)&0x80) result+=((*++(*ptr))&0x7F)<<14; | ||
232 | if((**ptr)&0x80) result+=((*++(*ptr))&0x7F)<<21; | ||
233 | if((**ptr)&0x80) result+=((*++(*ptr))&0x7F)<<28; | ||
234 | |||
235 | (*ptr)++; | ||
236 | |||
237 | return(result); | ||
238 | } | ||
239 | |||
240 | |||
241 | /*++++++++++++++++++++++++++++++++++++++ | ||
242 | Parse a PBF int64 data value. | ||
243 | |||
244 | int64_t pbf_int64 Returns the integer value. | ||
245 | |||
246 | unsigned char **ptr The pointer to read the data from. | ||
247 | ++++++++++++++++++++++++++++++++++++++*/ | ||
248 | |||
249 | static inline int64_t pbf_int64(unsigned char **ptr) | ||
250 | { | ||
251 | uint64_t result=(**ptr)&0x7F; | ||
252 | |||
253 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<7; | ||
254 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<14; | ||
255 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<21; | ||
256 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<28; | ||
257 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<35; | ||
258 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<42; | ||
259 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<49; | ||
260 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<56; | ||
261 | if((**ptr)&0x80) result+=(uint64_t)((*++(*ptr))&0x7F)<<63; | ||
262 | |||
263 | (*ptr)++; | ||
264 | |||
265 | return(result); | ||
266 | } | ||
267 | |||
268 | |||
269 | /*++++++++++++++++++++++++++++++++++++++ | ||
270 | Parse a PBF sint64 data value. | ||
271 | |||
272 | int64_t pbf_sint64 Returns the integer value. | ||
273 | |||
274 | unsigned char **ptr The pointer to read the data from. | ||
275 | ++++++++++++++++++++++++++++++++++++++*/ | ||
276 | |||
277 | static inline int64_t pbf_sint64(unsigned char **ptr) | ||
278 | { | ||
279 | int64_t result=((**ptr)&0x7E)>>1; | ||
280 | int sign=(**ptr)&0x01; | ||
281 | |||
282 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<6; | ||
283 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<13; | ||
284 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<20; | ||
285 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<27; | ||
286 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<34; | ||
287 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<41; | ||
288 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<48; | ||
289 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<55; | ||
290 | if((**ptr)&0x80) result+=(int64_t)((*++(*ptr))&0x7F)<<62; | ||
291 | |||
292 | (*ptr)++; | ||
293 | |||
294 | if(sign) | ||
295 | result=-result-1; | ||
296 | |||
297 | return(result); | ||
298 | } | ||
299 | |||
300 | |||
301 | /*++++++++++++++++++++++++++++++++++++++ | ||
302 | Parse a PBF length delimited data value. | ||
303 | |||
304 | unsigned char *pbf_length_delimited Returns a pointer to the start of the data. | ||
305 | |||
306 | unsigned char **ptr The pointer to read the data from. | ||
307 | |||
308 | uint32_t *length Returns the length of the data. | ||
309 | ++++++++++++++++++++++++++++++++++++++*/ | ||
310 | |||
311 | static inline unsigned char *pbf_length_delimited(unsigned char **ptr,uint32_t *length) | ||
312 | { | ||
313 | uint32_t len=pbf_int32(ptr); | ||
314 | |||
315 | if(length) | ||
316 | *length=len; | ||
317 | |||
318 | *ptr+=len; | ||
319 | |||
320 | return(*ptr-len); | ||
321 | } | ||
322 | |||
323 | |||
324 | /*++++++++++++++++++++++++++++++++++++++ | ||
325 | Skip any pbf field from a message. | ||
326 | |||
327 | unsigned char **ptr The pointer to read the data from. | ||
328 | |||
329 | int type The type of the data. | ||
330 | ++++++++++++++++++++++++++++++++++++++*/ | ||
331 | |||
332 | static inline void pbf_skip(unsigned char **ptr,int type) | ||
333 | { | ||
334 | uint32_t length; | ||
335 | |||
336 | switch(type) | ||
337 | { | ||
338 | case 0: /* varint */ | ||
339 | while((**ptr)&0x80) (*ptr)++; | ||
340 | (*ptr)++; | ||
341 | break; | ||
342 | case 1: /* 64-bit */ | ||
343 | *ptr+=8; | ||
344 | break; | ||
345 | case 2: /* length delimited */ | ||
346 | length=pbf_int32(ptr); | ||
347 | *ptr+=length; | ||
348 | break; | ||
349 | case 3: /* deprecated */ | ||
350 | break; | ||
351 | case 4: /* deprecated */ | ||
352 | break; | ||
353 | case 5: /* 32-bit */ | ||
354 | *ptr+=4; | ||
355 | break; | ||
356 | } | ||
357 | } | ||
358 | |||
359 | |||
360 | /*++++++++++++++++++++++++++++++++++++++ | ||
361 | Parse the PBF and call the functions for each OSM item as seen. | ||
362 | |||
363 | int ParsePBF Returns 0 if OK or something else in case of an error. | ||
364 | |||
365 | amb | 1230 | int fd The file descriptor of the file to parse. |
366 | amb | 1221 | ++++++++++++++++++++++++++++++++++++++*/ |
367 | |||
368 | amb | 1526 | static int ParsePBF(int fd) |
369 | amb | 1221 | { |
370 | int state; | ||
371 | unsigned char *error=NULL; | ||
372 | |||
373 | /* Print the initial message */ | ||
374 | |||
375 | printf_first("Reading: Bytes=0 Nodes=0 Ways=0 Relations=0"); | ||
376 | |||
377 | /* The actual parser. */ | ||
378 | |||
379 | amb | 1784 | byteno=0; |
380 | |||
381 | amb | 1420 | nnodes=0,nways=0,nrelations=0; |
382 | |||
383 | amb | 1221 | string_table_allocated=16384; |
384 | string_table_length=0; | ||
385 | amb | 1225 | string_table=(unsigned char **)malloc(string_table_allocated*sizeof(unsigned char *)); |
386 | string_table_string_lengths=(uint32_t *)malloc(string_table_allocated*sizeof(uint32_t)); | ||
387 | amb | 1221 | |
388 | zbuffer_allocated=0; | ||
389 | zbuffer=NULL; | ||
390 | |||
391 | buffer_allocated=65536; | ||
392 | buffer=(unsigned char*)malloc(buffer_allocated); | ||
393 | |||
394 | buffer_ptr=buffer_end=buffer; | ||
395 | |||
396 | while(1) | ||
397 | { | ||
398 | int32_t blob_header_length=0; | ||
399 | int osm_data=0,osm_header=0; | ||
400 | int32_t blob_length=0; | ||
401 | uint32_t raw_size=0,compressed_size=0,uncompressed_size=0; | ||
402 | unsigned char *raw_data=NULL,*zlib_data=NULL; | ||
403 | uint32_t length; | ||
404 | unsigned char *data; | ||
405 | |||
406 | /* ================ Parsing states ================ */ | ||
407 | |||
408 | |||
409 | BUFFER_CHARS_EOF(4); | ||
410 | |||
411 | blob_header_length=(256*(256*(256*(int)buffer_ptr[0])+(int)buffer_ptr[1])+(int)buffer_ptr[2])+buffer_ptr[3]; | ||
412 | buffer_ptr+=4; | ||
413 | |||
414 | if(blob_header_length==0 || blob_header_length>LENGTH_32M) | ||
415 | BEGIN(PBF_ERROR_BLOB_HEADER_LEN); | ||
416 | |||
417 | |||
418 | BUFFER_CHARS(blob_header_length); | ||
419 | |||
420 | osm_header=0; | ||
421 | osm_data=0; | ||
422 | |||
423 | while(buffer_ptr<buffer_end) | ||
424 | { | ||
425 | int fieldtype=pbf_int32(&buffer_ptr); | ||
426 | int field=PBF_FIELD(fieldtype); | ||
427 | |||
428 | switch(field) | ||
429 | { | ||
430 | case PBF_VAL_BLOBHEADER_TYPE: /* string */ | ||
431 | { | ||
432 | uint32_t length=0; | ||
433 | unsigned char *type=NULL; | ||
434 | |||
435 | type=pbf_length_delimited(&buffer_ptr,&length); | ||
436 | |||
437 | if(length==9 && !strncmp((char*)type,"OSMHeader",9)) | ||
438 | osm_header=1; | ||
439 | |||
440 | if(length==7 && !strncmp((char*)type,"OSMData",7)) | ||
441 | osm_data=1; | ||
442 | } | ||
443 | break; | ||
444 | |||
445 | case PBF_VAL_BLOBHEADER_SIZE: /* int32 */ | ||
446 | blob_length=pbf_int32(&buffer_ptr); | ||
447 | break; | ||
448 | |||
449 | default: | ||
450 | pbf_skip(&buffer_ptr,PBF_TYPE(fieldtype)); | ||
451 | } | ||
452 | } | ||
453 | |||
454 | if(blob_length==0 || blob_length>LENGTH_32M) | ||
455 | BEGIN(PBF_ERROR_BLOB_LEN); | ||
456 | |||
457 | if(!osm_data && !osm_header) | ||
458 | BEGIN(PBF_ERROR_NOT_OSM); | ||
459 | |||
460 | |||
461 | BUFFER_CHARS(blob_length); | ||
462 | |||
463 | while(buffer_ptr<buffer_end) | ||
464 | { | ||
465 | int fieldtype=pbf_int32(&buffer_ptr); | ||
466 | int field=PBF_FIELD(fieldtype); | ||
467 | |||
468 | switch(field) | ||
469 | { | ||
470 | case PBF_VAL_BLOB_RAW_DATA: /* bytes */ | ||
471 | raw_data=pbf_length_delimited(&buffer_ptr,&raw_size); | ||
472 | break; | ||
473 | |||
474 | case PBF_VAL_BLOB_RAW_SIZE: /* int32 */ | ||
475 | uncompressed_size=pbf_int32(&buffer_ptr); | ||
476 | break; | ||
477 | |||
478 | case PBF_VAL_BLOB_ZLIB_DATA: /* bytes */ | ||
479 | zlib_data=pbf_length_delimited(&buffer_ptr,&compressed_size); | ||
480 | break; | ||
481 | |||
482 | default: | ||
483 | pbf_skip(&buffer_ptr,PBF_TYPE(fieldtype)); | ||
484 | } | ||
485 | } | ||
486 | |||
487 | if(raw_data && zlib_data) | ||
488 | BEGIN(PBF_ERROR_BLOB_BOTH); | ||
489 | |||
490 | if(!raw_data && !zlib_data) | ||
491 | BEGIN(PBF_ERROR_BLOB_NEITHER); | ||
492 | |||
493 | if(zlib_data) | ||
494 | { | ||
495 | #if defined(USE_GZIP) && USE_GZIP | ||
496 | int newstate=uncompress_pbf(zlib_data,compressed_size,uncompressed_size); | ||
497 | |||
498 | if(newstate) | ||
499 | BEGIN(newstate); | ||
500 | #else | ||
501 | BEGIN(PBF_ERROR_NO_GZIP); | ||
502 | #endif | ||
503 | } | ||
504 | else | ||
505 | { | ||
506 | buffer_ptr=raw_data; | ||
507 | buffer_end=raw_data+raw_size; | ||
508 | } | ||
509 | |||
510 | |||
511 | if(osm_header) | ||
512 | { | ||
513 | while(buffer_ptr<buffer_end) | ||
514 | { | ||
515 | int fieldtype=pbf_int32(&buffer_ptr); | ||
516 | int field=PBF_FIELD(fieldtype); | ||
517 | |||
518 | switch(field) | ||
519 | { | ||
520 | case PBF_VAL_REQUIRED_FEATURES: /* string */ | ||
521 | { | ||
522 | uint32_t length=0; | ||
523 | unsigned char *feature=NULL; | ||
524 | |||
525 | feature=pbf_length_delimited(&buffer_ptr,&length); | ||
526 | |||
527 | if(strncmp((char*)feature,"OsmSchema-V0.6",14) && | ||
528 | amb | 1226 | strncmp((char*)feature,"DenseNodes",10)) |
529 | amb | 1221 | { |
530 | feature[length]=0; | ||
531 | error=feature; | ||
532 | BEGIN(PBF_ERROR_UNSUPPORTED); | ||
533 | } | ||
534 | } | ||
535 | break; | ||
536 | |||
537 | case PBF_VAL_OPTIONAL_FEATURES: /* string */ | ||
538 | pbf_length_delimited(&buffer_ptr,NULL); | ||
539 | break; | ||
540 | |||
541 | default: | ||
542 | pbf_skip(&buffer_ptr,PBF_TYPE(fieldtype)); | ||
543 | } | ||
544 | } | ||
545 | } | ||
546 | |||
547 | |||
548 | if(osm_data) | ||
549 | { | ||
550 | unsigned char *primitive_group[8]={NULL}; | ||
551 | uint32_t primitive_group_length[8]={0}; | ||
552 | amb | 1301 | uint32_t nprimitive_groups=0,i; |
553 | amb | 1221 | |
554 | granularity=100; | ||
555 | lat_offset=lon_offset=0; | ||
556 | |||
557 | while(buffer_ptr<buffer_end) | ||
558 | { | ||
559 | int fieldtype=pbf_int32(&buffer_ptr); | ||
560 | int field=PBF_FIELD(fieldtype); | ||
561 | |||
562 | switch(field) | ||
563 | { | ||
564 | case PBF_VAL_STRING_TABLE: /* bytes */ | ||
565 | data=pbf_length_delimited(&buffer_ptr,&length); | ||
566 | process_string_table(data,length); | ||
567 | break; | ||
568 | |||
569 | case PBF_VAL_PRIMITIVE_GROUP: /* bytes */ | ||
570 | primitive_group[nprimitive_groups]=pbf_length_delimited(&buffer_ptr,&primitive_group_length[nprimitive_groups]); | ||
571 | |||
572 | if(++nprimitive_groups>(sizeof(primitive_group)/sizeof(primitive_group[0]))) | ||
573 | BEGIN(PBF_ERROR_TOO_MANY_GROUPS); | ||
574 | break; | ||
575 | |||
576 | case PBF_VAL_GRANULARITY: /* int32 */ | ||
577 | granularity=pbf_int32(&buffer_ptr); | ||
578 | break; | ||
579 | |||
580 | case PBF_VAL_LAT_OFFSET: /* int64 */ | ||
581 | lat_offset=pbf_int64(&buffer_ptr); | ||
582 | break; | ||
583 | |||
584 | case PBF_VAL_LON_OFFSET: /* int64 */ | ||
585 | lon_offset=pbf_int64(&buffer_ptr); | ||
586 | break; | ||
587 | |||
588 | default: | ||
589 | pbf_skip(&buffer_ptr,PBF_TYPE(fieldtype)); | ||
590 | } | ||
591 | } | ||
592 | |||
593 | if(nprimitive_groups) | ||
594 | for(i=0;i<nprimitive_groups;i++) | ||
595 | process_primitive_group(primitive_group[i],primitive_group_length[i]); | ||
596 | } | ||
597 | } | ||
598 | |||
599 | |||
600 | finish_parsing: | ||
601 | |||
602 | switch(state) | ||
603 | { | ||
604 | /* End of file */ | ||
605 | |||
606 | case PBF_EOF: | ||
607 | break; | ||
608 | |||
609 | |||
610 | /* ================ Error states ================ */ | ||
611 | |||
612 | |||
613 | case PBF_ERROR_UNEXP_EOF: | ||
614 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": unexpected end of file seen.\n",byteno); |
615 | amb | 1221 | break; |
616 | |||
617 | case PBF_ERROR_BLOB_HEADER_LEN: | ||
618 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": BlobHeader length is wrong (0<x<=32M).\n",byteno); |
619 | amb | 1221 | break; |
620 | |||
621 | case PBF_ERROR_BLOB_LEN: | ||
622 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Blob length is wrong (0<x<=32M).\n",byteno); |
623 | amb | 1221 | break; |
624 | |||
625 | case PBF_ERROR_NOT_OSM: | ||
626 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": BlobHeader is neither 'OSMData' or 'OSMHeader'.\n",byteno); |
627 | amb | 1221 | break; |
628 | |||
629 | case PBF_ERROR_BLOB_BOTH: | ||
630 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Blob has both zlib compressed and raw uncompressed data.\n",byteno); |
631 | amb | 1221 | break; |
632 | |||
633 | case PBF_ERROR_BLOB_NEITHER: | ||
634 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Blob has neither zlib compressed or raw uncompressed data.\n",byteno); |
635 | amb | 1221 | break; |
636 | |||
637 | case PBF_ERROR_NO_GZIP: | ||
638 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Blob is compressed but no gzip support is available.\n",byteno); |
639 | amb | 1221 | break; |
640 | |||
641 | case PBF_ERROR_GZIP_INIT: | ||
642 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Blob is compressed but failed to initialise decompression.\n",byteno); |
643 | amb | 1221 | break; |
644 | |||
645 | case PBF_ERROR_GZIP_INFLATE: | ||
646 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Blob is compressed but failed to uncompress it.\n",byteno); |
647 | amb | 1221 | break; |
648 | |||
649 | case PBF_ERROR_GZIP_WRONG_LEN: | ||
650 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Blob is compressed and wrong size when uncompressed.\n",byteno); |
651 | amb | 1221 | break; |
652 | |||
653 | case PBF_ERROR_GZIP_END: | ||
654 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Blob is compressed but failed to finalise decompression.\n",byteno); |
655 | amb | 1221 | break; |
656 | |||
657 | case PBF_ERROR_UNSUPPORTED: | ||
658 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": Unsupported required feature '%s'.\n",byteno,error); |
659 | amb | 1221 | break; |
660 | |||
661 | case PBF_ERROR_TOO_MANY_GROUPS: | ||
662 | amb | 1301 | fprintf(stderr,"PBF Parser: Error at byte %"PRIu64": OsmData message contains too many PrimitiveGroup messages.\n",byteno); |
663 | amb | 1221 | break; |
664 | } | ||
665 | |||
666 | /* Free the parser variables */ | ||
667 | |||
668 | free(string_table); | ||
669 | free(string_table_string_lengths); | ||
670 | |||
671 | free(buffer); | ||
672 | if(zbuffer) | ||
673 | free(zbuffer); | ||
674 | |||
675 | /* Print the final message */ | ||
676 | |||
677 | amb | 1235 | printf_last("Read: Bytes=%"PRIu64" Nodes=%"PRIu64" Ways=%"PRIu64" Relations=%"PRIu64,byteno,nnodes,nways,nrelations); |
678 | amb | 1221 | |
679 | return(state); | ||
680 | } | ||
681 | |||
682 | |||
683 | /*++++++++++++++++++++++++++++++++++++++ | ||
684 | Process a PBF StringTable message. | ||
685 | |||
686 | unsigned char *data The data to process. | ||
687 | |||
688 | uint32_t length The length of the data. | ||
689 | ++++++++++++++++++++++++++++++++++++++*/ | ||
690 | |||
691 | static void process_string_table(unsigned char *data,uint32_t length) | ||
692 | { | ||
693 | unsigned char *end=data+length; | ||
694 | unsigned char *string; | ||
695 | uint32_t string_length; | ||
696 | |||
697 | string_table_length=0; | ||
698 | |||
699 | while(data<end) | ||
700 | { | ||
701 | int fieldtype=pbf_int32(&data); | ||
702 | int field=PBF_FIELD(fieldtype); | ||
703 | |||
704 | switch(field) | ||
705 | { | ||
706 | case PBF_VAL_STRING: /* string */ | ||
707 | string=pbf_length_delimited(&data,&string_length); | ||
708 | |||
709 | if(string_table_length==string_table_allocated) | ||
710 | { | ||
711 | amb | 1225 | string_table_allocated+=8192; |
712 | string_table=(unsigned char **)realloc(string_table,string_table_allocated*sizeof(unsigned char *)); | ||
713 | string_table_string_lengths=(uint32_t *)realloc(string_table_string_lengths,string_table_allocated*sizeof(uint32_t)); | ||
714 | amb | 1221 | } |
715 | |||
716 | string_table[string_table_length]=string; | ||
717 | string_table_string_lengths[string_table_length]=string_length; | ||
718 | |||
719 | string_table_length++; | ||
720 | break; | ||
721 | |||
722 | default: | ||
723 | pbf_skip(&data,PBF_TYPE(fieldtype)); | ||
724 | } | ||
725 | } | ||
726 | } | ||
727 | |||
728 | |||
729 | /*++++++++++++++++++++++++++++++++++++++ | ||
730 | Process a PBF PrimitiveGroup message. | ||
731 | |||
732 | unsigned char *data The data to process. | ||
733 | |||
734 | uint32_t length The length of the data. | ||
735 | ++++++++++++++++++++++++++++++++++++++*/ | ||
736 | |||
737 | static void process_primitive_group(unsigned char *data,uint32_t length) | ||
738 | { | ||
739 | unsigned char *end=data+length; | ||
740 | unsigned char *subdata; | ||
741 | uint32_t sublength; | ||
742 | int i; | ||
743 | |||
744 | /* Fixup the strings (not null terminated in buffer) */ | ||
745 | |||
746 | for(i=0;i<string_table_length;i++) | ||
747 | string_table[i][string_table_string_lengths[i]]=0; | ||
748 | |||
749 | |||
750 | while(data<end) | ||
751 | { | ||
752 | int fieldtype=pbf_int32(&data); | ||
753 | int field=PBF_FIELD(fieldtype); | ||
754 | |||
755 | switch(field) | ||
756 | { | ||
757 | case PBF_VAL_NODES: /* message */ | ||
758 | subdata=pbf_length_delimited(&data,&sublength); | ||
759 | process_nodes(subdata,sublength); | ||
760 | break; | ||
761 | |||
762 | case PBF_VAL_DENSE_NODES: /* message */ | ||
763 | subdata=pbf_length_delimited(&data,&sublength); | ||
764 | process_dense_nodes(subdata,sublength); | ||
765 | break; | ||
766 | |||
767 | case PBF_VAL_WAYS: /* message */ | ||
768 | subdata=pbf_length_delimited(&data,&sublength); | ||
769 | process_ways(subdata,sublength); | ||
770 | break; | ||
771 | |||
772 | case PBF_VAL_RELATIONS: /* message */ | ||
773 | subdata=pbf_length_delimited(&data,&sublength); | ||
774 | process_relations(subdata,sublength); | ||
775 | break; | ||
776 | |||
777 | default: | ||
778 | pbf_skip(&data,PBF_TYPE(fieldtype)); | ||
779 | } | ||
780 | } | ||
781 | } | ||
782 | |||
783 | |||
784 | /*++++++++++++++++++++++++++++++++++++++ | ||
785 | Process a PBF Node message. | ||
786 | |||
787 | unsigned char *data The data to process. | ||
788 | |||
789 | uint32_t length The length of the data. | ||
790 | ++++++++++++++++++++++++++++++++++++++*/ | ||
791 | |||
792 | static void process_nodes(unsigned char *data,uint32_t length) | ||
793 | { | ||
794 | unsigned char *end=data+length; | ||
795 | int64_t id=0; | ||
796 | amb | 1226 | unsigned char *keys=NULL,*vals=NULL; |
797 | amb | 1221 | unsigned char *keys_end=NULL,*vals_end=NULL; |
798 | amb | 1226 | uint32_t keylen=0,vallen=0; |
799 | amb | 1221 | int64_t lat=0,lon=0; |
800 | amb | 2170 | TagList *tags=NULL; |
801 | amb | 1221 | |
802 | while(data<end) | ||
803 | { | ||
804 | int fieldtype=pbf_int32(&data); | ||
805 | int field=PBF_FIELD(fieldtype); | ||
806 | |||
807 | switch(field) | ||
808 | { | ||
809 | case PBF_VAL_NODE_ID: /* sint64 */ | ||
810 | id=pbf_sint64(&data); | ||
811 | break; | ||
812 | |||
813 | case PBF_VAL_NODE_KEYS: /* packed int32 */ | ||
814 | keys=pbf_length_delimited(&data,&keylen); | ||
815 | keys_end=keys+keylen; | ||
816 | break; | ||
817 | |||
818 | case PBF_VAL_NODE_VALS: /* packed int32 */ | ||
819 | vals=pbf_length_delimited(&data,&vallen); | ||
820 | vals_end=vals+vallen; | ||
821 | break; | ||
822 | |||
823 | case PBF_VAL_NODE_LAT: /* sint64 */ | ||
824 | lat=pbf_sint64(&data); | ||
825 | break; | ||
826 | |||
827 | case PBF_VAL_NODE_LON: /* sint64 */ | ||
828 | lon=pbf_sint64(&data); | ||
829 | break; | ||
830 | |||
831 | default: | ||
832 | pbf_skip(&data,PBF_TYPE(fieldtype)); | ||
833 | } | ||
834 | } | ||
835 | |||
836 | /* Mangle the data and send it to the OSM parser */ | ||
837 | |||
838 | nnodes++; | ||
839 | |||
840 | if(!(nnodes%10000)) | ||
841 | amb | 1235 | printf_middle("Reading: Bytes=%"PRIu64" Nodes=%"PRIu64" Ways=%"PRIu64" Relations=%"PRIu64,byteno,nnodes,nways,nrelations); |
842 | amb | 1221 | |
843 | tags=NewTagList(); | ||
844 | |||
845 | if(keys && vals) | ||
846 | { | ||
847 | while(keys<keys_end && vals<vals_end) | ||
848 | { | ||
849 | uint32_t key=pbf_int32(&keys); | ||
850 | uint32_t val=pbf_int32(&vals); | ||
851 | |||
852 | AppendTag(tags,(char*)string_table[key],(char*)string_table[val]); | ||
853 | } | ||
854 | } | ||
855 | |||
856 | amb | 2170 | AddNode(id,PBF_LATITUDE(lat),PBF_LONGITUDE(lon),MODE_NORMAL,tags); |
857 | amb | 1221 | } |
858 | |||
859 | |||
860 | /*++++++++++++++++++++++++++++++++++++++ | ||
861 | Process a PBF DenseNode message. | ||
862 | |||
863 | unsigned char *data The data to process. | ||
864 | |||
865 | uint32_t length The length of the data. | ||
866 | ++++++++++++++++++++++++++++++++++++++*/ | ||
867 | |||
868 | static void process_dense_nodes(unsigned char *data,uint32_t length) | ||
869 | { | ||
870 | unsigned char *end=data+length; | ||
871 | amb | 1226 | unsigned char *ids=NULL,*keys_vals=NULL,*lats=NULL,*lons=NULL; |
872 | amb | 1221 | unsigned char *ids_end=NULL; |
873 | amb | 1226 | uint32_t idlen=0; |
874 | amb | 1221 | int64_t id=0; |
875 | int64_t lat=0,lon=0; | ||
876 | amb | 2170 | TagList *tags=NULL; |
877 | amb | 1221 | |
878 | while(data<end) | ||
879 | { | ||
880 | int fieldtype=pbf_int32(&data); | ||
881 | int field=PBF_FIELD(fieldtype); | ||
882 | |||
883 | switch(field) | ||
884 | { | ||
885 | case PBF_VAL_DENSE_NODE_ID: /* packed sint64 */ | ||
886 | ids=pbf_length_delimited(&data,&idlen); | ||
887 | ids_end=ids+idlen; | ||
888 | break; | ||
889 | |||
890 | case PBF_VAL_DENSE_NODE_LAT: /* packed sint64 */ | ||
891 | lats=pbf_length_delimited(&data,NULL); | ||
892 | break; | ||
893 | |||
894 | case PBF_VAL_DENSE_NODE_LON: /* packed sint64 */ | ||
895 | lons=pbf_length_delimited(&data,NULL); | ||
896 | break; | ||
897 | |||
898 | case PBF_VAL_DENSE_NODE_KEYS_VALS: /* packed int32 */ | ||
899 | keys_vals=pbf_length_delimited(&data,NULL); | ||
900 | break; | ||
901 | |||
902 | default: | ||
903 | pbf_skip(&data,PBF_TYPE(fieldtype)); | ||
904 | } | ||
905 | } | ||
906 | |||
907 | while(ids<ids_end) | ||
908 | { | ||
909 | int64_t delta_id; | ||
910 | int64_t delta_lat,delta_lon; | ||
911 | |||
912 | delta_id=pbf_sint64(&ids); | ||
913 | delta_lat=pbf_sint64(&lats); | ||
914 | delta_lon=pbf_sint64(&lons); | ||
915 | |||
916 | id+=delta_id; | ||
917 | lat+=delta_lat; | ||
918 | lon+=delta_lon; | ||
919 | |||
920 | /* Mangle the data and send it to the OSM parser */ | ||
921 | |||
922 | nnodes++; | ||
923 | |||
924 | if(!(nnodes%10000)) | ||
925 | amb | 1235 | printf_middle("Reading: Bytes=%"PRIu64" Nodes=%"PRIu64" Ways=%"PRIu64" Relations=%"PRIu64,byteno,nnodes,nways,nrelations); |
926 | amb | 1221 | |
927 | tags=NewTagList(); | ||
928 | |||
929 | if(keys_vals) | ||
930 | { | ||
931 | while(1) | ||
932 | { | ||
933 | uint32_t key=pbf_int32(&keys_vals),val; | ||
934 | |||
935 | if(key==0) | ||
936 | break; | ||
937 | |||
938 | val=pbf_int32(&keys_vals); | ||
939 | |||
940 | AppendTag(tags,(char*)string_table[key],(char*)string_table[val]); | ||
941 | } | ||
942 | } | ||
943 | |||
944 | amb | 2170 | AddNode(id,PBF_LATITUDE(lat),PBF_LONGITUDE(lon),MODE_NORMAL,tags); |
945 | amb | 1221 | } |
946 | } | ||
947 | |||
948 | |||
949 | /*++++++++++++++++++++++++++++++++++++++ | ||
950 | Process a PBF Way message. | ||
951 | |||
952 | unsigned char *data The data to process. | ||
953 | |||
954 | uint32_t length The length of the data. | ||
955 | ++++++++++++++++++++++++++++++++++++++*/ | ||
956 | |||
957 | static void process_ways(unsigned char *data,uint32_t length) | ||
958 | { | ||
959 | unsigned char *end=data+length; | ||
960 | int64_t id=0; | ||
961 | amb | 1226 | unsigned char *keys=NULL,*vals=NULL,*refs=NULL; |
962 | amb | 1221 | unsigned char *keys_end=NULL,*vals_end=NULL,*refs_end=NULL; |
963 | amb | 1226 | uint32_t keylen=0,vallen=0,reflen=0; |
964 | amb | 1221 | int64_t ref=0; |
965 | amb | 2170 | TagList *tags=NULL; |
966 | amb | 1221 | |
967 | while(data<end) | ||
968 | { | ||
969 | int fieldtype=pbf_int32(&data); | ||
970 | int field=PBF_FIELD(fieldtype); | ||
971 | |||
972 | switch(field) | ||
973 | { | ||
974 | case PBF_VAL_WAY_ID: /* int64 */ | ||
975 | id=pbf_int64(&data); | ||
976 | break; | ||
977 | |||
978 | case PBF_VAL_WAY_KEYS: /* packed int32 */ | ||
979 | keys=pbf_length_delimited(&data,&keylen); | ||
980 | keys_end=keys+keylen; | ||
981 | break; | ||
982 | |||
983 | case PBF_VAL_WAY_VALS: /* packed int32 */ | ||
984 | vals=pbf_length_delimited(&data,&vallen); | ||
985 | vals_end=vals+vallen; | ||
986 | break; | ||
987 | |||
988 | case PBF_VAL_WAY_REFS: /* packed sint64 */ | ||
989 | refs=pbf_length_delimited(&data,&reflen); | ||
990 | refs_end=refs+reflen; | ||
991 | break; | ||
992 | |||
993 | default: | ||
994 | pbf_skip(&data,PBF_TYPE(fieldtype)); | ||
995 | } | ||
996 | } | ||
997 | |||
998 | /* Mangle the data and send it to the OSM parser */ | ||
999 | |||
1000 | nways++; | ||
1001 | |||
1002 | if(!(nways%1000)) | ||
1003 | amb | 1235 | printf_middle("Reading: Bytes=%"PRIu64" Nodes=%"PRIu64" Ways=%"PRIu64" Relations=%"PRIu64,byteno,nnodes,nways,nrelations); |
1004 | amb | 1221 | |
1005 | tags=NewTagList(); | ||
1006 | |||
1007 | if(keys && vals) | ||
1008 | { | ||
1009 | while(keys<keys_end && vals<vals_end) | ||
1010 | { | ||
1011 | uint32_t key=pbf_int32(&keys); | ||
1012 | uint32_t val=pbf_int32(&vals); | ||
1013 | |||
1014 | AppendTag(tags,(char*)string_table[key],(char*)string_table[val]); | ||
1015 | } | ||
1016 | } | ||
1017 | |||
1018 | amb | 1234 | AddWayRefs(0); |
1019 | amb | 1221 | |
1020 | if(refs) | ||
1021 | while(refs<refs_end) | ||
1022 | { | ||
1023 | int64_t delta_ref; | ||
1024 | |||
1025 | delta_ref=pbf_sint64(&refs); | ||
1026 | |||
1027 | ref+=delta_ref; | ||
1028 | |||
1029 | if(ref==0) | ||
1030 | break; | ||
1031 | |||
1032 | amb | 1234 | AddWayRefs(ref); |
1033 | amb | 1221 | } |
1034 | |||
1035 | amb | 2170 | AddWay(id,MODE_NORMAL,tags); |
1036 | amb | 1221 | } |
1037 | |||
1038 | |||
1039 | /*++++++++++++++++++++++++++++++++++++++ | ||
1040 | Process a PBF Relation message. | ||
1041 | |||
1042 | unsigned char *data The data to process. | ||
1043 | |||
1044 | uint32_t length The length of the data. | ||
1045 | ++++++++++++++++++++++++++++++++++++++*/ | ||
1046 | |||
1047 | static void process_relations(unsigned char *data,uint32_t length) | ||
1048 | { | ||
1049 | unsigned char *end=data+length; | ||
1050 | int64_t id=0; | ||
1051 | amb | 1226 | unsigned char *keys=NULL,*vals=NULL,*roles=NULL,*memids=NULL,*types=NULL; |
1052 | amb | 1221 | unsigned char *keys_end=NULL,*vals_end=NULL,*memids_end=NULL,*types_end=NULL; |
1053 | amb | 1226 | uint32_t keylen=0,vallen=0,rolelen=0,memidlen=0,typelen=0; |
1054 | amb | 1221 | int64_t memid=0; |
1055 | amb | 2170 | TagList *tags=NULL; |
1056 | amb | 1221 | |
1057 | while(data<end) | ||
1058 | { | ||
1059 | int fieldtype=pbf_int32(&data); | ||
1060 | int field=PBF_FIELD(fieldtype); | ||
1061 | |||
1062 | switch(field) | ||
1063 | { | ||
1064 | case PBF_VAL_RELATION_ID: /* int64 */ | ||
1065 | id=pbf_int64(&data); | ||
1066 | break; | ||
1067 | |||
1068 | case PBF_VAL_RELATION_KEYS: /* packed string */ | ||
1069 | keys=pbf_length_delimited(&data,&keylen); | ||
1070 | keys_end=keys+keylen; | ||
1071 | break; | ||
1072 | |||
1073 | case PBF_VAL_RELATION_VALS: /* packed string */ | ||
1074 | vals=pbf_length_delimited(&data,&vallen); | ||
1075 | vals_end=vals+vallen; | ||
1076 | break; | ||
1077 | |||
1078 | case PBF_VAL_RELATION_ROLES: /* packed int32 */ | ||
1079 | roles=pbf_length_delimited(&data,&rolelen); | ||
1080 | break; | ||
1081 | |||
1082 | case PBF_VAL_RELATION_MEMIDS: /* packed sint64 */ | ||
1083 | memids=pbf_length_delimited(&data,&memidlen); | ||
1084 | memids_end=memids+memidlen; | ||
1085 | break; | ||
1086 | |||
1087 | case PBF_VAL_RELATION_TYPES: /* packed enum */ | ||
1088 | types=pbf_length_delimited(&data,&typelen); | ||
1089 | types_end=types+typelen; | ||
1090 | break; | ||
1091 | |||
1092 | default: | ||
1093 | pbf_skip(&data,PBF_TYPE(fieldtype)); | ||
1094 | } | ||
1095 | } | ||
1096 | |||
1097 | /* Mangle the data and send it to the OSM parser */ | ||
1098 | |||
1099 | nrelations++; | ||
1100 | |||
1101 | if(!(nrelations%1000)) | ||
1102 | amb | 1235 | printf_middle("Reading: Bytes=%"PRIu64" Nodes=%"PRIu64" Ways=%"PRIu64" Relations=%"PRIu64,byteno,nnodes,nways,nrelations); |
1103 | amb | 1221 | |
1104 | amb | 1234 | AddRelationRefs(0,0,0,NULL); |
1105 | amb | 1221 | |
1106 | tags=NewTagList(); | ||
1107 | |||
1108 | if(keys && vals) | ||
1109 | { | ||
1110 | while(keys<keys_end && vals<vals_end) | ||
1111 | { | ||
1112 | uint32_t key=pbf_int32(&keys); | ||
1113 | uint32_t val=pbf_int32(&vals); | ||
1114 | |||
1115 | AppendTag(tags,(char*)string_table[key],(char*)string_table[val]); | ||
1116 | } | ||
1117 | } | ||
1118 | |||
1119 | if(memids && types) | ||
1120 | while(memids<memids_end && types<types_end) | ||
1121 | { | ||
1122 | int64_t delta_memid; | ||
1123 | unsigned char *role=NULL; | ||
1124 | int type; | ||
1125 | |||
1126 | delta_memid=pbf_sint64(&memids); | ||
1127 | type=pbf_int32(&types); | ||
1128 | |||
1129 | if(roles) | ||
1130 | role=string_table[pbf_int32(&roles)]; | ||
1131 | |||
1132 | memid+=delta_memid; | ||
1133 | |||
1134 | if(type==0) | ||
1135 | amb | 1234 | AddRelationRefs(memid,0,0,(char*)role); |
1136 | amb | 1221 | else if(type==1) |
1137 | amb | 1234 | AddRelationRefs(0,memid,0,(char*)role); |
1138 | amb | 1221 | else if(type==2) |
1139 | amb | 1234 | AddRelationRefs(0,0,memid,(char*)role); |
1140 | amb | 1221 | } |
1141 | |||
1142 | amb | 2170 | AddRelation(id,MODE_NORMAL,tags); |
1143 | amb | 1221 | } |
1144 | |||
1145 | |||
1146 | #if defined(USE_GZIP) && USE_GZIP | ||
1147 | |||
1148 | /*++++++++++++++++++++++++++++++++++++++ | ||
1149 | Uncompress the part of the PBF data that is compressed. | ||
1150 | |||
1151 | int uncompress_pbf Returns the error state or 0 if OK. | ||
1152 | |||
1153 | unsigned char *data The data to uncompress. | ||
1154 | |||
1155 | uint32_t compressed The number of bytes to uncompress. | ||
1156 | |||
1157 | uint32_t uncompressed The number of bytes expected when uncompressed. | ||
1158 | ++++++++++++++++++++++++++++++++++++++*/ | ||
1159 | |||
1160 | static int uncompress_pbf(unsigned char *data,uint32_t compressed,uint32_t uncompressed) | ||
1161 | { | ||
1162 | z_stream z={0}; | ||
1163 | |||
1164 | if(uncompressed>zbuffer_allocated) | ||
1165 | zbuffer=(unsigned char *)realloc(zbuffer,zbuffer_allocated=uncompressed); | ||
1166 | |||
1167 | if(inflateInit2(&z,15+32)!=Z_OK) | ||
1168 | return(PBF_ERROR_GZIP_INIT); | ||
1169 | |||
1170 | z.next_in=data; | ||
1171 | z.avail_in=compressed; | ||
1172 | |||
1173 | z.next_out=zbuffer; | ||
1174 | z.avail_out=uncompressed; | ||
1175 | |||
1176 | if(inflate(&z,Z_FINISH)!=Z_STREAM_END) | ||
1177 | return(PBF_ERROR_GZIP_INFLATE); | ||
1178 | |||
1179 | if(z.avail_out!=0) | ||
1180 | return(PBF_ERROR_GZIP_WRONG_LEN); | ||
1181 | |||
1182 | if(inflateEnd(&z)!=Z_OK) | ||
1183 | return(PBF_ERROR_GZIP_END); | ||
1184 | |||
1185 | buffer_ptr=zbuffer; | ||
1186 | buffer_end=zbuffer+uncompressed; | ||
1187 | |||
1188 | return(0); | ||
1189 | } | ||
1190 | |||
1191 | #endif /* USE_GZIP */ | ||
1192 | amb | 1356 | |
1193 | |||
1194 | /*++++++++++++++++++++++++++++++++++++++ | ||
1195 | Parse a PBF format OSM file (from planet download). | ||
1196 | |||
1197 | int ParsePBFFile Returns 0 if OK or something else in case of an error. | ||
1198 | |||
1199 | int fd The file descriptor of the file to read from. | ||
1200 | |||
1201 | NodesX *OSMNodes The data structure of nodes to fill in. | ||
1202 | |||
1203 | WaysX *OSMWays The data structure of ways to fill in. | ||
1204 | |||
1205 | RelationsX *OSMRelations The data structure of relations to fill in. | ||
1206 | ++++++++++++++++++++++++++++++++++++++*/ | ||
1207 | |||
1208 | int ParsePBFFile(int fd,NodesX *OSMNodes,WaysX *OSMWays,RelationsX *OSMRelations) | ||
1209 | { | ||
1210 | int retval; | ||
1211 | |||
1212 | /* Initialise the parser */ | ||
1213 | |||
1214 | InitialiseParser(OSMNodes,OSMWays,OSMRelations); | ||
1215 | |||
1216 | /* Parse the file */ | ||
1217 | |||
1218 | retval=ParsePBF(fd); | ||
1219 | |||
1220 | /* Cleanup the parser */ | ||
1221 | |||
1222 | CleanupParser(); | ||
1223 | |||
1224 | return(retval); | ||
1225 | } |