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 089de43fcd..e31f8ef03b 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RouteSegmentResult.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RouteSegmentResult.java @@ -271,7 +271,7 @@ public class RouteSegmentResult implements StringExternalizable bundle.putString("turnLanes", TurnType.lanesToString(turnLanes)); } } - bundle.putLong("id", object.id); + bundle.putLong("id", object.id >> 6); // OsmAnd ID to OSM ID bundle.putArray("types", convertTypes(object.types, rules)); int start = Math.min(startPointIndex, endPointIndex); @@ -327,22 +327,21 @@ public class RouteSegmentResult implements StringExternalizable Location prevLocation = null; for (int i = 0; i < length; i++) { Location location = resources.getLocation(index); - if (location == null) { - continue; - } - double dist = 0; - if (prevLocation != null) { - dist = MapUtils.getDistance(prevLocation.getLatitude(), prevLocation.getLongitude(), location.getLatitude(), location.getLongitude()); - distance += dist; - } - prevLocation = location; - object.pointsX[i] = MapUtils.get31TileNumberX(location.getLongitude()); - object.pointsY[i] = MapUtils.get31TileNumberY(location.getLatitude()); - if (location.hasAltitude() && object.heightDistanceArray.length > 0) { - object.heightDistanceArray[i * 2] = (float) dist; - object.heightDistanceArray[i * 2 + 1] = (float) location.getAltitude(); - } else { - object.heightDistanceArray = new float[0]; + if (location != null) { + double dist = 0; + if (prevLocation != null) { + dist = MapUtils.getDistance(prevLocation.getLatitude(), prevLocation.getLongitude(), location.getLatitude(), location.getLongitude()); + distance += dist; + } + prevLocation = location; + object.pointsX[i] = MapUtils.get31TileNumberX(location.getLongitude()); + object.pointsY[i] = MapUtils.get31TileNumberY(location.getLatitude()); + if (location.hasAltitude() && object.heightDistanceArray.length > 0) { + object.heightDistanceArray[i * 2] = (float) dist; + object.heightDistanceArray[i * 2 + 1] = (float) location.getAltitude(); + } else { + object.heightDistanceArray = new float[0]; + } } if (plus) { index++; diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java index dd3295b2c2..d1dae05a6d 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java @@ -517,47 +517,90 @@ public class MeasurementEditingContext { roadSegmentData.clear(); List routePoints = new ArrayList<>(); List gpxPoints = gpxApproximation.finalPoints; - for (int i = 0; i < gpxPoints.size() - 1; i++) { - GpxPoint rp1 = gpxPoints.get(i); - GpxPoint rp2 = gpxPoints.get(i + 1); - WptPt p1 = new WptPt(); - p1.lat = rp1.loc.getLatitude(); - p1.lon = rp1.loc.getLongitude(); - p1.setProfileType(mode.getStringKey()); - if (i == 0) { - routePoints.add(p1); - } - WptPt p2 = new WptPt(); - p2.lat = rp2.loc.getLatitude(); - p2.lon = rp2.loc.getLongitude(); - p2.setProfileType(mode.getStringKey()); - routePoints.add(p2); - Pair pair = new Pair<>(p1, p2); + for (int i = 0; i < gpxPoints.size(); i++) { + GpxPoint gp1 = gpxPoints.get(i); + boolean lastGpxPoint = isLastGpxPoint(gpxPoints, i); List points = new ArrayList<>(); List segments = new ArrayList<>(); - for (RouteSegmentResult seg : rp1.routeToTarget) { - segments.add(seg); - int ind = seg.getStartPointIndex(); - boolean plus = seg.isForwardDirection(); - float[] pf = seg.getObject().calculateHeightArray(); - while (ind != seg.getEndPointIndex()) { - LatLon l = seg.getPoint(ind); - WptPt pt = new WptPt(); - if (pf != null && pf.length > ind * 2 + 1) { - pt.ele = pf[ind * 2 + 1]; - - } - pt.lat = l.getLatitude(); - pt.lon = l.getLongitude(); - points.add(pt); - ind = plus ? ind + 1 : ind - 1; + for (int k = 0; k < gp1.routeToTarget.size(); k++) { + RouteSegmentResult seg = gp1.routeToTarget.get(k); + if (seg.getStartPointIndex() != seg.getEndPointIndex()) { + segments.add(seg); } } - roadSegmentData.put(pair, new RoadSegmentData(appMode, pair.first, pair.second, points, segments)); + for (int k = 0; k < segments.size(); k++) { + RouteSegmentResult seg = segments.get(k); + fillPointsArray(points, seg, lastGpxPoint && k == segments.size() - 1); + } + if (!points.isEmpty()) { + WptPt wp1 = new WptPt(); + wp1.lat = gp1.loc.getLatitude(); + wp1.lon = gp1.loc.getLongitude(); + wp1.setProfileType(mode.getStringKey()); + routePoints.add(wp1); + WptPt wp2 = new WptPt(); + if (lastGpxPoint) { + wp2.lat = points.get(points.size() - 1).getLatitude(); + wp2.lon = points.get(points.size() - 1).getLongitude(); + routePoints.add(wp2); + } else { + GpxPoint gp2 = gpxPoints.get(i + 1); + wp2.lat = gp2.loc.getLatitude(); + wp2.lon = gp2.loc.getLongitude(); + } + wp2.setProfileType(mode.getStringKey()); + Pair pair = new Pair<>(wp1, wp2); + roadSegmentData.put(pair, new RoadSegmentData(appMode, pair.first, pair.second, points, segments)); + } + if (lastGpxPoint) { + break; + } } addPoints(routePoints); } + private boolean isLastGpxPoint(List gpxPoints, int index) { + if (index == gpxPoints.size() - 1) { + return true; + } else { + for (int i = index + 1; i < gpxPoints.size(); i++) { + GpxPoint gp = gpxPoints.get(i); + for (int k = 0; k < gp.routeToTarget.size(); k++) { + RouteSegmentResult seg = gp.routeToTarget.get(k); + if (seg.getStartPointIndex() != seg.getEndPointIndex()) { + return false; + } + } + + } + } + return true; + } + + private void fillPointsArray(List points, RouteSegmentResult seg, boolean includeEndPoint) { + int ind = seg.getStartPointIndex(); + boolean plus = seg.isForwardDirection(); + float[] heightArray = seg.getObject().calculateHeightArray(); + while (ind != seg.getEndPointIndex()) { + addPointToArray(points, seg, ind, heightArray); + ind = plus ? ind + 1 : ind - 1; + } + if (includeEndPoint) { + addPointToArray(points, seg, ind, heightArray); + } + } + + private void addPointToArray(List points, RouteSegmentResult seg, int index, float[] heightArray) { + LatLon l = seg.getPoint(index); + WptPt pt = new WptPt(); + if (heightArray != null && heightArray.length > index * 2 + 1) { + pt.ele = heightArray[index * 2 + 1]; + } + pt.lat = l.getLatitude(); + pt.lon = l.getLongitude(); + points.add(pt); + } + private int findPointIndex(WptPt point, List points, int firstIndex) { double minDistance = Double.MAX_VALUE; int index = 0; @@ -740,10 +783,7 @@ public class MeasurementEditingContext { } locations.add(l); } - pair.second.setTrkPtIndex(locations.size() - 1); - if (i < size - 2 && !locations.isEmpty()) { - locations.remove(locations.size() - 1); - } + pair.second.setTrkPtIndex(i < size - 1 ? locations.size() : locations.size() - 1); route.addAll(data.segments); } } diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java b/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java index df6a6eaafa..a6dcc20fca 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java @@ -356,17 +356,6 @@ public class RouteCalculationResult { } lastHeight = h; } - // FIXME: investigate gpx file - if (s.getObject().getPoint31XTile(i) == 0 && s.getObject().getPoint31YTile(i) == 0) { - if (locations.size() > 0) { - Location prev = locations.get(locations.size() - 1); - n.setLatitude(prev.getLatitude()); - n.setLongitude(prev.getLongitude()); - if (prev.hasAltitude()) { - n.setAltitude(prev.getAltitude()); - } - } - } locations.add(n); attachAlarmInfo(alarms, s, i, locations.size()); segmentsToPopulate.add(s);