2020-03-14 19:18:30 +01:00
|
|
|
package net.osmand.router;
|
|
|
|
|
|
|
|
import net.osmand.GPXUtilities;
|
|
|
|
import net.osmand.GPXUtilities.GPXExtensionsReader;
|
|
|
|
import net.osmand.GPXUtilities.GPXFile;
|
|
|
|
import net.osmand.GPXUtilities.WptPt;
|
|
|
|
import net.osmand.Location;
|
|
|
|
import net.osmand.PlatformUtil;
|
|
|
|
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion;
|
|
|
|
import net.osmand.binary.RouteDataBundle;
|
|
|
|
import net.osmand.binary.RouteDataObject;
|
|
|
|
import net.osmand.binary.StringBundle;
|
|
|
|
import net.osmand.binary.StringBundleReader;
|
|
|
|
import net.osmand.binary.StringBundleXmlReader;
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-03-19 10:54:39 +01:00
|
|
|
import static net.osmand.binary.RouteDataObject.HEIGHT_UNDEFINED;
|
|
|
|
|
2020-03-14 19:18:30 +01:00
|
|
|
public class RouteImporter {
|
|
|
|
|
|
|
|
public final static Log log = PlatformUtil.getLog(RouteImporter.class);
|
|
|
|
|
|
|
|
private File file;
|
2020-03-17 16:38:56 +01:00
|
|
|
private GPXFile gpxFile;
|
2020-03-14 19:18:30 +01:00
|
|
|
|
|
|
|
public RouteImporter(File file) {
|
|
|
|
this.file = file;
|
|
|
|
}
|
|
|
|
|
2020-03-17 16:38:56 +01:00
|
|
|
public RouteImporter(GPXFile gpxFile) {
|
|
|
|
this.gpxFile = gpxFile;
|
|
|
|
}
|
|
|
|
|
2020-03-14 19:18:30 +01:00
|
|
|
public List<RouteSegmentResult> importRoute() {
|
|
|
|
|
|
|
|
final List<RouteSegmentResult> route = new ArrayList<>();
|
|
|
|
final RouteRegion region = new RouteRegion();
|
|
|
|
final RouteDataResources resources = new RouteDataResources();
|
|
|
|
|
|
|
|
GPXExtensionsReader extensionsReader = new GPXExtensionsReader() {
|
|
|
|
@Override
|
2020-03-17 16:38:56 +01:00
|
|
|
public boolean readExtensions(GPXFile res, XmlPullParser parser) throws Exception {
|
2020-03-14 19:18:30 +01:00
|
|
|
if (!resources.hasLocations()) {
|
|
|
|
List<Location> locations = resources.getLocations();
|
2020-03-19 10:54:39 +01:00
|
|
|
double lastElevation = HEIGHT_UNDEFINED;
|
2020-03-14 19:18:30 +01:00
|
|
|
if (res.tracks.size() > 0 && res.tracks.get(0).segments.size() > 0 && res.tracks.get(0).segments.get(0).points.size() > 0) {
|
|
|
|
for (WptPt point : res.tracks.get(0).segments.get(0).points) {
|
2020-03-17 20:26:52 +01:00
|
|
|
Location loc = new Location("", point.getLatitude(), point.getLongitude());
|
|
|
|
if (!Double.isNaN(point.ele)) {
|
|
|
|
loc.setAltitude(point.ele);
|
2020-03-19 10:54:39 +01:00
|
|
|
lastElevation = point.ele;
|
|
|
|
} else if (lastElevation != HEIGHT_UNDEFINED) {
|
|
|
|
loc.setAltitude(lastElevation);
|
2020-03-17 20:26:52 +01:00
|
|
|
}
|
|
|
|
locations.add(loc);
|
2020-03-14 19:18:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String tag = parser.getName();
|
|
|
|
if ("route".equals(tag)) {
|
|
|
|
int tok;
|
|
|
|
while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
|
|
|
if (tok == XmlPullParser.START_TAG) {
|
|
|
|
tag = parser.getName();
|
|
|
|
if ("segment".equals(tag)) {
|
|
|
|
StringBundleReader bundleReader = new StringBundleXmlReader(parser);
|
|
|
|
RouteDataObject object = new RouteDataObject(region);
|
|
|
|
RouteSegmentResult segment = new RouteSegmentResult(object);
|
|
|
|
bundleReader.readBundle();
|
|
|
|
segment.readFromBundle(new RouteDataBundle(resources, bundleReader.getBundle()));
|
|
|
|
route.add(segment);
|
|
|
|
}
|
|
|
|
} else if (tok == XmlPullParser.END_TAG) {
|
|
|
|
tag = parser.getName();
|
|
|
|
if ("route".equals(tag)) {
|
2020-03-17 16:38:56 +01:00
|
|
|
return true;
|
2020-03-14 19:18:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ("types".equals(tag)) {
|
|
|
|
int tok;
|
|
|
|
int i = 0;
|
|
|
|
while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
|
|
|
if (tok == XmlPullParser.START_TAG) {
|
|
|
|
tag = parser.getName();
|
|
|
|
if ("type".equals(tag)) {
|
|
|
|
StringBundleReader bundleReader = new StringBundleXmlReader(parser);
|
|
|
|
bundleReader.readBundle();
|
|
|
|
StringBundle bundle = bundleReader.getBundle();
|
|
|
|
String t = bundle.getString("t", null);
|
|
|
|
String v = bundle.getString("v", null);
|
|
|
|
region.initRouteEncodingRule(i++, t, v);
|
|
|
|
}
|
|
|
|
} else if (tok == XmlPullParser.END_TAG) {
|
|
|
|
tag = parser.getName();
|
|
|
|
if ("types".equals(tag)) {
|
2020-03-17 16:38:56 +01:00
|
|
|
return true;
|
2020-03-14 19:18:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 16:38:56 +01:00
|
|
|
return false;
|
2020-03-14 19:18:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-17 16:38:56 +01:00
|
|
|
if (gpxFile != null) {
|
|
|
|
GPXUtilities.loadGPXFile(null, gpxFile, extensionsReader);
|
2020-03-14 19:18:30 +01:00
|
|
|
for (RouteSegmentResult segment : route) {
|
2020-03-19 19:43:19 +01:00
|
|
|
segment.fillNames(resources);
|
2020-03-14 19:18:30 +01:00
|
|
|
}
|
2020-03-17 16:38:56 +01:00
|
|
|
} else if (file != null) {
|
|
|
|
FileInputStream fis = null;
|
2020-03-14 19:18:30 +01:00
|
|
|
try {
|
2020-03-17 16:38:56 +01:00
|
|
|
fis = new FileInputStream(file);
|
|
|
|
GPXFile gpxFile = GPXUtilities.loadGPXFile(fis, null, extensionsReader);
|
|
|
|
for (RouteSegmentResult segment : route) {
|
2020-03-19 19:43:19 +01:00
|
|
|
segment.fillNames(resources);
|
2020-03-17 16:38:56 +01:00
|
|
|
}
|
|
|
|
gpxFile.path = file.getAbsolutePath();
|
|
|
|
gpxFile.modifiedTime = file.lastModified();
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("Error importing route " + file.getAbsolutePath(), e);
|
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (fis != null) {
|
|
|
|
fis.close();
|
|
|
|
}
|
|
|
|
} catch (IOException ignore) {
|
|
|
|
// ignore
|
2020-03-14 19:18:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
}
|