File: garmin.c

package info (click to toggle)
navit 0.5.3%2Bdfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 27,740 kB
  • sloc: ansic: 171,689; sh: 5,496; cpp: 5,439; perl: 4,474; java: 3,995; xml: 1,411; objc: 1,004; php: 444; yacc: 288; python: 147; makefile: 56
file content (934 lines) | stat: -rw-r--r-- 25,579 bytes parent folder | download
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/*
	Copyright (C) 2007  Alexander Atanasov      <aatanasov@gmail.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; version 2 of the License.

	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

	Garmin and MapSource are registered trademarks or trademarks
	of Garmin Ltd. or one of its subsidiaries.

*/

#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "config.h"
#include "plugin.h"
#include "data.h"
#include "projection.h"
#include "item.h"
#include "debug.h"
#include "map.h"
#include "maptype.h"
#include "attr.h"
#include "coord.h"
#include "transform.h"
#include <stdio.h>
#include "attr.h"
#include "coord.h"
#include <libgarmin.h>
#include "garmin.h"
#include "gar2navit.h"
#include "../../file.h"


static int map_id;

struct map_priv {
    int id;
    char *filename;
    struct gar2nav_conv *conv;
    struct gar *g;
};

struct map_rect_priv {
    int id;
    struct coord_rect r;
    char *label;		// FIXME: Register all strings for searches
    int limit;
    struct map_priv *mpriv;
    struct gmap *gmap;
    struct gobject *cobj;
    struct gobject *objs;
    struct item item;
    unsigned int last_coord;
    void *last_itterated;
    struct coord last_c;
    void *last_oattr;
    unsigned int last_attr;
    struct gar_search *search;
};

int garmin_debug = 10;

void logfn(char *file, int line, int level, char *fmt, ...) {
    va_list ap;
    char fileline[256];
    int sz;
    if (level > garmin_debug)
        return;
    va_start(ap, fmt);
    sz = sprintf(fileline, "%s:%d:%d|", file, line, level);
    debug_vprintf(0, "", strlen(""), fileline, sz,
                  1, fmt, ap);
    va_end(ap);
}
// need a base map and a map
struct gscale {
    char *label;
    float scale;
    int bits;
};

static struct gscale mapscales[] = {
    {"7000 km", 70000.0, 8}
    ,{"5000 km", 50000.0, 8}
    ,{"3000 km", 30000.0, 9}
    ,{"2000 km", 20000.0, 9}
    ,{"1500 km", 15000.0, 10}
    ,{"1000 km", 10000.0, 10}
    ,{"700 km", 7000.0, 11}
    ,{"500 km", 5000.0, 11}
    ,{"300 km", 3000.0, 13}
    ,{"200 km", 2000.0, 13}
    ,{"150 km", 1500.0, 13}
    ,{"100 km", 1000.0, 14}
    ,{"70 km", 700.0, 15}
    ,{"50 km", 500.0, 16}
    ,{"30 km", 300.0, 16}
    ,{"20 km", 200.0, 17}
    ,{"15 km", 150.0, 17}
    ,{"10 km", 100.0, 18}
    ,{"7 km", 70.0, 18}
    ,{"5 km", 50.0, 19}
    ,{"3 km", 30.0, 19}
    ,{"2 km", 20.0, 20}
    ,{"1.5 km", 15.0, 22}
    ,{"1 km", 10.0, 24}
    ,{"700 m", 7.0, 24}
    ,{"500 m", 5.0, 24}
    ,{"300 m", 3.0, 24}
    ,{"200 m", 2.0, 24}
    ,{"150 m", 1.5, 24}
    ,{"100 m", 1.0, 24}
    ,{"70 m", 0.7, 24}
    ,{"50 m", 0.5, 24}
    ,{"30 m", 0.3, 24}
    ,{"20 m", 0.2, 24}
    ,{"15 m", 0.1, 24}
    ,{"10 m", 0.15, 24}
};


