Add compatibility to read GPX file generated by Canimap.

This commit is contained in:
chteufleur 2019-01-22 18:06:39 +01:00
parent 684f8f0d06
commit 11df4593ab
1 changed files with 75 additions and 27 deletions

View File

@ -26,6 +26,25 @@ public class ImportGpx extends Gpx {
private static final String TAG = Traces.class.getName(); private static final String TAG = Traces.class.getName();
private static final String BALISE_GPX = "gpx";
private static final String BALISE_WPT = "wpt";
private static final String BALISE_TRK = "trk";
private static final String BALISE_TRKSEQ = "trkseg";
private static final String BALISE_TRKPT = "trkpt";
private static final String BALISE_RTE = "rte";
private static final String BALISE_RTEPT = "rtept";
private static final String BALISE_NAME = "name";
private static final String BALISE_TIME = "time";
private static final String BALISE_EXTENSIONS = "extensions";
private static final String BALISE_EXTENSIONS_FOUND = "found";
private static final String BALISE_ATTR_POINT_LAT = "lat";
private static final String BALISE_ATTR_POINT_LON = "lon";
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String PATTERN_DATE = "([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2})Z";
private String traceName = ""; private String traceName = "";
public ImportGpx(File filePath) { public ImportGpx(File filePath) {
@ -53,16 +72,18 @@ public class ImportGpx extends Gpx {
private List<MyLocation> readGpx(XmlPullParser parser) throws XmlPullParserException, IOException { private List<MyLocation> readGpx(XmlPullParser parser) throws XmlPullParserException, IOException {
List<MyLocation> entries = new ArrayList<>(); List<MyLocation> entries = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, "gpx"); parser.require(XmlPullParser.START_TAG, null, BALISE_GPX);
while (parser.next() != XmlPullParser.END_TAG) { while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) {
continue; continue;
} }
String name = parser.getName(); String name = parser.getName();
if (name.equals("wpt")) { if (name.equals(BALISE_WPT)) {
entries.add(readWaypoint(parser)); entries.add(readWaypoint(parser));
} else if (name.equals("trk")) { } else if (name.equals(BALISE_TRK)) {
entries.addAll(readTrk(parser)); entries.addAll(readTrk(parser));
} else if (name.equals(BALISE_RTE)) {
entries.addAll(readRte(parser));
} else { } else {
skip(parser); skip(parser);
} }
@ -73,13 +94,13 @@ public class ImportGpx extends Gpx {
private List<MyLocation> readTrk(XmlPullParser parser) throws XmlPullParserException, IOException { private List<MyLocation> readTrk(XmlPullParser parser) throws XmlPullParserException, IOException {
List<MyLocation> entries = new ArrayList<>(); List<MyLocation> entries = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, "trk"); parser.require(XmlPullParser.START_TAG, null, BALISE_TRK);
while (parser.next() != XmlPullParser.END_TAG) { while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) {
continue; continue;
} }
String name = parser.getName(); String name = parser.getName();
if (name.equals("trkseg")) { if (name.equals(BALISE_TRKSEQ)) {
entries = readTrkseq(parser); entries = readTrkseq(parser);
} else { } else {
skip(parser); skip(parser);
@ -91,15 +112,15 @@ public class ImportGpx extends Gpx {
private List<MyLocation> readTrkseq(XmlPullParser parser) throws XmlPullParserException, IOException { private List<MyLocation> readTrkseq(XmlPullParser parser) throws XmlPullParserException, IOException {
List<MyLocation> entries = new ArrayList<>(); List<MyLocation> entries = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, "trkseg"); parser.require(XmlPullParser.START_TAG, null, BALISE_TRKSEQ);
while (parser.next() != XmlPullParser.END_TAG) { while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) {
continue; continue;
} }
String name = parser.getName(); String name = parser.getName();
if (name.equals("trkpt")) { if (name.equals(BALISE_TRKPT)) {
entries.add(readTrkpt(parser)); entries.add(readTrkpt(parser));
} else if (name.equals("name")) { } else if (name.equals(BALISE_NAME)) {
traceName = readName(parser); traceName = readName(parser);
} else { } else {
skip(parser); skip(parser);
@ -110,10 +131,37 @@ public class ImportGpx extends Gpx {
private MyLocation readTrkpt(XmlPullParser parser) throws XmlPullParserException, IOException { private MyLocation readTrkpt(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, null, "trkpt"); parser.require(XmlPullParser.START_TAG, null, BALISE_TRKPT);
return readPoint(parser);
}
String latStr = parser.getAttributeValue(null, "lat");
String lonStr = parser.getAttributeValue(null, "lon"); private List<MyLocation> readRte(XmlPullParser parser) throws XmlPullParserException, IOException {
List<MyLocation> entries = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, BALISE_RTE);
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals(BALISE_RTEPT)) {
entries.add(readRteptseq(parser));
} else {
skip(parser);
}
}
return entries;
}
private MyLocation readRteptseq(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, null, BALISE_RTEPT);
return readPoint(parser);
}
private MyLocation readPoint(XmlPullParser parser) throws XmlPullParserException, IOException {
String latStr = parser.getAttributeValue(null, BALISE_ATTR_POINT_LAT);
String lonStr = parser.getAttributeValue(null, BALISE_ATTR_POINT_LON);
double lat = Double.parseDouble(latStr); double lat = Double.parseDouble(latStr);
double lon = Double.parseDouble(lonStr); double lon = Double.parseDouble(lonStr);
long time = -1; long time = -1;
@ -123,7 +171,7 @@ public class ImportGpx extends Gpx {
continue; continue;
} }
String name = parser.getName(); String name = parser.getName();
if (name.equals("time")) { if (name.equals(BALISE_TIME)) {
time = readTime(parser); time = readTime(parser);
} else { } else {
skip(parser); skip(parser);
@ -134,10 +182,10 @@ public class ImportGpx extends Gpx {
private MyLocation readWaypoint(XmlPullParser parser) throws XmlPullParserException, IOException { private MyLocation readWaypoint(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, null, "wpt"); parser.require(XmlPullParser.START_TAG, null, BALISE_WPT);
String latStr = parser.getAttributeValue(null, "lat"); String latStr = parser.getAttributeValue(null, BALISE_ATTR_POINT_LAT);
String lonStr = parser.getAttributeValue(null, "lon"); String lonStr = parser.getAttributeValue(null, BALISE_ATTR_POINT_LON);
double lat = Double.parseDouble(latStr); double lat = Double.parseDouble(latStr);
double lon = Double.parseDouble(lonStr); double lon = Double.parseDouble(lonStr);
long time = -1; long time = -1;
@ -148,9 +196,9 @@ public class ImportGpx extends Gpx {
continue; continue;
} }
String name = parser.getName(); String name = parser.getName();
if (name.equals("time")) { if (name.equals(BALISE_TIME)) {
time = readTime(parser); time = readTime(parser);
} else if (name.equals("extensions")) { } else if (name.equals(BALISE_EXTENSIONS)) {
isFound = readExtensions(parser); isFound = readExtensions(parser);
} else { } else {
skip(parser); skip(parser);
@ -162,11 +210,11 @@ public class ImportGpx extends Gpx {
private long readTime(XmlPullParser parser) throws XmlPullParserException, IOException { private long readTime(XmlPullParser parser) throws XmlPullParserException, IOException {
long ret = 0; long ret = 0;
parser.require(XmlPullParser.START_TAG, null, "time"); parser.require(XmlPullParser.START_TAG, null, BALISE_TIME);
String timeStr = readText(parser); String timeStr = readText(parser);
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat formater = new SimpleDateFormat(DATE_FORMAT);
Pattern pattern = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2})Z"); Pattern pattern = Pattern.compile(PATTERN_DATE);
Matcher matcher = pattern.matcher(timeStr); Matcher matcher = pattern.matcher(timeStr);
while (matcher.find()) { while (matcher.find()) {
@ -179,20 +227,20 @@ public class ImportGpx extends Gpx {
} }
} }
} }
parser.require(XmlPullParser.END_TAG, null, "time"); parser.require(XmlPullParser.END_TAG, null, BALISE_TIME);
return ret; return ret;
} }
private boolean readExtensions(XmlPullParser parser) throws XmlPullParserException, IOException { private boolean readExtensions(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean ret = false; boolean ret = false;
parser.require(XmlPullParser.START_TAG, null, "extensions"); parser.require(XmlPullParser.START_TAG, null, BALISE_EXTENSIONS);
while (parser.next() != XmlPullParser.END_TAG) { while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) {
continue; continue;
} }
String name = parser.getName(); String name = parser.getName();
if (name.equals("found")) { if (name.equals(BALISE_EXTENSIONS_FOUND)) {
ret = readFound(parser); ret = readFound(parser);
} else { } else {
skip(parser); skip(parser);
@ -204,18 +252,18 @@ public class ImportGpx extends Gpx {
private boolean readFound(XmlPullParser parser) throws XmlPullParserException, IOException { private boolean readFound(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean ret; boolean ret;
parser.require(XmlPullParser.START_TAG, null, "found"); parser.require(XmlPullParser.START_TAG, null, BALISE_EXTENSIONS_FOUND);
ret = Boolean.parseBoolean(readText(parser)); ret = Boolean.parseBoolean(readText(parser));
parser.require(XmlPullParser.END_TAG, null, "found"); parser.require(XmlPullParser.END_TAG, null, BALISE_EXTENSIONS_FOUND);
return ret; return ret;
} }
private String readName(XmlPullParser parser) throws XmlPullParserException, IOException { private String readName(XmlPullParser parser) throws XmlPullParserException, IOException {
String ret; String ret;
parser.require(XmlPullParser.START_TAG, null, "name"); parser.require(XmlPullParser.START_TAG, null, BALISE_NAME);
ret = readText(parser); ret = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "name"); parser.require(XmlPullParser.END_TAG, null, BALISE_NAME);
return ret; return ret;
} }