/[zanavi_public1]/navit/navit/maptool/osm_xml.c
ZANavi

Contents of /navit/navit/maptool/osm_xml.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (7 years, 6 months ago) by zoff99
File MIME type: text/plain
File size: 10679 byte(s)
new map version, lots of fixes and experimental new features
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2011 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38 #include <string.h>
39 #include <stdlib.h>
40 #include <math.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include "maptool.h"
44
45 int osm_xml_get_attribute(char *xml, char *attribute, char *buffer, int buffer_size)
46 {
47 int len = strlen(attribute);
48 char *pos, *i, s, attr[len + 2];
49 strcpy(attr, attribute);
50 strcpy(attr + len, "=");
51 pos = strstr(xml, attr);
52 if (!pos)
53 return 0;
54 pos += len + 1;
55 s = *pos++;
56 if (!s)
57 return 0;
58 i = strchr(pos, s);
59 if (!i)
60 return 0;
61 if (i - pos > buffer_size)
62 {
63 fprintf(stderr, "Buffer overflow %ld vs %d\n", (long) (i - pos), buffer_size);
64 return 0;
65 }
66 strncpy(buffer, pos, i - pos);
67 buffer[i - pos] = '\0';
68 return 1;
69 }
70
71 static struct entity
72 {
73 char *entity;
74 char c;
75 } entities[] = { { "&quot;", '"' }, { "&apos;", '\'' }, { "&amp;", '&' }, { "&lt;", '<' }, { "&gt;", '>' }, { "&#34;", '"' }, { "&#39;", '\'' }, { "&#38;", '&' }, { "&#60;", '<' }, { "&#62;", '>' }, { "&#123;", '{' }, { "&#125;", '}' }, };
76
77 void osm_xml_decode_entities(char *buffer)
78 {
79 char *pos = buffer;
80 int i, len, found;
81
82 while ((pos = strchr(pos, '&')))
83 {
84 found = 0;
85 for (i = 0; i < sizeof(entities) / sizeof(struct entity); i++)
86 {
87 len = strlen(entities[i].entity);
88 if (!strncmp(pos, entities[i].entity, len))
89 {
90 *pos = entities[i].c;
91 memmove(pos + 1, pos + len, strlen(pos + len) + 1);
92 found = 1;
93 break;
94 }
95 }
96 pos++;
97 }
98 }
99
100 static int parse_tag(char *p)
101 {
102 char k_buffer[BUFFER_SIZE];
103 char v_buffer[BUFFER_SIZE];
104 if (!osm_xml_get_attribute(p, "k", k_buffer, BUFFER_SIZE))
105 {
106 return 0;
107 }
108 if (!osm_xml_get_attribute(p, "v", v_buffer, BUFFER_SIZE))
109 {
110 return 0;
111 }
112 osm_xml_decode_entities(v_buffer);
113 osm_add_tag(k_buffer, v_buffer);
114 return 1;
115 }
116
117 static int parse_node(char *p)
118 {
119 char id_buffer[BUFFER_SIZE];
120 char lat_buffer[BUFFER_SIZE];
121 char lon_buffer[BUFFER_SIZE];
122 if (!osm_xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
123 return 0;
124 if (!osm_xml_get_attribute(p, "lat", lat_buffer, BUFFER_SIZE))
125 return 0;
126 if (!osm_xml_get_attribute(p, "lon", lon_buffer, BUFFER_SIZE))
127 return 0;
128 osm_add_node(atoll(id_buffer), atof(lat_buffer), atof(lon_buffer));
129 return 1;
130 }
131
132 static int parse_way(char *p)
133 {
134 char id_buffer[BUFFER_SIZE];
135 if (!osm_xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
136 return 0;
137 osm_add_way(atoll(id_buffer));
138 return 1;
139 }
140
141 static int parse_relation(char *p)
142 {
143 char id_buffer[BUFFER_SIZE];
144 if (!osm_xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
145 return 0;
146 osm_add_relation(atoll(id_buffer));
147 return 1;
148 }
149
150 static int parse_member(char *p)
151 {
152 char type_buffer[BUFFER_SIZE];
153 char ref_buffer[BUFFER_SIZE];
154 char role_buffer[BUFFER_SIZE];
155 int type;
156 if (!osm_xml_get_attribute(p, "type", type_buffer, BUFFER_SIZE))
157 return 0;
158 if (!osm_xml_get_attribute(p, "ref", ref_buffer, BUFFER_SIZE))
159 return 0;
160 if (!osm_xml_get_attribute(p, "role", role_buffer, BUFFER_SIZE))
161 return 0;
162 if (!strcmp(type_buffer, "node"))
163 type = 1;
164 else if (!strcmp(type_buffer, "way"))
165 type = 2;
166 else if (!strcmp(type_buffer, "relation"))
167 type = 3;
168 else
169 {
170 //fprintf(stderr,"Unknown type %s\n",type_buffer);
171 type = 0;
172 }
173 osm_add_member(type, atoll(ref_buffer), role_buffer);
174
175 return 1;
176 }
177
178 static int parse_nd(char *p)
179 {
180 char ref_buffer[BUFFER_SIZE];
181 if (!osm_xml_get_attribute(p, "ref", ref_buffer, BUFFER_SIZE))
182 return 0;
183 osm_add_nd(atoll(ref_buffer));
184 return 1;
185 }
186
187 int map_collect_data_osm(FILE *in, struct maptool_osm *osm)
188 {
189 int size = 1024 * 5; // 5kb
190 // char buffer[size];
191 char *buffer_ptr;
192 char *p;
193
194 time_t start_tt, end_tt;
195 double diff_tt;
196 long long diff2_tt;
197 long long pos_in;
198 long long pos_in_local;
199 int _c = 0;
200 int _e = 5000000;
201 int first_rel = 1;
202 int first_way = 1;
203 int line_length = 0;
204
205 buffer_ptr = malloc(size);
206
207 //sig_alrm(0);
208
209 // reset
210 pos_in = 0;
211 pos_in_local = 0;
212 diff2_tt = 0;
213
214 while (fgets(buffer_ptr, size, in))
215 {
216 // we just read "size" bytes from "in"
217
218 line_length = strlen(buffer_ptr);
219
220 pos_in = pos_in + line_length;
221 pos_in_local = pos_in_local + line_length;
222
223 if (_c == 0)
224 {
225 time(&start_tt);
226 pos_in_local = 0;
227 }
228 _c++;
229
230 p = strchr(buffer_ptr, '<');
231 if (!p)
232 {
233 //fprintf(stderr,"WARNING: wrong line %s\n", buffer_ptr);
234 continue;
235 }
236 if (!strncmp(p, "<?xml ", 6))
237 {
238 }
239 else if (!strncmp(p, "<osm ", 5))
240 {
241 }
242 else if (!strncmp(p, "<bound ", 7))
243 {
244 }
245 else if (!strncmp(p, "<node ", 6))
246 {
247 if (!parse_node(p))
248 {
249 //fprintf(stderr,"WARNING: failed to parse %s\n", buffer_ptr);
250 }
251 processed_nodes++;
252 }
253 else if (!strncmp(p, "<tag ", 5))
254 {
255 if (!parse_tag(p))
256 {
257 //fprintf(stderr,"WARNING: failed to parse %s\n", buffer_ptr);
258 }
259 }
260 else if (!strncmp(p, "<way ", 5))
261 {
262 if (first_way == 1)
263 {
264 first_way = 0;
265
266 sql_counter = 0;
267 sql_counter2 = 0;
268 sql_counter3 = 0;
269 sql_counter4 = 0;
270 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
271 sqlite3_exec(sql_handle002a, "COMMIT", 0, 0, 0);
272 sqlite3_exec(sql_handle003a, "COMMIT", 0, 0, 0);
273 sqlite3_exec(sql_handle002b, "COMMIT", 0, 0, 0);
274 sqlite3_exec(sql_handle003b, "COMMIT", 0, 0, 0);
275 sqlite3_exec(sql_handle004, "COMMIT", 0, 0, 0);
276 sqlite3_exec(sql_handle005, "COMMIT", 0, 0, 0);
277 sqlite3_exec(sql_handle006, "COMMIT", 0, 0, 0);
278 sqlite3_exec(sql_handle007, "COMMIT", 0, 0, 0);
279 fprintf(stderr, "nodes processed:%lld\n", processed_nodes);
280
281 // reopen
282 sql_db_close();
283 sql_db_open();
284 sql_db_init(0);
285
286 fprintf(stderr, "SQL: (create index 003) start\n");
287 sql_create_index003();
288 fprintf(stderr, "SQL: (create index 003) ready\n");
289
290 // reopen for indexes to be used
291 sql_db_close();
292 sql_db_open();
293 sql_db_init(0);
294
295 fprintf(stderr, "SQL: (first way) COMMIT\n");
296
297 }
298
299 if (!parse_way(p))
300 {
301 //fprintf(stderr,"WARNING: failed to parse %s\n", buffer_ptr);
302 }
303 processed_ways++;
304 }
305 else if (!strncmp(p, "<nd ", 4))
306 {
307 if (!parse_nd(p))
308 {
309 //fprintf(stderr,"WARNING: failed to parse %s\n", buffer_ptr);
310 }
311 }
312 else if (!strncmp(p, "<relation ", 10))
313 {
314 if (first_rel == 1)
315 {
316 first_rel = 0;
317
318 sql_counter = 0;
319 sql_counter2 = 0;
320 sql_counter3 = 0;
321 sql_counter4 = 0;
322 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
323 sqlite3_exec(sql_handle002a, "COMMIT", 0, 0, 0);
324 sqlite3_exec(sql_handle003a, "COMMIT", 0, 0, 0);
325 sqlite3_exec(sql_handle002b, "COMMIT", 0, 0, 0);
326 sqlite3_exec(sql_handle003b, "COMMIT", 0, 0, 0);
327 sqlite3_exec(sql_handle004, "COMMIT", 0, 0, 0);
328 sqlite3_exec(sql_handle005, "COMMIT", 0, 0, 0);
329 sqlite3_exec(sql_handle006, "COMMIT", 0, 0, 0);
330 sqlite3_exec(sql_handle007, "COMMIT", 0, 0, 0);
331 fprintf(stderr, "ways processed:%lld\n", processed_ways);
332
333 // reopen
334 sql_db_close();
335 sql_db_open();
336 sql_db_init(0);
337
338 fprintf(stderr, "SQL: (create index 001) start\n");
339 sql_create_index001();
340 fprintf(stderr, "SQL: (create index 001) ready\n");
341
342 // reopen for indexes to be used
343 sql_db_close();
344 sql_db_open();
345 sql_db_init(0);
346
347 fprintf(stderr, "SQL: (first relation) COMMIT\n");
348 }
349
350 if (!parse_relation(p))
351 {
352 if (verbose_mode)
353 fprintf(stderr, "WARNING: failed to parse %s\n", buffer_ptr);
354 }
355 processed_relations++;
356 }
357 else if (!strncmp(p, "<member ", 8))
358 {
359 if (!parse_member(p))
360 {
361 //fprintf(stderr,"WARNING: failed to parse %s\n", buffer_ptr);
362 }
363 }
364 else if (!strncmp(p, "</node>", 7))
365 {
366 osm_end_node(osm);
367 }
368 else if (!strncmp(p, "</way>", 6))
369 {
370 osm_end_way(osm);
371 }
372 else if (!strncmp(p, "</relation>", 11))
373 {
374 osm_end_relation(osm);
375 }
376 else if (!strncmp(p, "</osm>", 6))
377 {
378 }
379 else
380 {
381 // fprintf(stderr,"WARNING: unknown tag in %s\n", buffer_ptr);
382 }
383
384 if (_c > _e)
385 {
386 _c = 0;
387
388 time(&end_tt);
389 diff_tt = difftime(end_tt, start_tt);
390 diff2_tt = diff2_tt + (long) diff_tt;
391
392 char outstring[200];
393 char outstring2[200];
394 convert_to_human_time(diff2_tt, outstring);
395 convert_to_human_bytes(pos_in, outstring2);
396 fprintf(stderr, "-RUNTIME-LOOP-COLLECT-DATA: %s elapsed (%s read)\n", outstring, outstring2);
397 convert_to_human_bytes((pos_in / diff2_tt), outstring2);
398 fprintf(stderr, "-RUNTIME-LOOP-COLLECT-DATA: %s/s read\n", outstring2);
399
400 convert_to_human_bytes((pos_in_local / diff_tt), outstring2);
401 fprintf(stderr, "-RUNTIME-LOOP-COLLECT-DATA (local loop): %s/s read\n", outstring2);
402 }
403 }
404
405 // just in case, commit all we got left over
406 //if (sql_counter > 0)
407 //{
408 sql_counter = 0;
409 sql_counter2 = 0;
410 sql_counter3 = 0;
411 sql_counter4 = 0;
412 sqlite3_exec(sql_handle, "COMMIT", 0, 0, 0);
413 sqlite3_exec(sql_handle002a, "COMMIT", 0, 0, 0);
414 sqlite3_exec(sql_handle003a, "COMMIT", 0, 0, 0);
415 sqlite3_exec(sql_handle002b, "COMMIT", 0, 0, 0);
416 sqlite3_exec(sql_handle003b, "COMMIT", 0, 0, 0);
417 sqlite3_exec(sql_handle004, "COMMIT", 0, 0, 0);
418 sqlite3_exec(sql_handle005, "COMMIT", 0, 0, 0);
419 sqlite3_exec(sql_handle006, "COMMIT", 0, 0, 0);
420 sqlite3_exec(sql_handle007, "COMMIT", 0, 0, 0);
421 fprintf(stderr, "SQL: (final) COMMIT\n");
422 fprintf(stderr, "relations processed:%d\n", processed_relations);
423
424 //}
425
426 //sig_alrm(0);
427 //sig_alrm_end();
428
429 free(buffer_ptr);
430
431 return 1;
432 }
433

   
Visit the ZANavi Wiki