Skip to content

Added distance and elevation (min/max) computation #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<string name="trackdetail">Track Details</string>
<string name="trackdetail_startdate">Start time:</string>
<string name="trackdetail_enddate">End time:</string>
<string name="trackdetail_distance">Distance:</string>
<string name="trackdetail_distance_unit">Km</string>
<string name="trackdetail_elevation">Elevation(min/max):</string>
<string name="trackdetail_speed">Speed(min/max):</string>
<string name="trackdetail_startloc">Starts at:</string>
<string name="trackdetail_endloc">Ends at:</string>
<string name="trackdetail_exportdate">Exported:</string>
Expand Down
14 changes: 14 additions & 0 deletions src/me/guillaumin/android/osmtracker/activity/TrackDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Date;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -155,6 +156,19 @@ protected void onResume() {
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_enddate));
map.put(ITEM_VALUE, t.getEndDateAsString());
data.add(map);

DecimalFormat df = new DecimalFormat("####0.00");
// Distance
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_distance));
map.put(ITEM_VALUE, df.format(t.getDistance()/1000) + " " +getResources().getString(R.string.trackdetail_distance_unit));
data.add(map);

// Elevation
map = new HashMap<String, String>();
map.put(ITEM_KEY, getResources().getString(R.string.trackdetail_elevation));
map.put(ITEM_VALUE, df.format(t.getElevationMin()) + " / " + df.format(t.getElevationMax()));
data.add(map);

// Start point
map = new HashMap<String, String>();
Expand Down
62 changes: 62 additions & 0 deletions src/me/guillaumin/android/osmtracker/db/model/Track.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.guillaumin.android.osmtracker.R;
import me.guillaumin.android.osmtracker.db.TrackContentProvider;
import me.guillaumin.android.osmtracker.db.TrackContentProvider.Schema;
import me.guillaumin.android.osmtracker.util.DistanceUtil;
import android.content.ContentResolver;
import android.database.Cursor;

Expand Down Expand Up @@ -57,6 +58,8 @@ public static OSMVisibility fromPosition(int position) {

private Long startDate=null, endDate=null;
private Float startLat=null, startLong=null, endLat=null, endLong=null;
private Double distance=0.0;
private Float elevationMax=0.0f,elevationMin=0.0f;

private boolean extraInformationRead = false;

Expand Down Expand Up @@ -117,10 +120,54 @@ private void readExtraInformation(){
}
endCursor.close();

readExtraInformation_stats();

extraInformationRead = true;
}
}

/**
*
* @todo: allow to exclude zero speed values
*/
private void readExtraInformation_stats() {
Cursor cursor = cr.query(TrackContentProvider.trackPointsUri(trackId), null, null, null, null);
Float latitudeCurrent, longitudeCurrent, latitudePrev, longitudePrev;

Float elevationCurr, speedMax, speedMin, speedCurr;

if(cursor != null && cursor.moveToFirst()) {
// Initialize the min/max values
elevationMin = elevationMax = cursor.getFloat(cursor.getColumnIndex(Schema.COL_ELEVATION));
speedMin = speedMax = cursor.getFloat(cursor.getColumnIndex(Schema.COL_SPEED));
latitudePrev = cursor.getFloat(cursor.getColumnIndex(Schema.COL_LATITUDE));
longitudePrev = cursor.getFloat(cursor.getColumnIndex(Schema.COL_LONGITUDE));

// Iterate over all points
while (cursor.moveToNext()) {
latitudeCurrent = cursor.getFloat(cursor.getColumnIndex(Schema.COL_LATITUDE));
longitudeCurrent = cursor.getFloat(cursor.getColumnIndex(Schema.COL_LONGITUDE));
distance += DistanceUtil.getDistance(latitudePrev, longitudePrev, latitudeCurrent, longitudeCurrent);

latitudePrev = latitudeCurrent;
longitudePrev = longitudeCurrent;

// Compute the Elevation
elevationCurr = cursor.getFloat(cursor.getColumnIndex(Schema.COL_ELEVATION));
elevationMin = Math.min(elevationMin, elevationCurr);
elevationMax = Math.max(elevationMax, elevationCurr);

// Compute the Speed
speedCurr = cursor.getFloat(cursor.getColumnIndex(Schema.COL_SPEED));
speedMin = Math.min(speedMin, speedCurr);
speedMax = Math.max(speedMax, speedCurr);
}

cursor.close();
}
}


public void setName(String name) {
this.name = name;
}
Expand Down Expand Up @@ -177,6 +224,21 @@ public Integer getTpCount() {
return tpCount;
}

public Double getDistance() {
readExtraInformation();
return distance;
}

public Float getElevationMin() {
readExtraInformation();
return elevationMin;
}

public Float getElevationMax() {
readExtraInformation();
return elevationMax;
}

public String getName() {
if (name != null && name.length() > 0) {
return name;
Expand Down
26 changes: 26 additions & 0 deletions src/me/guillaumin/android/osmtracker/util/DistanceUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.guillaumin.android.osmtracker.util;

public class DistanceUtil {

private static final double DEG2RAD = Math.PI / 180.0;
// private static final double RAD2DEG = 180.0 / Math.PI;
private static final double RADIUS_EARTH_METERS = 6371.01;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value is expressed in Km, so the final computation do not need to convert from meters.
The correct "meters" values is 63781370.0


/**
* Computes the distance with Spherical Law of Cosines
* http://www.movable-type.co.uk/scripts/latlong.html
* @param lat1
* @param lon1
* @param lat2
* @param lon2
* @return The distance (in meters) between point1 and point2
*/
public static double getDistance(final double lat1, final double lon1, final double lat2, final double lon2) {
double theta = lon1 - lon2;
double a = Math.sin(DEG2RAD * lat1) * Math.sin(DEG2RAD * lat2);
double b = Math.cos(DEG2RAD * lat1) * Math.cos(DEG2RAD * lat2) * Math.cos(DEG2RAD * theta);

return Math.acos(a + b) * RADIUS_EARTH_METERS;
}

}