From 03e67d495b0d695259da796508bfe775240f7ba6 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Sun, 15 Mar 2020 19:17:38 +0300 Subject: [PATCH] Fixed gpx to route feature --- .../net/osmand/router/RouteDataResources.java | 10 +-- .../net/osmand/router/RouteSegmentResult.java | 62 +++++++++++-------- .../main/java/net/osmand/router/TurnType.java | 14 ++++- .../router/RouteResultPreparationTest.java | 2 +- 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RouteDataResources.java b/OsmAnd-java/src/main/java/net/osmand/router/RouteDataResources.java index bd9563f8f0..634dd7219e 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RouteDataResources.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RouteDataResources.java @@ -35,12 +35,12 @@ public class RouteDataResources { return locations.size() > 0; } - public Location currentLocation() { - return currentLocation < locations.size() ? locations.get(currentLocation) : null; + public Location getLocation(int index) { + index += currentLocation; + return index < locations.size() ? locations.get(index) : null; } - public Location nextLocation() { - currentLocation++; - return currentLocation < locations.size() ? locations.get(currentLocation) : null; + public void incrementCurrentLocation(int index) { + currentLocation += index; } } diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RouteSegmentResult.java b/OsmAnd-java/src/main/java/net/osmand/router/RouteSegmentResult.java index 8ba8e78376..36d61dd67d 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RouteSegmentResult.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RouteSegmentResult.java @@ -171,21 +171,21 @@ public class RouteSegmentResult implements StringExternalizable @Override public void writeToBundle(RouteDataBundle bundle) { Map rules = bundle.getResources().getRules(); - bundle.putInt("length", Math.abs(endPointIndex - startPointIndex) + 1); + bundle.putInt("length", (Math.abs(endPointIndex - startPointIndex) + 1) * (endPointIndex >= startPointIndex ? 1 : -1)); bundle.putFloat("segmentTime", segmentTime); bundle.putString("speed", SPEED_FORMATTER.format(speed)); if (turnType != null) { bundle.putString("turnType", turnType.toXmlString()); + if (turnType.isSkipToSpeak()) { + bundle.putBoolean("skipTurn", turnType.isSkipToSpeak()); + } + if (turnType.getTurnAngle() != 0) { + bundle.putFloat("turnAngle", turnType.getTurnAngle()); + } int[] turnLanes = turnType.getLanes(); if (turnLanes != null && turnLanes.length > 0) { - StringBuilder turnLanesRes = new StringBuilder(); - for (int lane : turnLanes) { - if (turnLanesRes.length() > 0) { - turnLanesRes.append(","); - } - turnLanesRes.append(TurnType.valueOf(lane, false).toXmlString()); - } - bundle.putString("turnLanes", turnLanesRes.toString()); + //bundle.putArray("turnLanes", turnLanes); + bundle.putString("turnLanes", TurnType.lanesToString(turnLanes)); } } bundle.putArray("types", convertTypes(object.types, rules)); @@ -196,39 +196,47 @@ public class RouteSegmentResult implements StringExternalizable @Override public void readFromBundle(RouteDataBundle bundle) { int length = bundle.getInt("length", 0); - startPointIndex = 0; - endPointIndex = length > 0 ? length - 1 : 0; + boolean plus = length >= 0; + length = Math.abs(length); + startPointIndex = plus ? 0 : length - 1; + endPointIndex = plus ? 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); - } + turnType.setSkipToSpeak(bundle.getBoolean("skipTurn", turnType.isSkipToSpeak())); + turnType.setTurnAngle(bundle.getFloat("turnAngle", turnType.getTurnAngle())); + //int[] turnLanes = bundle.getIntArray("turnLanes", null); + int[] turnLanes = TurnType.lanesFromString(bundle.getString("turnLanes", null)); + turnType.setLanes(turnLanes); } object.types = bundle.getIntArray("types", null); object.pointTypes = bundle.getIntIntArray("pointTypes", null); object.nameIds = bundle.getIntArray("names", null); + RouteDataResources resources = bundle.getResources(); 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(); + int index = plus ? 0 : length - 1; + float distance = 0; + Location prevLocation = null; + for (int i = 0; i < length; i++) { + Location location = resources.getLocation(index); + if (prevLocation != null) { + distance += MapUtils.getDistance(prevLocation.getLatitude(), prevLocation.getLongitude(), location.getLatitude(), location.getLongitude()); + } + prevLocation = location; object.pointsX[i] = MapUtils.get31TileNumberX(location.getLongitude()); object.pointsY[i] = MapUtils.get31TileNumberY(location.getLatitude()); + if (plus) { + index++; + } else { + index--; + } } + this.distance = distance; + resources.incrementCurrentLocation(length - 1); } public float[] getHeightValues() { diff --git a/OsmAnd-java/src/main/java/net/osmand/router/TurnType.java b/OsmAnd-java/src/main/java/net/osmand/router/TurnType.java index 34244b34ed..e7b6811365 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/TurnType.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/TurnType.java @@ -1,5 +1,7 @@ package net.osmand.router; +import net.osmand.util.Algorithms; + import gnu.trove.set.hash.TIntHashSet; public class TurnType { @@ -231,7 +233,7 @@ public class TurnType { return (laneValue >> 10); } - public static String toString(int[] lns) { + public static String lanesToString(int[] lns) { StringBuilder s = new StringBuilder(); for (int h = 0; h < lns.length; h++) { if (h > 0) { @@ -257,6 +259,14 @@ public class TurnType { } return s.toString(); } + + public static int[] lanesFromString(String lanes) { + if (Algorithms.isEmpty(lanes)) { + return null; + } + return null; + } + public int[] getLanes() { return lanes; } @@ -329,7 +339,7 @@ public class TurnType { } if(vl != null) { if(lanes != null) { - vl += "(" + toString(lanes) +")"; + vl += "(" + lanesToString(lanes) +")"; } return vl; } diff --git a/OsmAnd-java/src/test/java/net/osmand/router/RouteResultPreparationTest.java b/OsmAnd-java/src/test/java/net/osmand/router/RouteResultPreparationTest.java index 658bf28243..31f4b3b4fa 100644 --- a/OsmAnd-java/src/test/java/net/osmand/router/RouteResultPreparationTest.java +++ b/OsmAnd-java/src/test/java/net/osmand/router/RouteResultPreparationTest.java @@ -138,7 +138,7 @@ public class RouteResultPreparationTest { private String getLanesString(RouteSegmentResult segment) { final int[] lns = segment.getTurnType().getLanes(); if (lns != null) { - return TurnType.toString(lns); + return TurnType.lanesToString(lns); } return null; }