Skip to content

Commit

Permalink
cleanup datum lookup & constants (GPSBabel#962)
Browse files Browse the repository at this point in the history
* cleanup datum lookup

* use constexpr for datum contstants
  • Loading branch information
tsteven4 authored Dec 28, 2022
1 parent 8df666b commit 17859c9
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 37 deletions.
10 changes: 3 additions & 7 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ constexpr double KNOTS_TO_MPS(double a) {return a * kMPSPerKnot;}
#define CENTI_TO_MICRO(t) ((t) * 10000) /* Centiseconds to Microseconds */
#define MICRO_TO_CENTI(t) ((t) / 10000) /* Centiseconds to Microseconds */

constexpr int kDatumOSGB36 = 86; // GPS_Lookup_Datum_Index("OSGB36")
constexpr int kDautmWGS84 = 118; // GPS_Lookup_Datum_Index("WGS 84")

/* Pathname separator character */
#if __WIN32__
# define GB_PATHSEP '\\'
Expand Down Expand Up @@ -1154,9 +1157,6 @@ enum grid_type {
#define GRID_INDEX_MIN grid_lat_lon_ddd
#define GRID_INDEX_MAX grid_swiss

#define DATUM_OSGB36 86
#define DATUM_WGS84 118

/* bit manipulation functions (util.c) */

char gb_getbit(const void* buf, uint32_t nr);
Expand Down Expand Up @@ -1199,8 +1199,4 @@ int color_to_bbggrr(const char* cname);
#define unknown_alt -99999999.0
#define unknown_color -1

// TODO: this is a (probably temporary) shim for the C->QString conversion.
// It's here instead of gps to avoid C/C++ linkage issues.
int32_t GPS_Lookup_Datum_Index(const QString& n);

#endif // DEFS_H_INCLUDED_
2 changes: 1 addition & 1 deletion exif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ ExifFormat::exif_waypt_from_exif_app(ExifApp* app) const
if (idatum < 0) {
fatal(MYNAME ": Unknown GPSMapDatum \"%s\"!\n", datum);
}
if (idatum != DATUM_WGS84) {
if (idatum != kDautmWGS84) {
GPS_Math_WGS84_To_Known_Datum_M(wpt->latitude, wpt->longitude, 0.0,
&wpt->latitude, &wpt->longitude, &alt, idatum);
}
Expand Down
6 changes: 3 additions & 3 deletions garmin_txt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ convert_datum(const Waypoint* wpt, double* dest_lat, double* dest_lon)
{
double alt;

if (datum_index == DATUM_WGS84) {
if (datum_index == kDautmWGS84) {
*dest_lat = wpt->latitude;
*dest_lon = wpt->longitude;
} else GPS_Math_WGS84_To_Known_Datum_M(wpt->latitude, wpt->longitude, 0.0,
Expand Down Expand Up @@ -776,10 +776,10 @@ garmin_txt_wr_init(const QString& fname)

switch (grid_index) {
case grid_bng: /* force datum to "Ord Srvy Grt Britn" */
datum_index = DATUM_OSGB36;
datum_index = kDatumOSGB36;
break;
case grid_swiss: /* force datum to WGS84 */
datum_index = DATUM_WGS84;
datum_index = kDautmWGS84;
break;
default:
datum_index = gt_lookup_datum_index(datum_str, MYNAME);
Expand Down
1 change: 1 addition & 0 deletions jeeps/gpsmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
double lambda0, double E0, double N0);

int32 GPS_Lookup_Datum_Index(const char* n);
int32 GPS_Lookup_Datum_Index(const QString& n);
const char* GPS_Math_Get_Datum_Name(int datum_index);

#endif
5 changes: 5 additions & 0 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include <cassert> // for assert
#include <clocale> // for setlocale, LC_NUMERIC, LC_TIME
#include <csignal> // for signal, SIGINT, SIG_ERR
#include <cstdio> // for printf, fflush, fgetc, fprintf, stderr, stdin, stdout
Expand Down Expand Up @@ -50,6 +51,7 @@
#include "format.h" // for Format
#include "gbversion.h" // for VERSION_SHA
#include "inifile.h" // for inifile_done, inifile_init
#include "jeeps/gpsmath.h" // for GPS_Lookup_Datum_Index
#include "session.h" // for start_session, session_exit, session_init
#include "src/core/datetime.h" // for DateTime
#include "src/core/file.h" // for File
Expand Down Expand Up @@ -718,6 +720,9 @@ main(int argc, char* argv[])
global_opts.inifile = inifile_init(QString(), MYNAME);
}

assert(GPS_Lookup_Datum_Index("OSGB36") == kDatumOSGB36);
assert(GPS_Lookup_Datum_Index("WGS 84") == kDautmWGS84);

Vecs::Instance().init_vecs();
FilterVecs::Instance().init_filter_vecs();
session_init();
Expand Down
4 changes: 2 additions & 2 deletions nmea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ NmeaFormat::nmea_add_wpt(Waypoint* wpt, route_head* trk) const
// This also indicates to nmea_release_wpt that ownership has been
// transferred to either the global_waypoint_list or global_track_list.
wpt->extra_data = nullptr;
if (datum != DATUM_WGS84) {
if (datum != kDautmWGS84) {
double lat, lon, alt;
GPS_Math_Known_Datum_To_WGS84_M(
wpt->latitude, wpt->longitude, 0,
Expand Down Expand Up @@ -221,7 +221,7 @@ NmeaFormat::rd_init(const QString& fname)
{
curr_waypt = nullptr;
last_waypt = nullptr;
datum = DATUM_WGS84;
datum = kDautmWGS84;
had_checksum = false;

CHECK_BOOL(opt_gprmc);
Expand Down
2 changes: 1 addition & 1 deletion ozi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ ozi_set_time_str(const QString& str, Waypoint* waypointp)
static void
ozi_convert_datum(Waypoint* wpt)
{
if (datum != DATUM_WGS84) {
if (datum != kDautmWGS84) {
double lat, lon, alt;
GPS_Math_Known_Datum_To_WGS84_M(wpt->latitude, wpt->longitude, 0.0,
&lat, &lon, &alt, datum);
Expand Down
6 changes: 3 additions & 3 deletions parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ parse_coordinates(const char* str, int datum, const grid_type grid,
break;

case grid_bng:
datum = DATUM_WGS84; /* fix */
datum = kDautmWGS84; /* fix */
format = "%2s %lf %lf%n";
ct = sscanf(str, format,
map, &lx, &ly,
Expand Down Expand Up @@ -238,7 +238,7 @@ parse_coordinates(const char* str, int datum, const grid_type grid,
case grid_swiss: {
double east, north;

datum = DATUM_WGS84; /* fix */
datum = kDautmWGS84; /* fix */
format = "%lf %lf%n";
ct = sscanf(str, format,
&east, &north, &result);
Expand All @@ -265,7 +265,7 @@ parse_coordinates(const char* str, int datum, const grid_type grid,
lon = -lon;
}

if (datum != DATUM_WGS84) {
if (datum != kDautmWGS84) {
double alt;
GPS_Math_Known_Datum_To_WGS84_M(lat, lon, 0.0,
&lat, &lon, &alt, datum);
Expand Down
24 changes: 12 additions & 12 deletions unicsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -596,15 +596,15 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
&wpt->latitude, &wpt->longitude, MYNAME);
/* coordinates from parse_coordinates are in WGS84
don't convert a second time */
src_datum = DATUM_WGS84;
src_datum = kDautmWGS84;
break;

case fld_bng:
parse_coordinates(value, DATUM_OSGB36, grid_bng,
parse_coordinates(value, kDatumOSGB36, grid_bng,
&wpt->latitude, &wpt->longitude, MYNAME);
/* coordinates from parse_coordinates are in WGS84
don't convert a second time */
src_datum = DATUM_WGS84;
src_datum = kDautmWGS84;
break;

case fld_bng_zone:
Expand All @@ -621,11 +621,11 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
break;

case fld_swiss:
parse_coordinates(value, DATUM_WGS84, grid_swiss,
parse_coordinates(value, kDautmWGS84, grid_swiss,
&wpt->latitude, &wpt->longitude, MYNAME);
/* coordinates from parse_coordinates are in WGS84
don't convert a second time */
src_datum = DATUM_WGS84;
src_datum = kDautmWGS84;
break;

case fld_swiss_easting:
Expand Down Expand Up @@ -1051,15 +1051,15 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
fatal(MYNAME ": Unable to convert BNG coordinates (%s %.f %.f)!\n",
bng_zone, bng_easting, bng_northing);
}
src_datum = DATUM_WGS84; /* don't convert afterwards */
src_datum = kDautmWGS84; /* don't convert afterwards */
} else if ((swiss_easting != kUnicsvUnknown) && (swiss_northing != kUnicsvUnknown)) {
GPS_Math_Swiss_EN_To_WGS84(swiss_easting, swiss_northing,
&wpt->latitude, &wpt->longitude);
src_datum = DATUM_WGS84; /* don't convert afterwards */
src_datum = kDautmWGS84; /* don't convert afterwards */
}
}

if ((src_datum != DATUM_WGS84) &&
if ((src_datum != kDautmWGS84) &&
(wpt->latitude != kUnicsvUnknown) && (wpt->longitude != kUnicsvUnknown)) {
double alt;
GPS_Math_Known_Datum_To_WGS84_M(wpt->latitude, wpt->longitude, 0.0,
Expand Down Expand Up @@ -1311,7 +1311,7 @@ UnicsvFormat::unicsv_waypt_disp_cb(const Waypoint* wpt)
QString shortname = wpt->shortname;
garmin_fs_t* gmsd = garmin_fs_t::find(wpt);

if (unicsv_datum_idx == DATUM_WGS84) {
if (unicsv_datum_idx == kDautmWGS84) {
lat = wpt->latitude;
lon = wpt->longitude;
alt = wpt->altitude;
Expand Down Expand Up @@ -1712,7 +1712,7 @@ UnicsvFormat::wr_init(const QString& fname)

memset(&unicsv_outp_flags, 0, sizeof(unicsv_outp_flags));
unicsv_grid_idx = grid_unknown;
unicsv_datum_idx = DATUM_WGS84;
unicsv_datum_idx = kDautmWGS84;
unicsv_fieldsep = kUnicsvFieldSep;
unicsv_waypt_ct = 0;

Expand All @@ -1733,11 +1733,11 @@ UnicsvFormat::wr_init(const QString& fname)
/* force datum to "Ord Srvy Grt Britn" / OSGB36 */
/* ! ignore parameter "Datum" ! */
{
unicsv_datum_idx = DATUM_OSGB36;
unicsv_datum_idx = kDatumOSGB36;
} else if (unicsv_grid_idx == grid_swiss)
/* ! ignore parameter "Datum" ! */
{
unicsv_datum_idx = DATUM_WGS84; /* internal, becomes CH1903 */
unicsv_datum_idx = kDautmWGS84; /* internal, becomes CH1903 */
} else {
unicsv_datum_idx = gt_lookup_datum_index(opt_datum, MYNAME);
}
Expand Down
10 changes: 4 additions & 6 deletions xcsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ XcsvFormat::xcsv_parse_val(const QString& value, Waypoint* wpt, const XcsvStyle:
break;
/* SPECIAL COORDINATES/GRID */
case XcsvStyle::XT_MAP_EN_BNG:
parse_coordinates(s, DATUM_OSGB36, grid_bng,
parse_coordinates(s, kDatumOSGB36, grid_bng,
&wpt->latitude, &wpt->longitude, MYNAME);
break;
case XcsvStyle::XT_UTM_ZONE:
Expand Down Expand Up @@ -891,7 +891,7 @@ XcsvFormat::read()
wpt_tmp->longitude = -wpt_tmp->longitude;
}

if ((xcsv_file->gps_datum_idx > -1) && (xcsv_file->gps_datum_idx != gps_datum_wgs84)) {
if ((xcsv_file->gps_datum_idx > -1) && (xcsv_file->gps_datum_idx != kDautmWGS84)) {
double alt;
GPS_Math_Known_Datum_To_WGS84_M(wpt_tmp->latitude, wpt_tmp->longitude, 0.0,
&wpt_tmp->latitude, &wpt_tmp->longitude, &alt, xcsv_file->gps_datum_idx);
Expand All @@ -902,7 +902,7 @@ XcsvFormat::read()
&wpt_tmp->longitude,
parse_data.utm_easting, parse_data.utm_northing,
parse_data.utm_zone, parse_data.utm_zonec,
DATUM_WGS84);
kDautmWGS84);
}

if (parse_data.link_) {
Expand Down Expand Up @@ -1018,7 +1018,7 @@ XcsvFormat::xcsv_waypt_pr(const Waypoint* wpt)
description = shortname;
}

if ((xcsv_file->gps_datum_idx > -1) && (xcsv_file->gps_datum_idx != gps_datum_wgs84)) {
if ((xcsv_file->gps_datum_idx > -1) && (xcsv_file->gps_datum_idx != kDautmWGS84)) {
double alt;
GPS_Math_WGS84_To_Known_Datum_M(latitude, longitude, 0.0,
&latitude, &longitude, &alt, xcsv_file->gps_datum_idx);
Expand Down Expand Up @@ -1863,7 +1863,6 @@ XcsvFormat::rd_init(const QString& fname)
if (xcsv_file->gps_datum_idx < 0) {
fatal(MYNAME ": datum \"%s\" is not supported.", qPrintable(datum_name));
}
assert(gps_datum_wgs84 == GPS_Lookup_Datum_Index("WGS 84"));
}

void
Expand Down Expand Up @@ -1944,7 +1943,6 @@ XcsvFormat::wr_init(const QString& fname)
if (xcsv_file->gps_datum_idx < 0) {
fatal(MYNAME ": datum \"%s\" is not supported.", qPrintable(datum_name));
}
assert(gps_datum_wgs84 == GPS_Lookup_Datum_Index("WGS 84"));
}

void
Expand Down
2 changes: 0 additions & 2 deletions xcsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,6 @@ class XcsvFormat : public Format
return (a / 86400.0) + 25569.0;
}

static constexpr int gps_datum_wgs84 = 118; // GPS_Lookup_Datum_Index("WGS 84")

/* Member Functions */

static QDateTime yyyymmdd_to_time(const QString& s);
Expand Down

0 comments on commit 17859c9

Please sign in to comment.