Skip to content

Track segments #310

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 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions app/src/main/java/net/osmtracker/activity/DisplayTrackMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import net.osmtracker.R;
import net.osmtracker.db.TrackContentProvider;
import net.osmtracker.overlay.WayPointsOverlay;
import net.osmtracker.overlay.PathOverlays;

import org.osmdroid.api.IMapController;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.ITileSource;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.PathOverlay;
import org.osmdroid.views.overlay.mylocation.SimpleLocationOverlay;
import org.osmdroid.views.overlay.ScaleBarOverlay;

Expand Down Expand Up @@ -109,7 +109,7 @@ public class DisplayTrackMap extends Activity {
/**
* OSM view overlay that displays current path
*/
private PathOverlay pathOverlay;
private PathOverlays pathOverlay;

/**
* OSM view overlay that displays waypoints
Expand Down Expand Up @@ -379,9 +379,7 @@ private void createOverlays() {
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);

// set with to hopefully DPI independent 0.5mm
pathOverlay = new PathOverlay(Color.BLUE, (float)(metrics.densityDpi / 25.4 / 2),this);

osmView.getOverlays().add(pathOverlay);
pathOverlay = new PathOverlays(Color.BLUE, (float)(metrics.densityDpi / 25.4 / 2),this, osmView);

myLocationOverlay = new SimpleLocationOverlay(this);
osmView.getOverlays().add(myLocationOverlay);
Expand Down Expand Up @@ -426,7 +424,7 @@ private void pathChanged() {

// Projection: The columns to retrieve. Here, we want the latitude,
// longitude and primary key only
String[] projection = {TrackContentProvider.Schema.COL_LATITUDE, TrackContentProvider.Schema.COL_LONGITUDE, TrackContentProvider.Schema.COL_ID};
String[] projection = {TrackContentProvider.Schema.COL_LATITUDE, TrackContentProvider.Schema.COL_LONGITUDE, TrackContentProvider.Schema.COL_ID, TrackContentProvider.Schema.COL_NEW_SEGMENT};
// Selection: The where clause to use
String selection = null;
// SelectionArgs: The parameter replacements to use for the '?' in the selection
Expand Down Expand Up @@ -454,15 +452,21 @@ private void pathChanged() {
c.moveToFirst();
double lastLat = 0;
double lastLon = 0;
boolean newSegment = false;
int primaryKeyColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_ID);
int latitudeColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_LATITUDE);
int longitudeColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_LONGITUDE);

int newSegmentColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_NEW_SEGMENT);

// Add each new point to the track
while(!c.isAfterLast()) {
lastLat = c.getDouble(latitudeColumnIndex);
lastLon = c.getDouble(longitudeColumnIndex);
lastTrackPointIdProcessed = c.getInt(primaryKeyColumnIndex);
newSegment = c.getShort(newSegmentColumnIndex) > 0;
if(newSegment) {
pathOverlay.nextSegment();
}
pathOverlay.addPoint((int)(lastLat * 1e6), (int)(lastLon * 1e6));
if (doInitialBoundsCalc) {
if (lastLat < minLat) minLat = lastLat;
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/net/osmtracker/db/DataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public DataHelper(Context c) {
* @param pressure
* atmospheric pressure
*/
public void track(long trackId, Location location, float azimuth, int accuracy, float pressure) {
public void track(long trackId, Location location, float azimuth, int accuracy, float pressure, boolean newSeg) {
Log.v(TAG, "Tracking (trackId=" + trackId + ") location: " + location + " azimuth: " + azimuth + ", accuracy: " + accuracy);
ContentValues values = new ContentValues();
values.put(TrackContentProvider.Schema.COL_TRACK_ID, trackId);
Expand Down Expand Up @@ -146,6 +146,9 @@ public void track(long trackId, Location location, float azimuth, int accuracy,
values.put(TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE, pressure);
}

values.put(TrackContentProvider.Schema.COL_NEW_SEGMENT,
newSeg ? 1 : 0);

Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
contentResolver.insert(Uri.withAppendedPath(trackUri, TrackContentProvider.Schema.TBL_TRACKPOINT + "s"), values);
}
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/net/osmtracker/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
+ TrackContentProvider.Schema.COL_TIMESTAMP + " long not null,"
+ TrackContentProvider.Schema.COL_COMPASS + " double null,"
+ TrackContentProvider.Schema.COL_COMPASS_ACCURACY + " integer null,"
+ TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE + " double null" + ")";
+ TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE + " double null,"
+ TrackContentProvider.Schema.COL_NEW_SEGMENT + " integer default 0" + ")";

/**
* SQL for creating index TRACKPOINT_idx (track id)
Expand Down Expand Up @@ -125,7 +126,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
* v17: add TBL_TRACKPOINT.COL_ATMOSPHERIC_PRESSURE and TBL_WAYPOINT.COL_ATMOSPHERIC_PRESSURE
*</pre>
Copy link
Member

Choose a reason for hiding this comment

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

Update documentation:

* v18: add TBL_TRACKPOINT.COL_NEW_SEGMENT

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your feedback

would be nicer that every point get associated with a consecutive segment number and not with 0 or 1 only. What do you think?

You mean, every segment? Yes, that would be another way to represent multiple segments.
The way the my proposal currently works is that the first point of each segment has 1 (or true), and all the following ones are false again. In a way, it is the derivative of the segment number.
However, if you have a strong preference for the other way, I can change it. However, in that case, I'll apply this to my later pull request (#312, "Import a gpx file as a route to follow"), because there is some interdependence among both. Also, please note that I will be on the move until end of August, and might not get back to this until then.

*/
private static final int DB_VERSION = 17;
private static final int DB_VERSION = 18;

public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
Expand Down Expand Up @@ -178,6 +179,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
case 16:
db.execSQL("alter table " + TrackContentProvider.Schema.TBL_TRACKPOINT + " add column " + TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE + " double null");
db.execSQL("alter table " + TrackContentProvider.Schema.TBL_WAYPOINT + " add column " + TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE + " double null");
case 17:
db.execSQL("alter table "+TrackContentProvider.Schema.TBL_TRACKPOINT + " add column " + TrackContentProvider.Schema.COL_NEW_SEGMENT + " integer default 0");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ public static final class Schema {
public static final String COL_COMPASS = "compass_heading";
public static final String COL_COMPASS_ACCURACY = "compass_accuracy";
public static final String COL_ATMOSPHERIC_PRESSURE = "atmospheric_pressure";


public static final String COL_NEW_SEGMENT = "new_segment";

// virtual colums that are used in some sqls but dont exist in database
public static final String COL_TRACKPOINT_COUNT = "tp_count";
public static final String COL_WAYPOINT_COUNT = "wp_count";
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,15 @@ private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fil
fw.write("\t\t" + "<trkseg>" + "\n");

int i=0;
boolean havePoint=false;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext(),i++) {
StringBuffer out = new StringBuffer();
if(havePoint && c.getShort(c.getColumnIndex(TrackContentProvider.Schema.COL_NEW_SEGMENT)) > 0) {
fw.write("\t\t" + "</trkseg>" + "\n");
fw.write("\t\t" + "<trkseg>" + "\n");
}
havePoint=true;

out.append("\t\t\t" + "<trkpt lat=\""
+ c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LATITUDE)) + "\" "
+ "lon=\"" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LONGITUDE)) + "\">" + "\n");
Expand Down Expand Up @@ -612,4 +619,4 @@ public String sanitizeTrackName(String trackName){
public String getErrorMsg() {
return errorMsg;
}
}
}
59 changes: 59 additions & 0 deletions app/src/main/java/net/osmtracker/overlay/PathOverlays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.osmtracker.overlay;

