Skip to content

Alternative: Use only one line per TRKPT/WPT #264

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
2 changes: 2 additions & 0 deletions app/src/main/java/net/osmtracker/OSMTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static final class Preferences {
public final static String KEY_OUTPUT_GPX_HDOP_APPROXIMATION = "gpx.hdop.approximation";
public final static String KEY_OUTPUT_DIR_PER_TRACK = "gpx.directory_per_track";
public final static String KEY_OUTPUT_COMPASS = "gpx.compass_heading";
public final static String KEY_GPX_FORMAT_SHORT = "gpx.gpx_format_short";

public final static String KEY_UI_PICTURE_SOURCE = "ui.picture.source";
public final static String KEY_UI_BUTTONS_LAYOUT = "ui.buttons.layout";
Expand Down Expand Up @@ -75,6 +76,7 @@ public static final class Preferences {

public final static boolean VAL_OUTPUT_GPX_HDOP_APPROXIMATION = false;
public final static boolean VAL_OUTPUT_GPX_OUTPUT_DIR_PER_TRACK = true;
public final static boolean VAL_GPX_FORMAT_SHORT = false;

public final static String VAL_UI_PICTURE_SOURCE_CAMERA = "camera";
public final static String VAL_UI_PICTURE_SOURCE_GALLERY = "gallery";
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/net/osmtracker/gpx/ExportToStorageTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ protected File getExportDirectory(Date startDate) throws ExportTrackException {

boolean directoryPerTrack = prefs.getBoolean(OSMTracker.Preferences.KEY_OUTPUT_DIR_PER_TRACK,
OSMTracker.Preferences.VAL_OUTPUT_GPX_OUTPUT_DIR_PER_TRACK);

boolean gpxFormatShort = prefs.getBoolean(OSMTracker.Preferences.KEY_GPX_FORMAT_SHORT,
OSMTracker.Preferences.VAL_GPX_FORMAT_SHORT);

// Create the path to the directory to which we will be writing
// Trim the directory name, as additional spaces at the end will
Expand Down
206 changes: 200 additions & 6 deletions app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ private void writeGpxFile(String trackName, String tags, String track_descriptio
String compassOutput = PreferenceManager.getDefaultSharedPreferences(context).getString(
OSMTracker.Preferences.KEY_OUTPUT_COMPASS,
OSMTracker.Preferences.VAL_OUTPUT_COMPASS);
boolean gpxFormatShort = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
OSMTracker.Preferences.KEY_GPX_FORMAT_SHORT,
OSMTracker.Preferences.VAL_GPX_FORMAT_SHORT);

Log.v(TAG, "write preferences: compass:" + compassOutput);

Expand All @@ -315,8 +318,13 @@ private void writeGpxFile(String trackName, String tags, String track_descriptio

writer.write("\t</metadata>\n");

writeWayPoints(writer, cWayPoints, accuracyOutput, fillHDOP, compassOutput);
writeTrackPoints(context.getResources().getString(R.string.gpx_track_name), writer, cTrackPoints, fillHDOP, compassOutput);
if (gpxFormatShort) {
writeWayPoints_short(writer, cWayPoints, accuracyOutput, fillHDOP, compassOutput);
writeTrackPoints_short(context.getResources().getString(R.string.gpx_track_name), writer, cTrackPoints, fillHDOP, compassOutput);
} else {
writeWayPoints_long(writer, cWayPoints, accuracyOutput, fillHDOP, compassOutput);
writeTrackPoints_long(context.getResources().getString(R.string.gpx_track_name), writer, cTrackPoints, fillHDOP, compassOutput);
}
writer.write("</gpx>");

} finally {
Expand All @@ -327,15 +335,15 @@ private void writeGpxFile(String trackName, String tags, String track_descriptio
}

/**
* Iterates on track points and write them.
* Iterates on track points and write them. Long version, multiple lines per point.
* @param trackName Name of the track (metadata).
* @param fw Writer to the target file.
* @param c Cursor to track points.
* @param fillHDOP Indicates whether fill <hdop> tag with approximation from location accuracy.
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
* @throws IOException
*/
private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
private void writeTrackPoints_long(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
// Update dialog every 1%
int dialogUpdateThreshold = c.getCount() / 100;
if (dialogUpdateThreshold == 0) {
Expand Down Expand Up @@ -376,6 +384,7 @@ private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fil
}

String buff = "";
buff += "\t\t\t\t\t<accuracy>" + String.valueOf(c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY))) + "</accuracy>\n";
if(! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_SPEED))) {
buff += "\t\t\t\t\t" + "<speed>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_SPEED)) + "</speed>" + "\n";
}
Expand All @@ -402,15 +411,15 @@ private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fil
}

