Clean up formatting of numbers in gpx export.

Addresses issue #299 and ensures Latitude and Longitude are formatted
correctly as well.
This commit is contained in:
Jared Casper
2011-02-09 10:25:28 -08:00
parent ddafdcf392
commit 58e3c0604d
3 changed files with 22 additions and 11 deletions
@@ -26,6 +26,7 @@ import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
@@ -36,6 +37,9 @@ import java.util.TimeZone;
*/ */
public class GpxTrackWriter implements TrackFormatWriter { public class GpxTrackWriter implements TrackFormatWriter {
static final int LATLONG_FORMAT = Location.FORMAT_DEGREES;
static final DecimalFormat ELEVATION_FORMAT =
new DecimalFormat("#.#");
static final SimpleDateFormat TIMESTAMP_FORMAT = static final SimpleDateFormat TIMESTAMP_FORMAT =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
static { static {
@@ -118,10 +122,11 @@ public class GpxTrackWriter implements TrackFormatWriter {
@Override @Override
public void writeLocation(Location l) { public void writeLocation(Location l) {
if (pw != null) { if (pw != null) {
pw.println("<trkpt lat=\"" + l.getLatitude() + "\" lon=\"" pw.println("<trkpt lat=\""
+ l.getLongitude() + "\">"); + Location.convert(l.getLatitude(), LATLONG_FORMAT) + "\" lon=\""
+ Location.convert(l.getLongitude(), LATLONG_FORMAT) + "\">");
Date d = new Date(l.getTime()); Date d = new Date(l.getTime());
pw.println("<ele>" + l.getAltitude() + "</ele>"); pw.println("<ele>" + ELEVATION_FORMAT.format(l.getAltitude()) + "</ele>");
pw.println("<time>" + TIMESTAMP_FORMAT.format(d) + "</time>"); pw.println("<time>" + TIMESTAMP_FORMAT.format(d) + "</time>");
pw.println("</trkpt>"); pw.println("</trkpt>");
} }
@@ -141,14 +146,15 @@ public class GpxTrackWriter implements TrackFormatWriter {
// TODO: The gpx spec says waypoints should come *before* tracks // TODO: The gpx spec says waypoints should come *before* tracks
Location l = waypoint.getLocation(); Location l = waypoint.getLocation();
if (l != null) { if (l != null) {
pw.println("<wpt lat=\"" + l.getLatitude() + "\" lon=\"" pw.println("<wpt lat=\""
+ l.getLongitude() + "\">"); + Location.convert(l.getLatitude(), LATLONG_FORMAT) + "\" lon=\""
+ Location.convert(l.getLongitude(), LATLONG_FORMAT) + "\">");
pw.println("<ele>" + ELEVATION_FORMAT.format(l.getAltitude()) + "</ele>");
pw.println("<time>" + TIMESTAMP_FORMAT.format(l.getTime()) + "</time>");
pw.println("<name>" + StringUtils.stringAsCData(waypoint.getName()) pw.println("<name>" + StringUtils.stringAsCData(waypoint.getName())
+ "</name>"); + "</name>");
pw.println("<desc>" pw.println("<desc>"
+ StringUtils.stringAsCData(waypoint.getDescription()) + "</desc>"); + StringUtils.stringAsCData(waypoint.getDescription()) + "</desc>");
pw.println("<time>" + TIMESTAMP_FORMAT.format(l.getTime()) + "</time>");
pw.println("<ele>" + l.getAltitude() + "</ele>");
pw.println("</wpt>"); pw.println("</wpt>");
} }
} }
@@ -75,12 +75,17 @@ public class GpxTrackWriterTest extends TrackFormatWriterTest {
* Asserts that the given tag describes the given location. * Asserts that the given tag describes the given location.
*/ */
private void assertTagMatchesLocation(Element tag, Location loc) { private void assertTagMatchesLocation(Element tag, Location loc) {
assertEquals(Double.toString(loc.getLatitude()), tag.getAttribute("lat")); assertEquals(
assertEquals(Double.toString(loc.getLongitude()), tag.getAttribute("lon")); Location.convert(loc.getLatitude(), GpxTrackWriter.LATLONG_FORMAT),
tag.getAttribute("lat"));
assertEquals(
Location.convert(loc.getLongitude(), GpxTrackWriter.LATLONG_FORMAT),
tag.getAttribute("lon"));
assertEquals( assertEquals(
GpxTrackWriter.TIMESTAMP_FORMAT.format(new Date(loc.getTime())), GpxTrackWriter.TIMESTAMP_FORMAT.format(new Date(loc.getTime())),
getChildTextValue(tag, "time")); getChildTextValue(tag, "time"));
assertEquals(Double.toString(loc.getAltitude()), assertEquals(
GpxTrackWriter.ELEVATION_FORMAT.format(loc.getAltitude()),
getChildTextValue(tag, "ele")); getChildTextValue(tag, "ele"));
} }
} }
@@ -81,7 +81,7 @@ public abstract class TrackFormatWriterTest extends AndroidTestCase {
protected void populateLocations(MyTracksLocation... locs) { protected void populateLocations(MyTracksLocation... locs) {
for (int i = 0; i < locs.length; i++) { for (int i = 0; i < locs.length; i++) {
MyTracksLocation loc = locs[i]; MyTracksLocation loc = locs[i];
loc.setAltitude(i * 1000); loc.setAltitude(i * 5000000);
loc.setLatitude(i); loc.setLatitude(i);
loc.setLongitude(-i); loc.setLongitude(-i);
loc.setTime(10000000 + i * 1000); loc.setTime(10000000 + i * 1000);