static int garmin_object_label(struct gobject *o, struct attr *attr) {
    struct map_rect_priv *mr = o->priv_data;
    char *codepage;
    char *label;
    if (!mr) {
        dlog(1, "Error object do not have priv_data!!\n");
        return 0;
    }
    if (mr->label) {
        free(mr->label);
    }
    label = gar_get_object_lbl(o);
    if (label) {
        codepage = gar_obj_codepage(o);
        if (*codepage != 'a') {
            mr->label = g_convert(label, -1,"utf-8",codepage,NULL,NULL,NULL);
            free(label);
        } else
            mr->label = label;
    } else {
        mr->label = NULL;
        return 0;
    }
    if (mr->label) {
        char *cp = mr->label;
        /* FIXME Process label and give only the visible part */
        if (*mr->label == '@' || *mr->label == '^')
            cp++;
        /* FIXME: If zoomlevel is high convert ^ in the string to spaces */
        attr->u.str = cp;
        return 1;
    }
    return 0;
}

static int garmin_object_debug(struct gobject *o, struct attr *attr) {
    struct map_rect_priv *mr = o->priv_data;
    if (!mr) {
        dlog(1, "Error object do not have priv_data!!\n");
        return 0;
    }
    if (mr->label)
        free(mr->label);
    mr->label = gar_object_debug_str(o);
    if (mr->label) {
        attr->u.str = mr->label;
        return 1;
    }
    return 0;
}


static struct map_search_priv *gmap_search_new(struct map_priv *map, struct item *item, struct attr *search,
        int partial) {
    struct map_rect_priv *mr=g_new0(struct map_rect_priv, 1);
    struct gar_search *gs;
    int rc;

    dlog(1, "Called!\n");
    mr->mpriv=map;
    gs = g_new0(struct gar_search,1);
    if (!gs) {
        dlog(1, "Can not init search \n");
        free(mr);
        return NULL;
    }
    mr->search = gs;
    switch (search->type) {
    case attr_country_name:
        gs->type = GS_COUNTRY;
        break;
    case attr_town_name:
        gs->type = GS_CITY;
        break;
    case attr_town_postal:
        gs->type = GS_ZIP;
        break;
    case attr_street_name:
        gs->type = GS_ROAD;
        break;
#if 0 /* someday */
    case attr_region_name:
    case attr_intersection:
    case attr_housenumber:
#endif
    default:
        dlog(1, "Don't know how to search for %d\n", search->type);
        goto out_err;
    }
    gs->match = partial ? GM_START : GM_EXACT;
    gs->needle = strdup(search->u.str);
    dlog(5, "Needle: %s\n", gs->needle);

    mr->gmap = gar_find_subfiles(mr->mpriv->g, gs, GO_GET_SEARCH);
    if (!mr->gmap) {
        dlog(1, "Can not init search \n");
        goto out_err;
    }
    rc = gar_get_objects(mr->gmap, 0, gs, &mr->objs, GO_GET_SEARCH);
    if (rc < 0) {
        dlog(1, "Error loading objects\n");
        goto out_err;
    }
    mr->cobj = mr->objs;
    dlog(4, "Loaded %d objects\n", rc);
    return (struct map_search_priv *)mr;

out_err:
    free(gs);
    free(mr);
    return NULL;
}

/* Assumes that only one item will be itterated at time! */
static void coord_rewind(void *priv_data) {
    struct gobject *g = priv_data;
    struct map_rect_priv *mr = g->priv_data;
    mr->last_coord = 0;
};

static void attr_rewind(void *priv_data) {
    struct gobject *g = priv_data;
    struct map_rect_priv *mr = g->priv_data;
    mr->last_attr = 0;
};

static int point_coord_get(void *priv_data, struct coord *c, int count) {
    struct gobject *g = priv_data;
    struct map_rect_priv *mr = g->priv_data;
    struct gcoord gc;
    if (!count)
        return 0;
    if (g != mr->last_itterated) {
        mr->last_itterated = g;
        mr->last_coord = 0;
    }

    if (mr->last_coord > 0)
        return 0;

    gar_get_object_coord(mr->gmap, g, &gc);
    c->x = gc.x;
    c->y = gc.y;
    mr->last_coord++;
//	dlog(1,"point: x=%d y=%d\n", c->x, c->y);
    // dlog(1, "point: x=%f y=%f\n", GARDEG(c->x), GARDEG(c->y));
    return 1;
}