/**
* Iterates on way points and write them.
* Iterates on way points and write them. Long version, multiple lines per point.
* @param fw Writer to the target file.
* @param c Cursor to way points.
* @param accuracyInfo Constant describing how to include (or not) accuracy info for way points.
* @param fillHDOP Indicates whether fill <hdop> tag with approximation from location accuracy.
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
* @throws IOException
*/
private void writeWayPoints(Writer fw, Cursor c, String accuracyInfo, boolean fillHDOP, String compass) throws IOException {
private void writeWayPoints_long(Writer fw, Cursor c, String accuracyInfo, boolean fillHDOP, String compass) throws IOException {

// Update dialog every 1%
int dialogUpdateThreshold = c.getCount() / 100;
Expand Down Expand Up @@ -504,6 +513,191 @@ private void writeWayPoints(Writer fw, Cursor c, String accuracyInfo, boolean fi

fw.write(out.toString());

if (i % dialogUpdateThreshold == 0) {
publishProgress((long) dialogUpdateThreshold);
}
}
}

/**
* Iterates on track points and write them. Short version, one line per point.
* @param trackName Name of the track (metadata).
* @param fw Writer to the target file.
* @param c Cursor to track points.
* @param fillHDOP Indicates whether fill <hdop> tag with approximation from location accuracy.
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
* @throws IOException
*/
private void writeTrackPoints_short(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
// Update dialog every 1%
int dialogUpdateThreshold = c.getCount() / 100;
if (dialogUpdateThreshold == 0) {
dialogUpdateThreshold++;
}

fw.write("<trk>" + "\n");
fw.write("<name>" + CDATA_START + trackName + CDATA_END + "</name>" + "\n");
if (fillHDOP) {
fw.write("<cmt>"
+ CDATA_START
+ context.getResources().getString(R.string.gpx_hdop_approximation_cmt)
+ CDATA_END
+ "</cmt>" + "\n");
}
fw.write("<trkseg>" + "\n");

int i=0;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext(),i++) {
StringBuffer out = new StringBuffer();
out.append("<trkpt lat=\""
+ c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LATITUDE)) + "\" "
+ "lon=\"" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LONGITUDE)) + "\">");
if (! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ELEVATION))) {
out.append("<ele>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ELEVATION)) + "</ele>");
}
out.append("<time>" + pointDateFormatter.format(new Date(c.getLong(c.getColumnIndex(TrackContentProvider.Schema.COL_TIMESTAMP)))) + "</time>");

if(fillHDOP && ! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY))) {
out.append("<hdop>" + (c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) / OSMTracker.HDOP_APPROXIMATION_FACTOR) + "</hdop>");
}
if(OSMTracker.Preferences.VAL_OUTPUT_COMPASS_COMMENT.equals(compass) && !c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
out.append("<cmt>"+CDATA_START+"compass: " +
c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))+
" compAccuracy: " +
c.getLong(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY))+
CDATA_END+"</cmt>");
}

String buff = "";
buff += "<accuracy>" + String.valueOf(c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY))) + "</accuracy>";
if(! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_SPEED))) {
buff += "<speed>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_SPEED)) + "</speed>";
}
if(OSMTracker.Preferences.VAL_OUTPUT_COMPASS_EXTENSION.equals(compass) && !c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
buff += "<compass>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) + "</compass>";
buff += "<compass_accuracy>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) + "</compass_accuracy>";
}
if(! buff.equals("")) {
out.append("<extensions>");
out.append(buff);
out.append("</extensions>");
}

out.append("</trkpt>" + "\n");
fw.write(out.toString());

if (i % dialogUpdateThreshold == 0) {
publishProgress((long) dialogUpdateThreshold);
}
}

fw.write("</trkseg>" + "\n");
fw.write("</trk>" + "\n");
}

