1 |
/*
|
2 |
Copyright (C) 2007 Alexander Atanasov <aatanasov@gmail.com>
|
3 |
|
4 |
This program is free software; you can redistribute it and/or modify
|
5 |
it under the terms of the GNU General Public License as published by
|
6 |
the Free Software Foundation; version 2 of the License.
|
7 |
|
8 |
This program is distributed in the hope that it will be useful,
|
9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11 |
GNU General Public License for more details.
|
12 |
|
13 |
You should have received a copy of the GNU General Public License
|
14 |
along with this program; if not, write to the Free Software
|
15 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
16 |
MA 02110-1301 USA
|
17 |
|
18 |
Garmin and MapSource are registered trademarks or trademarks
|
19 |
of Garmin Ltd. or one of its subsidiaries.
|
20 |
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
File format is:
|
25 |
|
26 |
POINT
|
27 |
GROUP,0x0100 = town_label_1e5, Megapolis (10M +)
|
28 |
GROUP,0x0200 = town_label_5e4, Megapolis (5-10M)
|
29 |
...
|
30 |
GROUP,0x1e00-0x1e3f = district_label, District, Province, State Name
|
31 |
...
|
32 |
POLYLINE
|
33 |
GROUP,0x00 = ALL, street_1_land, Road
|
34 |
GROUP,0x01 = MCTL, highway_land, Major HWY thick
|
35 |
GROUP,0x02 = MCTL, street_4_land, Principal HWY-thick
|
36 |
GROUP,0x03 = MCTL, street_2_land, Principal HWY-medium
|
37 |
....
|
38 |
POLYGONE
|
39 |
GROUP,0x01 = town_poly, City (>200k)
|
40 |
GROUP,0x02 = town_poly, City (<200k)
|
41 |
GROUP,0x03 = town_poly, Village
|
42 |
|
43 |
GROUP is
|
44 |
0 - RGN1
|
45 |
1 - RGN2-4
|
46 |
*/
|
47 |
|
48 |
#include <stdio.h>
|
49 |
#include <string.h>
|
50 |
#include <malloc.h>
|
51 |
#include "item.h"
|
52 |
#include "attr.h"
|
53 |
#include "garmin.h"
|
54 |
#include "gar2navit.h"
|
55 |
|
56 |
static int add_def(struct gar2nav_conv *conv, int type, unsigned short minid,
|
57 |
unsigned short maxid, unsigned int group, char *ntype,
|
58 |
char *descr)
|
59 |
{
|
60 |
enum item_type it;
|
61 |
struct gar2navit *g2n;
|
62 |
dlog(11, "group=%d type=%d routable=%u min=%04X max=%04X ntype=%s descr=%s\n",
|
63 |
group, type, minid, maxid, ntype, descr);
|
64 |
it = item_from_name(ntype);
|
65 |
if (it==type_none) {
|
66 |
dlog(1, "Please define: %s\n", ntype);
|
67 |
}
|
68 |
g2n = calloc(1, sizeof(*g2n));
|
69 |
if (!g2n)
|
70 |
return -1;
|
71 |
g2n->id = minid;
|
72 |
g2n->maxid = maxid;
|
73 |
g2n->ntype = it;
|
74 |
g2n->descr = strdup(descr);
|
75 |
g2n->group = group;
|
76 |
if (type == 1) {
|
77 |
g2n->next = conv->points;
|
78 |
conv->points = g2n;
|
79 |
} else if (type == 2) {
|
80 |
g2n->next = conv->polylines;
|
81 |
conv->polylines = g2n;
|
82 |
} else if (type == 3) {
|
83 |
g2n->next = conv->polygons;
|
84 |
conv->polygons = g2n;
|
85 |
}
|
86 |
return 0;
|
87 |
}
|
88 |
|
89 |
static int load_types_file(char *file, struct gar2nav_conv *conv)
|
90 |
{
|
91 |
char buf[4096];
|
92 |
char descr[4096];
|
93 |
char ntype[4096];
|
94 |
FILE *fp;
|
95 |
unsigned int minid, maxid, group;
|
96 |
int rc;
|
97 |
int type = -1;
|
98 |
|
99 |
fp = fopen(file, "r");
|
100 |
if (!fp)
|
101 |
return -1;
|
102 |
while (fgets(buf, sizeof(buf), fp)) {
|
103 |
if (*buf == '#' || *buf == '\n')
|
104 |
continue;
|
105 |
if (!strncasecmp(buf, "POINT", 5)) {
|
106 |
type = 1;
|
107 |
continue;
|
108 |
} else if (!strncasecmp(buf, "POI", 3)) {
|
109 |
type = 1;
|
110 |
continue;
|
111 |
} else if (!strncasecmp(buf, "POLYLINE", 8)) {
|
112 |
type = 2;
|
113 |
continue;
|
114 |
} else if (!strncasecmp(buf, "POLYGONE", 8)) {
|
115 |
type = 3;
|
116 |
continue;
|
117 |
}
|
118 |
// assume only lines are routable
|
119 |
rc = sscanf(buf, "%d, 0x%04X - 0x%04X = %[^\t , ] , %[^\n]",
|
120 |
&group, &minid, &maxid, ntype, descr);
|
121 |
if (rc != 5) {
|
122 |
maxid = 0;
|
123 |
rc = sscanf(buf, "%d,0x%04X = %[^\t, ], %[^\n]",
|
124 |
&group, &minid, ntype, descr);
|
125 |
if (rc != 4) {
|
126 |
dlog(1, "Invalid line rc=%d:[%s]\n",rc, buf);
|
127 |
dlog(1, "minid=%04X ntype=[%s] des=[%s]\n",
|
128 |
minid, ntype, descr);
|
129 |
continue;
|
130 |
}
|
131 |
}
|
132 |
add_def(conv, type, minid, maxid, group, ntype, descr);
|
133 |
}
|
134 |
fclose(fp);
|
135 |
return 1;
|
136 |
}
|
137 |
|
138 |
struct gar2nav_conv *g2n_conv_load(char *file)
|
139 |
{
|
140 |
struct gar2nav_conv *c;
|
141 |
int rc;
|
142 |
|
143 |
c = calloc(1, sizeof(*c));
|
144 |
if (!c)
|
145 |
return c;
|
146 |
rc = load_types_file(file, c);
|
147 |
if (rc < 0) {
|
148 |
dlog(1, "Failed to load: [%s]\n", file);
|
149 |
free(c);
|
150 |
return NULL;
|
151 |
}
|
152 |
return c;
|
153 |
}
|
154 |
|
155 |
enum item_type g2n_get_type(struct gar2nav_conv *c, unsigned int type, unsigned short id)
|
156 |
{
|
157 |
struct gar2navit *def = NULL;
|
158 |
int group;
|
159 |
group = (type >> G2N_KIND_SHIFT);
|
160 |
type &= ~G2N_KIND_MASK;
|
161 |
if (type == G2N_POINT)
|
162 |
def = c->points;
|
163 |
else if (type == G2N_POLYLINE)
|
164 |
def = c->polylines;
|
165 |
else if (type == G2N_POLYGONE)
|
166 |
def = c->polygons;
|
167 |
else {
|
168 |
dlog(1, "Unknown conversion type:%d\n", type);
|
169 |
return type_none;
|
170 |
}
|
171 |
|
172 |
if (!def) {
|
173 |
dlog(5, "No conversion data for %d\n", type);
|
174 |
return type_none;
|
175 |
}
|
176 |
|
177 |
while (def) {
|
178 |
if (def->group == group &&
|
179 |
((!def->maxid && def->id == id) ||
|
180 |
(def->id <= id && id <= def->maxid)))
|
181 |
return def->ntype;
|
182 |
def = def->next;
|
183 |
}
|
184 |
dlog(5, "Type[%d]:ID:[%04X] unknown\n", type, id);
|
185 |
return type == G2N_POINT ? type_point_unkn : type_street_unkn;
|
186 |
}
|
187 |
|
188 |
char *g2n_get_descr(struct gar2nav_conv *c, int type, unsigned short id)
|
189 |
{
|
190 |
struct gar2navit *def = NULL;
|
191 |
if (type == G2N_POINT)
|
192 |
def = c->points;
|
193 |
else if (type == G2N_POLYLINE)
|
194 |
def = c->polylines;
|
195 |
else if (type == G2N_POLYGONE)
|
196 |
def = c->polygons;
|
197 |
else {
|
198 |
dlog(1, "Unknown conversion type:%d\n", type);
|
199 |
return NULL;
|
200 |
}
|
201 |
while (def) {
|
202 |
if ((!def->maxid && def->id == id) ||
|
203 |
(def->id <= id && id <= def->maxid))
|
204 |
return def->descr;
|
205 |
def = def->next;
|
206 |
}
|
207 |
dlog(5, "Type[%d]:ID:[%04X] unknown\n", type, id);
|
208 |
return NULL;
|
209 |
}
|
210 |
|
211 |
#if 0
|
212 |
int main(int argc, char **argv)
|
213 |
{
|
214 |
load_types_file(argv[1], NULL);
|
215 |
return 0;
|
216 |
}
|
217 |
#endif
|
218 |
|
219 |
#include "g2nbuiltin.h"
|