static int coord_is_node(void *priv_data) {
    struct gobject *g = priv_data;
    struct map_rect_priv *mr = g->priv_data;

    return gar_is_object_dcoord_node(mr->gmap, g, mr->last_coord);
}

static int poly_coord_get(void *priv_data, struct coord *c, int count) {
    struct gobject *g = priv_data;
    struct map_rect_priv *mr = g->priv_data;
    int ndeltas = 0, total = 0;
    struct gcoord dc;

    if (!count)
        return 0;

    if (g != mr->last_itterated) {
        mr->last_itterated = g;
        mr->last_coord = 0;
    }
    ndeltas = gar_get_object_deltas(g);
    if (mr->last_coord > ndeltas + 1)
        return 0;
    while (count --) {
        if (mr->last_coord == 0) {
            gar_get_object_coord(mr->gmap, g, &dc);
            mr->last_c.x = dc.x;
            mr->last_c.y = dc.y;
        } else {
            if (!gar_get_object_dcoord(mr->gmap, g, mr->last_coord - 1, &dc)) {
                mr->last_coord = ndeltas + 2;
                return total;
            }
            mr->last_c.x += dc.x;
            mr->last_c.y += dc.y;
        }
        c->x = mr->last_c.x;
        c->y = mr->last_c.y;
        ddlog(1, "poly: x=%f y=%f\n", GARDEG(c->x), GARDEG(c->y));
//		dlog(1,"poly: x=%d y=%d\n", c->x, c->y);
        c++;
        total++;
        mr->last_coord ++;
    }
    return total;
}

// for _any we must return one by one
static int point_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr) {
    struct gobject *g = priv_data;
    struct map_rect_priv *mr = g->priv_data;
    int rc;
    switch (attr_type) {
    case attr_any:
        if (g != mr->last_oattr) {
            mr->last_oattr = g;
            mr->last_attr = 0;
        }
        switch(mr->last_attr) {
        case 0:
            mr->last_attr++;
            attr->type = attr_label;
            rc = garmin_object_label(g, attr);
            if (rc)
                return rc;
        case 1:
            mr->last_attr++;
            attr->type = attr_debug;
            rc = garmin_object_debug(g, attr);
            if (rc)
                return rc;
        case 2:
            mr->last_attr++;
            if (g->type == GO_POLYLINE) {
                attr->type = attr_street_name;
                rc = garmin_object_label(g, attr);
                if (rc)
                    return rc;
            }
        case 3:
            mr->last_attr++;
            attr->type = attr_flags;
            attr->u.num = 0;
            rc = gar_object_flags(g);
            if (rc & F_ONEWAY)
                attr->u.num |= AF_ONEWAY;
            if (rc & F_SEGMENTED)
                attr->u.num |= AF_SEGMENTED;
            return 1;
        default:
            return 0;
        }
        break;
    case attr_label:
        attr->type = attr_label;
        return garmin_object_label(g, attr);
    case attr_town_name:
        attr->type = attr_town_name;
        return garmin_object_label(g, attr);
    case attr_street_name:
        attr->type = attr_type;
        return garmin_object_label(g, attr);
    case attr_street_name_systematic:
        /* TODO: Get secondary labels of roads */
        return 0;
    case attr_flags:
        attr->type = attr_flags;
        attr->u.num = 0;
        rc = gar_object_flags(g);
        if (rc & F_ONEWAY)
            attr->u.num |= AF_ONEWAY;
        if (rc & F_SEGMENTED)
            attr->u.num |= AF_SEGMENTED;
        return 1;
    default:
        dlog(1, "Don't know about attribute %d[%04X]=%s yet\n", attr_type,attr_type, attr_to_name(attr_type));
    }

    return 0;
}

static struct item_methods methods_garmin_point = {
    coord_rewind,
    point_coord_get,
    attr_rewind,
    point_attr_get,
};

static struct item_methods methods_garmin_poly = {
    coord_rewind,
    poly_coord_get,
    attr_rewind,	// point_attr_rewind,
    point_attr_get,	// poly_attr_get,
    coord_is_node,
};

