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.
30 #include "transform.h"
31 #include "projection.h"
35 struct transformation {
36 long scale; /* Scale factor */
37 int angle; /* Rotation angle */
38 double cos_val,sin_val; /* cos and sin of rotation angle */
40 struct map_selection *map_sel;
41 struct map_selection *screen_sel;
42 struct point screen_center;
43 struct coord map_center; /* Center of source rectangle */
46 struct transformation *
49 struct transformation *this_;
51 this_=g_new0(struct transformation, 1);
56 static const double gar2geo_units = 360.0/(1<<24);
57 static const double geo2gar_units = 1/(360.0/(1<<24));
60 transform_to_geo(enum projection pro, struct coord *c, struct coord_geo *g)
64 g->lng=c->x/6371000.0/M_PI*180;
65 g->lat=atan(exp(c->y/6371000.0))/M_PI*360-90;
67 case projection_garmin:
68 g->lng=c->x*gar2geo_units;
69 g->lat=c->y*gar2geo_units;
77 transform_from_geo(enum projection pro, struct coord_geo *g, struct coord *c)
81 c->x=g->lng*6371000.0*M_PI/180;
82 c->y=log(tan(M_PI_4+g->lat*M_PI/360))*6371000.0;
84 case projection_garmin:
85 c->x=g->lng*geo2gar_units;
86 c->y=g->lat*geo2gar_units;
94 transform_from_to(struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
97 transform_to_geo(from, cfrom, &g);
98 transform_from_geo(to, &g, cto);
102 transform_geo_to_cart(struct coord_geo *geo, double a, double b, struct coord_geo_cart *cart)
104 double n,ee=1-b*b/(a*a);
105 n = a/sqrt(1-ee*sin(geo->lat)*sin(geo->lat));
106 cart->x=n*cos(geo->lat)*cos(geo->lng);
107 cart->y=n*cos(geo->lat)*sin(geo->lng);
108 cart->z=n*(1-ee)*sin(geo->lat);
112 transform_cart_to_geo(struct coord_geo_cart *cart, double a, double b, struct coord_geo *geo)
114 double lat,lati,n,ee=1-b*b/(a*a), lng = atan(cart->y/cart->x);
116 lat = atan(cart->z / sqrt((cart->x * cart->x) + (cart->y * cart->y)));
121 n = a / sqrt(1-ee*sin(lat)*sin(lat));
122 lat = atan((cart->z + ee * n * sin(lat)) / sqrt(cart->x * cart->x + cart->y * cart->y));
124 while (fabs(lat - lati) >= 0.000000000000001);
126 geo->lng=lng/M_PI*180;
127 geo->lat=lat/M_PI*180;
132 transform_datum(struct coord_geo *from, enum map_datum from_datum, struct coord_geo *to, enum map_datum to_datum)
137 transform(struct transformation *t, enum projection pro, struct coord *c, struct point *p, int count, int unique)
148 for (i=0; i < count; i++) {
153 transform_to_geo(pro, &c[i], &g);
154 transform_from_geo(t->pro, &g, &c1);
158 // dbg(2,"0x%x, 0x%x - 0x%x,0x%x contains 0x%x,0x%x\n", t->r.lu.x, t->r.lu.y, t->r.rl.x, t->r.rl.y, c->x, c->y);
159 // ret=coord_rect_contains(&t->r, c);
164 xcn=xc*t->cos_val+yc*t->sin_val;
165 ycn=-xc*t->sin_val+yc*t->cos_val;
173 xc=xc/(double)(t->scale);
174 yc=yc/(double)(t->scale);
182 xc+=t->screen_center.x;
183 yc+=t->screen_center.y;
193 if (j == 0 || !unique || p[j-1].x != xc || p[j-1].y != yc) {
203 transform_reverse(struct transformation *t, struct point *p, struct coord *c)
208 xc-=t->screen_center.x;
209 yc-=t->screen_center.y;
214 xcn=xc*t->cos_val+yc*t->sin_val;
215 ycn=-xc*t->sin_val+yc*t->cos_val;
219 c->x=t->map_center.x+xc;
220 c->y=t->map_center.y+yc;
224 transform_get_projection(struct transformation *this_)
230 transform_set_projection(struct transformation *this_, enum projection pro)
236 min4(int v1,int v2, int v3, int v4)
249 max4(int v1,int v2, int v3, int v4)
261 struct map_selection *
262 transform_get_selection(struct transformation *this_, enum projection pro, int order)
265 struct map_selection *ret,*curri,*curro;
269 ret=map_selection_dup(this_->map_sel);
270 curri=this_->map_sel;
273 if (this_->pro != pro) {
274 transform_to_geo(this_->pro, &curri->u.c_rect.lu, &g);
275 transform_from_geo(pro, &g, &curro->u.c_rect.lu);
276 dbg(1,"%f,%f", g.lat, g.lng);
277 transform_to_geo(this_->pro, &curri->u.c_rect.rl, &g);
278 transform_from_geo(pro, &g, &curro->u.c_rect.rl);
279 dbg(1,": - %f,%f\n", g.lat, g.lng);
281 dbg(1,"transform rect for %d is %d,%d - %d,%d\n", pro, curro->u.c_rect.lu.x, curro->u.c_rect.lu.y, curro->u.c_rect.rl.x, curro->u.c_rect.rl.y);
282 for (i = 0 ; i < layer_end ; i++)
283 curro->order[i]+=order;
291 transform_center(struct transformation *this_)
293 return &this_->map_center;
297 transform_set_angle(struct transformation *t,int angle)
300 t->cos_val=cos(M_PI*t->angle/180);
301 t->sin_val=sin(M_PI*t->angle/180);
305 transform_get_angle(struct transformation *this_,int angle)
311 transform_set_screen_selection(struct transformation *t, struct map_selection *sel)
313 map_selection_destroy(t->screen_sel);
314 t->screen_sel=map_selection_dup(sel);
316 t->screen_center.x=(sel->u.p_rect.rl.x-sel->u.p_rect.lu.x)/2;
317 t->screen_center.y=(sel->u.p_rect.rl.y-sel->u.p_rect.lu.y)/2;
322 transform_set_screen_center(struct transformation *t, struct point *p)
329 transform_set_size(struct transformation *t, int width, int height)
337 transform_get_size(struct transformation *t, int *width, int *height)
339 struct point_rect *r;
341 r=&t->screen_sel->u.p_rect;
342 *width=r->rl.x-r->lu.x;
343 *height=r->rl.y-r->lu.y;
348 transform_setup(struct transformation *t, struct pcoord *c, int scale, int angle)
351 t->map_center.x=c->x;
352 t->map_center.y=c->y;
354 transform_set_angle(t, angle);
360 transform_setup_source_rect_limit(struct transformation *t, struct coord *center, int limit)
365 t->r.lu.x=center->x-limit;
366 t->r.rl.x=center->x+limit;
367 t->r.rl.y=center->y-limit;
368 t->r.lu.y=center->y+limit;
373 transform_setup_source_rect(struct transformation *t)
376 struct coord screen[4];
377 struct point screen_pnt[4];
378 struct point_rect *pr;
379 struct map_selection *ms,*msm,*next,**msm_last;
387 msm_last=&t->map_sel;
390 msm=g_new0(struct map_selection, 1);
393 screen_pnt[0].x=pr->lu.x;
394 screen_pnt[0].y=pr->lu.y;
395 screen_pnt[1].x=pr->rl.x;
396 screen_pnt[1].y=pr->lu.y;
397 screen_pnt[2].x=pr->lu.x;
398 screen_pnt[2].y=pr->rl.y;
399 screen_pnt[3].x=pr->rl.x;
400 screen_pnt[3].y=pr->rl.y;
401 for (i = 0 ; i < 4 ; i++) {
402 transform_reverse(t, &screen_pnt[i], &screen[i]);
404 msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
405 msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
406 msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
407 msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
415 transform_get_scale(struct transformation *t)
421 transform_set_scale(struct transformation *t, long scale)
428 transform_get_order(struct transformation *t)
444 transform_geo_text(struct coord_geo *g, char *buffer)
460 sprintf(buffer,"%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);
464 #define TWOPI (M_PI*2)
465 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
466 #define minf(a,b) ((a) < (b) ? (a) : (b))
469 transform_distance_garmin(struct coord *c1, struct coord *c2)
472 static const int earth_radius = 6371*1000; //m change accordingly
473 // static const int earth_radius = 3960; //miles
476 float lat1 = GC2RAD(c1->y);
477 float long1 = GC2RAD(c1->x);
480 float lat2 = GC2RAD(c2->y);
481 float long2 = GC2RAD(c2->x);
484 float dlong = long2-long1;
485 float dlat = lat2-lat1;
487 float sinlat = sinf(dlat/2);
488 float sinlong = sinf(dlong/2);
490 float a=(sinlat*sinlat)+cosf(lat1)*cosf(lat2)*(sinlong*sinlong);
491 float c=2*asinf(minf(1,sqrt(a)));
493 return round(earth_radius*c);
495 return earth_radius*c;
498 #define GMETER 2.3887499999999999
502 return sqrt(dx*dx+dy*dy)*GMETER;
508 transform_scale(int y)
514 transform_to_geo(projection_mg, &c, &g);
515 return 1/cos(g.lat/180*M_PI);
520 tab_sqrt[]={14142,13379,12806,12364,12018,11741,11517,11333,11180,11051,10943,10850,10770,10701,10640,10587,10540,10499,10462,10429,10400,10373,10349,10327,10307,10289,10273,10257,10243,10231,10219,10208};
524 transform_distance(enum projection pro, struct coord *c1, struct coord *c2)
526 if (pro == projection_mg) {
528 double dx,dy,scale=transform_scale((c1->y+c2->y)/2);
531 return sqrt(dx*dx+dy*dy)/scale;
533 int dx,dy,f,scale=15539;
540 while (dx > 20000 || dy > 20000) {
546 return dx*10000/scale;
548 return dy*10000/scale;
552 return dx*10000/scale;
553 return dx*tab_sqrt[f]/scale;
557 return dy*10000/scale;
558 return dy*tab_sqrt[f]/scale;
561 } else if (pro == projection_garmin) {
562 return transform_distance_garmin(c1, c2);
564 dbg(0,"Unknown projection: %d\n", pro);
570 transform_polyline_length(enum projection pro, struct coord *c, int count)
575 for (i = 0 ; i < count-1 ; i++)
576 ret+=transform_distance(pro, &c[i], &c[i+1]);
581 transform_distance_sq(struct coord *c1, struct coord *c2)
586 if (dx > 32767 || dy > 32767 || dx < -32767 || dy < -32767)
593 transform_distance_sq_pc(struct pcoord *c1, struct pcoord *c2)
596 p1.x = c1->x; p1.y = c1->y;
597 p2.x = c2->x; p2.y = c2->y;
598 return transform_distance_sq(&p1, &p2);
602 transform_distance_line_sq(struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
618 return transform_distance_sq(l0, ref);
624 return transform_distance_sq(l1, ref);
626 while (c1 > climit || c2 > climit) {
634 return transform_distance_sq(&l, ref);
638 transform_distance_polyline_sq(struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
646 dist=transform_distance_line_sq(&c[0], &c[1], ref, lpnt);
647 for (i=2 ; i < count ; i++) {
648 distn=transform_distance_line_sq(&c[i-1], &c[i], ref, &lp);
662 transform_print_deg(double deg)
664 printf("%2.0f:%2.0f:%2.4f", floor(deg), fmod(deg*60,60), fmod(deg*3600,60));
668 static int tab_atan[]={0,262,524,787,1051,1317,1584,1853,2126,2401,2679,2962,3249,3541,3839,4142,4452,4770,5095,5430,5774,6128,6494,6873,7265,7673,8098,8541,9004,9490,10000,10538};
671 atan2_int_lookup(int val)
673 int len=sizeof(tab_atan)/sizeof(int);
678 if (val < tab_atan[p])
681 if (val < tab_atan[p+1])
689 atan2_int(int dx, int dy)
691 int f,mul=1,add=0,ret;
693 return dy < 0 ? 180 : 0;
696 return dx < 0 ? -90 : 90;
707 while (dx > 20000 || dy > 20000) {
712 ret=90-atan2_int_lookup(dy*10000/dx);
714 ret=atan2_int_lookup(dx*10000/dy);
721 transform_get_angle_delta(struct coord *c1, struct coord *c2, int dir)
731 angle=atan2_int(dx,dy);
741 transform_within_border(struct transformation *this_, struct point *p, int border)
743 struct map_selection *ms=this_->screen_sel;
745 struct point_rect *r=&ms->u.p_rect;
746 if (p->x >= r->lu.x+border && p->x <= r->rl.x-border &&
747 p->y >= r->lu.y+border && p->y <= r->rl.y-border)
755 transform_within_dist_point(struct coord *ref, struct coord *c, int dist)
757 if (c->x-dist > ref->x)
759 if (c->x+dist < ref->x)
761 if (c->y-dist > ref->y)
763 if (c->y+dist < ref->y)
765 if ((c->x-ref->x)*(c->x-ref->x) + (c->y-ref->y)*(c->y-ref->y) <= dist*dist)
771 transform_within_dist_line(struct coord *ref, struct coord *c0, struct coord *c1, int dist)
778 if (c0->x-dist > ref->x)
780 if (c1->x+dist < ref->x)
783 if (c1->x-dist > ref->x)
785 if (c0->x+dist < ref->x)
789 if (c0->y-dist > ref->y)
791 if (c1->y+dist < ref->y)
794 if (c1->y-dist > ref->y)
796 if (c0->y+dist < ref->y)
806 return transform_within_dist_point(ref, c0, dist);
809 return transform_within_dist_point(ref, c1, dist);
813 return transform_within_dist_point(ref, &lc, dist);
817 transform_within_dist_polyline(struct coord *ref, struct coord *c, int count, int close, int dist)
820 for (i = 0 ; i < count-1 ; i++) {
821 if (transform_within_dist_line(ref,c+i,c+i+1,dist)) {
826 return (transform_within_dist_line(ref,c,c+count-1,dist));
831 transform_within_dist_polygon(struct coord *ref, struct coord *c, int count, int dist)
834 for (i = 0, j = count-1; i < count; j = i++) {
835 if ((((c[i].y <= ref->y) && ( ref->y < c[j].y )) ||
836 ((c[j].y <= ref->y) && ( ref->y < c[i].y))) &&
837 (ref->x < (c[j].x - c[i].x) * (ref->y - c[i].y) / (c[j].y - c[i].y) + c[i].x))
842 return transform_within_dist_polyline(ref, c, count, dist, 1);
850 transform_within_dist_item(struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
852 if (type < type_line)
853 return transform_within_dist_point(ref, c, dist);
854 if (type < type_area)
855 return transform_within_dist_polyline(ref, c, count, 0, dist);
856 return transform_within_dist_polygon(ref, c, count, dist);
860 transform_destroy(struct transformation *t)
867 Note: there are many mathematically equivalent ways to express these formulas. As usual, not all of them are computationally equivalent.
869 L = latitude in radians (positive north)
870 Lo = longitude in radians (positive east)
872 N = northing (meters)
877 N = r ln [ tan (pi/4 + L/2) ]
881 r = radius of the sphere (meters)
882 ln() is the natural logarithm
887 N = a * ln ( tan (pi/4 + L/2) * ( (1 - e * sin (L)) / (1 + e * sin (L))) ** (e/2) )
893 = a ln( tan( ---- + ---) (--------------) )
899 a = the length of the semi-major axis of the ellipsoid (meters)
900 e = the first eccentricity of the ellipsoid