Skip to content
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

Import a gpx file as a route to follow #312

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
27 changes: 17 additions & 10 deletions app/src/main/java/net/osmtracker/activity/DisplayTrackMap.java
Original file line number Diff line number Diff line change
@@ -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;

@@ -25,7 +25,6 @@
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
@@ -109,7 +108,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
@@ -148,7 +147,7 @@ public class DisplayTrackMap extends Activity {
* Initially null, to indicate that no data has yet been read.
*/
private Integer lastTrackPointIdProcessed = null;

/**
* Observes changes on trackpoints
*/
@@ -379,9 +378,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((float)(metrics.densityDpi / 25.4 / 2),this, osmView);

myLocationOverlay = new SimpleLocationOverlay(this);
osmView.getOverlays().add(myLocationOverlay);
@@ -426,7 +423,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, TrackContentProvider.Schema.COL_IS_ROUTE};
// Selection: The where clause to use
String selection = null;
// SelectionArgs: The parameter replacements to use for the '?' in the selection
@@ -454,16 +451,26 @@ private void pathChanged() {
c.moveToFirst();
double lastLat = 0;
double lastLon = 0;
boolean newSegment;
boolean isRoute;
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);
int isRouteColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_IS_ROUTE);

// Add each new point to the track
while(!c.isAfterLast()) {
lastLat = c.getDouble(latitudeColumnIndex);
lastLon = c.getDouble(longitudeColumnIndex);
lastTrackPointIdProcessed = c.getInt(primaryKeyColumnIndex);
pathOverlay.addPoint((int)(lastLat * 1e6), (int)(lastLon * 1e6));
newSegment = c.getShort(newSegmentColumnIndex) > 0;
isRoute = c.getShort(isRouteColumnIndex) > 0;
if(newSegment) {
pathOverlay.nextSegment(isRoute);
}

pathOverlay.addPoint((int)(lastLat * 1e6), (int)(lastLon * 1e6), isRoute);
if (doInitialBoundsCalc) {
if (lastLat < minLat) minLat = lastLat;
if (lastLon < minLon) minLon = lastLon;
60 changes: 60 additions & 0 deletions app/src/main/java/net/osmtracker/activity/TrackManager.java
Original file line number Diff line number Diff line change
@@ -10,13 +10,15 @@
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
@@ -39,6 +41,7 @@
import net.osmtracker.exception.CreateTrackException;
import net.osmtracker.gpx.ExportToStorageTask;
import net.osmtracker.gpx.ExportToTempFileTask;
import net.osmtracker.gpx.ImportRoute;
import net.osmtracker.util.FileSystemUtils;

import java.io.File;
@@ -61,6 +64,11 @@ public class TrackManager extends AppCompatActivity
final private int RC_GPS_PERMISSION = 5;
final private int RC_WRITE_PERMISSIONS_SHARE = 6;

/**
* Request code for callback after user has selected an import file
*/
private static final int REQCODE_IMPORT_OPEN = 0;

/** Bundle key for {@link #prevItemVisible} */
private static final String PREV_VISIBLE = "prev_visible";

@@ -367,6 +375,53 @@ public void onClick(DialogInterface dialog, int which) {
}.execute();
}

/* Import route
*/
private void importRoute() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); // GPX application type not known to Android...
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQCODE_IMPORT_OPEN);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQCODE_IMPORT_OPEN:
if(resultCode == Activity.RESULT_CANCELED) {
// cancelled by user
return;
}
if(resultCode != Activity.RESULT_OK) {
// something unexpected
Toast.makeText(this,
"Result code="+resultCode,
Toast.LENGTH_LONG).show();
return;
}

Uri uri = data.getData();
try {
AssetFileDescriptor afd = getContentResolver()
.openAssetFileDescriptor(uri, "r");
new ImportRoute(this,
contextMenuSelectedTrackid)
.doImport(afd,()->updateTrackItemsInRecyclerView());
} catch(Exception e) {
new AlertDialog.Builder(this)
.setTitle("Exception received")
.setMessage(Log.getStackTraceString(e))
.setNeutralButton("Ok",
(dlg,id)->dlg.dismiss())
.create()
.show();
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo, long trackId) {
super.onCreateContextMenu(menu, v, menuInfo);
@@ -474,6 +529,11 @@ public void onClick(DialogInterface dialog, int which) {
i.putExtra(TrackContentProvider.Schema.COL_TRACK_ID, contextMenuSelectedTrackid);
startActivity(i);
break;

case R.id.trackmgr_contextmenu_import:
importRoute();
break;

}
return super.onContextItemSelected(item);
}
15 changes: 14 additions & 1 deletion app/src/main/java/net/osmtracker/db/DataHelper.java
Original file line number Diff line number Diff line change
@@ -111,8 +111,15 @@ public DataHelper(Context c) {
* ignored if azimuth is invalid.
* @param pressure
* atmospheric pressure
* @param newSeg
* whether this point is to start a new track segment
* @param isRoute
* whether this point is a point of a route, rather
* than a track (routes are imported paths that we
* want to follow, and shown in green rather than
* blue)
*/
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, boolean isRoute) {
Log.v(TAG, "Tracking (trackId=" + trackId + ") location: " + location + " azimuth: " + azimuth + ", accuracy: " + accuracy);
ContentValues values = new ContentValues();
values.put(TrackContentProvider.Schema.COL_TRACK_ID, trackId);
@@ -146,6 +153,12 @@ 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);

values.put(TrackContentProvider.Schema.COL_IS_ROUTE,
isRoute ? 1 : 0);

Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
contentResolver.insert(Uri.withAppendedPath(trackUri, TrackContentProvider.Schema.TBL_TRACKPOINT + "s"), values);
}
11 changes: 9 additions & 2 deletions app/src/main/java/net/osmtracker/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
@@ -39,7 +39,10 @@ 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,"
+ TrackContentProvider.Schema.COL_IS_ROUTE + " integer default 0"
+ ")";

/**
* SQL for creating index TRACKPOINT_idx (track id)
@@ -125,7 +128,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
* v17: add TBL_TRACKPOINT.COL_ATMOSPHERIC_PRESSURE and TBL_WAYPOINT.COL_ATMOSPHERIC_PRESSURE
*</pre>
*/
private static final int DB_VERSION = 17;
private static final int DB_VERSION = 19;

public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
@@ -178,6 +181,10 @@ 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");
case 18:
db.execSQL("alter table "+TrackContentProvider.Schema.TBL_TRACKPOINT + " add column " + TrackContentProvider.Schema.COL_IS_ROUTE + " integer default 0");
}
}

Original file line number Diff line number Diff line change
@@ -496,7 +496,10 @@ 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";
public static final String COL_IS_ROUTE = "is_route";

// 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";
12 changes: 11 additions & 1 deletion app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java
Original file line number Diff line number Diff line change
@@ -358,8 +358,18 @@ 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(c.getShort(c.getColumnIndex(TrackContentProvider.Schema.COL_IS_ROUTE)) > 0)
// do not re-export route points
continue;
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");
@@ -612,4 +622,4 @@ public String sanitizeTrackName(String trackName){
public String getErrorMsg() {
return errorMsg;
}
}
}
Loading
Oops, something went wrong.