static int search_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr) {
    struct gobject *g = priv_data;
    struct map_rect_priv *mr = g->priv_data;
    int rc;
    switch (attr_type) {
    case attr_any:
        if (g != mr->last_oattr) {
            mr->last_oattr = g;
            mr->last_attr = 0;
        }
        switch(mr->last_attr) {
        case 0:
            mr->last_attr++;
            attr->type = attr_label;
            rc = garmin_object_label(g, attr);
            if (rc)
                return rc;
        case 1:
            mr->last_attr++;
            attr->type = attr_debug;
            rc = garmin_object_debug(g, attr);
            if (rc)
                return rc;
        case 2:
            mr->last_attr++;
            if (g->type == GO_POLYLINE) {
                attr->type = attr_street_name;
                rc = garmin_object_label(g, attr);
                if (rc)
                    return rc;
            }
        case 3:
            mr->last_attr++;
            attr->type = attr_flags;
            attr->u.num = 0;
            rc = gar_object_flags(g);
            if (rc & F_ONEWAY)
                attr->u.num |= AF_ONEWAY;
            if (rc & F_SEGMENTED)
                attr->u.num |= AF_SEGMENTED;
            return 1;
        default:
            return 0;
        }
        break;
    case attr_label:
        attr->type = attr_label;
        return garmin_object_label(g, attr);
    case attr_town_name:
        attr->type = attr_town_name;
        if (mr->label)
            free(mr->label);
        mr->label = gar_srch_get_city(g);
        attr->u.str = mr->label;
        if (attr->u.str)
            return 1;
        return 0;
    case attr_town_id:
        rc = gar_srch_get_cityid(g);
        if (rc) {
            attr->type = attr_town_id;
            attr->u.num = rc;
            return 1;
        }
        return 0;
    case attr_town_postal:
        attr->type = attr_town_postal;
        attr->u.str = gar_srch_get_zip(g);
        if (attr->u.str)
            return 1;
        return 0;
    case attr_street_name:
        attr->type = attr_street_name;
        if (mr->label)
            free(mr->label);
        mr->label = gar_srch_get_roadname(g);
        attr->u.str = mr->label;
        if (attr->u.str)
            return 1;
        return 0;
    case attr_street_id:
        attr->type = attr_street_id;
        attr->u.num = gar_srch_get_roadid(g);
        if (attr->u.num)
            return 1;
        return 0;
    case attr_flags:
        attr->type = attr_flags;
        attr->u.num = 0;
        rc = gar_object_flags(g);
        if (rc & F_ONEWAY)
            attr->u.num |= AF_ONEWAY;
        if (rc & F_SEGMENTED)
            attr->u.num |= AF_SEGMENTED;
        return 1;
    case attr_country_id:
        rc = gar_srch_get_countryid(g);
        if (rc) {
            attr->type = attr_country_id;
            attr->u.num = rc;
            return 1;
        }
        return 0;
    case attr_country_name:
        attr->type = attr_country_name;
        attr->u.str = gar_srch_get_country(g);
        if (attr->u.str)
            return 1;
        return 0;
    case attr_district_id:
        rc = gar_srch_get_regionid(g);
        if (rc) {
            attr->type = attr_district_id;
            attr->u.num = rc;
            return 1;
        }
        return 0;
    case attr_district_name:
        attr->type = attr_district_name;
        attr->u.str = gar_srch_get_region(g);
        if (attr->u.str)
            return 1;
        return 0;
    case attr_town_streets_item:
        return 0;
    default:
        dlog(1, "Don't know about attribute %d[%04X]=%s yet\n",
             attr_type,attr_type, attr_to_name(attr_type));
    }

    return 0;
}

static int search_coord_get(void *priv_data, struct coord *c, int count) {
    struct gobject *g = priv_data;
    struct map_rect_priv *mr = g->priv_data;
    struct gcoord gc;
    if (!count)
        return 0;
    if (g != mr->last_itterated) {
        mr->last_itterated = g;
        mr->last_coord = 0;
    }

    if (mr->last_coord > 0)
        return 0;

    if (gar_get_object_coord(mr->gmap, g, &gc)) {
        c->x = gc.x;
        c->y = gc.y;
        mr->last_coord++;
        return 1;
    }
    return 0;
}

