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