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.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.TimeZone;
@@ -36,6 +37,9 @@ import java.util.TimeZone;
*/
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 =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
static {
@@ -118,10 +122,11 @@ public class GpxTrackWriter implements TrackFormatWriter {
@Override
public void writeLocation(Location l) {
if (pw != null) {
pw.println("<trkpt lat=\"" + l.getLatitude() + "\" lon=\""
+ l.getLongitude() + "\">");
pw.println("<trkpt lat=\""
+ Location.convert(l.getLatitude(), LATLONG_FORMAT) + "\" lon=\""
+ Location.convert(l.getLongitude(), LATLONG_FORMAT) + "\">");
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("</trkpt>");
}
@@ -141,14 +146,15 @@ public class GpxTrackWriter implements TrackFormatWriter {
// TODO: The gpx spec says waypoints should come *before* tracks
Location l = waypoint.getLocation();
if (l != null) {
pw.println("<wpt lat=\"" + l.getLatitude() + "\" lon=\""
+ l.getLongitude() + "\">");
pw.println("<wpt lat=\""
+ 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())
+ "</name>");
pw.println("<desc>"
+ StringUtils.stringAsCData(waypoint.getDescription()) + "</desc>");
pw.println("<time>" + TIMESTAMP_FORMAT.format(l.getTime()) + "</time>");
pw.println("<ele>" + l.getAltitude() + "</ele>");
pw.println("</wpt>");
}
}
@@ -75,12 +75,17 @@ public class GpxTrackWriterTest extends TrackFormatWriterTest {
* Asserts that the given tag describes the given location.
*/
private void assertTagMatchesLocation(Element tag, Location loc) {
assertEquals(Double.toString(loc.getLatitude()), tag.getAttribute("lat"));
assertEquals(Double.toString(loc.getLongitude()), tag.getAttribute("lon"));
assertEquals(
Location.convert(loc.getLatitude(), GpxTrackWriter.LATLONG_FORMAT),
tag.getAttribute("lat"));
assertEquals(
Location.convert(loc.getLongitude(), GpxTrackWriter.LATLONG_FORMAT),
tag.getAttribute("lon"));
assertEquals(
GpxTrackWriter.TIMESTAMP_FORMAT.format(new Date(loc.getTime())),
getChildTextValue(tag, "time"));
assertEquals(Double.toString(loc.getAltitude()),
assertEquals(
GpxTrackWriter.ELEVATION_FORMAT.format(loc.getAltitude()),
getChildTextValue(tag, "ele"));
}
}
@@ -81,7 +81,7 @@ public abstract class TrackFormatWriterTest extends AndroidTestCase {
protected void populateLocations(MyTracksLocation... locs) {
for (int i = 0; i < locs.length; i++) {
MyTracksLocation loc = locs[i];
loc.setAltitude(i * 1000);
loc.setAltitude(i * 5000000);
loc.setLatitude(i);
loc.setLongitude(-i);
loc.setTime(10000000 + i * 1000);