static struct item_methods methods_garmin_search = {
    coord_rewind,
    search_coord_get,
    attr_rewind,
    search_attr_get,
};


static struct item *garmin_poi2item(struct map_rect_priv *mr, struct gobject *o, unsigned short otype) {
    if (mr->mpriv->conv) {
        int mask = gar_object_group(o) << G2N_KIND_SHIFT;
        mr->item.type = g2n_get_type(mr->mpriv->conv, G2N_POINT|mask, otype);
    }
    mr->item.meth = &methods_garmin_point;
    return &mr->item;
}

static struct item *garmin_pl2item(struct map_rect_priv *mr, struct gobject *o, unsigned short otype) {
    if (mr->mpriv->conv) {
        int mask = gar_object_group(o) << G2N_KIND_SHIFT;
        mr->item.type = g2n_get_type(mr->mpriv->conv, G2N_POLYLINE|mask, otype);
    }
    mr->item.meth = &methods_garmin_poly;
    return &mr->item;
}

static struct item *garmin_pg2item(struct map_rect_priv *mr, struct gobject *o, unsigned short otype) {
    if (mr->mpriv->conv) {
        int mask = gar_object_group(o) << G2N_KIND_SHIFT;
        mr->item.type = g2n_get_type(mr->mpriv->conv, G2N_POLYGONE|mask, otype);
    }
    mr->item.meth = &methods_garmin_poly;
    return &mr->item;
}

static struct item *garmin_srch2item(struct map_rect_priv *mr, struct gobject *o, unsigned short otype) {
    mr->item.type = type_country_label;
    mr->item.meth = &methods_garmin_search;
    return &mr->item;
}

static struct item *garmin_obj2item(struct map_rect_priv *mr, struct gobject *o) {
    unsigned short otype;
    otype = gar_obj_type(o);
    mr->item.type = type_none;
    switch (o->type) {
    case GO_POINT:
        return garmin_poi2item(mr, o, otype);
    case GO_POLYLINE:
        return garmin_pl2item(mr, o, otype);
    case GO_POLYGON:
        return garmin_pg2item(mr, o, otype);
    case GO_ROAD:
        return garmin_pl2item(mr, o, otype);
#if 0
    case GO_SEARCH:
        return garmin_srch2item(mr, o, otype);
#endif
    default:
        dlog(1, "Unknown garmin object type:%d\n",
             o->type);
    }
    return NULL;
}

static struct item *gmap_rect_get_item_byid(struct map_rect_priv *mr, int id_hi, int id_lo) {
    struct gobject *o;
    o = mr->objs = gar_get_object_by_id(mr->mpriv->g, id_hi, id_lo);
    if (!o) {
        dlog(1, "Can not find object\n");
        return NULL;
    }

    mr->item.id_hi = id_hi;
    mr->item.id_lo = id_lo;
    mr->item.priv_data = o;
    mr->item.type = type_none;
    o->priv_data = mr;
    if (!garmin_obj2item(mr, o))
        return NULL;
    return &mr->item;
}

static struct item *gmap_rect_get_item(struct map_rect_priv *mr) {
    struct gobject *o;
    if (!mr->objs)
        return NULL;
    if (!mr->cobj)
        return NULL;
    // mr->cobj = mr->objs;
    o = mr->cobj;
//	dlog(1, "gi:o=%p\n", o);
    mr->cobj = mr->cobj->next;
    if (o) {
        mr->item.id_hi = gar_object_mapid(o);
        mr->item.id_lo = gar_object_index(o);
        mr->item.priv_data = o;
        mr->item.type = type_none;
        o->priv_data = mr;
        if (!garmin_obj2item(mr, o))
            return NULL;
        return &mr->item;
    }
    return NULL;
}

#define max(a,b) ((a) > (b) ? (a) : (b))
struct nl2gl_t {
    int g;
    int bits;
    char *descr;
};

