2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
28 #include "transform.h"
29 #include "projection.h"
31 * @defgroup coord Coordinate handling functions
38 * @param p Pointer to the coordinate
39 * @returns the coordinate
43 coord_get(unsigned char **p)
45 struct coord *ret=(struct coord *)(*p);
51 coord_new(int x, int y)
53 struct coord *c=g_new(struct coord, 1);
62 coord_new_from_attrs(struct attr *parent, struct attr **attrs)
65 x=attr_search(attrs, NULL, attr_x);
66 y=attr_search(attrs, NULL, attr_y);
69 return coord_new(x->u.num, y->u.num);
74 coord_destroy(struct coord *c)
80 coord_rect_new(struct coord *lu, struct coord *rl)
82 struct coord_rect *r=g_new(struct coord_rect, 1);
84 dbg_assert(lu->x <= rl->x);
85 dbg_assert(lu->y >= rl->y);
95 coord_rect_destroy(struct coord_rect *r)
101 coord_rect_overlap(struct coord_rect *r1, struct coord_rect *r2)
103 dbg_assert(r1->lu.x <= r1->rl.x);
104 dbg_assert(r1->lu.y >= r1->rl.y);
105 dbg_assert(r2->lu.x <= r2->rl.x);
106 dbg_assert(r2->lu.y >= r2->rl.y);
107 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);
108 if (r1->lu.x > r2->rl.x)
110 if (r1->rl.x < r2->lu.x)
112 if (r1->lu.y < r2->rl.y)
114 if (r1->rl.y > r2->lu.y)
120 coord_rect_contains(struct coord_rect *r, struct coord *c)
122 dbg_assert(r->lu.x <= r->rl.x);
123 dbg_assert(r->lu.y >= r->rl.y);
136 coord_rect_extend(struct coord_rect *r, struct coord *c)
148 /* [Proj:][Ð]DMM.ss[S][S]... N/S [D][D]DMM.ss[S][S]... E/W */
149 /* [Proj:][-][D]D.d[d]... [-][D][D]D.d[d]... */
150 /* [Proj:][-]0xX [-]0xX */
152 * Currently supported:
154 * - where Proj can be mg/garmin, defaults to mg
155 * [Proj:][D][D]Dmm.ss[S][S] N/S [D][D]DMM.ss[S][S]... E/W
156 * [Proj:][-][D]D.d[d]... [-][D][D]D.d[d]
157 * - where Proj can be geo
161 coord_parse(const char *c_str, enum projection pro, struct coord *c_ret)
164 char *proj=NULL,*s,*co;
165 const char *str=c_str;
169 enum projection str_pro=projection_none;
171 dbg(1,"enter('%s',%d,%p)\n", c_str, pro, c_ret);
175 proj=malloc(co-str+1);
176 strncpy(proj, str, co-str);
178 dbg(1,"projection=%s\n", proj);
181 if (!strcmp(proj, "mg"))
182 str_pro = projection_mg;
183 else if (!strcmp(proj, "garmin"))
184 str_pro = projection_garmin;
185 else if (!strcmp(proj, "geo"))
186 str_pro = projection_none;
188 dbg(0, "Unknown projection: %s\n", proj);
197 if (!strncmp(s, "0x", 2) || !strncmp(s, "-0x", 3)) {
198 args=sscanf(str, "%i %i%n",&c.x, &c.y, &ret);
201 dbg(1,"str='%s' x=0x%x y=0x%x c=%d\n", str, c.x, c.y, ret);
202 dbg(1,"rest='%s'\n", str+ret);
204 if (str_pro == projection_none)
205 str_pro=projection_mg;
206 if (str_pro != pro) {
207 transform_to_geo(str_pro, &c, &g);
208 transform_from_geo(pro, &g, &c);
211 } else if (*s == 'N' || *s == 'n' || *s == 'S' || *s == 's') {
214 dbg(1,"str='%s'\n", str);
215 args=sscanf(str, "%lf %c %lf %c%n", &lat, &ns, &lng, &ew, &ret);
218 if (str_pro == projection_none) {
219 g.lat=floor(lat/100);
222 g.lng=floor(lng/100);
225 if (ns == 's' || ns == 'S')
227 if (ew == 'w' || ew == 'W')
229 transform_from_geo(pro, &g, c_ret);
231 dbg(3,"str='%s' x=%f ns=%c y=%f ew=%c c=%d\n", str, lng, ns, lat, ew, ret);
232 dbg(3,"rest='%s'\n", str+ret);
235 args=sscanf(str, "%lf %lf%n", &lng, &lat, &ret);
238 dbg(1,"str='%s' x=%f y=%f c=%d\n", str, lng, lat, ret);
239 dbg(1,"rest='%s'\n", str+ret);
242 transform_from_geo(pro, &g, c_ret);
245 printf("rest='%s'\n", str+ret);
248 printf("args=%d\n", args);
249 printf("ret=%d delta=%d ret_str='%s'\n", ret, str-c_str, c_str+ret);
258 coord_print(enum projection pro, struct coord *c, FILE *out) {
276 fprintf( out, "%s: %s0x%x %s0x%x\n",
277 projection_to_name( pro ),
284 * @brief Converts a lat/lon into a text formatted text string.
285 * @param lat The latitude
286 * @param lng The longitude
287 * @param fmt The format to use.
288 * @li DEGREES=>(45.5000N 100.9000S)
289 * @li DEGREES_MINUTES=>(45 30.))00N 100 120.54.0000S)
290 * @li DEGREES_MINUTES_SECONDS=>(4530.0000N 12054.0000S)
293 * @param buffer A buffer large enough to hold the output + a terminating NULL (26 bytes)
294 * @param size The size of the buffer
297 void coord_format(float lat,float lng, enum coord_format fmt, char * buffer, int size)
302 float lat_deg,lat_min,lat_sec;
303 float lng_deg,lng_min,lng_sec;
314 lat_min=(lat-floor(lat_deg))*60;
315 lat_sec=fmod(lat*3600,60);
317 lng_min=(lng-floor(lng_deg))*60;
318 lng_sec=fmod(lng*3600,60);
322 case DEGREES_DECIMAL:
323 snprintf(buffer,size,"%02.6f%c %03.7f%c",lat,lat_c,lng,lng_c);
325 case DEGREES_MINUTES:
326 snprintf(buffer,size,"%02.0f %07.4f%c %03.0f %07.4f%c",floor(lat_deg),lat_min , lat_c, floor(lng), lng_min, lng_c);
328 case DEGREES_MINUTES_SECONDS:
329 snprintf(buffer,size,"%02.0f%07.4f%c %03.0f%07.4f%c",floor(lat), fmod(lat*60,60), lat_c, floor(lng), fmod(lng*60,60), lng_c);