Cleanup GpxTrackWriter

- remove xml-stylesheet (not used)
- remove the "<number>" tag (do not want to expose internally used track id)
- add <metadata> for name and description
- change the text for the "creator" attribute
- change KmlTrackWriter to use FileUtils.FILE_TIMESTAMP_FORMAT
This commit is contained in:
Jimmy Shih
2012-02-23 18:46:54 -08:00
parent 6087c862d8
commit 81122b59ad
7 changed files with 132 additions and 129 deletions
@@ -20,51 +20,41 @@ import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat; import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat;
import com.google.android.apps.mytracks.util.FileUtils; import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.apps.mytracks.util.StringUtils; import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.location.Location; import android.location.Location;
import android.os.Build;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale; import java.util.Locale;
/** /**
* Log of one track. * Write track as GPX to a file.
* *
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class GpxTrackWriter implements TrackFormatWriter { public class GpxTrackWriter implements TrackFormatWriter {
private final NumberFormat elevationFormatter; private static final NumberFormat ELEVATION_FORMAT = NumberFormat.getInstance(Locale.US);
private final NumberFormat coordinateFormatter; private static final NumberFormat COORDINATE_FORMAT = NumberFormat.getInstance(Locale.US);
private PrintWriter pw = null; static {
private Track track;
public GpxTrackWriter() {
// GPX readers expect to see fractional numbers with US-style punctuation. // GPX readers expect to see fractional numbers with US-style punctuation.
// That is, they want periods for decimal points, rather than commas. // That is, they want periods for decimal points, rather than commas.
elevationFormatter = NumberFormat.getInstance(Locale.US); ELEVATION_FORMAT.setMaximumFractionDigits(1);
elevationFormatter.setMaximumFractionDigits(1); ELEVATION_FORMAT.setGroupingUsed(false);
elevationFormatter.setGroupingUsed(false);
coordinateFormatter = NumberFormat.getInstance(Locale.US); COORDINATE_FORMAT.setMaximumFractionDigits(5);
coordinateFormatter.setMaximumFractionDigits(5); COORDINATE_FORMAT.setMaximumIntegerDigits(3);
coordinateFormatter.setMaximumIntegerDigits(3); COORDINATE_FORMAT.setGroupingUsed(false);
coordinateFormatter.setGroupingUsed(false);
} }
private String formatLocation(Location l) { private final Context context;
return "lat=\"" + coordinateFormatter.format(l.getLatitude()) private Track track;
+ "\" lon=\"" + coordinateFormatter.format(l.getLongitude()) + "\""; private PrintWriter printWriter;
}
@SuppressWarnings("hiding") public GpxTrackWriter(Context context) {
@Override this.context = context;
public void prepare(Track track, OutputStream out) {
this.track = track;
this.pw = new PrintWriter(out);
} }
@Override @Override
@@ -72,81 +62,85 @@ public class GpxTrackWriter implements TrackFormatWriter {
return TrackFileFormat.GPX.getExtension(); return TrackFileFormat.GPX.getExtension();
} }
@Override
public void prepare(Track aTrack, OutputStream outputStream) {
this.track = aTrack;
this.printWriter = new PrintWriter(outputStream);
}
@Override
public void close() {
if (printWriter != null) {
printWriter.close();
printWriter = null;
}
}
@Override @Override
public void writeHeader() { public void writeHeader() {
if (pw != null) { if (printWriter != null) {
pw.format("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>\n", printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
Charset.defaultCharset().name()); printWriter.println("<gpx");
pw.println("<?xml-stylesheet type=\"text/xsl\" href=\"details.xsl\"?>"); printWriter.println("version=\"1.1\"");
pw.println("<gpx"); printWriter.println(
pw.println(" version=\"1.1\""); "creator=\"" + context.getString(R.string.send_google_by_my_tracks, "", "") + "\"");
pw.format(" creator=\"My Tracks running on %s\"\n", Build.MODEL); printWriter.println("xmlns=\"http://www.topografix.com/GPX/1/1\"");
pw.println(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); printWriter.println(
pw.println(" xmlns=\"http://www.topografix.com/GPX/1/1\""); "xmlns:topografix=\"http://www.topografix.com/GPX/Private/TopoGrafix/0/1\"");
pw.print(" xmlns:topografix=\"http://www.topografix.com/GPX/Private/" printWriter.println("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
+ "TopoGrafix/0/1\""); printWriter.println("xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1"
pw.print(" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 "); + " http://www.topografix.com/GPX/1/1/gpx.xsd"
pw.print("http://www.topografix.com/GPX/1/1/gpx.xsd "); + " http://www.topografix.com/GPX/Private/TopoGrafix/0/1"
pw.print("http://www.topografix.com/GPX/Private/TopoGrafix/0/1 "); + " http://www.topografix.com/GPX/Private/TopoGrafix/0/1/topografix.xsd\">");
pw.println("http://www.topografix.com/GPX/Private/TopoGrafix/0/1/" printWriter.println("<metadata>");
+ "topografix.xsd\">"); printWriter.println("<name>" + StringUtils.stringAsCData(track.getName()) + "</name>");
// TODO: Author etc. printWriter.println("<desc>" + StringUtils.stringAsCData(track.getDescription()) + "</desc>");
printWriter.println("</metadata>");
} }
} }
@Override @Override
public void writeFooter() { public void writeFooter() {
if (pw != null) { if (printWriter != null) {
pw.println("</gpx>"); printWriter.println("</gpx>");
} }
} }
@Override @Override
public void writeBeginTrack(Location firstPoint) { public void writeBeginTrack(Location firstLocation) {
if (pw != null) { if (printWriter != null) {
pw.println("<trk>"); printWriter.println("<trk>");
pw.println("<name>" + StringUtils.stringAsCData(track.getName()) printWriter.println("<name>" + StringUtils.stringAsCData(track.getName()) + "</name>");
+ "</name>"); printWriter.println("<desc>" + StringUtils.stringAsCData(track.getDescription()) + "</desc>");
pw.println("<desc>" + StringUtils.stringAsCData(track.getDescription()) printWriter.println("<extensions><topografix:color>c0c0c0</topografix:color></extensions>");
+ "</desc>");
pw.println("<number>" + track.getId() + "</number>");
pw.println("<extensions><topografix:color>c0c0c0</topografix:color></extensions>");
} }
} }
@Override @Override
public void writeEndTrack(Location lastPoint) { public void writeEndTrack(Location lastLocation) {
if (pw != null) { if (printWriter != null) {
pw.println("</trk>"); printWriter.println("</trk>");
} }
} }
@Override @Override
public void writeOpenSegment() { public void writeOpenSegment() {
pw.println("<trkseg>"); printWriter.println("<trkseg>");
} }
@Override @Override
public void writeCloseSegment() { public void writeCloseSegment() {
pw.println("</trkseg>"); printWriter.println("</trkseg>");
} }
@Override @Override
public void writeLocation(Location l) { public void writeLocation(Location location) {
if (pw != null) { if (printWriter != null) {
pw.println("<trkpt " + formatLocation(l) + ">"); printWriter.println("<trkpt " + formatLocation(location) + ">");
Date d = new Date(l.getTime()); printWriter.println("<ele>" + ELEVATION_FORMAT.format(location.getAltitude()) + "</ele>");
pw.println("<ele>" + elevationFormatter.format(l.getAltitude()) + "</ele>"); printWriter.println(
pw.println("<time>" + FileUtils.FILE_TIMESTAMP_FORMAT.format(d) + "</time>"); "<time>" + FileUtils.FILE_TIMESTAMP_FORMAT.format(location.getTime()) + "</time>");
pw.println("</trkpt>"); printWriter.println("</trkpt>");
}
}
@Override
public void close() {
if (pw != null) {
pw.close();
pw = null;
} }
} }
@@ -162,18 +156,28 @@ public class GpxTrackWriter implements TrackFormatWriter {
@Override @Override
public void writeWaypoint(Waypoint waypoint) { public void writeWaypoint(Waypoint waypoint) {
if (pw != null) { if (printWriter != null) {
Location l = waypoint.getLocation(); Location location = waypoint.getLocation();
if (l != null) { if (location != null) {
pw.println("<wpt " + formatLocation(l) + ">"); printWriter.println("<wpt " + formatLocation(location) + ">");
pw.println("<ele>" + elevationFormatter.format(l.getAltitude()) + "</ele>"); printWriter.println("<ele>" + ELEVATION_FORMAT.format(location.getAltitude()) + "</ele>");
pw.println("<time>" + FileUtils.FILE_TIMESTAMP_FORMAT.format(l.getTime()) + "</time>"); printWriter.println(
pw.println("<name>" + StringUtils.stringAsCData(waypoint.getName()) "<time>" + FileUtils.FILE_TIMESTAMP_FORMAT.format(location.getTime()) + "</time>");
+ "</name>"); printWriter.println("<name>" + StringUtils.stringAsCData(waypoint.getName()) + "</name>");
pw.println("<desc>" printWriter.println(
+ StringUtils.stringAsCData(waypoint.getDescription()) + "</desc>"); "<desc>" + StringUtils.stringAsCData(waypoint.getDescription()) + "</desc>");
pw.println("</wpt>"); printWriter.println("</wpt>");
} }
} }
} }
/**
* Formats a location with latitude and longitude coordinates.
*
* @param location the location
*/
private String formatLocation(Location location) {
return "lat=\"" + COORDINATE_FORMAT.format(location.getLatitude()) + "\" lon=\""
+ COORDINATE_FORMAT.format(location.getLongitude()) + "\"";
}
} }
@@ -24,6 +24,7 @@ import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;
import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.Waypoint; import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat; import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat;
import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.apps.mytracks.util.StringUtils; import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
@@ -231,7 +232,8 @@ public class KmlTrackWriter implements TrackFormatWriter {
@Override @Override
public void writeLocation(Location location) { public void writeLocation(Location location) {
if (printWriter != null) { if (printWriter != null) {
printWriter.println("<when>" + StringUtils.formatXmlDateTime(location.getTime()) + "</when>"); printWriter.println(
"<when>" + FileUtils.FILE_TIMESTAMP_FORMAT.format(location.getTime()) + "</when>");
printWriter.println( printWriter.println(
"<gx:coord>" + location.getLongitude() + " " + location.getLatitude() + " " "<gx:coord>" + location.getLongitude() + " " + location.getLatitude() + " "
+ location.getAltitude() + "</gx:coord>"); + location.getAltitude() + "</gx:coord>");
@@ -38,7 +38,7 @@ public class TrackWriterFactory {
GPX { GPX {
@Override @Override
TrackFormatWriter newFormatWriter(Context context) { TrackFormatWriter newFormatWriter(Context context) {
return new GpxTrackWriter(); return new GpxTrackWriter(context);
} }
}, },
KML { KML {
@@ -144,15 +144,6 @@ public class StringUtils {
BASE_XML_DATE_FORMAT.setTimeZone(new SimpleTimeZone(0, "UTC")); BASE_XML_DATE_FORMAT.setTimeZone(new SimpleTimeZone(0, "UTC"));
} }
/**
* Formats the time to the XML date time format.
*
* @param time time in milliseconds
*/
public static final String formatXmlDateTime(long time) {
return BASE_XML_DATE_FORMAT.format(new Date(time));
}
private static final Pattern XML_DATE_EXTRAS_PATTERN = private static final Pattern XML_DATE_EXTRAS_PATTERN =
Pattern.compile("^(\\.\\d+)?(?:Z|([+-])(\\d{2}):(\\d{2}))?$"); Pattern.compile("^(\\.\\d+)?(?:Z|([+-])(\\d{2}):(\\d{2}))?$");
@@ -1,23 +1,34 @@
// Copyright 2010 Google Inc. All Rights Reserved. /*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.io.file; package com.google.android.apps.mytracks.io.file;
import com.google.android.apps.mytracks.io.file.GpxTrackWriter; import java.util.List;
import com.google.android.apps.mytracks.io.file.TrackFormatWriter;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import java.util.List;
/** /**
* Tests for the GPX track exporter. * Tests for {@link GpxTrackWriter}.
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class GpxTrackWriterTest extends TrackFormatWriterTest { public class GpxTrackWriterTest extends TrackFormatWriterTest {
public void testXmlOutput() throws Exception { public void testXmlOutput() throws Exception {
TrackFormatWriter writer = new GpxTrackWriter(); TrackFormatWriter writer = new GpxTrackWriter(getContext());
String result = writeTrack(writer); String result = writeTrack(writer);
Document doc = parseXmlDocument(result); Document doc = parseXmlDocument(result);
@@ -25,44 +36,41 @@ public class GpxTrackWriterTest extends TrackFormatWriterTest {
Element trackTag = getChildElement(gpxTag, "trk"); Element trackTag = getChildElement(gpxTag, "trk");
assertEquals(TRACK_NAME, getChildTextValue(trackTag, "name")); assertEquals(TRACK_NAME, getChildTextValue(trackTag, "name"));
assertEquals(TRACK_DESCRIPTION, getChildTextValue(trackTag, "desc")); assertEquals(TRACK_DESCRIPTION, getChildTextValue(trackTag, "desc"));
assertEquals(Long.toString(TRACK_ID),
getChildTextValue(trackTag, "number"));
List<Element> segmentTags = getChildElements(trackTag, "trkseg", 2); List<Element> segmentTags = getChildElements(trackTag, "trkseg", 2);
List<Element> segPointTags = getChildElements(segmentTags.get(0), "trkpt", 2); List<Element> segPointTags = getChildElements(segmentTags.get(0), "trkpt", 2);
assertTagMatchesLocation(segPointTags.get(0), assertTagMatchesLocation(segPointTags.get(0), "0", "0", "1970-01-01T02:46:40Z", "0");
"0", "0", "1970-01-01T02:46:40Z", "0"); assertTagMatchesLocation(segPointTags.get(1), "1", "-1", "1970-01-01T02:46:41Z", "5000000");
assertTagMatchesLocation(segPointTags.get(1),
"1", "-1", "1970-01-01T02:46:41Z", "5000000");
segPointTags = getChildElements(segmentTags.get(1), "trkpt", 2); segPointTags = getChildElements(segmentTags.get(1), "trkpt", 2);
assertTagMatchesLocation(segPointTags.get(0), assertTagMatchesLocation(segPointTags.get(0), "2", "-2", "1970-01-01T02:46:42Z", "10000000");
"2", "-2", "1970-01-01T02:46:42Z", "10000000"); assertTagMatchesLocation(segPointTags.get(1), "3", "-3", "1970-01-01T02:46:43Z", "15000000");
assertTagMatchesLocation(segPointTags.get(1),
"3", "-3", "1970-01-01T02:46:43Z", "15000000");
List<Element> waypointTags = getChildElements(gpxTag, "wpt", 2); List<Element> waypointTags = getChildElements(gpxTag, "wpt", 2);
Element wptTag = waypointTags.get(0); Element wptTag = waypointTags.get(0);
assertEquals(WAYPOINT1_NAME, getChildTextValue(wptTag, "name")); assertEquals(WAYPOINT1_NAME, getChildTextValue(wptTag, "name"));
assertEquals(WAYPOINT1_DESCRIPTION, getChildTextValue(wptTag, "desc")); assertEquals(WAYPOINT1_DESCRIPTION, getChildTextValue(wptTag, "desc"));
assertTagMatchesLocation(wptTag, assertTagMatchesLocation(wptTag, "1", "-1", "1970-01-01T02:46:41Z", "5000000");
"1", "-1", "1970-01-01T02:46:41Z", "5000000");
wptTag = waypointTags.get(1); wptTag = waypointTags.get(1);
assertEquals(WAYPOINT2_NAME, getChildTextValue(wptTag, "name")); assertEquals(WAYPOINT2_NAME, getChildTextValue(wptTag, "name"));
assertEquals(WAYPOINT2_DESCRIPTION, getChildTextValue(wptTag, "desc")); assertEquals(WAYPOINT2_DESCRIPTION, getChildTextValue(wptTag, "desc"));
assertTagMatchesLocation(wptTag, assertTagMatchesLocation(wptTag, "2", "-2", "1970-01-01T02:46:42Z", "10000000");
"2", "-2", "1970-01-01T02:46:42Z", "10000000");
} }
/** /**
* Asserts that the given tag describes the location given by the * Asserts that the given tag describes a location.
* Strings lat, lon, time, and ele. *
* @param tag the tag
* @param latitude the location's latitude
* @param longitude the location's longitude
* @param time the location's time
* @param elevation the location's elevation
*/ */
private void assertTagMatchesLocation(Element tag, String lat, private void assertTagMatchesLocation(
String lon, String time, String ele) { Element tag, String latitude, String longitude, String time, String elevation) {
assertEquals(lat, tag.getAttribute("lat")); assertEquals(latitude, tag.getAttribute("lat"));
assertEquals(lon, tag.getAttribute("lon")); assertEquals(longitude, tag.getAttribute("lon"));
assertEquals(time, getChildTextValue(tag, "time")); assertEquals(time, getChildTextValue(tag, "time"));
assertEquals(ele, getChildTextValue(tag, "ele")); assertEquals(elevation, getChildTextValue(tag, "ele"));
} }
} }
@@ -13,7 +13,6 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.google.android.apps.mytracks.io.file; package com.google.android.apps.mytracks.io.file;
import com.google.android.apps.mytracks.content.DescriptionGenerator; import com.google.android.apps.mytracks.content.DescriptionGenerator;
@@ -34,6 +33,7 @@ import org.w3c.dom.Element;
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class KmlTrackWriterTest extends TrackFormatWriterTest { public class KmlTrackWriterTest extends TrackFormatWriterTest {
private static final String FULL_TRACK_DESCRIPTION = "full track description"; private static final String FULL_TRACK_DESCRIPTION = "full track description";
/** /**
@@ -45,7 +45,6 @@ public abstract class TrackFormatWriterTest extends AndroidTestCase {
protected static final String WAYPOINT2_NAME = "point]]>2"; protected static final String WAYPOINT2_NAME = "point]]>2";
protected static final String WAYPOINT2_DESCRIPTION = "point 2]]>description"; protected static final String WAYPOINT2_DESCRIPTION = "point 2]]>description";
private static final int BUFFER_SIZE = 10240; private static final int BUFFER_SIZE = 10240;
protected static final long TRACK_ID = 12345L;
protected Track track; protected Track track;
protected MyTracksLocation location1, location2, location3, location4; protected MyTracksLocation location1, location2, location3, location4;
protected Waypoint wp1, wp2; protected Waypoint wp1, wp2;
@@ -55,7 +54,6 @@ public abstract class TrackFormatWriterTest extends AndroidTestCase {
super.setUp(); super.setUp();
track = new Track(); track = new Track();
track.setId(TRACK_ID);
track.setName(TRACK_NAME); track.setName(TRACK_NAME);
track.setDescription(TRACK_DESCRIPTION); track.setDescription(TRACK_DESCRIPTION);