Patch:core:Added geo coordinate formatting function that supports multiple formats |
authorsteven_s <steven_s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 9 Nov 2008 16:13:30 +0000 (16:13 +0000)
committersteven_s <steven_s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 9 Nov 2008 16:13:30 +0000 (16:13 +0000)
Replacing local geoformatting with calls to this function.

git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk/navit@1709 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/coord.c
navit/coord.h
navit/data/garmin_img/garmin_img.c
navit/gui/gtk/gui_gtk_statusbar.c
navit/popup.c
navit/transform.c
navit/transform.h

index 7879347..f74eb32 100644 (file)
@@ -280,4 +280,58 @@ coord_print(enum projection pro, struct coord *c, FILE *out) {
        return;
 }
 
+/**
+ * @brief Converts a lat/lon into a text formatted text string.
+ * @param lat The latitude
+ * @param lng The longitude
+ * @param fmt The format to use. 
+ *    @li DEGREES=>(45.5000N 100.9000S)
+ *    @li DEGREES_MINUTES=>(45 30.))00N 100 120.54.0000S)
+ *    @li DEGREES_MINUTES_SECONDS=>(4530.0000N 12054.0000S)
+ *           
+ * 
+ * @param buffer  A buffer large enough to hold the output + a terminating NULL (26 bytes)
+ * @param size The size of the buffer
+ *
+ */
+void coord_format(float lat,float lng, enum coord_format fmt, char * buffer, int size)
+{
+
+       char lat_c='N';
+       char lng_c='E';
+       float lat_deg,lat_min,lat_sec;
+       float lng_deg,lng_min,lng_sec;
+
+       if (lng < 0) {
+               lng=-lng;
+               lng_c='W';
+       }
+       if (lat < 0) {
+               lat=-lat;
+               lat_c='S';
+       }
+       lat_deg=lat;
+       lat_min=(lat-floor(lat_deg))*60;
+       lat_sec=fmod(lat*3600,60);
+       lng_deg=lng;
+       lng_min=(lng-floor(lng_deg))*60;
+       lng_sec=fmod(lng*3600,60);
+       switch(fmt)
+       {
+
+       case DEGREES_DECIMAL:
+         snprintf(buffer,size,"%02.6f%c %03.7f%c",lat,lat_c,lng,lng_c);
+         break;
+       case DEGREES_MINUTES:
+         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);
+                  break;
+       case DEGREES_MINUTES_SECONDS:
+         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);
+         break;
+         
+       
+       }
+       
+}
+
 /** @} */
index 3ffef90..d5546b1 100644 (file)
@@ -59,6 +59,30 @@ struct coord_geo_cart {
        double z; /*!< Z-Value */
 };
 
+/**
+ * An enumeration of formats for printing geographic coordinates in.
+ *
+ */
+enum coord_format 
+{
+       /**
+        * Degrees with decimal places.
+        * Ie 20.5000 N 110.5000 E
+        */
+       DEGREES_DECIMAL,
+
+       /**
+        * Degrees and minutes.
+        * ie 20 30.00 N 110 30.00 E
+        */
+       DEGREES_MINUTES,
+       /**
+        * Degrees, minutes and seconds.
+        * ie 20 30 30.00 N 110 30 30 E
+        */
+       DEGREES_MINUTES_SECONDS 
+};
+
 enum projection;
 struct attr;
 
@@ -73,6 +97,6 @@ void coord_rect_destroy(struct coord_rect *r);
 int coord_rect_overlap(struct coord_rect *r1, struct coord_rect *r2);
 int coord_rect_contains(struct coord_rect *r, struct coord *c);
 void coord_rect_extend(struct coord_rect *r, struct coord *c);
-
+void coord_format(float lat,float lng, enum coord_format, char * buffer, int size);
 
 #endif
index 4c7a962..58fe489 100644 (file)
@@ -697,7 +697,8 @@ dump_region_item(struct subdivision *sub, struct file *rgn, struct map_rect_priv
                                        g.lng=(triple(&sub->center.lng)+(pnt->lng_delta << shift))*conv;
                                        g.lat=(triple(&sub->center.lat)+(pnt->lat_delta << shift))*conv;
                                        printf("%f %f\n", g.lng, g.lat);
-                                       transform_geo_text(&g, buffer);
+                                       coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,
+                                                    buffer,sizeof(buffer));
                                        printf("%s\n", buffer);
                                        dump_label_offset(mr, triple_u(&pnt->lbl_offset));
                                        if (pnt->info & 0x80) 
index 69b1b88..1d70dd5 100644 (file)
@@ -57,23 +57,14 @@ statusbar_destroy(struct statusbar_priv *this)
 static void
 statusbar_gps_update(struct statusbar_priv *this, int sats, int qual, double lng, double lat, double height, double direction, double speed)
 {
-       char lat_c='N';
-       char lng_c='E';
-    char *dirs[]={_("N"),_("NE"),_("E"),_("SE"),_("S"),_("SW"),_("W"),_("NW"),_("N")};
+       char *dirs[]={_("N"),_("NE"),_("E"),_("SE"),_("S"),_("SW"),_("W"),_("NW"),_("N")};
        char *dir;
        int dir_idx;
+       char pos_text[26];
 
-       if (lng < 0) {
-               lng=-lng;
-               lng_c='W';
-       }
-       if (lat < 0) {
-               lat=-lat;
-               lat_c='S';
-       }
-       dir_idx=(direction+22.5)/45;
+       coord_format(lat,lng,DEGREES_MINUTES_SECONDS,pos_text,sizeof(pos_text));
        dir=dirs[dir_idx];
-       sprintf(this->gps_text, "GPS %02d/%02d %02.0f%07.4f%c %03.0f%07.4f%c %4.0fm %3.0f°%-2s %3.0fkm/h", sats, qual, floor(lat), fmod(lat*60,60), lat_c, floor(lng), fmod(lng*60,60), lng_c, height, direction, dir, speed);
+       sprintf(this->gps_text, "GPS %02d/%02d %s %4.0fm %3.0f°%-2s %3.0fkm/h", sats, qual, pos_text, height, direction, dir, speed);
        gtk_label_set_text(GTK_LABEL(this->gps), this->gps_text);
 
 }
