1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
|
/**
* Navit, a modular navigation system.
* Copyright (C) 2005-2008 Navit Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include <glib.h>
#include <assert.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <zlib.h>
#include "file.h"
#include "item.h"
#include "map.h"
#include "zipfile.h"
#include "main.h"
#include "config.h"
#include "linguistics.h"
#include "plugin.h"
#ifdef HAVE_POSTGRESQL
#include <libpq-fe.h>
#endif
#include "maptool.h"
GList *aux_tile_list;
struct tile_head *tile_head_root;
GHashTable *strings_hash,*tile_hash,*tile_hash2;
static char* string_hash_lookup( const char* key )
{
char* key_ptr = NULL;
if ( strings_hash == NULL ) {
strings_hash = g_hash_table_new(g_str_hash, g_str_equal);
}
if ( ( key_ptr = g_hash_table_lookup(strings_hash, key )) == NULL ) {
key_ptr = g_strdup( key );
g_hash_table_insert(strings_hash, key_ptr, (gpointer)key_ptr );
}
return key_ptr;
}
static char** th_get_subtile( const struct tile_head* th, int idx )
{
char* subtile_ptr = NULL;
subtile_ptr = (char*)th + sizeof( struct tile_head ) + idx * sizeof( char *);
return (char**)subtile_ptr;
}
int
tile(struct rect *r, char *suffix, char *ret, int max, int overlap, struct rect *tr)
{
int x0,x2,x4;
int y0,y2,y4;
int xo,yo;
int i;
x0=world_bbox.l.x;
y0=world_bbox.l.y;
x4=world_bbox.h.x;
y4=world_bbox.h.y;
for (i = 0 ; i < max ; i++) {
x2=(x0+x4)/2;
y2=(y0+y4)/2;
xo=(x4-x0)*overlap/100;
yo=(y4-y0)*overlap/100;
if ( contains_bbox(x0,y0,x2+xo,y2+yo,r)) {
strcat(ret,"d");
x4=x2+xo;
y4=y2+yo;
} else if (contains_bbox(x2-xo,y0,x4,y2+yo,r)) {
strcat(ret,"c");
x0=x2-xo;
y4=y2+yo;
} else if (contains_bbox(x0,y2-yo,x2+xo,y4,r)) {
strcat(ret,"b");
x4=x2+xo;
y0=y2-yo;
} else if (contains_bbox(x2-xo,y2-yo,x4,y4,r)) {
strcat(ret,"a");
x0=x2-xo;
y0=y2-yo;
} else
break;
}
if (tr) {
tr->l.x=x0;
tr->l.y=y0;
tr->h.x=x4;
tr->h.y=y4;
}
if (suffix)
strcat(ret,suffix);
return i;
}
void
tile_bbox(char *tile, struct rect *r, int overlap)
{
struct coord c;
int xo,yo;
*r=world_bbox;
while (*tile) {
c.x=(r->l.x+r->h.x)/2;
c.y=(r->l.y+r->h.y)/2;
xo=(r->h.x-r->l.x)*overlap/100;
yo=(r->h.y-r->l.y)*overlap/100;
switch (*tile) {
case 'a':
r->l.x=c.x-xo;
r->l.y=c.y-yo;
break;
case 'b':
r->h.x=c.x+xo;
r->l.y=c.y-yo;
break;
case 'c':
r->l.x=c.x-xo;
r->h.y=c.y+yo;
break;
case 'd':
r->h.x=c.x+xo;
r->h.y=c.y+yo;
break;
}
tile++;
}
}
int
tile_len(char *tile)
{
int ret=0;
while (tile[0] >= 'a' && tile[0] <= 'd') {
tile++;
ret++;
}
return ret;
}
static void
tile_extend(char *tile, struct item_bin *ib, GList **tiles_list)
{
struct tile_head *th=NULL;
if (debug_tile(tile))
fprintf(stderr,"Tile:Writing %d bytes to '%s' (%p,%p) 0x%x %Ld\n", (ib->len+1)*4, tile, g_hash_table_lookup(tile_hash, tile), tile_hash2 ? g_hash_table_lookup(tile_hash2, tile) : NULL, ib->type, item_bin_get_id(ib));
if (tile_hash2)
th=g_hash_table_lookup(tile_hash2, tile);
if (!th)
th=g_hash_table_lookup(tile_hash, tile);
if (! th) {
th=malloc(sizeof(struct tile_head)+ sizeof( char* ) );
assert(th != NULL);
// strcpy(th->subtiles, tile);
th->num_subtiles=1;
th->total_size=0;
th->total_size_used=0;
th->zipnum=0;
th->zip_data=NULL;
th->name=string_hash_lookup(tile);
*th_get_subtile( th, 0 ) = th->name;
if (tile_hash2)
g_hash_table_insert(tile_hash2, string_hash_lookup( th->name ), th);
if (tiles_list)
*tiles_list=g_list_append(*tiles_list, string_hash_lookup( th->name ) );
processed_tiles++;
if (debug_tile(tile))
fprintf(stderr,"new '%s'\n", tile);
}
th->total_size+=ib->len*4+4;
if (debug_tile(tile))
fprintf(stderr,"New total size of %s(%p):%d\n", th->name, th, th->total_size);
g_hash_table_insert(tile_hash, string_hash_lookup( th->name ), th);
}
static int
tile_data_size(char *tile)
{
struct tile_head *th;
th=g_hash_table_lookup(tile_hash, tile);
if (! th)
return 0;
return th->total_size;
}
static int
merge_tile(char *base, char *sub)
{
struct tile_head *thb, *ths;
thb=g_hash_table_lookup(tile_hash, base);
ths=g_hash_table_lookup(tile_hash, sub);
if (! ths)
return 0;
if (debug_tile(base) || debug_tile(sub))
fprintf(stderr,"merging '%s'(%p) (%d) with '%s'(%p) (%d)\n", base, thb, thb ? thb->total_size : 0, sub, ths, ths->total_size);
if (! thb) {
thb=ths;
g_hash_table_remove(tile_hash, sub);
thb->name=string_hash_lookup(base);
g_hash_table_insert(tile_hash, string_hash_lookup( thb->name ), thb);
} else {
thb=realloc(thb, sizeof(struct tile_head)+( ths->num_subtiles+thb->num_subtiles ) * sizeof( char*) );
assert(thb != NULL);
memcpy( th_get_subtile( thb, thb->num_subtiles ), th_get_subtile( ths, 0 ), ths->num_subtiles * sizeof( char*) );
thb->num_subtiles+=ths->num_subtiles;
thb->total_size+=ths->total_size;
g_hash_table_insert(tile_hash, string_hash_lookup( thb->name ), thb);
g_hash_table_remove(tile_hash, sub);
g_free(ths);
}
return 1;
}
static void
get_tiles_list_func(char *key, struct tile_head *th, GList **list)
{
*list=g_list_prepend(*list, key);
}
static GList *
get_tiles_list(void)
{
GList *ret=NULL;
g_hash_table_foreach(tile_hash, (GHFunc)get_tiles_list_func, &ret);
return ret;
}
#if 0
static void
write_tile(char *key, struct tile_head *th, gpointer dummy)
{
FILE *f;
char buffer[1024];
fprintf(stderr,"DEBUG: Writing %s\n", key);
strcpy(buffer,"tiles/");
strcat(buffer,key);
#if 0
strcat(buffer,".bin");
#endif
f=fopen(buffer, "wb+");
while (th) {
fwrite(th->data, th->size, 1, f);
th=th->next;
}
fclose(f);
}
#endif
static void
write_item(char *tile, struct item_bin *ib, FILE *reference)
{
struct tile_head *th;
int size;
th=g_hash_table_lookup(tile_hash2, tile);
if (debug_itembin(ib)) {
fprintf(stderr,"tile head %p\n",th);
}
if (! th)
th=g_hash_table_lookup(tile_hash, tile);
if (th) {
if (debug_itembin(ib)) {
fprintf(stderr,"Match %s %d %s\n",tile,th->process,th->name);
dump_itembin(ib);
}
if (th->process != 0 && th->process != 1) {
fprintf(stderr,"error with tile '%s' of length %d\n", tile, (int)strlen(tile));
abort();
}
if (! th->process) {
if (reference)
fseek(reference, 8, SEEK_CUR);
return;
}
if (debug_tile(tile))
fprintf(stderr,"Data:Writing %d bytes to '%s' (%p,%p) 0x%x\n", (ib->len+1)*4, tile, g_hash_table_lookup(tile_hash, tile), tile_hash2 ? g_hash_table_lookup(tile_hash2, tile) : NULL, ib->type);
size=(ib->len+1)*4;
if (th->total_size_used+size > th->total_size) {
fprintf(stderr,"Overflow in tile %s (used %d max %d item %d)\n", tile, th->total_size_used, th->total_size, size);
exit(1);
return;
}
if (reference) {
fwrite(&th->zipnum, sizeof(th->zipnum), 1, reference);
fwrite(&th->total_size_used, sizeof(th->total_size_used), 1, reference);
}
memcpy(th->zip_data+th->total_size_used, ib, size);
th->total_size_used+=size;
} else {
fprintf(stderr,"no tile hash found for %s\n", tile);
exit(1);
}
}
void
tile_write_item_to_tile(struct tile_info *info, struct item_bin *ib, FILE *reference, char *name)
{
if (info->write)
write_item(name, ib, reference);
else
tile_extend(name, ib, info->tiles_list);
}
static void
tile_write_item_minmax(struct tile_info *info, struct item_bin *ib, FILE *reference, int min, int max)
{
struct rect r;
char buffer[1024];
bbox((struct coord *)(ib+1), ib->clen/2, &r);
buffer[0]='\0';
tile(&r, info->suffix, buffer, max, overlap, NULL);
tile_write_item_to_tile(info, ib, reference, buffer);
}
int
add_aux_tile(struct zip_info *zip_info, char *name, char *filename, int size)
{
struct aux_tile *at;
GList *l;
l=aux_tile_list;
while (l) {
at=l->data;
if (!strcmp(at->name, name)) {
fprintf(stderr,"exists %s vs %s\n",at->name, name);
return -1;
}
l=g_list_next(l);
}
at=g_new0(struct aux_tile, 1);
at->name=g_strdup(name);
at->filename=g_strdup(filename);
at->size=size;
aux_tile_list=g_list_append(aux_tile_list, at);
return zip_info->zipnum++;
}
int
write_aux_tiles(struct zip_info *zip_info)
{
GList *l=aux_tile_list;
struct aux_tile *at;
char *buffer;
FILE *f;
int count=0;
while (l) {
at=l->data;
buffer=malloc(at->size);
assert(buffer != NULL);
f=fopen(at->filename,"rb");
assert(f != NULL);
fread(buffer, at->size, 1, f);
fclose(f);
write_zipmember(zip_info, at->name, zip_info->maxnamelen, buffer, at->size);
free(buffer);
count++;
l=g_list_next(l);
zip_info->zipnum++;
}
return count;
}
static int
add_tile_hash(struct tile_head *th)
{
int idx,len,maxnamelen=0;
char **data;
#if 0
g_hash_table_insert(tile_hash2, string_hash_lookup( th->name ), th);
#endif
for( idx = 0; idx < th->num_subtiles; idx++ ) {
data = th_get_subtile( th, idx );
if (debug_tile(((char *)data)) || debug_tile(th->name)) {
fprintf(stderr,"Parent for '%s' is '%s'\n", *data, th->name);
}
g_hash_table_insert(tile_hash2, *data, th);
len = strlen( *data );
if (len > maxnamelen) {
maxnamelen=len;
}
}
return maxnamelen;
}
int
create_tile_hash(void)
{
struct tile_head *th;
int len,maxnamelen=0;
tile_hash2=g_hash_table_new(g_str_hash, g_str_equal);
th=tile_head_root;
while (th) {
len=add_tile_hash(th);
if (len > maxnamelen)
maxnamelen=len;
th=th->next;
}
return maxnamelen;
}
static void
create_tile_hash_list(GList *list)
{
GList *next;
struct tile_head *th;
tile_hash2=g_hash_table_new(g_str_hash, g_str_equal);
next=g_list_first(list);
while (next) {
th=g_hash_table_lookup(tile_hash, next->data);
if (!th) {
fprintf(stderr,"No tile found for '%s'\n", (char *)(next->data));
}
add_tile_hash(th);
next=g_list_next(next);
}
}
void
write_tilesdir(struct tile_info *info, struct zip_info *zip_info, FILE *out)
{
int idx,len,maxlen;
GList *next,*tiles_list;
char **data;
struct tile_head *th,**last=NULL;
tiles_list=get_tiles_list();
info->tiles_list=&tiles_list;
if (phase == 3)
create_tile_hash_list(tiles_list);
next=g_list_first(tiles_list);
last=&tile_head_root;
maxlen=info->maxlen;
if (! maxlen) {
while (next) {
if (strlen(next->data) > maxlen)
maxlen=strlen(next->data);
next=g_list_next(next);
}
}
len=maxlen;
while (len >= 0) {
#if 0
fprintf(stderr,"PROGRESS: collecting tiles with len=%d\n", len);
#endif
next=g_list_first(tiles_list);
while (next) {
if (strlen(next->data) == len) {
th=g_hash_table_lookup(tile_hash, next->data);
if (phase == 3) {
*last=th;
last=&th->next;
th->next=NULL;
th->zipnum=zip_info->zipnum;
fprintf(out,"%s:%d",(char *)next->data,th->total_size);
for ( idx = 0; idx< th->num_subtiles; idx++ ){
data= th_get_subtile( th, idx );
fprintf(out,":%s", *data);
}
fprintf(out,"\n");
}
if (th->name[0])
index_submap_add(info, th);
zip_info->zipnum++;
processed_tiles++;
}
next=g_list_next(next);
}
len--;
}
}
void
merge_tiles(struct tile_info *info)
{
struct tile_head *th;
char basetile[1024];
char subtile[1024];
GList *tiles_list_sorted,*last;
int i,i_min,len,size_all,size[5],size_min,work_done;
long long zip_size;
do {
tiles_list_sorted=get_tiles_list();
fprintf(stderr,"PROGRESS: sorting %d tiles\n", g_list_length(tiles_list_sorted));
tiles_list_sorted=g_list_sort(tiles_list_sorted, (GCompareFunc)strcmp);
fprintf(stderr,"PROGRESS: sorting %d tiles done\n", g_list_length(tiles_list_sorted));
last=g_list_last(tiles_list_sorted);
zip_size=0;
while (last) {
th=g_hash_table_lookup(tile_hash, last->data);
zip_size+=th->total_size;
last=g_list_previous(last);
}
last=g_list_last(tiles_list_sorted);
work_done=0;
while (last) {
processed_tiles++;
len=tile_len(last->data);
if (len >= 1) {
strcpy(basetile,last->data);
basetile[len-1]='\0';
strcat(basetile, info->suffix);
strcpy(subtile,last->data);
for (i = 0 ; i < 4 ; i++) {
subtile[len-1]='a'+i;
size[i]=tile_data_size(subtile);
}
size[4]=tile_data_size(basetile);
size_all=size[0]+size[1]+size[2]+size[3]+size[4];
if (size_all < 65536 && size_all > 0 && size_all != size[4]) {
for (i = 0 ; i < 4 ; i++) {
subtile[len-1]='a'+i;
work_done+=merge_tile(basetile, subtile);
}
} else {
for (;;) {
size_min=size_all;
i_min=-1;
for (i = 0 ; i < 4 ; i++) {
if (size[i] && size[i] < size_min) {
size_min=size[i];
i_min=i;
}
}
if (i_min == -1)
break;
if (size[4]+size_min >= 65536)
break;
subtile[len-1]='a'+i_min;
work_done+=merge_tile(basetile, subtile);
size[4]+=size[i_min];
size[i_min]=0;
}
}
}
last=g_list_previous(last);
}
g_list_free(tiles_list_sorted);
fprintf(stderr,"PROGRESS: merged %d tiles\n", work_done);
} while (work_done);
}
void
index_init(struct zip_info *info, int version)
{
item_bin_init(item_bin, type_map_information);
item_bin_add_attr_int(item_bin, attr_version, version);
item_bin_write(item_bin, info->index);
}
void
index_submap_add(struct tile_info *info, struct tile_head *th)
{
int tlen=tile_len(th->name);
int len=tlen;
char index_tile[len+1+strlen(info->suffix)];
struct rect r;
strcpy(index_tile, th->name);
if (len > 6)
len=6;
else
len=0;
index_tile[len]=0;
if (tlen)
strcat(index_tile, info->suffix);
tile_bbox(th->name, &r, overlap);
item_bin_init(item_bin, type_submap);
item_bin_add_coord_rect(item_bin, &r);
item_bin_add_attr_range(item_bin, attr_order, (tlen > 4)?tlen-4 : 0, 255);
item_bin_add_attr_int(item_bin, attr_zipfile_ref, th->zipnum);
tile_write_item_to_tile(info, item_bin, NULL, index_tile);
}
|