Gpx to route in progress

This commit is contained in:
max-klaus 2020-03-14 21:18:30 +03:00
parent 2bbb8b212d
commit 86f4cf45f7
14 changed files with 489 additions and 81 deletions

View file

@ -98,6 +98,10 @@ public class GPXUtilities {
public void writeExtensions(XmlSerializer serializer); public void writeExtensions(XmlSerializer serializer);
} }
public interface GPXExtensionsReader {
public void readExtensions(GPXFile res, XmlPullParser parser) throws Exception;
}
public static class GPXExtensions { public static class GPXExtensions {
Map<String, String> extensions = null; Map<String, String> extensions = null;
GPXExtensionsWriter extensionsWriter = null; GPXExtensionsWriter extensionsWriter = null;
@ -1799,6 +1803,10 @@ public class GPXUtilities {
} }
public static GPXFile loadGPXFile(InputStream f) { public static GPXFile loadGPXFile(InputStream f) {
return loadGPXFile(f, null);
}
public static GPXFile loadGPXFile(InputStream f, GPXExtensionsReader extensionsReader) {
GPXFile res = new GPXFile(null); GPXFile res = new GPXFile(null);
SimpleDateFormat format = new SimpleDateFormat(GPX_TIME_FORMAT, Locale.US); SimpleDateFormat format = new SimpleDateFormat(GPX_TIME_FORMAT, Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC")); format.setTimeZone(TimeZone.getTimeZone("UTC"));
@ -1828,6 +1836,13 @@ public class GPXUtilities {
} }
break; break;
case "route":
case "types":
if (extensionsReader != null) {
extensionsReader.readExtensions(res, parser);
}
break;
default: default:
Map<String, String> values = readTextMap(parser, tag); Map<String, String> values = readTextMap(parser, tag);
if (values.size() > 0) { if (values.size() > 0) {

View file

@ -67,14 +67,17 @@ public class BinaryMapRouteReaderAdapter {
public final static int TRAFFIC_SIGNALS = 6; public final static int TRAFFIC_SIGNALS = 6;
public final static int RAILWAY_CROSSING = 7; public final static int RAILWAY_CROSSING = 7;
private final static int LANES = 8; private final static int LANES = 8;
private final String t; private String t;
private final String v; private String v;
private int intValue; private int intValue;
private float floatValue; private float floatValue;
private int type; private int type;
private List<RouteTypeCondition> conditions = null; private List<RouteTypeCondition> conditions = null;
private int forward; private int forward;
public RouteTypeRule() {
}
public RouteTypeRule(String t, String v) { public RouteTypeRule(String t, String v) {
this.t = t.intern(); this.t = t.intern();
if ("true".equals(v)) { if ("true".equals(v)) {
@ -116,27 +119,21 @@ public class BinaryMapRouteReaderAdapter {
@Override @Override
public void writeToBundle(RouteDataBundle bundle) { public void writeToBundle(RouteDataBundle bundle) {
bundle.putString("t", t); bundle.putString("t", t);
if (v != null) {
bundle.putString("v", v); bundle.putString("v", v);
/*
if (intValue != 0) {
bundle.putInt("i", intValue);
} }
if (floatValue != 0) {
bundle.putFloat("f", floatValue);
}
if (type != 0) {
bundle.putInt("tp", type);
}
if (forward != 0) {
bundle.putInt("fw", forward);
}
bundle.putList("conditions", "c", conditions);
*/
} }
@Override @Override
public void readFromBundle(RouteDataBundle bundle) { public void readFromBundle(RouteDataBundle bundle) {
t = bundle.getString("t", null);
v = bundle.getString("v", null);
try {
analyze();
} catch(RuntimeException e) {
System.err.println("Error analyzing tag/value = " + t + "/" +v);
throw e;
}
} }
@Override @Override
@ -330,6 +327,14 @@ public class BinaryMapRouteReaderAdapter {
return -1; return -1;
} }
public int getNameTypeRule() {
return nameTypeRule;
}
public int getRefTypeRule() {
return refTypeRule;
}
public RouteTypeRule quickGetEncodingRule(int id) { public RouteTypeRule quickGetEncodingRule(int id) {
return routeEncodingRules.get(id); return routeEncodingRules.get(id);
} }

View file

@ -10,6 +10,11 @@ public class RouteDataBundle extends StringBundle {
this.resources = resources; this.resources = resources;
} }
public RouteDataBundle(RouteDataResources resources, StringBundle bundle) {
super(bundle.getMap());
this.resources = resources;
}
@Override @Override
public StringBundle newInstance() { public StringBundle newInstance() {
return new RouteDataBundle(resources); return new RouteDataBundle(resources);

View file

@ -1,5 +1,7 @@
package net.osmand.binary; package net.osmand.binary;
import net.osmand.util.Algorithms;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -19,6 +21,13 @@ public class StringBundle {
MAP MAP
} }
public StringBundle() {
}
protected StringBundle(Map<String, Item> map) {
this.map = map;
}
public StringBundle newInstance() { public StringBundle newInstance() {
return new StringBundle(); return new StringBundle();
} }
@ -70,8 +79,52 @@ public class StringBundle {
super(name, ItemType.STRING, String.valueOf(value)); super(name, ItemType.STRING, String.valueOf(value));
} }
public int asInt() throws NumberFormatException { private int asInt(int defaultValue) {
try {
return Integer.parseInt(getValue()); return Integer.parseInt(getValue());
} catch (NumberFormatException e) {
return defaultValue;
}
}
private long asLong(long defaultValue) {
try {
return Long.parseLong(getValue());
} catch (NumberFormatException e) {
return defaultValue;
}
}
private float asFloat(float defaultValue) {
try {
return Float.parseFloat(getValue());
} catch (NumberFormatException e) {
return defaultValue;
}
}
private boolean asBoolean(boolean defaultValue) {
try {
return Boolean.parseBoolean(getValue());
} catch (NumberFormatException e) {
return defaultValue;
}
}
private int[] asIntArray(int[] defaultValue) {
try {
return stringToIntArray(getValue());
} catch (NumberFormatException e) {
return defaultValue;
}
}
private int[][] asIntIntArray(int[][] defaultValue) {
try {
return stringToIntIntArray(getValue());
} catch (NumberFormatException e) {
return defaultValue;
}
} }
} }
@ -100,37 +153,57 @@ public class StringBundle {
return Collections.unmodifiableMap(map); return Collections.unmodifiableMap(map);
} }
public Item getItem(String key) {
return map.get(key);
}
public void putInt(String key, int value) { public void putInt(String key, int value) {
map.put(key, new StringItem(key, value)); map.put(key, new StringItem(key, value));
} }
public int getInt(String key) throws IllegalArgumentException { public int getInt(String key, int defaultValue) {
Item item = map.get(key); Item item = map.get(key);
if (item instanceof StringItem) { return item instanceof StringItem ? ((StringItem) item).asInt(defaultValue) : defaultValue;
return ((StringItem) item).asInt();
} else {
throw new IllegalArgumentException("The item is " + item.getClass().getSimpleName() + " but must be StringItem");
}
} }
public void putLong(String key, long value) { public void putLong(String key, long value) {
map.put(key, new StringItem(key, value)); map.put(key, new StringItem(key, value));
} }
public long getLong(String key, long defaultValue) {
Item item = map.get(key);
return item instanceof StringItem ? ((StringItem) item).asLong(defaultValue) : defaultValue;
}
public void putFloat(String key, float value) { public void putFloat(String key, float value) {
map.put(key, new StringItem(key, value)); map.put(key, new StringItem(key, value));
} }
public float getFloat(String key, float defaultValue) {
Item item = map.get(key);
return item instanceof StringItem ? ((StringItem) item).asFloat(defaultValue) : defaultValue;
}
public void putBoolean(String key, boolean value) { public void putBoolean(String key, boolean value) {
map.put(key, new StringItem(key, value)); map.put(key, new StringItem(key, value));
} }
public boolean getBoolean(String key, boolean defaultValue) {
Item item = map.get(key);
return item instanceof StringItem ? ((StringItem) item).asBoolean(defaultValue) : defaultValue;
}
public void putString(String key, String value) { public void putString(String key, String value) {
if (value != null) { if (value != null) {
map.put(key, new StringItem(key, value)); map.put(key, new StringItem(key, value));
} }
} }
public String getString(String key, String defaultValue) {
Item item = map.get(key);
return item instanceof StringItem ? ((StringItem) item).getValue() : defaultValue;
}
public void putObject(String key, StringExternalizable object) { public void putObject(String key, StringExternalizable object) {
if (object != null) { if (object != null) {
StringBundle bundle = newInstance(); StringBundle bundle = newInstance();
@ -173,12 +246,22 @@ public class StringBundle {
} }
} }
public int[] getIntArray(String key, int[] defaultValue) {
Item item = map.get(key);
return item instanceof StringItem ? ((StringItem) item).asIntArray(defaultValue) : defaultValue;
}
public void putArray(String key, int[][] array) { public void putArray(String key, int[][] array) {
if (array != null) { if (array != null) {
map.put(key, new StringItem(key, intIntArrayToString(array))); map.put(key, new StringItem(key, intIntArrayToString(array)));
} }
} }
public int[][] getIntIntArray(String key, int[][] defaultValue) {
Item item = map.get(key);
return item instanceof StringItem ? ((StringItem) item).asIntIntArray(defaultValue) : defaultValue;
}
public void putArray(String key, long[] array) { public void putArray(String key, long[] array) {
if (array != null) { if (array != null) {
map.put(key, new StringItem(key, longArrayToString(array))); map.put(key, new StringItem(key, longArrayToString(array)));
@ -227,7 +310,7 @@ public class StringBundle {
} }
} }
private String intArrayToString(int[] a) { private static String intArrayToString(int[] a) {
if (a == null) { if (a == null) {
return null; return null;
} }
@ -241,7 +324,7 @@ public class StringBundle {
return b.toString(); return b.toString();
} }
private int[] stringToIntArray(String a) throws NumberFormatException { private static int[] stringToIntArray(String a) throws NumberFormatException {
if (a == null) { if (a == null) {
return null; return null;
} }
@ -253,7 +336,7 @@ public class StringBundle {
return res; return res;
} }
private String longArrayToString(long[] a) { private static String longArrayToString(long[] a) {
if (a == null) { if (a == null) {
return null; return null;
} }
@ -267,7 +350,7 @@ public class StringBundle {
return b.toString(); return b.toString();
} }
private long[] stringToLongArray(String a) throws NumberFormatException { private static long[] stringToLongArray(String a) throws NumberFormatException {
if (a == null) { if (a == null) {
return null; return null;
} }
@ -279,7 +362,7 @@ public class StringBundle {
return res; return res;
} }
private String floatArrayToString(float[] a) { private static String floatArrayToString(float[] a) {
if (a == null) { if (a == null) {
return null; return null;
} }
@ -293,7 +376,7 @@ public class StringBundle {
return b.toString(); return b.toString();
} }
private float[] stringToFloatArray(String a) throws NumberFormatException { private static float[] stringToFloatArray(String a) throws NumberFormatException {
if (a == null) { if (a == null) {
return null; return null;
} }
@ -305,7 +388,7 @@ public class StringBundle {
return res; return res;
} }
private String intIntArrayToString(int[][] a) { private static String intIntArrayToString(int[][] a) {
if (a == null) { if (a == null) {
return null; return null;
} }
@ -333,10 +416,24 @@ public class StringBundle {
return b.toString(); return b.toString();
} }
private int[][] stringToIntIntArray(String a) throws NumberFormatException { private static int[][] stringToIntIntArray(String a) throws NumberFormatException {
if (a == null) { if (a == null) {
return null; return null;
} }
String[] items = a.split(";");
int[][] res = new int[items.length][];
for (int i = 0; i < items.length; i++) {
String item = items[i];
if (Algorithms.isEmpty(item)) {
res[i] = null;
} else {
String[] subItems = item.split(",");
res[i] = new int[subItems.length];
for (int k = 0; k < subItems.length; k++) {
res[i][k] = Integer.parseInt(subItems[k]);
}
}
}
/* /*
String[] items = a.split(";"); String[] items = a.split(";");
int[][] res = new int[items.length][]; int[][] res = new int[items.length][];
@ -348,10 +445,10 @@ public class StringBundle {
} }
} }
*/ */
return null;//res; return res;
} }
private String strArrayToString(String[] a) { private static String strArrayToString(String[] a) {
if (a == null) { if (a == null) {
return null; return null;
} }
@ -365,7 +462,7 @@ public class StringBundle {
return b.toString(); return b.toString();
} }
private String strStrArrayToString(String[][] a) { private static String strStrArrayToString(String[][] a) {
if (a == null) { if (a == null) {
return null; return null;
} }

View file

@ -0,0 +1,12 @@
package net.osmand.binary;
public abstract class StringBundleReader {
private StringBundle bundle = new StringBundle();
public StringBundle getBundle() {
return bundle;
}
public abstract void readBundle();
}

View file

@ -12,7 +12,7 @@ public abstract class StringBundleWriter {
this.bundle = bundle; this.bundle = bundle;
} }
protected StringBundle getBundle() { public StringBundle getBundle() {
return bundle; return bundle;
} }

View file

@ -0,0 +1,27 @@
package net.osmand.binary;
import net.osmand.PlatformUtil;
import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParser;
public class StringBundleXmlReader extends StringBundleReader {
public final static Log log = PlatformUtil.getLog(StringBundleXmlReader.class);
private XmlPullParser parser;
public StringBundleXmlReader(XmlPullParser parser) {
this.parser = parser;
}
@Override
public void readBundle() {
StringBundle bundle = getBundle();
for (int i = 0; i < parser.getAttributeCount(); i++) {
String name = parser.getAttributeName(i);
String value = parser.getAttributeValue(i);
bundle.putString(name, value);
}
}
}

View file

@ -1,16 +1,46 @@
package net.osmand.router; package net.osmand.router;
import net.osmand.Location;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule; import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule;
import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class RouteDataResources { public class RouteDataResources {
private Map<RouteTypeRule, Integer> rules = new LinkedHashMap<>(); private Map<RouteTypeRule, Integer> rules = new LinkedHashMap<>();
private List<Location> locations;
private int currentLocation;
public RouteDataResources() {
this.locations = new ArrayList<>();
}
public RouteDataResources(List<Location> locations) {
this.locations = locations;
}
public Map<RouteTypeRule, Integer> getRules() { public Map<RouteTypeRule, Integer> getRules() {
return rules; return rules;
} }
public List<Location> getLocations() {
return locations;
}
public boolean hasLocations() {
return locations.size() > 0;
}
public Location currentLocation() {
return currentLocation < locations.size() ? locations.get(currentLocation) : null;
}
public Location nextLocation() {
currentLocation++;
return currentLocation < locations.size() ? locations.get(currentLocation) : null;
}
} }

View file

@ -37,8 +37,8 @@ public class RouteExporter {
this.locations = locations; this.locations = locations;
} }
public Exception export() { public Exception exportRoute() {
RouteDataResources resources = new RouteDataResources(); RouteDataResources resources = new RouteDataResources(locations);
final RouteDataBundle bundle = new RouteDataBundle(resources); final RouteDataBundle bundle = new RouteDataBundle(resources);
for (RouteSegmentResult sr : route) { for (RouteSegmentResult sr : route) {
@ -56,14 +56,14 @@ public class RouteExporter {
} }
bundle.putBundleList("route", "segment", routeItems); bundle.putBundleList("route", "segment", routeItems);
List<StringBundle> dataObjects = new ArrayList<>(); List<StringBundle> typeList = new ArrayList<>();
Map<RouteTypeRule, Integer> rules = resources.getRules(); Map<RouteTypeRule, Integer> rules = resources.getRules();
for (RouteTypeRule rule : rules.keySet()) { for (RouteTypeRule rule : rules.keySet()) {
RouteDataBundle objectBundle = new RouteDataBundle(resources); RouteDataBundle typeBundle = new RouteDataBundle(resources);
rule.writeToBundle(objectBundle); rule.writeToBundle(typeBundle);
dataObjects.add(objectBundle); typeList.add(typeBundle);
} }
bundle.putBundleList("types", "type", dataObjects); bundle.putBundleList("types", "type", typeList);
GPXFile gpx = new GPXFile("OsmAnd"); GPXFile gpx = new GPXFile("OsmAnd");
gpx.author = OSMAND_ROUTER; gpx.author = OSMAND_ROUTER;
@ -98,7 +98,6 @@ public class RouteExporter {
}; };
gpx.setExtensionsWriter(extensionsWriter); gpx.setExtensionsWriter(extensionsWriter);
Exception result = GPXUtilities.writeGpxFile(file, gpx); return GPXUtilities.writeGpxFile(file, gpx);
return result;
} }
} }

View file

@ -0,0 +1,122 @@
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;
public class RouteImporter {
public final static Log log = PlatformUtil.getLog(RouteImporter.class);
private File file;
public RouteImporter(File file) {
this.file = file;
}
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
public void readExtensions(GPXFile res, XmlPullParser parser) throws Exception {
if (!resources.hasLocations()) {
List<Location> locations = resources.getLocations();
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) {
locations.add(new Location("", point.getLatitude(), point.getLongitude()));
}
}
}
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)) {
return;
}
}
}
} 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)) {
return;
}
}
}
}
}
};
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
GPXFile gpxFile = GPXUtilities.loadGPXFile(fis, extensionsReader);
for (RouteSegmentResult segment : route) {
segment.fillData();
}
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
}
}
return route;
}
}

