Skip to content

Commit

Permalink
Use NAN instead of 0.0 for invalid values.
Browse files Browse the repository at this point in the history
Altitudes and DOP values can validly be 0, so use NANs throughout
 (as per other double values such as speed and course)
  • Loading branch information
rnorris committed Mar 17, 2019
1 parent 4f59f57 commit 808ea53
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 74 deletions.
3 changes: 2 additions & 1 deletion src/geonamessearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -76,7 +77,7 @@ static found_geoname *new_found_geoname()
ret->desc = NULL;
ret->ll.lat = 0.0;
ret->ll.lon = 0.0;
ret->elevation = VIK_DEFAULT_ALTITUDE;
ret->elevation = NAN;
return ret;
}

Expand Down
3 changes: 0 additions & 3 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ a cutting at 60s/km or equivalent (in other units) has been set*/
#define VIK_MPS_TO_KNOTS(X) ((X)*VIK_KNOTS_IN_MPS)
#define VIK_KNOTS_TO_MPS(X) ((X)/VIK_KNOTS_IN_MPS)

#define VIK_DEFAULT_ALTITUDE 0.0
#define VIK_DEFAULT_DOP 0.0

#define VIK_GTK_WINDOW_FROM_WIDGET(x) GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(x)))
#define VIK_GTK_WINDOW_FROM_LAYER(x) VIK_GTK_WINDOW_FROM_WIDGET(VIK_LAYER(x)->vt)

Expand Down
26 changes: 13 additions & 13 deletions src/gpspoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ static VikWaypointImageDirectionRef line_image_direction_ref = WP_IMAGE_DIRECTIO
static gboolean line_newsegment = FALSE;
static gboolean line_has_timestamp = FALSE;
static time_t line_timestamp = 0;
static gdouble line_altitude = VIK_DEFAULT_ALTITUDE;
static gdouble line_altitude = NAN;
static gboolean line_visible = TRUE;

static gboolean line_extended = FALSE;
static gdouble line_speed = NAN;
static gdouble line_course = NAN;
static gint line_sat = 0;
static gint line_fix = 0;
static gdouble line_hdop = VIK_DEFAULT_DOP;
static gdouble line_vdop = VIK_DEFAULT_DOP;
static gdouble line_pdop = VIK_DEFAULT_DOP;
static gdouble line_hdop = NAN;
static gdouble line_vdop = NAN;
static gdouble line_pdop = NAN;
/* other possible properties go here */


Expand Down Expand Up @@ -383,7 +383,7 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f, const gchar *dirpath )
line_newsegment = FALSE;
line_has_timestamp = FALSE;
line_timestamp = 0;
line_altitude = VIK_DEFAULT_ALTITUDE;
line_altitude = NAN;
line_visible = TRUE;
line_symbol = NULL;

Expand All @@ -392,9 +392,9 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f, const gchar *dirpath )
line_course = NAN;
line_sat = 0;
line_fix = 0;
line_hdop = VIK_DEFAULT_DOP;
line_vdop = VIK_DEFAULT_DOP;
line_pdop = VIK_DEFAULT_DOP;
line_hdop = NAN;
line_vdop = NAN;
line_pdop = NAN;
line_name_label = 0;
line_dist_label = 0;
}
Expand Down Expand Up @@ -629,7 +629,7 @@ static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp
fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, tmp_name );
g_free ( tmp_name );

