Skip to content

Commit 9d17835

Browse files
Hans-Joachim ZimmerHans-Joachim Zimmer
Hans-Joachim Zimmer
authored and
Hans-Joachim Zimmer
committed
Option to choose btw. long/short GPX format
1 parent a3ef296 commit 9d17835

File tree

5 files changed

+260
-56
lines changed

5 files changed

+260
-56
lines changed

app/src/main/java/net/osmtracker/OSMTracker.java

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static final class Preferences {
2929
public final static String KEY_OUTPUT_GPX_HDOP_APPROXIMATION = "gpx.hdop.approximation";
3030
public final static String KEY_OUTPUT_DIR_PER_TRACK = "gpx.directory_per_track";
3131
public final static String KEY_OUTPUT_COMPASS = "gpx.compass_heading";
32+
public final static String KEY_GPX_FORMAT_SHORT = "gpx.gpx_format_short";
3233

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

7677
public final static boolean VAL_OUTPUT_GPX_HDOP_APPROXIMATION = false;
7778
public final static boolean VAL_OUTPUT_GPX_OUTPUT_DIR_PER_TRACK = true;
79+
public final static boolean VAL_GPX_FORMAT_SHORT = false;
7880

7981
public final static String VAL_UI_PICTURE_SOURCE_CAMERA = "camera";
8082
public final static String VAL_UI_PICTURE_SOURCE_GALLERY = "gallery";

app/src/main/java/net/osmtracker/gpx/ExportToStorageTask.java

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ protected File getExportDirectory(Date startDate) throws ExportTrackException {
3939

4040
boolean directoryPerTrack = prefs.getBoolean(OSMTracker.Preferences.KEY_OUTPUT_DIR_PER_TRACK,
4141
OSMTracker.Preferences.VAL_OUTPUT_GPX_OUTPUT_DIR_PER_TRACK);
42+
43+
boolean gpxFormatShort = prefs.getBoolean(OSMTracker.Preferences.KEY_GPX_FORMAT_SHORT,
44+
OSMTracker.Preferences.VAL_GPX_FORMAT_SHORT);
4245

4346
// Create the path to the directory to which we will be writing
4447
// Trim the directory name, as additional spaces at the end will

app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java

+198-6
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ private void writeGpxFile(String trackName, String tags, String track_descriptio
290290
String compassOutput = PreferenceManager.getDefaultSharedPreferences(context).getString(
291291
OSMTracker.Preferences.KEY_OUTPUT_COMPASS,
292292
OSMTracker.Preferences.VAL_OUTPUT_COMPASS);
293+
boolean gpxFormatShort = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
294+
OSMTracker.Preferences.KEY_GPX_FORMAT_SHORT,
295+
OSMTracker.Preferences.VAL_GPX_FORMAT_SHORT);
293296

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

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

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

318-
writeWayPoints(writer, cWayPoints, accuracyOutput, fillHDOP, compassOutput);
319-
writeTrackPoints(context.getResources().getString(R.string.gpx_track_name), writer, cTrackPoints, fillHDOP, compassOutput);
321+
if (gpxFormatShort) {
322+
writeWayPoints_short(writer, cWayPoints, accuracyOutput, fillHDOP, compassOutput);
323+
writeTrackPoints_short(context.getResources().getString(R.string.gpx_track_name), writer, cTrackPoints, fillHDOP, compassOutput);
324+
} else {
325+
writeWayPoints_long(writer, cWayPoints, accuracyOutput, fillHDOP, compassOutput);
326+
writeTrackPoints_long(context.getResources().getString(R.string.gpx_track_name), writer, cTrackPoints, fillHDOP, compassOutput);
327+
}
320328
writer.write("</gpx>");
321329

322330
} finally {
@@ -327,15 +335,199 @@ private void writeGpxFile(String trackName, String tags, String track_descriptio
327335
}
328336

329337
/**
330-
* Iterates on track points and write them.
338+
* Iterates on track points and write them. Long version, multiple lines per point.
339+
* @param trackName Name of the track (metadata).
340+
* @param fw Writer to the target file.
341+
* @param c Cursor to track points.
342+
* @param fillHDOP Indicates whether fill <hdop> tag with approximation from location accuracy.
343+
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
344+
* @throws IOException
345+
*/
346+
private void writeTrackPoints_long(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
347+
// Update dialog every 1%
348+
int dialogUpdateThreshold = c.getCount() / 100;
349+
if (dialogUpdateThreshold == 0) {
350+
dialogUpdateThreshold++;
351+
}
352+
353+
fw.write("\t" + "<trk>" + "\n");
354+
fw.write("\t\t" + "<name>" + CDATA_START + trackName + CDATA_END + "</name>" + "\n");
355+
if (fillHDOP) {
356+
fw.write("\t\t" + "<cmt>"
357+
+ CDATA_START
358+
+ context.getResources().getString(R.string.gpx_hdop_approximation_cmt)
359+
+ CDATA_END
360+
+ "</cmt>" + "\n");
361+
}
362+
fw.write("\t\t" + "<trkseg>" + "\n");
363+
364+
int i=0;
365+
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext(),i++) {
366+
StringBuffer out = new StringBuffer();
367+
out.append("\t\t\t" + "<trkpt lat=\""
368+
+ c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LATITUDE)) + "\" "
369+
+ "lon=\"" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LONGITUDE)) + "\">" + "\n");
370+
if (! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ELEVATION))) {
371+
out.append("\t\t\t\t" + "<ele>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ELEVATION)) + "</ele>" + "\n");
372+
}
373+
out.append("\t\t\t\t" + "<time>" + pointDateFormatter.format(new Date(c.getLong(c.getColumnIndex(TrackContentProvider.Schema.COL_TIMESTAMP)))) + "</time>" + "\n");
374+
375+
if(fillHDOP && ! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY))) {
376+
out.append("\t\t\t\t" + "<hdop>" + (c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) / OSMTracker.HDOP_APPROXIMATION_FACTOR) + "</hdop>" + "\n");
377+
}
378+
if(OSMTracker.Preferences.VAL_OUTPUT_COMPASS_COMMENT.equals(compass) && !c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
379+
out.append("\t\t\t\t" + "<cmt>"+CDATA_START+"compass: " +
380+
c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))+
381+
"\n\t\t\t\t\tcompAccuracy: " +
382+
c.getLong(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY))+
383+
CDATA_END+"</cmt>"+"\n");
384+
}
385+
386+
String buff = "";
387+
if(! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_SPEED))) {
388+
buff += "\t\t\t\t\t" + "<speed>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_SPEED)) + "</speed>" + "\n";
389+
}
390+
if(OSMTracker.Preferences.VAL_OUTPUT_COMPASS_EXTENSION.equals(compass) && !c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
391+
buff += "\t\t\t\t\t" + "<compass>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) + "</compass>" + "\n";
392+
buff += "\t\t\t\t\t" + "<compass_accuracy>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) + "</compass_accuracy>" + "\n";
393+
}
394+
if(! buff.equals("")) {
395+
out.append("\t\t\t\t" + "<extensions>\n");
396+
out.append(buff);
397+
out.append("\t\t\t\t" + "</extensions>\n");
398+
}
399+
400+
out.append("\t\t\t" + "</trkpt>" + "\n");
401+
fw.write(out.toString());
402+
403+
if (i % dialogUpdateThreshold == 0) {
404+
publishProgress((long) dialogUpdateThreshold);
405+
}
406+
}
407+
408+
fw.write("\t\t" + "</trkseg>" + "\n");
409+
fw.write("\t" + "</trk>" + "\n");
410+
}
411+
412+
/**
413+
* Iterates on way points and write them. Long version, multiple lines per point.
414+
* @param fw Writer to the target file.
415+
* @param c Cursor to way points.
416+
* @param accuracyInfo Constant describing how to include (or not) accuracy info for way points.
417+
* @param fillHDOP Indicates whether fill <hdop> tag with approximation from location accuracy.
418+
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
419+
* @throws IOException
420+
*/
421+
private void writeWayPoints_long(Writer fw, Cursor c, String accuracyInfo, boolean fillHDOP, String compass) throws IOException {
422+
423+
// Update dialog every 1%
424+
int dialogUpdateThreshold = c.getCount() / 100;
425+
if (dialogUpdateThreshold == 0) {
426+
dialogUpdateThreshold++;
427+
}
428+
429+
// Label for meter unit
430+
String meterUnit = context.getResources().getString(R.string.various_unit_meters);
431+
// Word "accuracy"
432+
String accuracy = context.getResources().getString(R.string.various_accuracy);
433+
434+
int i=0;
435+
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext(), i++) {
436+
StringBuffer out = new StringBuffer();
437+
out.append("\t" + "<wpt lat=\""
438+
+ c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LATITUDE)) + "\" "
439+
+ "lon=\"" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LONGITUDE)) + "\">" + "\n");
440+
if (! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ELEVATION))) {
441+
out.append("\t\t" + "<ele>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ELEVATION)) + "</ele>" + "\n");
442+
}
443+
out.append("\t\t" + "<time>" + pointDateFormatter.format(new Date(c.getLong(c.getColumnIndex(TrackContentProvider.Schema.COL_TIMESTAMP)))) + "</time>" + "\n");
444+
445+
String name = c.getString(c.getColumnIndex(TrackContentProvider.Schema.COL_NAME));
446+
447+
if (! OSMTracker.Preferences.VAL_OUTPUT_ACCURACY_NONE.equals(accuracyInfo) && ! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY))) {
448+
// Outputs accuracy info for way point
449+
if (OSMTracker.Preferences.VAL_OUTPUT_ACCURACY_WPT_NAME.equals(accuracyInfo)) {
450+
// Output accuracy with name
451+
out.append("\t\t" + "<name>"
452+
+ CDATA_START
453+
+ name
454+
+ " (" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) + meterUnit + ")"
455+
+ CDATA_END
456+
+ "</name>" + "\n");
457+
if (OSMTracker.Preferences.VAL_OUTPUT_COMPASS_COMMENT.equals(compass) &&
458+
! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
459+
out.append("\t\t"+ "<cmt>" + CDATA_START + "compass: " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) +
460+
"\n\t\t\tcompass accuracy: " + c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) + CDATA_END + "</cmt>\n");
461+
}
462+
} else if (OSMTracker.Preferences.VAL_OUTPUT_ACCURACY_WPT_CMT.equals(accuracyInfo)) {
463+
// Output accuracy in separate tag
464+
out.append("\t\t" + "<name>" + CDATA_START + name + CDATA_END + "</name>" + "\n");
465+
if (OSMTracker.Preferences.VAL_OUTPUT_COMPASS_COMMENT.equals(compass) &&
466+
! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
467+
out.append("\t\t" + "<cmt>" + CDATA_START + accuracy + ": " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) + meterUnit +
468+
"\n\t\t\t compass heading: " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) +
469+
"deg\n\t\t\t compass accuracy: " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) +CDATA_END + "</cmt>" + "\n");
470+
} else {
471+
out.append("\t\t" + "<cmt>" + CDATA_START + accuracy + ": " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) + meterUnit + CDATA_END + "</cmt>" + "\n");
472+
}
473+
} else {
474+
// Unknown value for accuracy info, shouldn't occur but who knows ?
475+
// See issue #68. Output at least the name just in case.
476+
out.append("\t\t" + "<name>" + CDATA_START + name + CDATA_END + "</name>" + "\n");
477+
}
478+
} else {
479+
// No accuracy info requested, or available
480+
out.append("\t\t" + "<name>" + CDATA_START + name + CDATA_END + "</name>" + "\n");
481+
if (OSMTracker.Preferences.VAL_OUTPUT_COMPASS_COMMENT.equals(compass) &&
482+
! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
483+
out.append("\t\t"+ "<cmt>" + CDATA_START + "compass: " + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) +
484+
"\n\t\t\tcompass accuracy: " + c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) + CDATA_END + "</cmt>\n");
485+
}
486+
}
487+
488+
String link = c.getString(c.getColumnIndex(TrackContentProvider.Schema.COL_LINK));
489+
if (link != null) {
490+
out.append("\t\t" + "<link href=\"" + URLEncoder.encode(link) + "\">" + "\n");
491+
out.append("\t\t\t" + "<text>" + link +"</text>\n");
492+
out.append("\t\t" + "</link>" + "\n");
493+
}
494+
495+
if (! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_NBSATELLITES))) {
496+
out.append("\t\t" + "<sat>" + c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_NBSATELLITES)) + "</sat>" + "\n");
497+
}
498+
499+
if(fillHDOP && ! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY))) {
500+
out.append("\t\t" + "<hdop>" + (c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_ACCURACY)) / OSMTracker.HDOP_APPROXIMATION_FACTOR) + "</hdop>" + "\n");
501+
}
502+
503+
if (OSMTracker.Preferences.VAL_OUTPUT_COMPASS_EXTENSION.equals(compass) &&
504+
! c.isNull(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS))) {
505+
out.append("\t\t<extensions>\n");
506+
out.append("\t\t\t"+ "<compass>" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS)) + "</compass>\n");
507+
out.append("\t\t\t" + "<compass_accuracy>" + c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_COMPASS_ACCURACY)) + "</compass_accuracy>" + "\n");
508+
out.append("\t\t</extensions>\n");
509+
}
510+
511+
out.append("\t" + "</wpt>" + "\n");
512+
513+
fw.write(out.toString());
514+
515+
if (i % dialogUpdateThreshold == 0) {
516+
publishProgress((long) dialogUpdateThreshold);
517+
}
518+
}
519+
}
520+
521+
/**
522+
* Iterates on track points and write them. Short version, one line per point.
331523
* @param trackName Name of the track (metadata).
332524
* @param fw Writer to the target file.
333525
* @param c Cursor to track points.
334526
* @param fillHDOP Indicates whether fill <hdop> tag with approximation from location accuracy.
335527
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
336528
* @throws IOException
337529
*/
338-
private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
530+
private void writeTrackPoints_short(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
339531
// Update dialog every 1%
340532
int dialogUpdateThreshold = c.getCount() / 100;
341533
if (dialogUpdateThreshold == 0) {
@@ -402,15 +594,15 @@ private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fil
402594
}
403595

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

415607
// Update dialog every 1%
416608
int dialogUpdateThreshold = c.getCount() / 100;

app/src/main/res/values/strings-preferences.xml

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
<string name="prefs_storage_dir_hint">Effective for the next track (not the current one)</string>
7878
<string name="prefs_output_one_dir_per_track">One directory per track</string>
7979
<string name="prefs_output_one_dir_per_track_summary">Save each track and associated files to its own directory</string>
80+
<string name="prefs_gpx_format_type">Short format</string>
81+
<string name="prefs_gpx_format_type_summary">One line per track- or waypoint</string>
8082
<string name="prefs_output_filename">Filename for named tracks</string>
8183
<string name="prefs_output_filename_summary">Pattern for filename if the track has a name</string>
8284
<string-array name="prefs_output_filename_keys">

0 commit comments

Comments
 (0)