Dive site: create new dive site at location from GPS data

Some dive computers save GPS data. Currently, this is stored
by libdivecomputer in an "extra field". When generating a
new dive site for a dive try to use this data to place the
dive site.

To do so, create a "dive_get_gps_location()" function. This
function can be extended later to use e.g. event. When creating
a dive site, use the result of this function over a potential
pre-existing dive site.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-04-24 23:59:59 +02:00 committed by Dirk Hohndel
parent c529cfd361
commit bab7033ccb
3 changed files with 56 additions and 0 deletions

View file

@ -4500,6 +4500,53 @@ int dive_has_gps_location(const struct dive *dive)
return dive_site_has_gps_location(dive->dive_site);
}
/* Extract GPS location of a dive computer stored in the GPS1
* or GPS2 extra data fields */
static location_t dc_get_gps_location(const struct divecomputer *dc)
{
location_t res = { };
for (struct extra_data *data = dc->extra_data; data; data = data->next) {
if (!strcmp(data->key, "GPS1")) {
parse_location(data->value, &res);
/* If we found a valid GPS1 field exit early since
* it has priority over GPS2 */
if (has_location(&res))
break;
} else if (!strcmp(data->key, "GPS2")) {
/* For GPS2 fields continue searching, as we might
* still find a GPS1 field */
parse_location(data->value, &res);
}
}
return res;
}
/* Get GPS location for a dive. Highest priority is given to the GPS1
* extra data written by libdivecomputer, as this comes from a real GPS
* device. If that doesn't exits, use the currently set dive site.
* This function is potentially slow, therefore only call sparingly
* and remember the result.
*/
location_t dive_get_gps_location(const struct dive *d)
{
location_t res = { };
for (const struct divecomputer *dc = &d->dc; dc; dc = dc->next) {
res = dc_get_gps_location(dc);
if (has_location(&res))
return res;
}
/* No libdivecomputer generated GPS data found.
* Let's use the location of the current dive site.
*/
if (d->dive_site)
res = d->dive_site->location;
return res;
}
/* When evaluated at the time of a gasswitch, this returns the new gas */
struct gasmix get_gasmix(const struct dive *dive, const struct divecomputer *dc, int time, const struct event **evp, struct gasmix gasmix)
{

View file

@ -456,6 +456,7 @@ extern struct dive *get_dive_by_uniq_id(int id);
extern int get_idx_by_uniq_id(int id);
extern bool dive_site_has_gps_location(const struct dive_site *ds);
extern int dive_has_gps_location(const struct dive *dive);
extern location_t dive_get_gps_location(const struct dive *d);
extern int report_error(const char *fmt, ...);
extern void set_error_cb(void(*cb)(char *)); // Callback takes ownership of passed string

View file

@ -341,6 +341,14 @@ static struct dive_site *createDiveSite(const QString &name)
copy_dive_site(old, ds);
free(ds->name); // Free name, as we will overwrite it with our own version
}
// If the current dive has a location, use that as location for the new dive site
if (current_dive) {
location_t loc = dive_get_gps_location(current_dive);
if (has_location(&loc))
ds->location = loc;
}
ds->name = copy_qstring(name);
return ds;
}