/**
* Iterates on way points and write them. Short version, one line per point.
* @param fw Writer to the target file.
* @param c Cursor to way points.
* @param accuracyInfo Constant describing how to include (or not) accuracy info for way points.
* @param fillHDOP Indicates whether fill <hdop> tag with approximation from location accuracy.
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
* @throws IOException
*/
private void writeWayPoints_short(Writer fw, Cursor c, String accuracyInfo, boolean fillHDOP, String compass) throws IOException {

// Update dialog every 1%
int dialogUpdateThreshold = c.getCount() / 100;
if (dialogUpdateThreshold == 0) {
dialogUpdateThreshold++;
}

// Label for meter unit
String meterUnit = context.getResources().getString(R.string.various_unit_meters);
// Word "accuracy"
String accuracy = context.getResources().getString(R.string.various_accuracy);

int i=0;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext(), i++) {
StringBuffer out = new StringBuffer();
out.append("<wpt lat=\""
+ c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LATITUDE)) + "\" "
+ "lon=\"" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LONGITUDE)) + "\">");
if (! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ELEVATION))) {
out.append("<ele>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ELEVATION)) + "</ele>");
}
out.append("<time>" + pointDateFormatter.format(new Date(c.getLong(c.getColumnIndex(TrackContentProvider.Schema.COL_TIMESTAMP)))) + "</time>");

String name = c.getString(c.getColumnIndex(TrackContentProvider.Schema.COL_NAME));

if (! OSMTracker.Preferences.VAL_OUTPUT_ACCURACY_NONE.equals(accuracyInfo) && ! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY))) {
// Outputs accuracy info for way point
if (OSMTracker.Preferences.VAL_OUTPUT_ACCURACY_WPT_NAME.equals(accuracyInfo)) {
// Output accuracy with name
out.append("<name>"
+ CDATA_START
+ name
+ " (" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) + meterUnit + ")"
+ CDATA_END
+ "</name>");
if (OSMTracker.Preferences.VAL_OUTPUT_COMPASS_COMMENT.equals(compass) &&
! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
out.append("<cmt>" + CDATA_START + "compass: " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) +
" compass accuracy: " + c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) + CDATA_END + "</cmt>");
}
} else if (OSMTracker.Preferences.VAL_OUTPUT_ACCURACY_WPT_CMT.equals(accuracyInfo)) {
// Output accuracy in separate tag
out.append("<name>" + CDATA_START + name + CDATA_END + "</name>");
if (OSMTracker.Preferences.VAL_OUTPUT_COMPASS_COMMENT.equals(compass) &&
! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
out.append("<cmt>" + CDATA_START + accuracy + ": " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) + meterUnit +
" compass heading: " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) +
"deg compass accuracy: " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) +CDATA_END + "</cmt>");
} else {
out.append("<cmt>" + CDATA_START + accuracy + ": " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) + meterUnit + CDATA_END + "</cmt>");
}
} else {
// Unknown value for accuracy info, shouldn't occur but who knows ?
// See issue #68. Output at least the name just in case.
out.append("<name>" + CDATA_START + name + CDATA_END + "</name>");
}
} else {
// No accuracy info requested, or available
out.append("<name>" + CDATA_START + name + CDATA_END + "</name>");
if (OSMTracker.Preferences.VAL_OUTPUT_COMPASS_COMMENT.equals(compass) &&
! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
out.append("<cmt>" + CDATA_START + "compass: " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) +
" compass accuracy: " + c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) + CDATA_END + "</cmt>");
}
}

String link = c.getString(c.getColumnIndex(TrackContentProvider.Schema.COL_LINK));
if (link != null) {
out.append("<link href=\"" + URLEncoder.encode(link) + "\">");
out.append("<text>" + link +"</text>");
out.append("</link>");
}

if (! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_NBSATELLITES))) {
out.append("<sat>" + c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_NBSATELLITES)) + "</sat>");
}

if(fillHDOP && ! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY))) {
out.append("<hdop>" + (c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) / OSMTracker.HDOP_APPROXIMATION_FACTOR) + "</hdop>");
}

if (OSMTracker.Preferences.VAL_OUTPUT_COMPASS_EXTENSION.equals(compass) &&
! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
out.append("<extensions>");
out.append("<compass>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) + "</compass>");
out.append("<compass_accuracy>" + c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) + "</compass_accuracy>");
out.append("</extensions>");
}

out.append("</wpt>" + "\n");

fw.write(out.toString());

if (i % dialogUpdateThreshold == 0) {
publishProgress((long) dialogUpdateThreshold);
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings-preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
<string name="prefs_storage_dir_hint">Effective for the next track (not the current one)</string>
<string name="prefs_output_one_dir_per_track">One directory per track</string>
<string name="prefs_output_one_dir_per_track_summary">Save each track and associated files to its own directory</string>
<string name="prefs_gpx_format_type">Short format</string>
<string name="prefs_gpx_format_type_summary">One line per track- or waypoint</string>
<string name="prefs_output_filename">Filename for named tracks</string>
<string name="prefs_output_filename_summary">Pattern for filename if the track has a name</string>
<string-array name="prefs_output_filename_keys">
Expand Down
Loading