View file

@ -1,7 +1,7 @@
package net.osmand.router; package net.osmand.router;
import net.osmand.binary.BinaryMapRouteReaderAdapter; import net.osmand.Location;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion; import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule; import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule;
import net.osmand.binary.RouteDataBundle; import net.osmand.binary.RouteDataBundle;
@ -17,6 +17,8 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import gnu.trove.map.hash.TIntObjectHashMap;
public class RouteSegmentResult implements StringExternalizable<RouteDataBundle> { public class RouteSegmentResult implements StringExternalizable<RouteDataBundle> {
private final RouteDataObject object; private final RouteDataObject object;
@ -34,6 +36,9 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
private static final DecimalFormat SPEED_FORMATTER = new DecimalFormat("#.##"); private static final DecimalFormat SPEED_FORMATTER = new DecimalFormat("#.##");
public RouteSegmentResult(RouteDataObject object) {
this.object = object;
}
public RouteSegmentResult(RouteDataObject object, int startPointIndex, int endPointIndex) { public RouteSegmentResult(RouteDataObject object, int startPointIndex, int endPointIndex) {
this.object = object; this.object = object;
@ -58,10 +63,23 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
void collectNames(RouteDataResources resources) { void collectNames(RouteDataResources resources) {
Map<RouteTypeRule, Integer> rules = resources.getRules(); Map<RouteTypeRule, Integer> rules = resources.getRules();
RouteRegion region = object.region;
if (region.getNameTypeRule() != -1) {
RouteTypeRule r = region.quickGetEncodingRule(region.getNameTypeRule());
if (!rules.containsKey(r)) {
rules.put(r, rules.size());
}
}
if (region.getRefTypeRule() != -1) {
RouteTypeRule r = region.quickGetEncodingRule(region.getRefTypeRule());
if (!rules.containsKey(r)) {
rules.put(r, rules.size());
}
}
if (object.nameIds != null) { if (object.nameIds != null) {
for (int nameId : object.nameIds) { for (int nameId : object.nameIds) {
String name = object.names.get(nameId); String name = object.names.get(nameId);
String tag = object.region.quickGetEncodingRule(nameId).getTag(); String tag = region.quickGetEncodingRule(nameId).getTag();
RouteTypeRule r = new RouteTypeRule(tag, name); RouteTypeRule r = new RouteTypeRule(tag, name);
if (!rules.containsKey(r)) { if (!rules.containsKey(r)) {
rules.put(r, rules.size()); rules.put(r, rules.size());
@ -81,6 +99,9 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
} }
private int[] convertTypes(int[] types, Map<RouteTypeRule, Integer> rules) { private int[] convertTypes(int[] types, Map<RouteTypeRule, Integer> rules) {
if (types == null || types.length == 0) {
return null;
}
int[] res = new int[types.length]; int[] res = new int[types.length];
for (int i = 0; i < types.length; i++) { for (int i = 0; i < types.length; i++) {
int type = types[i]; int type = types[i];
@ -95,6 +116,9 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
} }
private int[][] convertTypes(int[][] types, Map<RouteTypeRule, Integer> rules) { private int[][] convertTypes(int[][] types, Map<RouteTypeRule, Integer> rules) {
if (types == null || types.length == 0) {
return null;
}
int[][] res = new int[types.length][]; int[][] res = new int[types.length][];
for (int i = 0; i < types.length; i++) { for (int i = 0; i < types.length; i++) {
int[] typesArr = types[i]; int[] typesArr = types[i];
@ -106,6 +130,9 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
} }
private int[] convertNameIds(int[] nameIds, Map<RouteTypeRule, Integer> rules) { private int[] convertNameIds(int[] nameIds, Map<RouteTypeRule, Integer> rules) {
if (nameIds == null || nameIds.length == 0) {
return null;
}
int[] res = new int[nameIds.length]; int[] res = new int[nameIds.length];
for (int i = 0; i < nameIds.length; i++) { for (int i = 0; i < nameIds.length; i++) {
int nameId = nameIds[i]; int nameId = nameIds[i];
@ -121,19 +148,38 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
return res; return res;
} }
public void fillData() {
if (object.nameIds != null && object.nameIds.length > 0) {
RouteRegion region = object.region;
int nameTypeRule = region.getNameTypeRule();
int refTypeRule = region.getRefTypeRule();
object.names = new TIntObjectHashMap<>();
for (int nameId : object.nameIds) {
RouteTypeRule rule = region.quickGetEncodingRule(nameId);
if (rule != null) {
if (nameTypeRule != -1 && "name".equals(rule.getTag())) {
nameId = nameTypeRule;
} else if (refTypeRule != -1 && "ref".equals(rule.getTag())) {
nameId = refTypeRule;
}
object.names.put(nameId, rule.getValue());
}
}
}
}
@Override @Override
public void writeToBundle(RouteDataBundle bundle) { public void writeToBundle(RouteDataBundle bundle) {
Map<RouteTypeRule, Integer> rules = bundle.getResources().getRules(); Map<RouteTypeRule, Integer> rules = bundle.getResources().getRules();
bundle.putInt("length", Math.abs(endPointIndex - startPointIndex) + 1);
bundle.putFloat("segmentTime", segmentTime); bundle.putFloat("segmentTime", segmentTime);
bundle.putString("speed", SPEED_FORMATTER.format(speed)); bundle.putString("speed", SPEED_FORMATTER.format(speed));
if (turnType != null) { if (turnType != null) {
bundle.putString("turnType", turnType.toXmlString()); bundle.putString("turnType", turnType.toXmlString());
String turnLanes = object.getValue("turn:lanes"); int[] turnLanes = turnType.getLanes();
if (!Algorithms.isEmpty(turnLanes)) { if (turnLanes != null && turnLanes.length > 0) {
int[] rawLanes = RouteResultPreparation.calculateRawTurnLanes(turnLanes, turnType.getValue());
if (rawLanes.length > 0) {
StringBuilder turnLanesRes = new StringBuilder(); StringBuilder turnLanesRes = new StringBuilder();
for (int lane : rawLanes) { for (int lane : turnLanes) {
if (turnLanesRes.length() > 0) { if (turnLanesRes.length() > 0) {
turnLanesRes.append(","); turnLanesRes.append(",");
} }
@ -142,7 +188,6 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
bundle.putString("turnLanes", turnLanesRes.toString()); bundle.putString("turnLanes", turnLanesRes.toString());
} }
} }
}
bundle.putArray("types", convertTypes(object.types, rules)); bundle.putArray("types", convertTypes(object.types, rules));
bundle.putArray("pointTypes", convertTypes(object.pointTypes, rules)); bundle.putArray("pointTypes", convertTypes(object.pointTypes, rules));
bundle.putArray("names", convertNameIds(object.nameIds, rules)); bundle.putArray("names", convertNameIds(object.nameIds, rules));
@ -150,7 +195,40 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
@Override @Override
public void readFromBundle(RouteDataBundle bundle) { public void readFromBundle(RouteDataBundle bundle) {
int length = bundle.getInt("length", 0);
startPointIndex = 0;
endPointIndex = length > 0 ? length - 1 : 0;
segmentTime = bundle.getFloat("segmentTime", segmentTime);
speed = bundle.getFloat("speed", speed);
String turnTypeStr = bundle.getString("turnType", null);
if (!Algorithms.isEmpty(turnTypeStr)) {
turnType = TurnType.fromString(turnTypeStr, false);
String turnLanesStr = bundle.getString("turnLanes", null);
if (!Algorithms.isEmpty(turnLanesStr)) {
String[] lanesArr = turnLanesStr.split(",");
int[] lanes = new int[lanesArr.length];
for (int i = 0; i < lanesArr.length; i++) {
String laneStr = lanesArr[i];
lanes[i] = TurnType.fromString(laneStr, false).getValue();
}
turnType.setLanes(lanes);
}
}
object.types = bundle.getIntArray("types", null);
object.pointTypes = bundle.getIntIntArray("pointTypes", null);
object.nameIds = bundle.getIntArray("names", null);
object.pointsX = new int[length];
object.pointsY = new int[length];
RouteDataResources resources = bundle.getResources();
Location location = resources.currentLocation();
object.pointsX[0] = MapUtils.get31TileNumberX(location.getLongitude());
object.pointsY[0] = MapUtils.get31TileNumberY(location.getLatitude());
for (int i = 1; i < length; i++) {
location = resources.nextLocation();
object.pointsX[i] = MapUtils.get31TileNumberX(location.getLongitude());
object.pointsY[i] = MapUtils.get31TileNumberY(location.getLatitude());
}
} }
public float[] getHeightValues() { public float[] getHeightValues() {

View file

@ -232,31 +232,30 @@ public class TurnType {
} }
public static String toString(int[] lns) { public static String toString(int[] lns) {
String s = ""; StringBuilder s = new StringBuilder();
for (int h = 0; h < lns.length; h++) { for (int h = 0; h < lns.length; h++) {
if (h > 0) { if (h > 0) {
s += "|"; s.append("|");
} }
if (lns[h] % 2 == 1) { if (lns[h] % 2 == 1) {
s += "+"; s.append("+");
} }
int pt = TurnType.getPrimaryTurn(lns[h]); int pt = TurnType.getPrimaryTurn(lns[h]);
if (pt == 0) { if (pt == 0) {
pt = 1; pt = 1;
} }
s += TurnType.valueOf(pt, false).toXmlString(); s.append(TurnType.valueOf(pt, false).toXmlString());
int st = TurnType.getSecondaryTurn(lns[h]); int st = TurnType.getSecondaryTurn(lns[h]);
if (st != 0) { if (st != 0) {
s += "," + TurnType.valueOf(st, false).toXmlString(); s.append(",").append(TurnType.valueOf(st, false).toXmlString());
} }
int tt = TurnType.getTertiaryTurn(lns[h]); int tt = TurnType.getTertiaryTurn(lns[h]);
if (tt != 0) { if (tt != 0) {
s += "," + TurnType.valueOf(tt, false).toXmlString(); s.append(",").append(TurnType.valueOf(tt, false).toXmlString());
} }
} }
s += ""; return s.toString();
return s;
} }
public int[] getLanes() { public int[] getLanes() {
return lanes; return lanes;

View file

@ -2,7 +2,6 @@ package net.osmand.plus.routing;
import android.content.Context; import android.content.Context;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.system.Os;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.Location; import net.osmand.Location;
@ -13,7 +12,6 @@ import net.osmand.binary.RouteDataObject;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.LocationPoint; import net.osmand.data.LocationPoint;
import net.osmand.data.QuadRect; import net.osmand.data.QuadRect;
import net.osmand.plus.AppInitializer;
import net.osmand.plus.ApplicationMode; import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -143,10 +141,17 @@ public class RouteCalculationResult {
public RouteCalculationResult(List<RouteSegmentResult> list, Location start, LatLon end, List<LatLon> intermediates, public RouteCalculationResult(List<RouteSegmentResult> list, Location start, LatLon end, List<LatLon> intermediates,
OsmandApplication ctx, boolean leftSide, RoutingContext rctx, List<LocationPoint> waypoints, ApplicationMode mode) { OsmandApplication ctx, boolean leftSide, RoutingContext rctx, List<LocationPoint> waypoints, ApplicationMode mode) {
if (rctx != null) {
this.routingTime = rctx.routingTime; this.routingTime = rctx.routingTime;
this.visitedSegments = rctx.visitedSegments; this.visitedSegments = rctx.visitedSegments;
this.loadedTiles = rctx.loadedTiles; this.loadedTiles = rctx.loadedTiles;
this.calculateTime = (float) (((System.nanoTime() - rctx.timeToCalculate) / 1e6) / 1000f); this.calculateTime = (float) (((System.nanoTime() - rctx.timeToCalculate) / 1e6) / 1000f);
} else {
this.routingTime = 0;
this.visitedSegments = 0;
this.loadedTiles = 0;
this.calculateTime = 0;
}
if (waypoints != null) { if (waypoints != null) {
this.locationPoints.addAll(waypoints); this.locationPoints.addAll(waypoints);
} }
@ -174,7 +179,7 @@ public class RouteCalculationResult {
ctx.getSettings().ROUTE_STRAIGHT_ANGLE.getModeValue(mode) : 0; ctx.getSettings().ROUTE_STRAIGHT_ANGLE.getModeValue(mode) : 0;
RouteExporter exporter = new RouteExporter(new File(ctx.getAppPath(IndexConstants.GPX_INDEX_DIR), "test.gpx"), list, locations); RouteExporter exporter = new RouteExporter(new File(ctx.getAppPath(IndexConstants.GPX_INDEX_DIR), "test.gpx"), list, locations);
exporter.export(); //exporter.exportRoute();
} }

View file

@ -32,6 +32,7 @@ import net.osmand.router.GeneralRouter;
import net.osmand.router.GeneralRouter.RoutingParameter; import net.osmand.router.GeneralRouter.RoutingParameter;
import net.osmand.router.GeneralRouter.RoutingParameterType; import net.osmand.router.GeneralRouter.RoutingParameterType;
import net.osmand.router.PrecalculatedRouteDirection; import net.osmand.router.PrecalculatedRouteDirection;
import net.osmand.router.RouteImporter;
import net.osmand.router.RoutePlannerFrontEnd; import net.osmand.router.RoutePlannerFrontEnd;
import net.osmand.router.RoutePlannerFrontEnd.RouteCalculationMode; import net.osmand.router.RoutePlannerFrontEnd.RouteCalculationMode;
import net.osmand.router.RouteSegmentResult; import net.osmand.router.RouteSegmentResult;
@ -49,6 +50,7 @@ import org.xml.sax.SAXException;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -172,6 +174,7 @@ public class RouteProvider {
public GPXRouteParams build(Location start, OsmandSettings settings) { public GPXRouteParams build(Location start, OsmandSettings settings) {
GPXRouteParams res = new GPXRouteParams(); GPXRouteParams res = new GPXRouteParams();
res.prepareGPXFile(this); res.prepareGPXFile(this);
res.file = file;
// if (passWholeRoute && start != null) { // if (passWholeRoute && start != null) {
// res.points.add(0, start); // res.points.add(0, start);
// } // }
@ -201,6 +204,7 @@ public class RouteProvider {
boolean calculateOsmAndRouteParts; boolean calculateOsmAndRouteParts;
boolean useIntermediatePointsRTE; boolean useIntermediatePointsRTE;
private List<LocationPoint> wpt; private List<LocationPoint> wpt;
private GPXFile file;
boolean addMissingTurns = true; boolean addMissingTurns = true;
@ -357,6 +361,15 @@ public class RouteProvider {
} }
private RouteCalculationResult calculateGpxRoute(RouteCalculationParams routeParams) throws IOException { private RouteCalculationResult calculateGpxRoute(RouteCalculationParams routeParams) throws IOException {
RouteImporter routeImporter = new RouteImporter(new File(routeParams.gpxRoute.file.path));
List<RouteSegmentResult> route = routeImporter.importRoute();
RouteCalculationResult res = new RouteCalculationResult(route, routeParams.start, routeParams.end,
routeParams.intermediates, routeParams.ctx, routeParams.leftSide, null, null, routeParams.mode);
return res;
/*
// get the closest point to start and to end // get the closest point to start and to end
GPXRouteParams gpxParams = routeParams.gpxRoute; GPXRouteParams gpxParams = routeParams.gpxRoute;
if(routeParams.gpxRoute.useIntermediatePointsRTE){ if(routeParams.gpxRoute.useIntermediatePointsRTE){
@ -407,6 +420,7 @@ public class RouteProvider {
RouteCalculationResult res = new RouteCalculationResult(gpxRoute, gpxDirections, routeParams, RouteCalculationResult res = new RouteCalculationResult(gpxRoute, gpxDirections, routeParams,
gpxParams == null? null: gpxParams.wpt, routeParams.gpxRoute.addMissingTurns); gpxParams == null? null: gpxParams.wpt, routeParams.gpxRoute.addMissingTurns);
return res; return res;
*/
} }
private RouteCalculationResult calculateOsmAndRouteWithIntermediatePoints(RouteCalculationParams routeParams, private RouteCalculationResult calculateOsmAndRouteWithIntermediatePoints(RouteCalculationParams routeParams,