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

Contents of /navit/navit/maptool/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 57 - (hide annotations) (download)
Sun Mar 19 16:46:08 2017 UTC (3 years, 6 months ago) by zoff99
File MIME type: text/plain
File size: 11839 byte(s)
updates
1 zoff99 8 /**
2 zoff99 37 * 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 zoff99 8 * Navit, a modular navigation system.
22     * Copyright (C) 2005-2008 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    
39 zoff99 57 #define NO_GTYPES_ 1
40    
41 zoff99 8 #define _FILE_OFFSET_BITS 64
42     #define _LARGEFILE_SOURCE
43     #define _LARGEFILE64_SOURCE
44     #include <stdlib.h>
45     #include <glib.h>
46     #include <assert.h>
47     #include <string.h>
48     #include <signal.h>
49     #include <stdio.h>
50     #include <math.h>
51     #include <getopt.h>
52     #include <unistd.h>
53     #include <fcntl.h>
54     #include <sys/stat.h>
55     #include <zlib.h>
56     #include "file.h"
57     #include "item.h"
58     #include "map.h"
59     #include "zipfile.h"
60     #include "main.h"
61     #include "config.h"
62     #include "linguistics.h"
63     #include "plugin.h"
64     #include "maptool.h"
65    
66 zoff99 37 struct rect world_bbox =
67     {
68     { -20000000, -20000000 },
69     { 20000000, 20000000 }, };
70 zoff99 8
71 zoff99 37 void bbox_extend(struct coord *c, struct rect *r)
72 zoff99 8 {
73     if (c->x < r->l.x)
74 zoff99 37 r->l.x = c->x;
75 zoff99 8 if (c->y < r->l.y)
76 zoff99 37 r->l.y = c->y;
77 zoff99 8 if (c->x > r->h.x)
78 zoff99 37 r->h.x = c->x;
79 zoff99 8 if (c->y > r->h.y)
80 zoff99 37 r->h.y = c->y;
81 zoff99 8 }
82    
83 zoff99 37 void bbox(struct coord *c, int count, struct rect *r)
84 zoff99 8 {
85 zoff99 37 if (!count)
86 zoff99 8 return;
87 zoff99 37 r->l = *c;
88     r->h = *c;
89     while (--count)
90     {
91 zoff99 8 c++;
92     bbox_extend(c, r);
93     }
94     }
95    
96 zoff99 37 int contains_bbox(int xl, int yl, int xh, int yh, struct rect *r)
97 zoff99 8 {
98 zoff99 37 if (r->h.x < xl || r->h.x > xh)
99     {
100 zoff99 8 return 0;
101     }
102 zoff99 37 if (r->l.x > xh || r->l.x < xl)
103     {
104 zoff99 8 return 0;
105     }
106 zoff99 37 if (r->h.y < yl || r->h.y > yh)
107     {
108 zoff99 8 return 0;
109     }
110 zoff99 37 if (r->l.y > yh || r->l.y < yl)
111     {
112 zoff99 8 return 0;
113     }
114     return 1;
115     }
116    
117 zoff99 37 int bbox_contains_coord(struct rect *r, struct coord *c)
118 zoff99 8 {
119 zoff99 37
120     //fprintf(stderr,"bbox_contains_coord: rhx=%d rhy=%d rlx=%d rly=%d, cx=%d cy=%d\n", r->h.x, r->h.y, r->l.x, r->l.y,c->x, c->y);
121    
122 zoff99 8 if (r->h.x < c->x)
123     return 0;
124     if (r->l.x > c->x)
125     return 0;
126     if (r->h.y < c->y)
127     return 0;
128     if (r->l.y > c->y)
129     return 0;
130     return 1;
131     }
132    
133 zoff99 37 int bbox_contains_bbox(struct rect *out, struct rect *in)
134 zoff99 8 {
135     if (out->h.x < in->h.x)
136     return 0;
137     if (out->l.x > in->l.x)
138     return 0;
139     if (out->h.y < in->h.y)
140     return 0;
141     if (out->l.y > in->l.y)
142     return 0;
143     return 1;
144     }
145    
146 zoff99 37 long long bbox_area(struct rect const *r)
147 zoff99 8 {
148 zoff99 37 return ((long long) r->h.x - r->l.x) * (r->h.y - r->l.y);
149 zoff99 8 }
150    
151 zoff99 37 void phase1_map(GList *maps, FILE *out_ways, FILE *out_nodes)
152 zoff99 8 {
153     struct map_rect *mr;
154     struct item *item;
155 zoff99 37 int count, max = 16384;
156 zoff99 8 struct coord ca[max];
157     struct attr attr;
158     struct item_bin *item_bin;
159    
160 zoff99 37 while (maps)
161     {
162     mr = map_rect_new(maps->data, NULL);
163     while ((item = map_rect_get_item(mr)))
164     {
165     count = item_coord_get(item, ca, item->type < type_line ? 1 : max);
166     item_bin = init_item(item->type, 0);
167 zoff99 8 item_bin_add_coord(item_bin, ca, count);
168 zoff99 37 while (item_attr_get(item, attr_any, &attr))
169     {
170     if (attr.type >= attr_type_string_begin && attr.type <= attr_type_string_end)
171     {
172     attr.u.str = map_convert_string(maps->data, attr.u.str);
173     if (attr.u.str)
174     {
175 zoff99 8 item_bin_add_attr(item_bin, &attr);
176     map_convert_free(attr.u.str);
177     }
178 zoff99 37 }
179     else
180 zoff99 8 item_bin_add_attr(item_bin, &attr);
181     }
182 zoff99 37 if (item->type >= type_line)
183 zoff99 8 item_bin_write(item_bin, out_ways);
184     else
185     item_bin_write(item_bin, out_nodes);
186     }
187     map_rect_destroy(mr);
188 zoff99 37 maps = g_list_next(maps);
189 zoff99 8 }
190     }
191    
192 zoff99 37 static void phase34_process_file(struct tile_info *info, FILE *in, FILE *reference)
193 zoff99 8 {
194     struct item_bin *ib;
195     int max;
196    
197 zoff99 37 while ((ib = read_item(in, 0)))
198     {
199 zoff99 8 if (ib->type < 0x80000000)
200 zoff99 37 {
201 zoff99 8 processed_nodes++;
202 zoff99 37 }
203 zoff99 8 else
204 zoff99 37 {
205 zoff99 8 processed_ways++;
206 zoff99 37 }
207 zoff99 36
208 zoff99 37 if (border_only_map == 1)
209 zoff99 36 {
210     // fprintf(stderr,"max=1\n");
211     // max=1; --> shows all borders, all the time. but is too slow!
212 zoff99 37 max = 4;
213 zoff99 8 }
214 zoff99 36 else
215     {
216 zoff99 37 max = 14;
217 zoff99 36 switch (ib->type)
218     {
219 zoff99 37 case type_town_label_1e7:
220     case type_town_label_5e6:
221     case type_town_label_2e6:
222     case type_town_label_1e6:
223     case type_town_label_5e5:
224     case type_district_label_1e7:
225     case type_district_label_5e6:
226     case type_district_label_2e6:
227     case type_district_label_1e6:
228     case type_district_label_5e5:
229     max = 6;
230     break;
231     case type_town_label_2e5:
232     case type_town_label_1e5:
233     case type_district_label_2e5:
234     case type_district_label_1e5:
235     case type_street_n_lanes:
236     case type_highway_city:
237     case type_highway_land:
238 zoff99 57 //case type_ramp:
239     case type_ramp_highway_land:
240 zoff99 37 max = 8;
241     break;
242     case type_town_label_5e4:
243     case type_town_label_2e4:
244     case type_town_label_1e4:
245     case type_district_label_5e4:
246     case type_district_label_2e4:
247     case type_district_label_1e4:
248     max = 9;
249     break;
250     case type_street_4_land:
251     case type_street_4_city:
252 zoff99 57 case type_ramp_street_4_city:
253 zoff99 37 max = 10;
254     break;
255     case type_town_label_5e3:
256     case type_town_label_2e3:
257     case type_town_label_1e3:
258     case type_district_label_5e3:
259     case type_district_label_2e3:
260     case type_district_label_1e3:
261     case type_street_3_city:
262     case type_street_3_land:
263 zoff99 57 case type_ramp_street_3_city:
264 zoff99 37 max = 12;
265     break;
266     default:
267     break;
268 zoff99 36 }
269     }
270 zoff99 37
271     // dont write "street unknown" items to binfile, if option "to ignore unknown types" is set
272     if ((ib->type == type_street_unkn) && (ignore_unkown == 1))
273     {
274     }
275     else
276     {
277     tile_write_item_minmax(info, ib, reference, 0, max);
278     }
279 zoff99 8 }
280     }
281    
282 zoff99 37 static void phase34_process_file_range(struct tile_info *info, FILE *in, FILE *reference)
283 zoff99 8 {
284     struct item_bin *ib;
285 zoff99 37 int min, max;
286 zoff99 8
287 zoff99 37 while ((ib = read_item_range(in, &min, &max, 0)))
288     {
289 zoff99 8 if (ib->type < 0x80000000)
290     processed_nodes++;
291     else
292     processed_ways++;
293 zoff99 37
294     if ((ib->type == type_street_unkn) && (ignore_unkown == 1))
295     {
296     }
297     else
298     {
299     tile_write_item_minmax(info, ib, reference, min, max);
300     }
301 zoff99 8 }
302     }
303    
304 zoff99 37 static int phase34(struct tile_info *info, struct zip_info *zip_info, FILE **in, FILE **reference, int in_count, int with_range)
305 zoff99 8 {
306     int i;
307    
308 zoff99 37 processed_nodes = processed_nodes_out = processed_ways = processed_relations = processed_tiles = 0;
309    
310     bytes_read = 0;
311     //sig_alrm(0);
312    
313     if (!info->write)
314     {
315     tile_hash = g_hash_table_new(g_str_hash, g_str_equal);
316     }
317    
318     for (i = 0; i < in_count; i++)
319     {
320     if (in[i])
321     {
322 zoff99 8 if (with_range)
323 zoff99 37 {
324     phase34_process_file_range(info, in[i], reference ? reference[i] : NULL);
325     }
326 zoff99 8 else
327 zoff99 37 {
328     phase34_process_file(info, in[i], reference ? reference[i] : NULL);
329     }
330 zoff99 8 }
331     }
332 zoff99 37
333     if (!info->write)
334     {
335 zoff99 8 merge_tiles(info);
336 zoff99 37 }
337    
338     //sig_alrm(0);
339     //sig_alrm_end();
340    
341 zoff99 8 write_tilesdir(info, zip_info, info->tilesdir_out);
342    
343     return 0;
344    
345     }
346    
347 zoff99 37 void dump(FILE *in)
348 zoff99 8 {
349     struct item_bin *ib;
350 zoff99 37 while ((ib = read_item(in, 0)))
351     {
352 zoff99 8 dump_itembin(ib);
353     }
354     }
355    
356 zoff99 37 int phase4(FILE **in, int in_count, int with_range, char *suffix, FILE *tilesdir_out, struct zip_info *zip_info)
357 zoff99 8 {
358     struct tile_info info;
359 zoff99 37 info.write = 0;
360     info.maxlen = 0;
361     info.suffix = suffix;
362     info.tiles_list = NULL;
363     info.tilesdir_out = tilesdir_out;
364 zoff99 8 return phase34(&info, zip_info, in, NULL, in_count, with_range);
365     }
366    
367 zoff99 37 static int process_slice(FILE **in, FILE **reference, int in_count, int with_range, long long size, char *suffix, struct zip_info *zip_info)
368 zoff99 8 {
369     struct tile_head *th;
370 zoff99 37 char *slice_data, *zip_data;
371     int zipfiles = 0;
372 zoff99 8 struct tile_info info;
373     int i;
374    
375 zoff99 37 slice_data = malloc(size);
376 zoff99 8 assert(slice_data != NULL);
377 zoff99 37 zip_data = slice_data;
378     th = tile_head_root;
379     while (th)
380     {
381     if (th->process)
382     {
383     th->zip_data = zip_data;
384     zip_data += th->total_size;
385 zoff99 8 }
386 zoff99 37 th = th->next;
387 zoff99 8 }
388 zoff99 37 for (i = 0; i < in_count; i++)
389     {
390 zoff99 8 if (in[i])
391     fseek(in[i], 0, SEEK_SET);
392 zoff99 37 if (reference && reference[i])
393     {
394 zoff99 8 fseek(reference[i], 0, SEEK_SET);
395     }
396     }
397 zoff99 37 info.write = 1;
398     info.maxlen = zip_get_maxnamelen(zip_info);
399     info.suffix = suffix;
400     info.tiles_list = NULL;
401     info.tilesdir_out = NULL;
402 zoff99 8 phase34(&info, zip_info, in, reference, in_count, with_range);
403    
404 zoff99 37 th = tile_head_root;
405     while (th)
406     {
407     if (th->process)
408     {
409     if (th->name[0])
410     {
411     if (th->total_size != th->total_size_used)
412     {
413     fprintf(stderr, "Size error '%s': %d vs %d\n", th->name, th->total_size, th->total_size_used);
414 zoff99 8 exit(1);
415     }
416     write_zipmember(zip_info, th->name, zip_get_maxnamelen(zip_info), th->zip_data, th->total_size);
417     zipfiles++;
418 zoff99 37 }
419     else
420 zoff99 8 fwrite(th->zip_data, th->total_size, 1, zip_get_index(zip_info));
421     }
422 zoff99 37 th = th->next;
423 zoff99 8 }
424     free(slice_data);
425    
426     return zipfiles;
427     }
428    
429 zoff99 37 int phase5(FILE **in, FILE **references, int in_count, int with_range, char *suffix, struct zip_info *zip_info)
430 zoff99 8 {
431     long long size;
432     int slices;
433 zoff99 37 int zipnum, written_tiles;
434     struct tile_head *th, *th2;
435    
436     time_t start_tt, end_tt;
437     double diff_tt;
438     double diff2_tt;
439    
440 zoff99 8 create_tile_hash();
441    
442 zoff99 37 th = tile_head_root;
443     size = 0;
444     slices = 0;
445 zoff99 57 fprintf_(stderr, "Maximum slice size "LONGLONG_FMT"\n", slice_size);
446 zoff99 37 while (th)
447     {
448     if (size + th->total_size > slice_size)
449     {
450 zoff99 57 fprintf_(stderr,"Slice %d is of size "LONGLONG_FMT"\n", slices, size);
451 zoff99 37 size = 0;
452 zoff99 8 slices++;
453     }
454 zoff99 37 size += th->total_size;
455     th = th->next;
456 zoff99 8 }
457 zoff99 37
458 zoff99 8 if (size)
459 zoff99 37 {
460 zoff99 57 fprintf_(stderr,"Slice %d is of size "LONGLONG_FMT"\n", slices, size);
461 zoff99 37 }
462    
463     int max_slices_ = slices;
464     th = tile_head_root;
465     size = 0;
466     slices = 0;
467    
468     while (th)
469     {
470     time(&start_tt);
471    
472 zoff99 57 fprintf_(stderr,"Slice #%d of %d\n", (slices + 1), (max_slices_ + 1));
473 zoff99 37
474     th2 = tile_head_root;
475     while (th2)
476     {
477     th2->process = 0;
478     th2 = th2->next;
479 zoff99 8 }
480 zoff99 37
481     size = 0;
482     while (th && size + th->total_size < slice_size)
483     {
484     size += th->total_size;
485     th->process = 1;
486     th = th->next;
487 zoff99 8 }
488 zoff99 37
489 zoff99 8 /* process_slice() modifies zip_info, but need to retain old info */
490 zoff99 37 zipnum = zip_get_zipnum(zip_info);
491     written_tiles = process_slice(in, references, in_count, with_range, size, suffix, zip_info);
492     zip_set_zipnum(zip_info, zipnum + written_tiles);
493    
494     time(&end_tt);
495     diff_tt = difftime(end_tt,start_tt);
496     char outstring[200];
497     convert_to_human_time(diff_tt, outstring);
498 zoff99 57 fprintf_(stderr, "-RUNTIME-LOOP-PHASE5: %s this loop run\n", outstring);
499 zoff99 37 diff2_tt = diff2_tt + diff_tt;
500     if ((slices + 1) > 0)
501     {
502     double eta_time = (diff2_tt / (slices + 1)) * (max_slices_ + 1 - (slices + 1));
503     convert_to_human_time(eta_time, outstring);
504 zoff99 57 fprintf_(stderr, "-RUNTIME-LOOP-PHASE5: %s left\n", outstring);
505 zoff99 37 }
506    
507 zoff99 8 slices++;
508     }
509 zoff99 37
510 zoff99 8 return 0;
511     }
512    
513 zoff99 37 void process_binfile(FILE *in, FILE *out)
514 zoff99 8 {
515     struct item_bin *ib;
516 zoff99 37 while ((ib = read_item(in, 0)))
517     {
518     fwrite(ib, (ib->len + 1) * 4, 1, out);
519 zoff99 8 }
520     }
521    
522 zoff99 37 void add_aux_tiles(char *name, struct zip_info *info)
523 zoff99 8 {
524     char buffer[4096];
525     char *s;
526     FILE *in;
527     FILE *tmp;
528 zoff99 37 in = fopen(name, "rb");
529 zoff99 8 if (!in)
530     return;
531 zoff99 37 while (fscanf(in, "%s", buffer) == 1)
532     {
533     s = strchr(buffer, '/');
534 zoff99 8 if (s)
535     s++;
536     else
537 zoff99 37 s = buffer;
538     tmp = fopen(buffer, "rb");
539     if (tmp)
540     {
541 zoff99 8 fseek(tmp, 0, SEEK_END);
542     add_aux_tile(info, s, buffer, ftell(tmp));
543     fclose(tmp);
544     }
545     }
546     fclose(in);
547     }

   
Visit the ZANavi Wiki