From 0265ee0b268929c627395164db6c1034e9afa9e9 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Thu, 12 Jun 2008 15:40:59 +0300 Subject: [PATCH] Path: cleanups and fixes --- libs/libgtkmap/path.c | 62 ++++++++++++++++++++++++------------------- libs/libgtkmap/path.h | 10 ++++--- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/libs/libgtkmap/path.c b/libs/libgtkmap/path.c index 59d72b2..1ea01a7 100644 --- a/libs/libgtkmap/path.c +++ b/libs/libgtkmap/path.c @@ -19,6 +19,7 @@ */ #include +#include #include #include #include @@ -27,8 +28,6 @@ #include "path.h" #include "position.h" -#include "utils.h" -#include "settings.h" #include "latlon.h" struct sql_select_stmt { @@ -67,6 +66,8 @@ static struct sql_select_stmt sql; #define PATH_SQL_INSERT_TRACK_POINT "insert into trackpoints (tid,dt,lat,lon,alt,hdop,vdop,pdop,sat,fix) values (?,?,?,?,?,?,?,?,?,?)" #define PATH_SQL_SELECT_TRACK_POINTS "select tid,dt,lat,lon,alt,hdop,vdop,pdop,sat,fix from trackpoints where tid=? order by dt" +#define ARRAY_CHUNK_SIZE (1024) + #define MACRO_PATH_INIT(path) { \ (path).head = (path).tail = g_new0(Point, ARRAY_CHUNK_SIZE); \ *((path).tail) = _point_null; \ @@ -142,6 +143,14 @@ G_DEFINE_TYPE(Path, path, G_TYPE_OBJECT); #define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PATH_TYPE, PathPrivate)) +static void path_dispose(GObject *object); +static void path_finalize(GObject *object); +static void path_class_init(PathClass *klass); +static void path_init(Path *path); +static gboolean path_resize(Path *path, guint size); + +static void path_check_waypoint_announce(Path *path); + static void path_dispose(GObject *object) { @@ -345,6 +354,7 @@ if (abs((gint)unitx-path->tail->unitx) > path->sensitivity || abs((gint)unity-pa path->tail->altitude=altitude; if (speed>path->maxspeed) path->maxspeed=speed; + path->speed=speed; path->tspeed+=speed; path->avgspeed=(path->points>0) ? path->tspeed/path->points : 0.0; path->points++; @@ -572,15 +582,17 @@ path_update_nears(path, &p, FALSE); * */ WayPoint * -path_find_nearest_waypoint(Path *path, guint unitx, guint unity) +path_find_nearest_waypoint(Path *path, gdouble lat, gdouble lon) { WayPoint *wcurr; WayPoint *wnear; guint64 nearest_squared; -Point pos = { unitx, unity, 0, NAN }; +Point pos = { 0, 0, 0, NAN }; g_return_val_if_fail(path, NULL); +latlon2unit(lat, lon, pos.unitx, pos.unity); + wcurr=wnear=path->whead; if (wcurr && wcurr->point && wcurr!=path->wtail) { nearest_squared=DISTANCE_SQUARED(pos, *(wcurr->point)); @@ -595,7 +607,7 @@ if (wcurr && wcurr->point && wcurr!=path->wtail) { } if (wnear && wnear->point) { - if (abs(unitx - wnear->point->unitx) < path->sensitivity && abs(unity - wnear->point->unity) < path->sensitivity) + if (abs(pos.unitx - wnear->point->unitx) < path->sensitivity && abs(pos.unity - wnear->point->unity) < path->sensitivity) return wnear; } @@ -605,24 +617,24 @@ return NULL; /** * path_check_waypoint_announce: * @path - * @gps + * @lat + * @lon * * Check if we should announce upcoming waypoint. * */ static void -path_check_waypoint_announce(Path *path, GpsData *gps) +path_check_waypoint_announce(Path *path) { guint a_thres_near, a_thres_at; g_return_if_fail(path); -g_return_if_fail(gps); if (!path->next_way) return; -a_thres_near=(20+(guint)gps->speed)*path->announce_notice_ratio*3; -a_thres_at=(20+(guint)gps->speed); +a_thres_near=(20+(guint)path->speed)*path->announce_notice_ratio*3; +a_thres_at=(20+(guint)path->speed); if (path->next_way_dist_squared<(a_thres_near * a_thres_near) && path->next_way!=path->announced_waypoint) { g_signal_emit(G_OBJECT(path), signals[NEAR_WAYPOINT], 0, NULL); @@ -754,7 +766,7 @@ return TRUE; /******************************************************************************/ -gboolean +static gboolean path_set_destination_from_last(Path *path, Position *pos) { Point *p; @@ -857,12 +869,8 @@ return g_quark_from_static_string ("path-gpx-write-error-quark"); #define WRITE_STRING(string) { \ if(GNOME_VFS_OK != (vfs_result = gnome_vfs_write(handle, (string), strlen((string)), &size))) { \ - g_set_error(error, \ - PATH_GPX_WRITE_ERROR, G_FILE_ERROR_FAILED, \ - "%s:\n%s\n%s", \ - _("Error while writing to file"), \ - _("File is incomplete."), \ - gnome_vfs_result_to_string(vfs_result)); \ + g_set_error(error, PATH_GPX_WRITE_ERROR, G_FILE_ERROR_FAILED, \ + "%s", gnome_vfs_result_to_string(vfs_result)); \ return FALSE; \ } \ } @@ -883,10 +891,10 @@ g_return_val_if_fail((error == NULL || *error == NULL), FALSE); gpx_time = time(NULL); /* Find first non-zero point. */ -for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) { - if (curr->unity) +for (curr=path->head, wcurr=path->whead; curr++!=path->tail;) { + if (curr->unity && curr->unitx) break; - else if (wcurr && curr == wcurr->point) + else if (wcurr && curr==wcurr->point) wcurr++; } @@ -984,10 +992,10 @@ return TRUE; * Handle a start tag in the parsing of a GPX file. */ #define MACRO_SET_UNKNOWN(utag) { \ - data->prev_state = data->state; \ - data->state = UNKNOWN; \ - data->unknown_depth = 1; \ - g_debug("GPX: unknown tag [%s]", (gchar *)utag); } + data->prev_state = data->state; \ + data->state = UNKNOWN; \ + data->unknown_depth = 1; \ + g_warning("GPX: unknown tag [%s]", (gchar *)utag); } static void gpx_start_element(SaxData *data, const xmlChar *name, const xmlChar **attrs) @@ -1394,7 +1402,7 @@ lon1=lon2; * Generate a GtkListStore with information about path waypoints (location, distance) */ GtkListStore * -path_get_waypoints_store(Path *path) +path_get_waypoints_store(Path *path, DegFormat degformat) { WayPoint *wcurr; GtkTreeIter iter; @@ -1429,8 +1437,8 @@ while (wcurr!=path->wtail) { } unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat2, lon2); - lat_format(_degformat, lat2, tmp1); - lon_format(_degformat, lon2, tmp2); + lat_format(degformat, lat2, tmp1); + lon_format(degformat, lon2, tmp2); g_snprintf(buffer1, sizeof(buffer1), "%s,%s", tmp1, tmp2); sum += calculate_distance(lat1, lon1, lat2, lon2); diff --git a/libs/libgtkmap/path.h b/libs/libgtkmap/path.h index 703021c..7514dac 100644 --- a/libs/libgtkmap/path.h +++ b/libs/libgtkmap/path.h @@ -26,6 +26,9 @@ #include #include +#include "position.h" +#include "latlon.h" + G_BEGIN_DECLS #define PATH_TYPE (path_get_type ()) @@ -142,6 +145,7 @@ struct _Path { gdouble length; gdouble tspeed; gdouble avgspeed; + gfloat speed; gfloat maxspeed; /* GPX metadata fields */ @@ -176,14 +180,12 @@ gboolean path_gpx_parse(Path *to_replace, gchar *buffer, gint size, gpx_path_pol Point *path_find_last_point(Path *path); -gboolean path_resize(Path *path, guint size); -gboolean path_wresize(Path *path, guint wsize); - gdouble path_get_distance_to(Path *path, Point *point, gdouble lat, gdouble lon); void path_find_nearest_point(Path *path, gdouble lat, gdouble lon); gboolean path_add_latlon(Path *path, gdouble lat, gdouble lon, time_t ptime, gfloat speed, gfloat altitude); +gboolean path_add_waypoint(Path *path, gdouble lat, gdouble lon, gchar *desc); gboolean path_has_points(Path *path); gboolean path_has_waypoints(Path *path); @@ -195,7 +197,7 @@ void path_insert_mark_audio(Path *path, gchar *audio); gboolean path_get_waypoint_latlon(WayPoint *way, gdouble *lat, gdouble *lon); -GtkListStore *path_get_waypoints_store(Path *path); +GtkListStore *path_get_waypoints_store(Path *path, DegFormat degformat); G_END_DECLS -- 2.39.5