2 * Copyright (C) 2006, 2007 John Costigan.
4 * POI and GPS-Info code originally written by Cezary Jackiewicz.
6 * Default map data provided by http://www.openstreetmap.org/
8 * This file is part of Maemo Mapper.
10 * Maemo Mapper is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * Maemo Mapper is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Maemo Mapper. If not, see <http://www.gnu.org/licenses/>.
35 # include <hildon/hildon-note.h>
37 # include <hildon-widgets/hildon-note.h>
49 * Pop up a modal dialog box with simple error information in it.
52 popup_error(GtkWidget *window, const gchar *error)
55 printf("%s(\"%s\")\n", __PRETTY_FUNCTION__, error);
57 dialog = hildon_note_new_information(GTK_WINDOW(window), error);
59 gtk_dialog_run(GTK_DIALOG(dialog));
60 gtk_widget_destroy(dialog);
62 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
66 deg_format(gdouble coor, gchar *scoor, gchar neg_char, gchar pos_char)
69 gdouble acoor = fabs(coor);
70 printf("%s()\n", __PRETTY_FUNCTION__);
77 // These formats should not be formatted in the same way
78 // - they need to be converted first, therefore if we reach
79 // this bit of code use the first available format - drop through.
81 sprintf(scoor, "%.5f°", coor);
85 sprintf(scoor, "%.5f° %c", acoor,
86 coor < 0.0 ? neg_char : pos_char);
89 sprintf(scoor, "%c %.5f°",
90 coor < 0.0 ? neg_char : pos_char,
95 sprintf(scoor, "%d°%06.3f'",
96 (int)coor, (acoor - (int)acoor)*60.0);
99 sprintf(scoor, "%d°%06.3f' %c",
100 (int)acoor, (acoor - (int)acoor)*60.0,
101 coor < 0.0 ? neg_char : pos_char);
104 sprintf(scoor, "%c %d° %06.3f'",
105 coor < 0.0 ? neg_char : pos_char,
106 (int)acoor, (acoor - (int)acoor)*60.0);
110 min = (acoor - (int)acoor)*60.0;
111 sprintf(scoor, "%d°%02d'%04.1f\"", (int)coor, (int)min,
112 ((min - (int)min)*60.0));
114 case DD_MM_SSPS_NSEW:
115 min = (acoor - (int)acoor)*60.0;
116 sprintf(scoor, "%d°%02d'%04.1f\" %c", (int)acoor, (int)min,
117 ((min - (int)min)*60.0),
118 coor < 0.0 ? neg_char : pos_char);
120 case NSEW_DD_MM_SSPS:
121 min = (acoor - (int)acoor)*60.0;
122 sprintf(scoor, "%c %d° %02d' %04.1f\"",
123 coor < 0.0 ? neg_char : pos_char,
124 (int)acoor, (int)min,
125 ((min - (int)min)*60.0));
128 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
131 /** Return the location (in units) of the given string address. This function
132 * makes a call to the internet, so it may take some time. */
133 Point locate_address(GtkWidget *parent, const gchar *addr)
136 Point retval = _point_null;
137 GnomeVFSResult vfs_result;
142 printf("%s(%s)\n", __PRETTY_FUNCTION__, addr);
144 addr_escaped = gnome_vfs_escape_string(addr);
145 buffer = g_strdup_printf(_route_dl_url, addr_escaped, addr_escaped);
146 g_free(addr_escaped);
148 /* Attempt to download the route from the server. */
149 if(GNOME_VFS_OK != (vfs_result = gnome_vfs_read_entire_file(
150 buffer, &size, &bytes)))
154 popup_error(parent, _("Failed to connect to GPX Directions server"));
160 MACRO_PATH_INIT(temp);
162 if(strncmp(bytes, "<?xml", strlen("<?xml")))
164 /* Not an XML document - must be bad locations. */
165 popup_error(parent, _("Invalid address."));
167 /* Else, if GPS is enabled, replace the route, otherwise append it. */
168 else if(!gpx_path_parse(&temp, bytes, size, 0) || !temp.head[1].unity)
170 popup_error(parent, _("Unknown error while locating address."));
174 /* Save Destination in Route Locations list. */
176 if(!g_slist_find_custom(_loc_list, addr, (GCompareFunc)strcmp))
178 _loc_list = g_slist_prepend(_loc_list, g_strdup(addr));
179 gtk_list_store_insert_with_values(_loc_model, &iter,
180 INT_MAX, 0, addr, -1);
183 retval = temp.head[1];
186 MACRO_PATH_FREE(temp);
189 vprintf("%s(): return (%d, %d)\n", __PRETTY_FUNCTION__,
190 retval.unitx, retval.unity);
195 * Calculate the distance between two lat/lon pairs. The distance is returned
196 * in nautical miles and should be converted using UNITS_CONVERT[_units].
199 calculate_distance(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2)
201 gdouble dlat, dlon, slat, slon, a;
203 /* Convert to radians. */
204 lat1 = deg2rad(lat1);
205 lon1 = deg2rad(lon1);
206 lat2 = deg2rad(lat2);
207 lon2 = deg2rad(lon2);
212 slat = sin(dlat / 2.0);
213 slon = sin(dlon / 2.0);
214 a = (slat * slat) + (cos(lat1) * cos(lat2) * slon * slon);
216 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
217 return ((2.0 * atan2(sqrt(a), sqrt(1.0 - a))) * EARTH_RADIUS);
221 * Calculate the bearing between two lat/lon pairs. The bearing is returned
222 * as the angle from lat1/lon1 to lat2/lon2.
225 calculate_bearing(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2)
228 gdouble dlon = deg2rad(lon2 - lon1);
229 lat1 = deg2rad(lat1);
230 lat2 = deg2rad(lat2);
232 y = sin(dlon) * cos(lat2);
233 x = (cos(lat1) * sin(lat2)) - (sin(lat1) * cos(lat2) * cos(dlon));
235 dlon = rad2deg(atan2(y, x));
244 force_min_visible_bars(HildonControlbar *control_bar, gint num_bars)
248 printf("%s()\n", __PRETTY_FUNCTION__);
249 memset(&val, 0, sizeof(val));
250 g_value_init(&val, G_TYPE_INT);
251 g_value_set_int(&val, num_bars);
252 g_object_set_property(G_OBJECT(control_bar), "minimum-visible-bars", &val);
253 vprintf("%s(): return\n", __PRETTY_FUNCTION__);
260 printf("%s()\n", __PRETTY_FUNCTION__);
262 /* Re-enable any banners that might have been up. */
266 gtk_widget_hide(_connect_banner);
267 gtk_widget_unrealize(_connect_banner);
268 gtk_widget_realize(_connect_banner);
269 gtk_widget_show(_connect_banner);
274 gtk_widget_hide(_fix_banner);
275 gtk_widget_unrealize(_fix_banner);
276 gtk_widget_realize(_fix_banner);
277 gtk_widget_show(_fix_banner);
282 gtk_widget_hide(_waypoint_banner);
283 gtk_widget_unrealize(_waypoint_banner);
284 gtk_widget_realize(_waypoint_banner);
285 gtk_widget_show(_waypoint_banner);
290 gtk_widget_hide(_download_banner);
291 gtk_widget_unrealize(_download_banner);
292 gtk_widget_realize(_download_banner);
293 gtk_widget_show(_download_banner);
297 ConnState old_state = _gps_state;
298 set_conn_state(RCVR_OFF);
299 set_conn_state(old_state);
303 vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
307 * Get one numeric token off the string, fractional part separator
308 * is ',' or '.' and number may follow any token from sep.
309 * Return value found is in d.
311 * If utf8_deg is set then accept also Unicode degree symbol U+00B0
312 * encoded as UTF-8 octets 0xc2 0xb0.
316 * 1 : when nothing or just white space was seen.
317 * 2 : when fractional number was seen.
318 * 3 : when whole number was seen.
319 * In case 0, endptr points to the beginning of the input string nptr,
322 * In case 1 endptr points past the whitespace, d is undefined.
324 * In cases 2 and 3 the found number is stored in d and endptr points
325 * to first char not part of the number.
329 strdmstod_1(gdouble *d, gchar *nptr, gchar **endptr, gchar *sep, gint utf8_deg)
337 while (*p && isspace(*p)) {
346 while (*p && isdigit(*p)) {
353 if (*p && (*p == '.' || *p == ',')) {
359 /* fractional part */
362 while (*p && isdigit(*p)) {
374 /* allow for extra sep char at the end */
375 if (*p && strchr(sep, *p)) {
377 } else if (utf8_deg && *p == 0xc2 && *(p+1) == 0xb0) {
382 if (endptr) *endptr = p;
383 return f_flag ? 2 : 3;
386 if (endptr) *endptr = p;
391 strdmstod_2(gchar *nptr, gchar **endptr)
400 ret = strdmstod_1(&d, p, endptr, "dD@", 1);
404 if (endptr) *endptr = (char *)nptr;
412 ret = strdmstod_1(&m, p, endptr, "mM'", 0);
416 case 2: return d + m/60.0;
421 ret = strdmstod_1(&s, p, endptr, "sS\"", 0);
424 case 1: return d + m/60.0;
426 case 3: return d + m/60.0 + s/3600.0;
434 * format: / \s* [+-NSWE]? \s* ( d | D \s+ m | D \s+ M \s+ s ) [NSWE]? /x
435 * where D := / \d+[@d°]? /ix
436 * M := / \d+['m]? /ix
437 * d := / D | \d+[,.]\d+[@d°]? /ix
438 * m := / M | \d+[,.]\d+['m]? /ix
439 * s := / S | \d+[,.]\d+["s]? /ix
441 * and N or W are treated as positive, S or E are treated as negative,
442 * they may occur only once.
445 strdmstod(const gchar *nptr, gchar **endptr)
454 while(*p && isspace(*p))
459 *endptr = (char *)nptr;
463 if(strchr("nwseNWSE-+", *p)) {
468 d = strdmstod_2(p, &end);
469 if(p == end && d == 0.0) {
470 if(endptr) *endptr = end;
475 while(*p && isspace(*p))
480 if(*p && strchr("nwseNWSE", *p)) {
481 if(tolower(*p) == 's' || tolower(*p) == 'w')
486 if(tolower(*sign) == 's' || tolower(*sign) == 'w' || *sign == '-')
488 printf("s: %d\n", s);
491 if(endptr) *endptr = p;
495 double marc(double bf0, double n, double phi0, double phi)
497 return bf0 * (((1 + n + ((5 / 4) * (n * n)) + ((5 / 4) * (n * n * n))) * (phi - phi0))
498 - (((3 * n) + (3 * (n * n)) + ((21 / 8) * (n * n * n))) * (sin(phi - phi0)) * (cos(phi + phi0)))
499 + ((((15 / 8) * (n * n)) + ((15 / 8) * (n * n * n))) * (sin(2 * (phi - phi0))) * (cos(2 * (phi + phi0))))
500 - (((35 / 24) * (n * n * n)) * (sin(3 * (phi - phi0))) * (cos(3 * (phi + phi0)))));
503 gboolean os_grid_check_lat_lon(double lat, double lon)
505 // TODO - Check exact OS Grid range
506 if(lat < 50.0 || lat > 62 || lon < -7.5 || lon > 2.2 )
516 gboolean coord_system_check_lat_lon (gdouble lat, gdouble lon, gint *fallback_deg_format)
518 // Is the current coordinate system applicable to the provided lat and lon?
519 gboolean valid = FALSE;
525 valid = os_grid_check_lat_lon(lat, lon);
526 if(fallback_deg_format != NULL) *fallback_deg_format = DDPDDDDD;
536 gboolean convert_lat_long_to_os_grid(double lat, double lon, int *easting, int *northing)
538 if(!os_grid_check_lat_lon(lat, lon))
543 const double deg2rad = (2 * PI / 360);
545 const double phi = lat * deg2rad; // convert latitude to radians
546 const double lam = lon * deg2rad; // convert longitude to radians
547 const double a = 6377563.396; // OSGB semi-major axis
548 const double b = 6356256.91; // OSGB semi-minor axis
549 const double e0 = 400000; // easting of false origin
550 const double n0 = -100000; // northing of false origin
551 const double f0 = 0.9996012717; // OSGB scale factor on central meridian
552 const double e2 = 0.0066705397616; // OSGB eccentricity squared
553 const double lam0 = -0.034906585039886591; // OSGB false east
554 const double phi0 = 0.85521133347722145; // OSGB false north
555 const double af0 = a * f0;
556 const double bf0 = b * f0;
559 double slat2 = sin(phi) * sin(phi);
560 double nu = af0 / (sqrt(1 - (e2 * (slat2))));
561 double rho = (nu * (1 - e2)) / (1 - (e2 * slat2));
562 double eta2 = (nu / rho) - 1;
563 double p = lam - lam0;
564 double IV = nu * cos(phi);
565 double clat3 = pow(cos(phi), 3);
566 double tlat2 = tan(phi) * tan(phi);
567 double V = (nu / 6) * clat3 * ((nu / rho) - tlat2);
568 double clat5 = pow(cos(phi), 5);
569 double tlat4 = pow(tan(phi), 4);
570 double VI = (nu / 120) * clat5 * ((5 - (18 * tlat2)) + tlat4 + (14 * eta2) - (58 * tlat2 * eta2));
571 double east = e0 + (p * IV) + (pow(p, 3) * V) + (pow(p, 5) * VI);
574 double n = (af0 - bf0) / (af0 + bf0);
575 double M = marc(bf0, n, phi0, phi);
577 double II = (nu / 2) * sin(phi) * cos(phi);
578 double III = ((nu / 24) * sin(phi) * pow(cos(phi), 3)) * (5 - pow(tan(phi), 2) + (9 * eta2));
579 double IIIA = ((nu / 720) * sin(phi) * clat5) * (61 - (58 * tlat2) + tlat4);
580 double north = I + ((p * p) * II) + (pow(p, 4) * III) + (pow(p, 6) * IIIA);
582 // make whole number values
583 *easting = round(east); // round to whole number
584 *northing = round(north); // round to whole number
589 gboolean convert_os_grid_to_bng(gint easting, gint northing, gchar* bng)
591 gdouble eX = (gdouble)easting / 500000.0;
592 gdouble nX = (gdouble)northing / 500000.0;
593 gdouble tmp = floor(eX) - 5.0 * floor(nX) + 17.0;
597 nX = 5.0 * (nX - floor(nX));
598 eX = 20.0 - 5.0 * floor(nX) + floor(5.0 * (eX - floor(eX)));
599 if (eX > 7.5) eX = eX + 1; // I is not used
600 if (tmp > 7.5) tmp = tmp + 1; // I is not used
602 snprintf(eing, 12, "%u", easting);
603 snprintf(ning, 12, "%u", northing);
605 gint lnth = strlen(eing);
606 // Note - we only want 4 numbers
607 snprintf(eing, 5, "%s", eing+(lnth-5));
610 snprintf(ning, 5, "%s", ning+(lnth-5));
612 sprintf(bng, "%c%c%s%s",
622 gboolean convert_os_xy_to_latlon(const gchar *easting, const gchar *northing, gdouble *d_lat, gdouble *d_lon)
624 gint64 i64_n = g_ascii_strtoll (northing, NULL, 10);
625 gint64 i64_e = g_ascii_strtoll (easting, NULL, 10);
627 const double deg2rad = (2 * PI / 360);
629 const gdouble N = (gdouble)i64_n;
630 const gdouble E = (gdouble)i64_e;
632 const gdouble a = 6377563.396, b = 6356256.910; // Airy 1830 major & minor semi-axes
633 const gdouble F0 = 0.9996012717; // NatGrid scale factor on central meridian
634 const gdouble lat0 = 49*PI/180, lon0 = -2*PI/180; // NatGrid true origin
635 const gdouble N0 = -100000, E0 = 400000; // northing & easting of true origin, metres
636 const gdouble e2 = 1 - (b*b)/(a*a); // eccentricity squared
637 const gdouble n = (a-b)/(a+b), n2 = n*n, n3 = n*n*n;
639 gdouble lat=lat0, M=0;
641 lat = (N-N0-M)/(a*F0) + lat;
643 const gdouble Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (lat-lat0);
644 const gdouble Mb = (3*n + 3*n*n + (21/8)*n3) * sin(lat-lat0) * cos(lat+lat0);
645 const gdouble Mc = ((15/8)*n2 + (15/8)*n3) * sin(2*(lat-lat0)) * cos(2*(lat+lat0));
646 const gdouble Md = (35/24)*n3 * sin(3*(lat-lat0)) * cos(3*(lat+lat0));
647 M = b * F0 * (Ma - Mb + Mc - Md); // meridional arc
649 } while (N-N0-M >= 0.00001); // ie until < 0.01mm
651 const gdouble cosLat = cos(lat), sinLat = sin(lat);
652 const gdouble nu = a*F0/sqrt(1-e2*sinLat*sinLat); // transverse radius of curvature
653 const gdouble rho = a*F0*(1-e2)/pow(1-e2*sinLat*sinLat, 1.5); // meridional radius of curvature
654 const gdouble eta2 = nu/rho-1;
656 const gdouble tanLat = tan(lat);
657 const gdouble tan2lat = tanLat*tanLat, tan4lat = tan2lat*tan2lat, tan6lat = tan4lat*tan2lat;
658 const gdouble secLat = 1/cosLat;
659 const gdouble nu3 = nu*nu*nu, nu5 = nu3*nu*nu, nu7 = nu5*nu*nu;
660 const gdouble VII = tanLat/(2*rho*nu);
661 const gdouble VIII = tanLat/(24*rho*nu3)*(5+3*tan2lat+eta2-9*tan2lat*eta2);
662 const gdouble IX = tanLat/(720*rho*nu5)*(61+90*tan2lat+45*tan4lat);
663 const gdouble X = secLat/nu;
664 const gdouble XI = secLat/(6*nu3)*(nu/rho+2*tan2lat);
665 const gdouble XII = secLat/(120*nu5)*(5+28*tan2lat+24*tan4lat);
666 const gdouble XIIA = secLat/(5040*nu7)*(61+662*tan2lat+1320*tan4lat+720*tan6lat);
668 const gdouble dE = (E-E0), dE2 = dE*dE, dE3 = dE2*dE, dE4 = dE2*dE2, dE5 = dE3*dE2;
669 const gdouble dE6 = dE4*dE2, dE7 = dE5*dE2;
670 lat = lat - VII*dE2 + VIII*dE4 - IX*dE6;
671 const gdouble lon = lon0 + X*dE - XI*dE3 + XII*dE5 - XIIA*dE7;
673 *d_lon = lon / deg2rad;
674 *d_lat = lat / deg2rad;
679 gboolean convert_os_ngr_to_latlon(const gchar *text, gdouble *d_lat, gdouble *d_lon)
681 // get numeric values of letter references, mapping A->0, B->1, C->2, etc:
685 gchar s_e[6], s_n[6];
686 gchar easting[7], northing[7];
691 if( ((gchar)text[0])>='a' && ((gchar)text[0]) <= 'z' )
692 l1 = text[0] - (gint)'a'; // lower case
693 else if( ((gchar)text[0])>='A' && ((gchar)text[0]) <= 'Z' )
694 l1 = text[0] - (gint)'A'; // upper case
696 return FALSE; // Not a letter - invalid grid ref
698 if( ((gchar)text[1])>='a' && ((gchar)text[1]) <= 'z' )
699 l2 = text[1] - (gint)'a'; // lower case
700 else if( ((gchar)text[1])>='A' && ((gchar)text[1]) <= 'Z' )
701 l2 = text[1] - (gint)'A'; // upper case
703 return FALSE; // Not a letter - invalid grid ref
706 // shuffle down letters after 'I' since 'I' is not used in grid:
710 // convert grid letters into 100km-square indexes from false origin (grid square SV):
711 gdouble e = ((l1-2)%5)*5 + (l2%5);
712 gdouble n = (19-floor(l1/5)*5) - floor(l2/5);
714 // skip grid letters to get numeric part of ref, stripping any spaces:
715 gchar *gridref = (gchar*)(text+2);
717 // user may have entered a space, so remove any spaces
718 while(gridref[0] == ' ')
720 gridref = (gchar*)(text+1);
724 // floor the length incase a space has been added
725 const gint len = (gint)floor((gdouble)strlen(gridref)/2.00); // normally this will be 4, often 3
726 if(len>5) return FALSE;
730 snprintf(s_e, len+1, "%s", gridref);
732 while( (gchar)((gint)gridref+len) == ' ' )
733 gridref = (gchar*)(gridref+1); // Allow for a space
735 snprintf(s_n, len+1, "%s", gridref+len);
737 i64_e = g_ascii_strtoll (s_e, NULL, 10);
738 i64_n = g_ascii_strtoll (s_n, NULL, 10);
740 // Move to most significate values
741 i64_e *= pow(10, 5-len);
742 i64_n *= pow(10, 5-len);
745 // append numeric part of references to grid index:
746 e = (e*100000) + (gdouble)i64_e;
747 n = (n*100000) + (gdouble)i64_n;
749 snprintf(easting, 7, "%06u", (gint)e);
750 snprintf(northing, 7, "%06u", (gint)n);
752 convert_os_xy_to_latlon(easting, northing, d_lat, d_lon);
758 // Attempt to convert any user entered grid reference to a double lat/lon
759 // return TRUE on valid
760 gboolean parse_coords(const gchar* txt_lat, const gchar* txt_lon, gdouble* lat, gdouble* lon)
762 gboolean valid = FALSE;
764 // UK_NGR starts with two letters, and then all numbers - it may contain spaces - no lon will be entered
765 if( _degformat == UK_NGR)
767 valid = convert_os_ngr_to_latlon(txt_lat, lat, lon);
769 if(!valid || *lat < -90. || *lat > 90.) { valid = FALSE; }
770 else if(*lon < -180. || *lon > 180.) { valid = FALSE; }
772 // UK_OSGB contains two 6 digit integers
773 else if( _degformat == UK_OSGB)
775 valid = convert_os_xy_to_latlon(txt_lat, txt_lon, lat, lon);
777 if(!valid || *lat < -90. || *lat > 90.) { valid = FALSE; }
778 else if(*lon < -180. || *lon > 180.) { valid = FALSE; }
781 else if( _degformat == IARU_LOC)
783 valid = convert_iaru_loc_to_lat_lon(txt_lat, lat, lon);
785 if(!valid || *lat < -90. || *lat > 90.) { valid = FALSE; }
786 else if(*lon < -180. || *lon > 180.) { valid = FALSE; }
788 // It must either be invalid, or a lat/lon format
792 *lat = strdmstod(txt_lat, &error_check);
794 if(txt_lat == error_check || *lat < -90. || *lat > 90.) { valid = FALSE; }
795 else { valid = TRUE; }
799 *lon = strdmstod(txt_lon, &error_check);
801 if(txt_lon == error_check || *lon < -180. || *lon > 180.) { valid = FALSE; }
810 gboolean convert_iaru_loc_to_lat_lon(const gchar* txt_lon, gdouble* lat, gdouble* lon)
821 if(strlen(txt_lon) >= 1)
823 if( ((gchar)txt_lon[0])>='a' && ((gchar)txt_lon[0]) <= 'z' )
824 u_first = txt_lon[0] - (gint)'a'; // lower case
825 else if( ((gchar)txt_lon[0])>='A' && ((gchar)txt_lon[0]) <= 'Z' )
826 u_first = txt_lon[0] - (gint)'A'; // upper case
829 if(strlen(txt_lon) >= 2)
831 if( ((gchar)txt_lon[1])>='a' && ((gchar)txt_lon[1]) <= 'z' )
832 u_second = txt_lon[1] - (gint)'a'; // lower case
833 else if( ((gchar)txt_lon[1])>='A' && ((gchar)txt_lon[1]) <= 'Z' )
834 u_second = txt_lon[1] - (gint)'A'; // upper case
837 if(strlen(txt_lon) >= 3)
838 u_third = txt_lon[2] - (gint)'0';
840 if(strlen(txt_lon) >= 4)
841 u_fourth = txt_lon[3] - (gint)'0';
843 if(strlen(txt_lon) >= 5)
845 if( ((gchar)txt_lon[4])>='a' && ((gchar)txt_lon[4]) <= 'z' )
846 u_fifth = txt_lon[4] - (gint)'a'; // lower case
847 else if( ((gchar)txt_lon[4])>='A' && ((gchar)txt_lon[4]) <= 'Z' )
848 u_fifth = txt_lon[4] - (gint)'A'; // upper case
851 if(strlen(txt_lon) >= 6)
853 if( ((gchar)txt_lon[5])>='a' && ((gchar)txt_lon[5]) <= 'z' )
854 u_sixth = txt_lon[5] - (gint)'a'; // lower case
855 else if( ((gchar)txt_lon[5])>='A' && ((gchar)txt_lon[5]) <= 'Z' )
856 u_sixth = txt_lon[5] - (gint)'A'; // upper case
860 if(strlen(txt_lon) >= 7)
861 u_seventh= txt_lon[6] - (gint)'0';
863 if(strlen(txt_lon) >= 8)
864 u_eighth = txt_lon[7] - (gint)'0';
866 *lat = ((gdouble)u_first * 20.0) + ((gdouble)u_third * 2.0) + ((gdouble)u_fifth * (2.0/24.0)) + ((gdouble)u_seventh * (2.0/240.0)) - 90.0;
867 *lon = ((gdouble)u_second * 10.0) + ((gdouble)u_fourth) + ((gdouble)u_sixth * (1.0/24.0)) + ((gdouble)u_eighth * (1.0/240.0)) - 180.0;
872 void convert_lat_lon_to_iaru_loc(gdouble d_lat, gdouble d_lon, gchar *loc)
874 const gdouble d_a_lat = (d_lat+90.0);
875 const gdouble d_a_lon = (d_lon+180.0);
877 const gint i_first = (gint)floor(d_a_lon/20.0);
878 const gint i_second = (gint)floor(d_a_lat/10.0);
879 const gint i_third = (gint)floor((d_a_lon - (20.0*i_first))/2.0);
880 const gint i_fourth = (gint)floor((d_a_lat - (10.0*i_second)));
881 const gint i_fifth = (gint)floor((d_a_lon - (20.0*i_first) - (2.0*i_third))/(5.0/60.0));
882 const gint i_sixth = (gint)floor((d_a_lat - (10.0*i_second) - (i_fourth))/(2.5/60.0));
884 const gint i_seventh = (gint)floor((d_a_lon - (20.0*i_first) - (2.0*i_third)
885 - ((2.0/24.0)*i_fifth))/(2.0/240.0) );
886 const gint i_eighth = (gint)floor((d_a_lat - (10.0*i_second) - (i_fourth)
887 - ((1.0/24.0)*i_sixth))/(1.0/240.0));
890 sprintf(loc, "%c%c%u%u%c%c%u%u",
901 gboolean convert_lat_lon_to_bng(gdouble lat, gdouble lon, gchar* bng)
903 gint easting, northing;
905 if( convert_lat_long_to_os_grid(lat, lon, &easting, &northing) )
907 if( convert_os_grid_to_bng(easting, northing, bng) )
917 void format_lat_lon(gdouble d_lat, gdouble d_lon, gchar* lat, gchar* lon)
926 if(convert_lat_long_to_os_grid(d_lat, d_lon, &east, &north))
928 sprintf(lat, "%06d", east);
929 sprintf(lon, "%06d", north);
933 // Failed (possibly out of range), so use defaults
934 lat_format(d_lat, lat);
935 lon_format(d_lon, lon);
940 if(convert_lat_lon_to_bng(d_lat, d_lon, lat))
946 // Failed (possibly out of range), so use defaults
947 lat_format(d_lat, lat);
948 lat_format(d_lon, lon);
953 convert_lat_lon_to_iaru_loc(d_lat, d_lon, lat);
958 lat_format(d_lat, lat);
959 lon_format(d_lon, lon);
972 { "-12.345d", -12.345 },
973 { "12.345 E", -12.345 },
974 { "12d34m", 12.5666667 },
975 { "N 12 34", 12.5666667 },
976 { "S 12d34.56m", -12.576 },
977 { "W 12 34.56", 12.576 },
978 { "E 12d34m56s", -12.582222 },
979 { "12 34 56 S", -12.582222 },
980 { "12 34 56", 12.582222 },
981 { "12d34m56.7s E", -12.582417 },
982 { "12 34 56.7 W", 12.582417 },
983 { "12° 34 56.7 W", 12.582417 },
996 for (t = t_cases; t->fmt; t++) {
1000 v = strdmstod(t->fmt, &endp);
1001 if (endp == t->fmt || *endp != 0) {
1002 fprintf(stderr, "FAIL syntax %s\n", t->fmt);
1004 } else if (fabs(v - t->value) > 0.000001) {
1005 fprintf(stderr, "FAIL value %s -> %.10g (%.10g)\n",
1006 t->fmt, v, t->value);
1014 fprintf(stderr, "ALL TESTS OK\n");
1016 fprintf(stderr, "FAIL %d, OK %d\n", fail, ok);