struct nl2gl_t nl2gl_1[] = {
    { /* 0 */  .g = 12, .descr = "0-120m", },
    { /* 1 */  .g = 11, .descr = "0-120m", },
    { /* 2 */  .g = 10, .descr = "0-120m", },
    { /* 3 */  .g = 9, .descr = "0-120m", },
    { /* 4 */  .g = 8, .descr = "0-120m", },
    { /* 5 */  .g = 7, .descr = "0-120m", },
    { /* 6 */  .g = 6, .descr = "0-120m", },
    { /* 7 */  .g = 5, .descr = "0-120m", },
    { /* 8 */  .g = 4, .descr = "0-120m", },
    { /* 9 */  .g = 4, .descr = "0-120m", },
    { /* 10 */ .g = 3, .descr = "0-120m", },
    { /* 11 */ .g = 3, .descr = "0-120m", },
    { /* 12 */ .g = 2, .descr = "0-120m", },
    { /* 13 */ .g = 2, .descr = "0-120m", },
    { /* 14 */ .g = 2, .descr = "0-120m", },
    { /* 15 */ .g = 1, .descr = "0-120m", },
    { /* 16 */ .g = 1, .descr = "0-120m", },
    { /* 17 */ .g = 1, .descr = "0-120m", },
    { /* 18 */ .g = 0, .descr = "0-120m", },
};

struct nl2gl_t nl2gl[] = {
    { /* 0 */  .g = 9, .descr = "0-120m", },
    { /* 1 */  .g = 9, .descr = "0-120m", },
    { /* 2 */  .g = 8, .descr = "0-120m", },
    { /* 3 */  .g = 8, .descr = "0-120m", },
    { /* 4 */  .g = 7, .descr = "0-120m", },
    { /* 5 */  .g = 7, .descr = "0-120m", },
    { /* 6 */  .g = 6, .descr = "0-120m", },
    { /* 7 */  .g = 6, .descr = "0-120m", },
    { /* 8 */  .g = 5, .descr = "0-120m", },
    { /* 9 */  .g = 5, .descr = "0-120m", },
    { /* 10 */ .g = 4, .descr = "0-120m", },
    { /* 11 */ .g = 4, .descr = "0-120m", },
    { /* 12 */ .g = 3, .descr = "0-120m", },
    { /* 13 */ .g = 3, .descr = "0-120m", },
    { /* 14 */ .g = 2, .descr = "0-120m", },
    { /* 15 */ .g = 2, .descr = "0-120m", },
    { /* 16 */ .g = 1, .descr = "0-120m", },
    { /* 17 */ .g = 1, .descr = "0-120m", },
    { /* 18 */ .g = 0, .descr = "0-120m", },
};

static int get_level(struct map_selection *sel) {
    return sel->order;
}

static int garmin_get_selection(struct map_rect_priv *map, struct map_selection *sel) {
    struct gar_rect r;
    struct gmap *gm;
    struct gobject **glast = NULL;
    int rc;
    int sl, el;
    int level = 0; // 18;	/* max level for maps, overview maps can have bigger
    /* levels we do not deal w/ them
    */
    int flags = 0;
    if (sel && sel->range.min == type_street_0 && sel->range.max == type_ferry) {
        // Get all roads
        flags = GO_GET_ROUTABLE;
    } else if (sel)
        flags = GO_GET_SORTED;

    if (sel) {
        r.lulat = sel->u.c_rect.lu.y;
        r.lulong = sel->u.c_rect.lu.x;
        r.rllat = sel->u.c_rect.rl.y;
        r.rllong = sel->u.c_rect.rl.x;
        level = get_level(sel);
//		level = nl2gl[level].g;
        dlog(2, "Looking level=%d for %f %f %f %f\n",
             level, r.lulat, r.lulong, r.rllat, r.rllong);
    }
    gm = gar_find_subfiles(map->mpriv->g, sel ? &r : NULL, flags);
    if (!gm) {
        if (sel) {
            dlog(1, "Can not find map data for the area: %f %f %f %f\n",
                 r.lulat, r.lulong, r.rllat, r.rllong);
        } else {
            dlog(1, "Can not find map data\n");
        }
        return -1;
    }
    sl = (18-gm->zoomlevels)/2;
    el = sl + gm->zoomlevels;
    if (level < sl)
        level = sl;
    if (level > el)
        level = el;
    level = level - sl;
    level = gm->basebits + level;
    dlog(3, "sl=%d el=%d level=%d\n", sl, el, level);
    map->gmap = gm;
    glast = &map->objs;
    while (*glast) {
        if ((*glast)->next) {
            *glast = (*glast)->next;
        } else
            break;
    }
    rc = gar_get_objects(gm, level, sel ? &r : NULL, glast, flags);
    if (rc < 0) {
        dlog(1, "Error loading objects\n");
        return -1;
    }
    map->cobj = map->objs;
    dlog(2, "Loaded %d objects\n", rc);
    return rc;
}
// Can not return NULL, navit segfaults
static struct map_rect_priv *gmap_rect_new(struct map_priv *map, struct map_selection *sel) {
    struct map_selection *ms = sel;
    struct map_rect_priv *mr;

