Support importing gpx and kml without altitude value.

This commit is contained in:
Jimmy Shih
2013-07-10 11:16:56 -07:00
parent 5a4860a45a
commit 8bd5818a81
3 changed files with 29 additions and 23 deletions
@@ -96,8 +96,6 @@ abstract class AbstractImporter extends DefaultHandler {
int numBufferedLocations = 0; int numBufferedLocations = 0;
} }
protected static final String DEFAULT_ALTITUDE = "0";
// The maximum number of buffered locations for bulk-insertion // The maximum number of buffered locations for bulk-insertion
private static final int MAX_BUFFERED_LOCATIONS = 512; private static final int MAX_BUFFERED_LOCATIONS = 512;
@@ -465,24 +463,29 @@ abstract class AbstractImporter extends DefaultHandler {
* Creates a location. * Creates a location.
*/ */
private Location createLocation() throws SAXException { private Location createLocation() throws SAXException {
if (latitude == null || longitude == null || altitude == null) { if (latitude == null || longitude == null) {
return null; return null;
} }
double longitudeValue;
double latitudeValue; double latitudeValue;
double altitudeValue; double longitudeValue;
try { try {
longitudeValue = Double.parseDouble(longitude);
latitudeValue = Double.parseDouble(latitude); latitudeValue = Double.parseDouble(latitude);
altitudeValue = Double.parseDouble(altitude); longitudeValue = Double.parseDouble(longitude);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new SAXException(createErrorMessage(String.format( throw new SAXException(createErrorMessage(
"Unable to parse longitude latitude altitude: %s %s %s", longitude, latitude, altitude)), String.format("Unable to parse latitude longitude: %s %s", latitude, longitude)), e);
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;
long timeValue;
if (time == null) { if (time == null) {
timeValue = trackData.importTime; timeValue = trackData.importTime;
} else { } else {
@@ -505,11 +508,15 @@ abstract class AbstractImporter extends DefaultHandler {
* @param timeValue the time value * @param timeValue the time value
*/ */
private Location createLocation( 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 location = new Location(LocationManager.GPS_PROVIDER);
location.setLatitude(latitudeValue); location.setLatitude(latitudeValue);
location.setLongitude(longitudeValue); location.setLongitude(longitudeValue);
location.setAltitude(altitudeValue); if (altitudeValue != null) {
location.setAltitude(altitudeValue);
} else {
location.removeAltitude();
}
location.setTime(timeValue); location.setTime(timeValue);
location.removeAccuracy(); location.removeAccuracy();
location.removeBearing(); location.removeBearing();
@@ -133,7 +133,7 @@ public class GpxImporter extends AbstractImporter {
private void onTrackPointStart(Attributes attributes) { private void onTrackPointStart(Attributes attributes) {
latitude = attributes.getValue(ATTRIBUTE_LAT); latitude = attributes.getValue(ATTRIBUTE_LAT);
longitude = attributes.getValue(ATTRIBUTE_LON); longitude = attributes.getValue(ATTRIBUTE_LON);
altitude = DEFAULT_ALTITUDE; altitude = null;
time = null; time = null;
} }
@@ -159,7 +159,7 @@ public class GpxImporter extends AbstractImporter {
category = null; category = null;
latitude = attributes.getValue(ATTRIBUTE_LAT); latitude = attributes.getValue(ATTRIBUTE_LAT);
longitude = attributes.getValue(ATTRIBUTE_LON); longitude = attributes.getValue(ATTRIBUTE_LON);
altitude = DEFAULT_ALTITUDE; altitude = null;
time = null; time = null;
waypointType = null; waypointType = null;
} }
@@ -148,7 +148,7 @@ public class KmlImporter extends AbstractImporter {
category = null; category = null;
latitude = null; latitude = null;
longitude = null; longitude = null;
altitude = DEFAULT_ALTITUDE; altitude = null;
time = null; time = null;
waypointType = null; waypointType = null;
} }
@@ -181,9 +181,7 @@ public class KmlImporter extends AbstractImporter {
} }
longitude = parts[0]; longitude = parts[0];
latitude = parts[1]; latitude = parts[1];
if (parts.length == 3) { altitude = parts.length == 3 ? parts[2] : null;
altitude = parts[2];
}
} }
} }
@@ -240,12 +238,13 @@ public class KmlImporter extends AbstractImporter {
return; return;
} }
String parts[] = content.trim().split(" "); String parts[] = content.trim().split(" ");
if (parts.length != 3) { if (parts.length != 2 && parts.length != 3) {
return; return;
} }
longitude = parts[0]; longitude = parts[0];
latitude = parts[1]; latitude = parts[1];
altitude = parts[2]; altitude = parts.length == 3 ? parts[2] : null;
Location location = getTrackPoint(); Location location = getTrackPoint();
if (location == null) { if (location == null) {
return; return;