@@ -103,8 +94,6 @@ statusbar_route_update(struct statusbar_priv *this, struct navit *navit, struct
        char buffer[128];
        double lng, lat, direction=0, height=0, speed=0, hdop=0;
        int sats=0, qual=0;
-       char lat_c='N';
-       char lng_c='E';
        int status;
        char *dirs[]={_("N"),_("NE"),_("E"),_("SE"),_("S"),_("SW"),_("W"),_("NW"),_("N")};
        char *dir;
@@ -137,14 +126,6 @@ statusbar_route_update(struct statusbar_priv *this, struct navit *navit, struct
                return;
        lng=attr.u.coord_geo->lng;
        lat=attr.u.coord_geo->lat;
-       if (lng < 0) {
-               lng=-lng;
-               lng_c='W';
-       }
-       if (lat < 0) {
-               lat=-lat;
-               lat_c='S';
-       }
        if (vehicle_get_attr(v, attr_position_fix_type, &attr, NULL))
                status=attr.u.num;
        if (vehicle_get_attr(v, attr_position_direction, &attr, NULL))
@@ -164,10 +145,10 @@ statusbar_route_update(struct statusbar_priv *this, struct navit *navit, struct
                sats=attr.u.num;
        if (vehicle_get_attr(v, attr_position_qual, &attr, NULL))
                qual=attr.u.num;
-       sprintf(this->gps_text,"GPS:%s %02d/%02d HD:%02.2f %02.0f%07.4f%c %03.0f%07.4f%c %4.0fm %3.0f°%-2s %3.0fkm/h", 
+       coord_format(lat,lng,DEGREES_MINUTES_SECONDS,buffer,sizeof(buffer));
+       sprintf(this->gps_text,"GPS:%s %02d/%02d HD:%02.2f %s %4.0fm %3.0f°%-2s %3.0fkm/h", 
                        status_fix2str(status),
-                       sats, qual, hdop, floor(lat), fmod(lat*60,60),
-                       lat_c, floor(lng), fmod(lng*60,60), lng_c, height,
+                       sats, qual, hdop, buffer, height,
                        direction, dir, speed);
        gtk_label_set_text(GTK_LABEL(this->gps), this->gps_text);
 }
index 9ba1812..1637605 100644 (file)
@@ -70,7 +70,7 @@ popup_set_destination(struct navit *nav, struct pcoord *pc)
        c.x = pc->x;
        c.y = pc->y;
        transform_to_geo(transform_get_projection(navit_get_trans(nav)), &c, &g);
-       transform_geo_text(&g, buffer_geo);     
+       coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer_geo,sizeof(buffer_geo));
        sprintf(buffer,"Map Point %s", buffer_geo);
        navit_set_destination(nav, pc, buffer);
 }
@@ -85,7 +85,7 @@ popup_set_bookmark(struct navit *nav, struct pcoord *pc)
        c.x = pc->x;
        c.y = pc->y;
        transform_to_geo(pc->pro, &c, &g);
-       transform_geo_text(&g, buffer_geo);
+       coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer_geo,sizeof(buffer_geo));
        sprintf(buffer,"Map Point %s", buffer_geo);
        if (!gui_add_bookmark(navit_get_gui(nav), pc, buffer)) 
                navit_add_bookmark(nav, pc, buffer);
@@ -262,7 +262,7 @@ popup(struct navit *nav, int button, struct point *p)
        men=popup_printf(popup, menu_type_submenu, _("Point 0x%x 0x%x"), co.x, co.y);
        popup_printf(men, menu_type_menu, _("Screen coord : %d %d"), p->x, p->y);
        transform_to_geo(transform_get_projection(navit_get_trans(nav)), &co, &g);
-       transform_geo_text(&g, buffer); 
+       coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer,sizeof(buffer));
        popup_printf(men, menu_type_menu, "%s", buffer);
        popup_printf(men, menu_type_menu, "%f %f", g.lat, g.lng);
        dbg(0,"%p %p\n", nav, &c);
index 39b089f..0ace912 100644 (file)
@@ -440,26 +440,6 @@ transform_get_order(struct transformation *t)
 }
 
 
-void
-transform_geo_text(struct coord_geo *g, char *buffer)
-{
-       double lng=g->lng;
-       double lat=g->lat;
-       char lng_c='E';
-       char lat_c='N';
-
-       if (lng < 0) {
-               lng=-lng;
-               lng_c='W';
-       }
-       if (lat < 0) {
-               lat=-lat;
-               lat_c='S';
-       }
-
-       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);
-
-}
 
 #define TWOPI (M_PI*2)
 #define GC2RAD(c) ((c) * TWOPI/(1<<24))
index 21f9296..138a218 100644 (file)
@@ -57,7 +57,6 @@ void transform_setup_source_rect(struct transformation *t);
 long transform_get_scale(struct transformation *t);
 void transform_set_scale(struct transformation *t, long scale);
 int transform_get_order(struct transformation *t);
-void transform_geo_text(struct coord_geo *g, char *buffer);
 double transform_scale(int y);
 double transform_distance(enum projection pro, struct coord *c1, struct coord *c2);
 double transform_polyline_length(enum projection pro, struct coord *c, int count);