X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/561e6ad0aaa8b7daa78e2e158421ce5c7ffa3044..a0d39f7e69eb9d6e1b47a4f47709bb62ac2f5b04:/src/gpx.c diff --git a/src/gpx.c b/src/gpx.c index 4c6d36b2..979c6560 100644 --- a/src/gpx.c +++ b/src/gpx.c @@ -29,6 +29,8 @@ #include "viking.h" #include #include +#include +#include #define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ" @@ -41,6 +43,7 @@ typedef enum { tt_wpt_desc, tt_wpt_name, tt_wpt_ele, + tt_wpt_sym, tt_wpt_link, /* New in GPX 1.1 */ tt_trk, @@ -51,6 +54,11 @@ typedef enum { tt_trk_trkseg_trkpt, tt_trk_trkseg_trkpt_ele, tt_trk_trkseg_trkpt_time, + /* extended */ + tt_trk_trkseg_trkpt_course, + tt_trk_trkseg_trkpt_speed, + tt_trk_trkseg_trkpt_fix, + tt_trk_trkseg_trkpt_sat, tt_waypoint, tt_waypoint_coord, @@ -79,6 +87,8 @@ tag_mapping tag_path_map[] = { { tt_wpt_ele, "/gpx/wpt/ele" }, { tt_wpt_name, "/gpx/wpt/name" }, { tt_wpt_desc, "/gpx/wpt/desc" }, + { tt_wpt_sym, "/gpx/wpt/sym" }, + { tt_wpt_sym, "/loc/waypoint/type" }, { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */ { tt_trk, "/gpx/trk" }, @@ -90,6 +100,11 @@ tag_mapping tag_path_map[] = { { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" }, { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" }, { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" }, + /* extended */ + { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" }, + { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" }, + { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" }, + { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" }, {0} }; @@ -140,8 +155,8 @@ static const char *get_attr ( const char **attr, const char *key ) static gboolean set_c_ll ( const char **attr ) { if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) { - c_ll.lat = strtod(c_slat, NULL); - c_ll.lon = strtod(c_slon, NULL); + c_ll.lat = g_strtod(c_slat, NULL); + c_ll.lon = g_strtod(c_slon, NULL); return TRUE; } return FALSE; @@ -237,7 +252,8 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) case tt_wpt: if ( ! c_wp_name ) c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++); - g_hash_table_insert ( vik_trw_layer_get_waypoints ( vtl ), c_wp_name, c_wp ); + vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp ); + g_free ( c_wp_name ); c_wp = NULL; c_wp_name = NULL; break; @@ -245,7 +261,8 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) case tt_trk: if ( ! c_tr_name ) c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_waypoints++); - g_hash_table_insert ( vik_trw_layer_get_tracks ( vtl ), c_tr_name, c_tr ); + vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr ); + g_free ( c_tr_name ); c_tr = NULL; c_tr_name = NULL; break; @@ -265,12 +282,12 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) break; case tt_wpt_ele: - c_wp->altitude = strtod ( c_cdata->str, NULL ); + c_wp->altitude = g_strtod ( c_cdata->str, NULL ); g_string_erase ( c_cdata, 0, -1 ); break; case tt_trk_trkseg_trkpt_ele: - c_tp->altitude = strtod ( c_cdata->str, NULL ); + c_tp->altitude = g_strtod ( c_cdata->str, NULL ); g_string_erase ( c_cdata, 0, -1 ); break; @@ -285,12 +302,21 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) g_string_erase ( c_cdata, 0, -1 ); break; + case tt_wpt_sym: { + gchar *tmp_lower = g_utf8_strdown(c_cdata->str, -1); /* for things like Geocache */ + vik_waypoint_set_symbol ( c_wp, tmp_lower ); + g_free ( tmp_lower ); + g_string_erase ( c_cdata, 0, -1 ); + break; + } + case tt_trk_desc: vik_track_set_comment ( c_tr, c_cdata->str ); g_string_erase ( c_cdata, 0, -1 ); break; case tt_trk_trkseg_trkpt_time: + if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */ c_tp->timestamp = mktime(&tm); c_tp->has_timestamp = TRUE; @@ -298,6 +324,35 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) g_string_erase ( c_cdata, 0, -1 ); break; + case tt_trk_trkseg_trkpt_course: + c_tp->extended = TRUE; + c_tp->course = g_strtod ( c_cdata->str, NULL ); + g_string_erase ( c_cdata, 0, -1 ); + break; + + case tt_trk_trkseg_trkpt_speed: + c_tp->extended = TRUE; + c_tp->speed = g_strtod ( c_cdata->str, NULL ); + g_string_erase ( c_cdata, 0, -1 ); + break; + + case tt_trk_trkseg_trkpt_fix: + c_tp->extended = TRUE; + if (!strcmp("2d", c_cdata->str)) + c_tp->fix_mode = VIK_GPS_MODE_2D; + else if (!strcmp("3d", c_cdata->str)) + c_tp->fix_mode = VIK_GPS_MODE_3D; + else /* TODO: more fix modes here */ + c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN; + g_string_erase ( c_cdata, 0, -1 ); + break; + + case tt_trk_trkseg_trkpt_sat: + c_tp->extended = TRUE; + c_tp->nsats = atoi ( c_cdata->str ); + g_string_erase ( c_cdata, 0, -1 ); + break; + default: break; } @@ -312,9 +367,14 @@ static void gpx_cdata(void *dta, const XML_Char *s, int len) case tt_wpt_ele: case tt_trk_trkseg_trkpt_ele: case tt_wpt_desc: + case tt_wpt_sym: case tt_wpt_link: case tt_trk_desc: case tt_trk_trkseg_trkpt_time: + case tt_trk_trkseg_trkpt_course: + case tt_trk_trkseg_trkpt_speed: + case tt_trk_trkseg_trkpt_fix: + case tt_trk_trkseg_trkpt_sat: case tt_waypoint_name: /* .loc name is really description. */ g_string_append_len ( c_cdata, s, len ); break; @@ -522,20 +582,30 @@ entitize(const char * str) /**** end GPSBabel code ****/ /* export GPX */ + static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) { static struct LatLon ll; + gchar *s_lat,*s_lon; gchar *tmp; vik_coord_to_latlon ( &(wp->coord), &ll ); - fprintf ( f, "\n", - ll.lat, ll.lon, wp->visible ? "" : " hidden=\"hidden\"" ); + s_lat = a_coords_dtostr( ll.lat ); + s_lon = a_coords_dtostr( ll.lon ); + fprintf ( f, "\n", + s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" ); + g_free ( s_lat ); + g_free ( s_lon ); tmp = entitize ( name ); fprintf ( f, " %s\n", tmp ); g_free ( tmp); if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) - fprintf ( f, " %f\n", wp->altitude ); + { + tmp = a_coords_dtostr ( wp->altitude ); + fprintf ( f, " %s\n", tmp ); + g_free ( tmp ); + } if ( wp->comment ) { tmp = entitize(wp->comment); @@ -548,26 +618,61 @@ static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) fprintf ( f, " %s\n", tmp ); g_free ( tmp ); } + if ( wp->symbol ) + { + tmp = entitize(wp->symbol); + fprintf ( f, " %s\n", tmp); + g_free ( tmp ); + } + fprintf ( f, "\n" ); } static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f ) { static struct LatLon ll; + gchar *s_lat,*s_lon, *s_alt; static gchar time_buf[30]; vik_coord_to_latlon ( &(tp->coord), &ll ); if ( tp->newsegment ) fprintf ( f, " \n \n" ); - fprintf ( f, " \n", ll.lat, ll.lon ); + s_lat = a_coords_dtostr( ll.lat ); + s_lon = a_coords_dtostr( ll.lon ); + fprintf ( f, " \n", s_lat, s_lon ); + g_free ( s_lat ); + g_free ( s_lon ); if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) - fprintf ( f, " %f\n", tp->altitude ); + { + s_alt = a_coords_dtostr ( tp->altitude ); + fprintf ( f, " %s\n", s_alt ); + g_free ( s_alt ); + } if ( tp->has_timestamp ) { time_buf [ strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&(tp->timestamp)) ) ] = '\0'; fprintf ( f, " \n", time_buf ); } + if (tp->extended && (tp->fix_mode >= VIK_GPS_MODE_2D)) { + if (!isnan(tp->course)) { + gchar *s_course = a_coords_dtostr(tp->course); + fprintf ( f, " %s\n", s_course ); + g_free(s_course); + } + if (!isnan(tp->speed)) { + gchar *s_speed = a_coords_dtostr(tp->speed); + fprintf ( f, " %s\n", s_speed ); + g_free(s_speed); + } + if (tp->fix_mode == VIK_GPS_MODE_2D) + fprintf ( f, " 2d\n"); + if (tp->fix_mode == VIK_GPS_MODE_3D) + fprintf ( f, " 3d\n"); + if (tp->nsats > 0) + fprintf ( f, " %d\n", tp->nsats ); + } + fprintf ( f, " \n" ); } @@ -575,7 +680,7 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f ) static void gpx_write_track ( const gchar *name, VikTrack *t, FILE *f ) { gchar *tmp; - gboolean first_tp_is_newsegment; /* must temporarily make it not so, but we want to restore state. not that it matters. */ + gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */ tmp = entitize ( name ); fprintf ( f, "\n %s\n", t->visible ? "" : " hidden=\"hidden\"", tmp ); @@ -593,24 +698,81 @@ static void gpx_write_track ( const gchar *name, VikTrack *t, FILE *f ) if ( t->trackpoints && t->trackpoints->data ) { first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment; VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write already */ - } - g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, f ); - if ( t->trackpoints && t->trackpoints->data ) + g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, f ); VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */ + } fprintf ( f, "\n\n" ); } -void a_gpx_write_file( VikTrwLayer *vtl, FILE *f ) +static void gpx_write_header( FILE *f ) { fprintf(f, "\n" "\n"); - g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_write_waypoint, f ); - g_hash_table_foreach ( vik_trw_layer_get_tracks ( vtl ), (GHFunc) gpx_write_track, f ); +} + +static void gpx_write_footer( FILE *f ) +{ fprintf(f, "\n"); +} + +typedef struct { + VikWaypoint *wp; + const gchar *name; +} gpx_waypoint_and_name; + +typedef struct { + gpx_waypoint_and_name *wps; + guint i; + guint n_wps; +} gpx_gather_waypoints_passalong_t; + +static void gpx_collect_waypoint ( const gchar *name, VikWaypoint *wp, gpx_gather_waypoints_passalong_t *passalong ) +{ + if ( passalong->i < passalong->n_wps ) { + passalong->wps[passalong->i].name = name; + passalong->wps[passalong->i].wp = wp; + passalong->i++; + } +} + +static int gpx_waypoint_and_name_compar(const void *x, const void *y) +{ + gpx_waypoint_and_name *a = (gpx_waypoint_and_name *)x; + gpx_waypoint_and_name *b = (gpx_waypoint_and_name *)y; + return strcmp(a->name,b->name); +} + +void a_gpx_write_file( VikTrwLayer *vtl, FILE *f ) +{ + int i; + + gpx_write_header ( f ); + + gpx_gather_waypoints_passalong_t passalong; + passalong.n_wps = g_hash_table_size ( vik_trw_layer_get_waypoints ( vtl ) ); + passalong.i = 0; + passalong.wps = g_new(gpx_waypoint_and_name,passalong.n_wps); + g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_collect_waypoint, &passalong ); + /* gather waypoints in a list, then sort */ + qsort(passalong.wps, passalong.n_wps, sizeof(gpx_waypoint_and_name), gpx_waypoint_and_name_compar); + for ( i = 0; i < passalong.n_wps; i++ ) + gpx_write_waypoint ( passalong.wps[i].name, passalong.wps[i].wp, f); + g_free ( passalong.wps ); + + g_hash_table_foreach ( vik_trw_layer_get_tracks ( vtl ), (GHFunc) gpx_write_track, f ); + + gpx_write_footer ( f ); +} + +void a_gpx_write_track_file ( const gchar *name, VikTrack *t, FILE *f ) +{ + gpx_write_header ( f ); + gpx_write_track ( name, t, f ); + gpx_write_footer ( f ); }