if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) {
if ( !isnan(wp->altitude) ) {
gchar s_alt[COORDS_STR_BUFFER_SIZE];
a_coords_dtostr_buffer ( wp->altitude, s_alt );
fprintf ( f, " altitude=\"%s\"", s_alt );
Expand Down Expand Up @@ -719,7 +719,7 @@ static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type
g_free(name);
}

if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
if ( !isnan(tp->altitude) ) {
a_coords_dtostr_buffer ( tp->altitude, s_alt );
fprintf ( f, " altitude=\"%s\"", s_alt );
}
Expand All @@ -745,17 +745,17 @@ static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type
if (tp->fix_mode > 0)
fprintf ( f, " fix=\"%d\"", tp->fix_mode );

if ( tp->hdop != VIK_DEFAULT_DOP ) {
if ( !isnan(tp->hdop) ) {
gchar ss[COORDS_STR_BUFFER_SIZE];
a_coords_dtostr_buffer ( tp->hdop, ss );
fprintf ( f, " hdop=\"%s\"", ss );
}
if ( tp->vdop != VIK_DEFAULT_DOP ) {
if ( !isnan(tp->vdop) ) {
gchar ss[COORDS_STR_BUFFER_SIZE];
a_coords_dtostr_buffer ( tp->vdop, ss );
fprintf ( f, " vdop=\"%s\"", ss );
}
if ( tp->pdop != VIK_DEFAULT_DOP ) {
if ( !isnan(tp->pdop) ) {
gchar ss[COORDS_STR_BUFFER_SIZE];
a_coords_dtostr_buffer ( tp->pdop, ss );
fprintf ( f, " pdop=\"%s\"", ss );
Expand Down
10 changes: 5 additions & 5 deletions src/gpx.c
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );

if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
if ( !isnan(wp->altitude) )
{
gchar s_alt[COORDS_STR_BUFFER_SIZE];
a_coords_dtostr_buffer ( wp->altitude, s_alt );
Expand Down Expand Up @@ -1010,7 +1010,7 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context
a_coords_dtostr_buffer ( ll.lon, s_lon );
fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );

if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
if ( !isnan(tp->altitude) )
{
a_coords_dtostr_buffer ( tp->altitude, s_alt );
fprintf ( f, " <ele>%s</ele>\n", s_alt );
Expand Down Expand Up @@ -1068,17 +1068,17 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context
if (tp->nsats > 0)
fprintf ( f, " <sat>%d</sat>\n", tp->nsats );

if ( tp->hdop != VIK_DEFAULT_DOP ) {
if ( !isnan(tp->hdop) ) {
a_coords_dtostr_buffer ( tp->hdop, s_dop );
fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
}

if ( tp->vdop != VIK_DEFAULT_DOP ) {
if ( !isnan(tp->vdop) ) {
a_coords_dtostr_buffer ( tp->vdop, s_dop );
fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
}

if ( tp->pdop != VIK_DEFAULT_DOP ) {
if ( !isnan(tp->pdop) ) {
a_coords_dtostr_buffer ( tp->pdop, s_dop );
fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
}
Expand Down
9 changes: 4 additions & 5 deletions src/vikgpslayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1687,8 +1687,8 @@ static VikTrackpoint* create_realtime_trackpoint(VikGpsLayer *vgl, gboolean forc
gboolean replace = FALSE;
int heading = isnan(vgl->realtime_fix.fix.track) ? 0 : (int)floor(vgl->realtime_fix.fix.track);
int last_heading = isnan(vgl->last_fix.fix.track) ? 0 : (int)floor(vgl->last_fix.fix.track);
int alt = isnan(vgl->realtime_fix.fix.altitude) ? VIK_DEFAULT_ALTITUDE : floor(vgl->realtime_fix.fix.altitude);
int last_alt = isnan(vgl->last_fix.fix.altitude) ? VIK_DEFAULT_ALTITUDE : floor(vgl->last_fix.fix.altitude);
int alt = isnan(vgl->realtime_fix.fix.altitude) ? 0 : (int)floor(vgl->realtime_fix.fix.altitude);
int last_alt = isnan(vgl->last_fix.fix.altitude) ? 0 : (int)floor(vgl->last_fix.fix.altitude);
if (((last_tp = g_list_last(vgl->realtime_track->trackpoints)) != NULL) &&
(vgl->realtime_fix.fix.mode > MODE_2D) &&
(vgl->last_fix.fix.mode <= MODE_2D) &&
Expand All @@ -1702,7 +1702,7 @@ static VikTrackpoint* create_realtime_trackpoint(VikGpsLayer *vgl, gboolean forc
((forced ||
((heading < last_heading) && (heading < (last_heading - 3))) ||
((heading > last_heading) && (heading > (last_heading + 3))) ||
((alt != VIK_DEFAULT_ALTITUDE) && (alt != last_alt)))))) {
(alt && (alt != last_alt)))))) {
/* TODO: check for new segments */
VikTrackpoint *tp = vik_trackpoint_new();
tp->newsegment = FALSE;
Expand Down Expand Up @@ -1922,8 +1922,7 @@ static gboolean rt_gpsd_try_connect(gpointer *data)
vgl->vgpsd->vgl = vgl;

vgl->realtime_fix.dirty = vgl->last_fix.dirty = FALSE;
/* track alt/time graph uses VIK_DEFAULT_ALTITUDE (0.0) as invalid */
vgl->realtime_fix.fix.altitude = vgl->last_fix.fix.altitude = VIK_DEFAULT_ALTITUDE;
vgl->realtime_fix.fix.altitude = vgl->last_fix.fix.altitude = NAN;
vgl->realtime_fix.fix.speed = vgl->last_fix.fix.speed = NAN;

if (vgl->realtime_record) {
Expand Down
42 changes: 23 additions & 19 deletions src/viktrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ VikTrackpoint *vik_trackpoint_new()
VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint));
tp->speed = NAN;
tp->course = NAN;
tp->altitude = VIK_DEFAULT_ALTITUDE;
tp->hdop = VIK_DEFAULT_DOP;
tp->vdop = VIK_DEFAULT_DOP;
tp->pdop = VIK_DEFAULT_DOP;
tp->altitude = NAN;
tp->hdop = NAN;
tp->vdop = NAN;
tp->pdop = NAN;
return tp;
}

Expand Down Expand Up @@ -523,9 +523,9 @@ void vik_track_to_routepoints ( VikTrack *tr )
VIK_TRACKPOINT(iter->data)->timestamp = 0;
VIK_TRACKPOINT(iter->data)->speed = NAN;
VIK_TRACKPOINT(iter->data)->course = NAN;
VIK_TRACKPOINT(iter->data)->hdop = VIK_DEFAULT_DOP;
VIK_TRACKPOINT(iter->data)->vdop = VIK_DEFAULT_DOP;
VIK_TRACKPOINT(iter->data)->pdop = VIK_DEFAULT_DOP;
VIK_TRACKPOINT(iter->data)->hdop = NAN;
VIK_TRACKPOINT(iter->data)->vdop = NAN;
VIK_TRACKPOINT(iter->data)->pdop = NAN;
VIK_TRACKPOINT(iter->data)->nsats = 0;
VIK_TRACKPOINT(iter->data)->fix_mode = VIK_GPS_MODE_NOT_SEEN;

Expand Down Expand Up @@ -789,7 +789,7 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
// Since when is 9.9999e+24 a valid elevation!!
// This can happen when a track (with no elevations) is uploaded to a GPS device and then redownloaded (e.g. using a Garmin Legend EtrexHCx)
// Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception)
if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE &&
if ( !isnan(VIK_TRACKPOINT(iter->data)->altitude) &&
VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) {
okay = TRUE; break;
}
Expand Down Expand Up @@ -897,7 +897,12 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
return pts;
}


/**
* vik_track_get_total_elevation_gain:
*
* elevation gains and losses may be NAN if no elevations are available
*
*/
void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
{
gdouble diff;
Expand All @@ -907,8 +912,7 @@ void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble
while (iter) {
VikTrackpoint *tp1 = VIK_TRACKPOINT(iter->data);
VikTrackpoint *tp2 = VIK_TRACKPOINT(iter->prev->data);
if ( (tp1->altitude != VIK_DEFAULT_ALTITUDE) &&
(tp2->altitude != VIK_DEFAULT_ALTITUDE) ) {
if ( !isnan(tp1->altitude) && !isnan(tp2->altitude) ) {
diff = tp1->altitude - tp2->altitude;
if ( diff > 0 )
*up += diff;
Expand All @@ -918,7 +922,7 @@ void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble
iter = iter->next;
}
} else
*up = *down = VIK_DEFAULT_ALTITUDE;
*up = *down = NAN;
}

gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
Expand Down Expand Up @@ -1127,7 +1131,7 @@ gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chu
/* test if there's anything worth calculating */
gboolean okay = FALSE;
while ( iter ) {
if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
if ( !isnan(VIK_TRACKPOINT(iter->data)->altitude) ) {
okay = TRUE;
break;
}
Expand Down Expand Up @@ -1539,7 +1543,7 @@ gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdoubl
gdouble tmp_alt;
while (iter) {
VikTrackpoint *tp = VIK_TRACKPOINT(iter->data);
if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
if ( !isnan(tp->altitude) ) {
tmp_alt = tp->altitude;
if ( tmp_alt > *max_alt )
*max_alt = tmp_alt;
Expand Down Expand Up @@ -1784,7 +1788,7 @@ gulong vik_track_apply_dem_data ( VikTrack *tr, gboolean skip_existing )
tp_iter = tr->trackpoints;
while ( tp_iter ) {
// Don't apply if the point already has a value and the overwrite is off
if ( !(skip_existing && VIK_TRACKPOINT(tp_iter->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
if ( !(skip_existing && !isnan(VIK_TRACKPOINT(tp_iter->data)->altitude)) ) {
/* TODO: of the 4 possible choices we have for choosing an elevation
* (trackpoint in between samples), choose the one with the least elevation change
* as the last */
Expand Down Expand Up @@ -1855,7 +1859,7 @@ gulong vik_track_smooth_missing_elevation_data ( VikTrack *tr, gboolean flat )
gulong num = 0;

GList *tp_iter;
gdouble elev = VIK_DEFAULT_ALTITUDE;
gdouble elev = NAN;

VikTrackpoint *tp_missing = NULL;
GList *iter_first = NULL;
Expand All @@ -1865,10 +1869,10 @@ gulong vik_track_smooth_missing_elevation_data ( VikTrack *tr, gboolean flat )
while ( tp_iter ) {
VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);

if ( VIK_DEFAULT_ALTITUDE == tp->altitude ) {
if ( isnan(tp->altitude) ) {
if ( flat ) {
// Simply assign to last known value
if ( elev != VIK_DEFAULT_ALTITUDE ) {
if ( !isnan(elev) ) {
tp->altitude = elev;
num++;
}
Expand All @@ -1890,7 +1894,7 @@ gulong vik_track_smooth_missing_elevation_data ( VikTrack *tr, gboolean flat )
// Altitude available (maybe again!)
// If this marks the end of a section of altitude-less points
// then apply smoothing for that section of points
if ( points > 0 && elev != VIK_DEFAULT_ALTITUDE )
if ( points > 0 && !isnan(elev) )
if ( !flat ) {
smoothie ( iter_first, tp_iter, elev, tp->altitude, points );
num = num + points;
Expand Down
8 changes: 4 additions & 4 deletions src/viktrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct _VikTrackpoint {
gboolean newsegment;
gboolean has_timestamp;
time_t timestamp;
gdouble altitude; /* VIK_DEFAULT_ALTITUDE if data unavailable */
gdouble altitude; /* NAN if data unavailable */
gdouble speed; /* NAN if data unavailable */
gdouble course; /* NAN if data unavailable */
guint nsats; /* number of satellites used. 0 if data unavailable */
Expand All @@ -54,9 +54,9 @@ struct _VikTrackpoint {
#define VIK_GPS_MODE_DGPS 4
#define VIK_GPS_MODE_PPS 5 /* military signal used */
gint fix_mode; /* VIK_GPS_MODE_NOT_SEEN if data unavailable */
gdouble hdop; /* VIK_DEFAULT_DOP if data unavailable */
gdouble vdop; /* VIK_DEFAULT_DOP if data unavailable */
gdouble pdop; /* VIK_DEFAULT_DOP if data unavailable */
gdouble hdop; /* NAN if data unavailable */
gdouble vdop; /* NAN if data unavailable */
gdouble pdop; /* NAN if data unavailable */
};

typedef enum {
Expand Down
4 changes: 2 additions & 2 deletions src/viktrwlayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,7 @@ static void trw_layer_draw_track ( const gpointer id, VikTrack *track, struct Dr

vik_viewport_draw_line ( dp->vp, main_gc, oldx, oldy, x, y);

if ( dp->vtl->drawelevation && list->next && VIK_TRACKPOINT(list->next->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
if ( dp->vtl->drawelevation && list->next && !isnan(VIK_TRACKPOINT(list->next->data)->altitude) ) {
GdkPoint tmp[4];
#define FIXALTITUDE(what) ((VIK_TRACKPOINT((what))->altitude-min_alt)/alt_diff*DRAW_ELEVATION_FACTOR*dp->vtl->elevation_factor/dp->xmpp)

Expand Down Expand Up @@ -9659,7 +9659,7 @@ static VikLayerToolFuncStatus tool_edit_track_move ( VikTrwLayer *vtl, GdkEventM
gdouble elev_new;
elev_new = (gdouble) a_dems_get_elev_by_coord ( &coord, VIK_DEM_INTERPOL_BEST );
if ( elev_new != VIK_DEM_INVALID_ELEVATION ) {
if ( last_tpt->altitude != VIK_DEFAULT_ALTITUDE ) {
if ( !isnan(last_tpt->altitude) ) {
// Adjust elevation of last track point
if ( elev_new > last_tpt->altitude )
// Going up
Expand Down
12 changes: 6 additions & 6 deletions src/viktrwlayer_propwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static void minmax_array(const gdouble *array, gdouble *min, gdouble *max, gbool
*min = 20000;
guint i;
for ( i=0; i < PROFILE_WIDTH; i++ ) {
if ( NO_ALT_TEST || (array[i] != VIK_DEFAULT_ALTITUDE) ) {
if ( NO_ALT_TEST || (!isnan(array[i])) ) {
if ( array[i] > *max )
*max = array[i];
if ( array[i] < *min )
Expand Down Expand Up @@ -1614,7 +1614,7 @@ static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widget
/* draw elevations */
guint height = MARGIN_Y+widgets->profile_height;
for ( i = 0; i < widgets->profile_width; i++ )
if ( widgets->altitudes[i] == VIK_DEFAULT_ALTITUDE )
if ( isnan(widgets->altitudes[i]) )
gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
i + MARGIN_X, MARGIN_Y, i + MARGIN_X, height );
else
Expand Down Expand Up @@ -2775,14 +2775,14 @@ GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, PropWidgets *widget
widgets->altitudes = vik_track_make_elevation_map ( widgets->tr, widgets->profile_width );

if ( widgets->altitudes == NULL ) {
*min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
*min_alt = *max_alt = NAN;
return NULL;
}

// Don't use minmax_array(widgets->altitudes), as that is a simplified representative of the points
// thus can miss the highest & lowest values by a few metres
if ( !vik_track_get_minmax_alt (widgets->tr, min_alt, max_alt) )
*min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
*min_alt = *max_alt = NAN;

pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y, -1 );
image = gtk_image_new_from_pixmap ( pix, NULL );
Expand Down Expand Up @@ -3500,7 +3500,7 @@ void vik_trw_layer_propwin_run ( GtkWindow *parent,
widgets->w_avg_dist = content[cnt++] = ui_label_new_selectable ( tmp_buf );

vik_units_height_t height_units = a_vik_get_units_height ();
if ( (min_alt == VIK_DEFAULT_ALTITUDE) && (max_alt == VIK_DEFAULT_ALTITUDE) )
if ( isnan(min_alt) && isnan(max_alt) )
g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
else {
switch (height_units) {
Expand All @@ -3518,7 +3518,7 @@ void vik_trw_layer_propwin_run ( GtkWindow *parent,
widgets->w_elev_range = content[cnt++] = ui_label_new_selectable ( tmp_buf );

vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
if ( (min_alt == VIK_DEFAULT_ALTITUDE) && (max_alt == VIK_DEFAULT_ALTITUDE) )
if ( isnan(min_alt) && isnan(max_alt) )
g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
else {
switch (height_units) {
Expand Down
Loading

0 comments on commit 808ea53

Please sign in to comment.