import java.util.ArrayList;
import java.util.List;

import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.PathOverlay;

import android.content.Context;

/**
* Collection of Overlays, useful to draw interrupted paths
*/
public class PathOverlays {
private int color;
private float width;
private Context ctx;
private MapView osmView;
private boolean havePoint;

private int curIdx=0;

private List<PathOverlay> paths = new ArrayList<PathOverlay>();

private void addPath() {
PathOverlay path = new PathOverlay(color, width, ctx);
paths.add(path);
osmView.getOverlays().add(path);
}

public void clearPath() {
for(PathOverlay path : paths)
path.clearPath();
curIdx=0;
}

public PathOverlays(int color, float width,
Context ctx, MapView osmView) {
this.color=color;
this.width=width;
this.ctx=ctx;
this.osmView = osmView;
addPath();
havePoint=false;
}

public void addPoint(double lat, double lon) {
if(curIdx >= paths.size())
addPath();
paths.get(curIdx).addPoint(lat, lon);
havePoint=true;
}

public void nextSegment() {
if(havePoint)
curIdx++;
havePoint=false;
}
}
9 changes: 7 additions & 2 deletions app/src/main/java/net/osmtracker/service/gps/GPSLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public class GPSLogger extends Service implements LocationListener {
*/
private PressureListener pressureListener = new PressureListener();

private boolean newSeg = false;

/**
* Receives Intent for way point tracking, and stop/start logging.
*/
Expand All @@ -129,7 +131,8 @@ public void onReceive(Context context, Intent intent) {
dataHelper.wayPoint(trackId, lastLocation, name, link, uuid, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure());

// If there is a waypoint in the track, there should also be a trackpoint
dataHelper.track(currentTrackId, lastLocation, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure());
dataHelper.track(currentTrackId, lastLocation, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure(),newSeg);
newSeg = false;
}
}
}
Expand All @@ -151,6 +154,7 @@ public void onReceive(Context context, Intent intent) {
dataHelper.deleteWayPoint(uuid);
}
} else if (OSMTracker.INTENT_START_TRACKING.equals(intent.getAction()) ) {
newSeg = true;
Bundle extras = intent.getExtras();
if (extras != null) {
Long trackId = extras.getLong(TrackContentProvider.Schema.COL_TRACK_ID);
Expand Down Expand Up @@ -306,7 +310,8 @@ public void onLocationChanged(Location location) {
lastLocation = location;

if (isTracking) {
dataHelper.track(currentTrackId, location, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure());
dataHelper.track(currentTrackId, location, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure(), newSeg);
newSeg = false;
}
}
}
Expand Down