From 8bd5818a81a8f196fbf2e1a8c195432a3bea3914 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Wed, 10 Jul 2013 11:16:56 -0700 Subject: [PATCH] Support importing gpx and kml without altitude value. --- .../mytracks/io/file/AbstractImporter.java | 35 +++++++++++-------- .../apps/mytracks/io/file/GpxImporter.java | 6 ++-- .../apps/mytracks/io/file/KmlImporter.java | 11 +++--- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/AbstractImporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/AbstractImporter.java index 2f6e832bd..86901a1d3 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/AbstractImporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/AbstractImporter.java @@ -96,8 +96,6 @@ abstract class AbstractImporter extends DefaultHandler { int numBufferedLocations = 0; } - protected static final String DEFAULT_ALTITUDE = "0"; - // The maximum number of buffered locations for bulk-insertion private static final int MAX_BUFFERED_LOCATIONS = 512; @@ -465,24 +463,29 @@ abstract class AbstractImporter extends DefaultHandler { * Creates a location. */ private Location createLocation() throws SAXException { - if (latitude == null || longitude == null || altitude == null) { + if (latitude == null || longitude == null) { return null; } - double longitudeValue; double latitudeValue; - double altitudeValue; - + double longitudeValue; try { - longitudeValue = Double.parseDouble(longitude); latitudeValue = Double.parseDouble(latitude); - altitudeValue = Double.parseDouble(altitude); + longitudeValue = Double.parseDouble(longitude); } catch (NumberFormatException e) { - throw new SAXException(createErrorMessage(String.format( - "Unable to parse longitude latitude altitude: %s %s %s", longitude, latitude, altitude)), - e); + throw new SAXException(createErrorMessage( + String.format("Unable to parse latitude longitude: %s %s", latitude, longitude)), e); } + Double altitudeValue = null; + if (altitude != null) { + try { + altitudeValue = Double.parseDouble(altitude); + } catch (NumberFormatException e) { + throw new SAXException( + createErrorMessage(String.format("Unable to parse altitude: %s", altitude)), e); + } + } + long timeValue; - if (time == null) { timeValue = trackData.importTime; } else { @@ -505,11 +508,15 @@ abstract class AbstractImporter extends DefaultHandler { * @param timeValue the time value */ private Location createLocation( - double latitudeValue, double longitudeValue, double altitudeValue, long timeValue) { + double latitudeValue, double longitudeValue, Double altitudeValue, long timeValue) { Location location = new Location(LocationManager.GPS_PROVIDER); location.setLatitude(latitudeValue); location.setLongitude(longitudeValue); - location.setAltitude(altitudeValue); + if (altitudeValue != null) { + location.setAltitude(altitudeValue); + } else { + location.removeAltitude(); + } location.setTime(timeValue); location.removeAccuracy(); location.removeBearing(); diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxImporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxImporter.java index 3f6acc670..d4b97697b 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxImporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxImporter.java @@ -47,7 +47,7 @@ public class GpxImporter extends AbstractImporter { private static final String ATTRIBUTE_LAT = "lat"; private static final String ATTRIBUTE_LON = "lon"; - + /** * Constructor. * @@ -133,7 +133,7 @@ public class GpxImporter extends AbstractImporter { private void onTrackPointStart(Attributes attributes) { latitude = attributes.getValue(ATTRIBUTE_LAT); longitude = attributes.getValue(ATTRIBUTE_LON); - altitude = DEFAULT_ALTITUDE; + altitude = null; time = null; } @@ -159,7 +159,7 @@ public class GpxImporter extends AbstractImporter { category = null; latitude = attributes.getValue(ATTRIBUTE_LAT); longitude = attributes.getValue(ATTRIBUTE_LON); - altitude = DEFAULT_ALTITUDE; + altitude = null; time = null; waypointType = null; } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlImporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlImporter.java index 59dd5b2e4..a15c8800e 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlImporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlImporter.java @@ -148,7 +148,7 @@ public class KmlImporter extends AbstractImporter { category = null; latitude = null; longitude = null; - altitude = DEFAULT_ALTITUDE; + altitude = null; time = null; waypointType = null; } @@ -181,9 +181,7 @@ public class KmlImporter extends AbstractImporter { } longitude = parts[0]; latitude = parts[1]; - if (parts.length == 3) { - altitude = parts[2]; - } + altitude = parts.length == 3 ? parts[2] : null; } } @@ -240,12 +238,13 @@ public class KmlImporter extends AbstractImporter { return; } String parts[] = content.trim().split(" "); - if (parts.length != 3) { + if (parts.length != 2 && parts.length != 3) { return; } longitude = parts[0]; latitude = parts[1]; - altitude = parts[2]; + altitude = parts.length == 3 ? parts[2] : null; + Location location = getTrackPoint(); if (location == null) { return;