    if (!map)
        return NULL;
    mr = calloc(1, sizeof(*mr));
    if (!mr)
        return mr;
    mr->mpriv = map;
    if (!sel) {
        return mr;
    } else {
        while (ms) {
            dlog(2, "order %d\n", ms->order);
            if (garmin_get_selection(mr, ms) < 0) {
                //	free(mr);
                //	return NULL;
            }
            ms = ms->next;
        }
    }
    return mr;
}

static void gmap_rect_destroy(struct map_rect_priv *mr) {
    dlog(11,"destroy maprect\n");
    if (mr->gmap)
        gar_free_gmap(mr->gmap);
    if (mr->objs)
        gar_free_objects(mr->objs);
    if (mr->label)
        free(mr->label);
    free(mr);
}

static void gmap_search_destroy(struct map_search_priv *ms) {
    gmap_rect_destroy((struct map_rect_priv *)ms);
}

static void gmap_destroy(struct map_priv *m) {
    dlog(5, "garmin_map_destroy\n");
    if (m->g)
        gar_free(m->g);
    if (m->filename)
        free(m->filename);
    free(m);
}

static struct map_methods map_methods = {
    projection_garmin,
    "utf-8",
    gmap_destroy,
    gmap_rect_new,
    gmap_rect_destroy,
    gmap_rect_get_item,
    gmap_rect_get_item_byid,
    gmap_search_new,
    gmap_search_destroy,
    NULL,
};

static struct map_priv *gmap_new(struct map_methods *meth, struct attr **attrs, struct callback_list *cbl) {
    struct map_priv *m;
    struct attr *data;
    struct attr *debug;
    struct attr *flags;
    char buf[PATH_MAX];
    struct stat st;
    int dl = 1;
    struct gar_config cfg;
    int debugmask = 0;

    data=attr_search(attrs, NULL, attr_data);
    if (! data)
        return NULL;
    debug=attr_search(attrs, NULL, attr_debug);
    if (debug) {
        dl = atoi(debug->u.str);
        if (!dl)
            dl = 1;
    }
    flags=attr_search(attrs, NULL, attr_flags);
    if (flags) {
        debugmask = flags->u.num;
    }
    m=g_new(struct map_priv, 1);
    m->id=++map_id;
    m->filename = strdup(data->u.str);
    if (!m->filename) {
        g_free(m);
        return NULL;
    }
    memset(&cfg, 0, sizeof(struct gar_config));
    cfg.opm = OPM_GPS;
    cfg.debuglevel = dl;
    cfg.debugmask = debugmask;
    garmin_debug = dl;
    m->g = gar_init_cfg(NULL, logfn, &cfg);
    if (!m->g) {
        g_free(m->filename);
        g_free(m);
        return NULL;
    }
    // we want the data now, later we can load only what's necessery
    if (gar_img_load(m->g, m->filename, 1) < 0) {
        gar_free(m->g);
        g_free(m->filename);
        g_free(m);
        return NULL;
    }
    m->conv = NULL;
    snprintf(buf, sizeof(buf), "%s.types", m->filename);
    if (!stat(buf, &st)) {
        dlog(1, "Loading custom types from %s\n", buf);
        m->conv = g2n_conv_load(buf);
    }
    if (!m->conv) {
        dlog(1, "Using builtin types\n");
        m->conv = g2n_default_conv();
    }
    if (!m->conv) {
        dlog(1, "Failed to load map types\n");
    }
    *meth=map_methods;
    return m;
}

void plugin_init(void) {
    plugin_register_category_map("garmin", gmap_new);
}