9 #include "projection.h"
11 * @defgroup coord Coordinate handling functions
18 * @param p Pointer to the coordinate
19 * @returns the coordinate
23 coord_get(unsigned char **p)
25 struct coord *ret=(struct coord *)(*p);
31 coord_new(int x, int y)
33 struct coord *c=g_new(struct coord, 1);
42 coord_destroy(struct coord *c)
48 coord_rect_new(struct coord *lu, struct coord *rl)
50 struct coord_rect *r=g_new(struct coord_rect, 1);
52 g_assert(lu->x <= rl->x);
53 g_assert(lu->y >= rl->y);
63 coord_rect_destroy(struct coord_rect *r)
69 coord_rect_overlap(struct coord_rect *r1, struct coord_rect *r2)
71 g_assert(r1->lu.x <= r1->rl.x);
72 g_assert(r1->lu.y >= r1->rl.y);
73 g_assert(r2->lu.x <= r2->rl.x);
74 g_assert(r2->lu.y >= r2->rl.y);
75 dbg(1,"0x%x,0x%x - 0x%x,0x%x vs 0x%x,0x%x - 0x%x,0x%x\n", r1->lu.x, r1->lu.y, r1->rl.x, r1->rl.y, r2->lu.x, r2->lu.y, r2->rl.x, r2->rl.y);
76 if (r1->lu.x > r2->rl.x)
78 if (r1->rl.x < r2->lu.x)
80 if (r1->lu.y < r2->rl.y)
82 if (r1->rl.y > r2->lu.y)
88 coord_rect_contains(struct coord_rect *r, struct coord *c)
90 g_assert(r->lu.x <= r->rl.x);
91 g_assert(r->lu.y >= r->rl.y);
104 coord_rect_extend(struct coord_rect *r, struct coord *c)
116 /* [Proj:][Ð]DMM.ss[S][S]... N/S [D][D]DMM.ss[S][S]... E/W */
117 /* [Proj:][-][D]D.d[d]... [-][D][D]D.d[d]... */
118 /* [Proj:][-]0xX [-]0xX */
120 * Currently supported:
122 * - where Proj can be mg/garmin, defaults to mg
123 * [Proj:][D][D]Dmm.ss[S][S] N/S [D][D]DMM.ss[S][S]... E/W
124 * [Proj:][-][D]D.d[d]... [-][D][D]D.d[d]
125 * - where Proj can be geo
129 coord_parse(const char *c_str, enum projection pro, struct coord *c_ret)
132 char *proj=NULL,*s,*co;
133 const char *str=c_str;
137 enum projection str_pro=projection_none;
139 dbg(1,"enter('%s',%d,%p)\n", c_str, pro, c_ret);
143 proj=malloc(co-str+1);
144 strncpy(proj, str, co-str);
146 dbg(1,"projection=%s\n", proj);
149 if (!strcmp(proj, "mg"))
150 str_pro = projection_mg;
151 else if (!strcmp(proj, "garmin"))
152 str_pro = projection_garmin;
153 else if (!strcmp(proj, "geo"))
154 str_pro = projection_none;
156 dbg(0, "Unknown projection: %s\n", proj);
165 if (!strncmp(str, "0x", 2) || !strncmp(str,"-0x", 3)) {
166 args=sscanf(str, "%x %x%n",&c.x, &c.y, &ret);
169 dbg(1,"str='%s' x=0x%x y=0x%x c=%d\n", str, c.x, c.y, ret);
170 dbg(1,"rest='%s'\n", str+ret);
172 if (str_pro == projection_none)
173 str_pro=projection_mg;
174 if (str_pro != pro) {
175 transform_to_geo(str_pro, &c, &g);
176 transform_from_geo(pro, &g, &c);
179 } else if (*s == 'N' || *s == 'n' || *s == 'S' || *s == 's') {
182 dbg(1,"str='%s'\n", str);
183 args=sscanf(str, "%lf %c %lf %c%n", &lat, &ns, &lng, &ew, &ret);
186 if (str_pro == projection_none) {
187 g.lat=floor(lat/100);
190 g.lng=floor(lng/100);
193 if (ns == 's' || ns == 'S')
195 if (ew == 'w' || ew == 'W')
197 transform_from_geo(pro, &g, c_ret);
199 dbg(3,"str='%s' x=%f ns=%c y=%f ew=%c c=%d\n", str, lng, ns, lat, ew, ret);
200 dbg(3,"rest='%s'\n", str+ret);
203 args=sscanf(str, "%lf %lf%n", &lng, &lat, &ret);
206 dbg(1,"str='%s' x=%f y=%f c=%d\n", str, lng, lat, ret);
207 dbg(1,"rest='%s'\n", str+ret);
210 transform_from_geo(pro, &g, c_ret);
213 printf("rest='%s'\n", str+ret);
216 printf("args=%d\n", args);
217 printf("ret=%d delta=%d ret_str='%s'\n", ret, str-c_str, c_str+ret);