//TODO: C Code. static functions are not good if we plan to have a test for them.
static bool merge_locations_into_dives(void)
{
	int i, j, tracer=0, changed=0;
	struct dive *gpsfix, *nextgpsfix, *dive;

	sort_table(&gps_location_table);

	for_each_dive (i, dive) {
		if (!dive_has_gps_location(dive)) {
			for (j = tracer; (gpsfix = get_dive_from_table(j, &gps_location_table)) !=NULL; j++) {
				if (time_during_dive_with_offset(dive, gpsfix->when, SAME_GROUP)) {
					qDebug() << "processing gpsfix @" << get_dive_date_string(gpsfix->when) << "which is withing six hours of dive from" <<
						 get_dive_date_string(dive->when) << "until" << get_dive_date_string(dive->when + dive->duration.seconds);
					/*
					 * If position is fixed during dive. This is the good one.
					 * Asign and mark position, and end gps_location loop
					 */
					if (time_during_dive_with_offset(dive, gpsfix->when, 0)) {
						qDebug() << "gpsfix is during the dive, pick that one";
						copy_gps_location(gpsfix, dive);
						changed++;
						tracer = j;
						break;
					} else {
						/*
						 * If it is not, check if there are more position fixes in SAME_GROUP range
						 */
						if ((nextgpsfix = get_dive_from_table(j + 1, &gps_location_table)) &&
						    time_during_dive_with_offset(dive, nextgpsfix->when, SAME_GROUP)) {
							qDebug() << "look at the next gps fix @" << get_dive_date_string(nextgpsfix->when);
							/* first let's test if this one is during the dive */
							if (time_during_dive_with_offset(dive, nextgpsfix->when, 0)) {
								qDebug() << "which is during the dive, pick that one";
								copy_gps_location(nextgpsfix, dive);
								changed++;
								tracer = j + 1;
								break;
							}
							/* we know the gps fixes are sorted; if they are both before the dive, ignore the first,
							 * if theay are both after the dive, take the first,
							 * if the first is before and the second is after, take the closer one */
							if (nextgpsfix->when < dive->when) {
								qDebug() << "which is closer to the start of the dive, do continue with that";
								continue;
							} else if (gpsfix->when > dive->when + dive->duration.seconds) {
								qDebug() << "which is even later after the end of the dive, so pick the previous one";
								copy_gps_location(gpsfix, dive);
								changed++;
								tracer = j;
								break;
							} else {
								/* ok, gpsfix is before, nextgpsfix is after */
								if (dive->when - gpsfix->when <= nextgpsfix->when - (dive->when + dive->duration.seconds)) {
									qDebug() << "pick the one before as it's closer to the start";
									copy_gps_location(gpsfix, dive);
									changed++;
									tracer = j;
									break;
								} else {
									qDebug() << "pick the one after as it's closer to the start";
									copy_gps_location(nextgpsfix, dive);
									changed++;
									tracer = j + 1;
									break;
								}
							}
						/*
						 * If no more positions in range, the actual is the one. Asign, mark and end loop.
						 */
						} else {
							qDebug() << "which seems to be the best one for this dive, so pick it";
							copy_gps_location(gpsfix, dive);
							changed++;
							tracer = j;
							break;
						}
					}
				} else {
					/* If position is out of SAME_GROUP range and in the future, mark position for
					 * next dive iteration and end the gps_location loop
					 */
					if (gpsfix->when >= dive->when + dive->duration.seconds + SAME_GROUP) {
						tracer = j;
						break;
					}
				}
			}
		}
	}
	return changed > 0;
}
Exemple #2
0
bool GpsLocation::applyLocations()
{
	int i;
	bool changed = false;
	int last = 0;
	int cnt = m_trackers.count();
	if (cnt == 0)
		return false;

	// create a table with the GPS information
	QList<struct gpsTracker> gpsTable = m_trackers.values();

	// now walk the dive table and see if we can fill in missing gps data
	struct dive *d;
	for_each_dive(i, d) {
		if (dive_has_gps_location(d))
			continue;
		for (int j = last; j < cnt; j++) {
			if (time_during_dive_with_offset(d, gpsTable[j].when, SAME_GROUP)) {
				if (verbose)
					qDebug() << "processing gpsFix @" << get_dive_date_string(gpsTable[j].when) <<
						    "which is withing six hours of dive from" <<
						    get_dive_date_string(d->when) << "until" <<
						    get_dive_date_string(dive_endtime(d));
				/*
				 * If position is fixed during dive. This is the good one.
				 * Asign and mark position, and end gps_location loop
				 */
				if (time_during_dive_with_offset(d, gpsTable[j].when, 0)) {
					if (verbose)
						qDebug() << "gpsFix is during the dive, pick that one";
					SET_LOCATION(d, gpsTable[j], j);
					break;
				} else {
					/*
					 * If it is not, check if there are more position fixes in SAME_GROUP range
					 */
					if (j + 1 < cnt && time_during_dive_with_offset(d, gpsTable[j+1].when, SAME_GROUP)) {
						if (verbose)
							qDebug() << "look at the next gps fix @" << get_dive_date_string(gpsTable[j+1].when);
						/* we know the gps fixes are sorted; if they are both before the dive, ignore the first,
						 * if theay are both after the dive, take the first,
						 * if the first is before and the second is after, take the closer one */
						if (gpsTable[j+1].when < d->when) {
							if (verbose)
								qDebug() << "which is closer to the start of the dive, do continue with that";
							continue;
						} else if (gpsTable[j].when > dive_endtime(d)) {
							if (verbose)
								qDebug() << "which is even later after the end of the dive, so pick the previous one";
							SET_LOCATION(d, gpsTable[j], j);
							break;
						} else {
							/* ok, gpsFix is before, nextgpsFix is after */
							if (d->when - gpsTable[j].when <= gpsTable[j+1].when - dive_endtime(d)) {
								if (verbose)
									qDebug() << "pick the one before as it's closer to the start";
								SET_LOCATION(d, gpsTable[j], j);
								break;
							} else {
								if (verbose)
									qDebug() << "pick the one after as it's closer to the start";
								SET_LOCATION(d, gpsTable[j + 1], j + 1);
								break;
							}
						}
					/*
					 * If no more positions in range, the actual is the one. Asign, mark and end loop.
					 */
					} else {
						if (verbose)
							qDebug() << "which seems to be the best one for this dive, so pick it";
						SET_LOCATION(d, gpsTable[j], j);
						break;
					}
				}
			} else {
				/* If position is out of SAME_GROUP range and in the future, mark position for
				 * next dive iteration and end the gps_location loop
				 */
				if (gpsTable[j].when >= dive_endtime(d) + SAME_GROUP) {
					last = j;
					break;
				}
			}

		}
	}
	if (changed)
		mark_divelist_changed(true);
	return changed;
}