From 0bbeff0b5b264e5741e7eb1d9955b4db0686d844 Mon Sep 17 00:00:00 2001 From: Matthew Simmons Date: Sun, 27 Feb 2011 20:40:14 -0500 Subject: [PATCH] Use locale-insensitive formatting for GPX files --- .../apps/mytracks/io/GpxTrackWriter.java | 50 ++++++++++++------- .../apps/mytracks/io/GpxTrackWriterTest.java | 5 -- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/GpxTrackWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/GpxTrackWriter.java index 65636ce54..86c27bf9e 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/GpxTrackWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/GpxTrackWriter.java @@ -1,12 +1,12 @@ /* * Copyright 2008 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 @@ -25,9 +25,10 @@ import android.location.Location; import java.io.OutputStream; import java.io.PrintWriter; import java.nio.charset.Charset; +import java.text.NumberFormat; import java.text.SimpleDateFormat; -import java.text.DecimalFormat; import java.util.Date; +import java.util.Locale; import java.util.TimeZone; /** @@ -36,22 +37,33 @@ import java.util.TimeZone; * @author Sandor Dornbush */ public class GpxTrackWriter implements TrackFormatWriter { + private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; - private static final int LATLONG_FORMAT = Location.FORMAT_DEGREES; - private static final DecimalFormat ELEVATION_FORMATTER = - new DecimalFormat("#.#"); - private static final SimpleDateFormat TIMESTAMP_FORMATTER = - new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - static { - TIMESTAMP_FORMATTER.setTimeZone(TimeZone.getTimeZone("UTC")); - } - + private final NumberFormat elevationFormatter; + private final NumberFormat coordinateFormatter; + private final SimpleDateFormat timestampFormatter; private PrintWriter pw = null; private Track track; + public GpxTrackWriter() { + // GPX readers expect to see fractional numbers with US-style punctuation. + // That is, they want periods for decimal points, rather than commas. + elevationFormatter = NumberFormat.getInstance(Locale.US); + elevationFormatter.setMaximumFractionDigits(1); + elevationFormatter.setGroupingUsed(false); + + coordinateFormatter = NumberFormat.getInstance(Locale.US); + coordinateFormatter.setMaximumFractionDigits(5); + coordinateFormatter.setMaximumIntegerDigits(3); + coordinateFormatter.setGroupingUsed(false); + + timestampFormatter = new SimpleDateFormat(TIMESTAMP_FORMAT); + timestampFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); + } + private String formatLocation(Location l) { - return "lat=\"" + Location.convert(l.getLatitude(), LATLONG_FORMAT) - + "\" lon=\"" + Location.convert(l.getLongitude(), LATLONG_FORMAT) + "\""; + return "lat=\"" + coordinateFormatter.format(l.getLatitude()) + + "\" lon=\"" + coordinateFormatter.format(l.getLongitude()) + "\""; } @Override @@ -129,8 +141,8 @@ public class GpxTrackWriter implements TrackFormatWriter { if (pw != null) { pw.println(""); Date d = new Date(l.getTime()); - pw.println("" + ELEVATION_FORMATTER.format(l.getAltitude()) + ""); - pw.println(""); + pw.println("" + elevationFormatter.format(l.getAltitude()) + ""); + pw.println(""); pw.println(""); } } @@ -149,8 +161,8 @@ public class GpxTrackWriter implements TrackFormatWriter { Location l = waypoint.getLocation(); if (l != null) { pw.println(""); - pw.println("" + ELEVATION_FORMATTER.format(l.getAltitude()) + ""); - pw.println(""); + pw.println("" + elevationFormatter.format(l.getAltitude()) + ""); + pw.println(""); pw.println("" + StringUtils.stringAsCData(waypoint.getName()) + ""); pw.println("" diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxTrackWriterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxTrackWriterTest.java index c9c3e7f42..9601875cb 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxTrackWriterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/GpxTrackWriterTest.java @@ -1,14 +1,9 @@ // Copyright 2010 Google Inc. All Rights Reserved. package com.google.android.apps.mytracks.io; -import com.google.android.apps.mytracks.content.Waypoint; - -import android.location.Location; - import org.w3c.dom.Document; import org.w3c.dom.Element; -import java.util.Date; import java.util.List; /**