Fixed gpx to route feature

This commit is contained in:
max-klaus 2020-03-15 19:17:38 +03:00
parent 86f4cf45f7
commit 03e67d495b
4 changed files with 53 additions and 35 deletions

View file

@ -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;
}
}

View file

@ -171,21 +171,21 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
@Override
public void writeToBundle(RouteDataBundle bundle) {
Map<RouteTypeRule, Integer> 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<RouteDataBundle>
@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() {

View file

@ -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;
}

View file

@ -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;
}