Merge pull request #10926 from osmandapp/multitrack_fixes

Multitrack fixes
This commit is contained in:
alex-osm 2021-02-18 12:47:07 +03:00 committed by GitHub
commit b5dc6c6354
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 70 deletions

View file

@ -62,6 +62,7 @@ public class MeasurementEditingContext implements IRouteSettingsListener {
private List<TrkSegment> afterSegmentsForSnap;
private GpxData gpxData;
private int selectedSegment = -1;
private int selectedPointPosition = -1;
private WptPt originalPointToMove;
@ -180,6 +181,10 @@ public class MeasurementEditingContext implements IRouteSettingsListener {
this.gpxData = gpxData;
}
public void setSelectedSegment(int selectedSegment) {
this.selectedSegment = selectedSegment;
}
public boolean hasRoutePoints() {
return gpxData != null && gpxData.getGpxFile() != null && gpxData.getGpxFile().hasRtePt();
}
@ -688,7 +693,7 @@ public class MeasurementEditingContext implements IRouteSettingsListener {
return res;
}
private List<Pair<WptPt, WptPt>> getOrderedRoadSegmentDataKeys() {
private List<Pair<WptPt, WptPt>> getOrderedRoadSegmentDataKeys() {
List<Pair<WptPt, WptPt>> keys = new ArrayList<>();
for (List<WptPt> points : Arrays.asList(before.points, after.points)) {
for (int i = 0; i < points.size() - 1; i++) {
@ -697,7 +702,7 @@ public class MeasurementEditingContext implements IRouteSettingsListener {
}
return keys;
}
private void recreateSegments(List<TrkSegment> segments, List<TrkSegment> segmentsForSnap, List<WptPt> points, boolean calculateIfNeeded) {
List<Integer> roadSegmentIndexes = new ArrayList<>();
TrkSegment s = new TrkSegment();
@ -767,67 +772,82 @@ public class MeasurementEditingContext implements IRouteSettingsListener {
if (Algorithms.isEmpty(segments)) {
return;
}
for (int si = 0; si < segments.size(); si++) {
TrkSegment segment = segments.get(si);
List<WptPt> points = segment.points;
if (selectedSegment != -1 && segments.size() > selectedSegment) {
TrkSegment segment = segments.get(selectedSegment);
if (segment.hasRoute()) {
RouteImporter routeImporter = new RouteImporter(segment);
List<RouteSegmentResult> routeSegments = routeImporter.importRoute();
List<WptPt> routePoints = gpxData.getGpxFile().getRoutePoints(si);
int prevPointIndex = 0;
if (routePoints.isEmpty() && points.size() > 1) {
routePoints.add(points.get(0));
routePoints.add(points.get(points.size() - 1));
}
for (int i = 0; i < routePoints.size() - 1; i++) {
Pair<WptPt, WptPt> pair = new Pair<>(routePoints.get(i), routePoints.get(i + 1));
int startIndex = pair.first.getTrkPtIndex();
if (startIndex < 0 || startIndex < prevPointIndex || startIndex >= points.size()) {
startIndex = MeasurementEditingContextUtils.findPointIndex(pair.first, points, prevPointIndex);
}
int endIndex = pair.second.getTrkPtIndex();
if (endIndex < 0 || endIndex < startIndex || endIndex >= points.size()) {
endIndex = MeasurementEditingContextUtils.findPointIndex(pair.second, points, startIndex);
}
if (startIndex >= 0 && endIndex >= 0) {
List<WptPt> pairPoints = new ArrayList<>();
for (int j = startIndex; j < endIndex && j < points.size(); j++) {
pairPoints.add(points.get(j));
prevPointIndex = j;
}
if (points.size() > prevPointIndex + 1 && i == routePoints.size() - 2) {
pairPoints.add(points.get(prevPointIndex + 1));
}
Iterator<RouteSegmentResult> it = routeSegments.iterator();
int k = endIndex - startIndex - 1;
List<RouteSegmentResult> pairSegments = new ArrayList<>();
if (k == 0 && !routeSegments.isEmpty()) {
pairSegments.add(routeSegments.remove(0));
} else {
while (it.hasNext() && k > 0) {
RouteSegmentResult s = it.next();
pairSegments.add(s);
it.remove();
k -= Math.abs(s.getEndPointIndex() - s.getStartPointIndex());
}
}
ApplicationMode appMode = ApplicationMode.valueOfStringKey(pair.first.getProfileType(), DEFAULT_APP_MODE);
roadSegmentData.put(pair, new RoadSegmentData(appMode, pair.first, pair.second, pairPoints, pairSegments));
}
}
if (!routePoints.isEmpty() && si < segments.size() - 1) {
routePoints.get(routePoints.size() - 1).setGap();
}
addPoints(routePoints);
addPoints(collectRoutePointsFromSegment(segment, selectedSegment));
} else {
addPoints(points);
if (!points.isEmpty() && si < segments.size() - 1) {
points.get(points.size() - 1).setGap();
addPoints(segment.points);
}
} else {
for (int si = 0; si < segments.size(); si++) {
TrkSegment segment = segments.get(si);
if (segment.hasRoute()) {
List<WptPt> routePoints = collectRoutePointsFromSegment(segment, si);
if (!routePoints.isEmpty() && si < segments.size() - 1) {
routePoints.get(routePoints.size() - 1).setGap();
}
addPoints(routePoints);
} else {
List<WptPt> points = segment.points;
addPoints(points);
if (!points.isEmpty() && si < segments.size() - 1) {
points.get(points.size() - 1).setGap();
}
}
}
}
}
private List<WptPt> collectRoutePointsFromSegment(TrkSegment segment, int segmentIndex) {
RouteImporter routeImporter = new RouteImporter(segment);
List<RouteSegmentResult> routeSegments = routeImporter.importRoute();
List<WptPt> routePoints = gpxData.getGpxFile().getRoutePoints(segmentIndex);
int prevPointIndex = 0;
List<WptPt> points = segment.points;
if (routePoints.isEmpty() && points.size() > 1) {
routePoints.add(points.get(0));
routePoints.add(points.get(points.size() - 1));
}
for (int i = 0; i < routePoints.size() - 1; i++) {
Pair<WptPt, WptPt> pair = new Pair<>(routePoints.get(i), routePoints.get(i + 1));
int startIndex = pair.first.getTrkPtIndex();
if (startIndex < 0 || startIndex < prevPointIndex || startIndex >= points.size()) {
startIndex = MeasurementEditingContextUtils.findPointIndex(pair.first, points, prevPointIndex);
}
int endIndex = pair.second.getTrkPtIndex();
if (endIndex < 0 || endIndex < startIndex || endIndex >= points.size()) {
endIndex = MeasurementEditingContextUtils.findPointIndex(pair.second, points, startIndex);
}
if (startIndex >= 0 && endIndex >= 0) {
List<WptPt> pairPoints = new ArrayList<>();
for (int j = startIndex; j < endIndex && j < points.size(); j++) {
pairPoints.add(points.get(j));
prevPointIndex = j;
}
if (points.size() > prevPointIndex + 1 && i == routePoints.size() - 2) {
pairPoints.add(points.get(prevPointIndex + 1));
}
Iterator<RouteSegmentResult> it = routeSegments.iterator();
int k = endIndex - startIndex - 1;
List<RouteSegmentResult> pairSegments = new ArrayList<>();
if (k == 0 && !routeSegments.isEmpty()) {
pairSegments.add(routeSegments.remove(0));
} else {
while (it.hasNext() && k > 0) {
RouteSegmentResult s = it.next();
pairSegments.add(s);
it.remove();
k -= Math.abs(s.getEndPointIndex() - s.getStartPointIndex());
}
}
ApplicationMode appMode = ApplicationMode.valueOfStringKey(pair.first.getProfileType(), DEFAULT_APP_MODE);
roadSegmentData.put(pair, new RoadSegmentData(appMode, pair.first, pair.second, pairPoints, pairSegments));
}
}
return routePoints;
}
public List<WptPt> setPoints(GpxRouteApproximation gpxApproximation, List<WptPt> originalPoints, ApplicationMode mode) {
if (gpxApproximation == null || Algorithms.isEmpty(gpxApproximation.finalPoints) || Algorithms.isEmpty(gpxApproximation.result)) {
return null;

View file

@ -226,16 +226,14 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca
GPXRouteParamsBuilder rparams = routingHelper.getCurrentGPXRoute();
boolean osmandRouter = mode.getRouteService() == RouteProvider.RouteService.OSMAND;
if (rparams != null && osmandRouter) {
boolean showReverseCard = !routingHelper.isCurrentGPXRouteV2();
if (showReverseCard) {
cardsContainer.addView(buildDividerView(cardsContainer, false));
cardsContainer.addView(buildDividerView(cardsContainer, false));
ReverseTrackCard reverseTrackCard = new ReverseTrackCard(mapActivity, rparams.isReverse());
reverseTrackCard.setListener(this);
cardsContainer.addView(reverseTrackCard.build(mapActivity));
ReverseTrackCard reverseTrackCard = new ReverseTrackCard(mapActivity, rparams.isReverse());
reverseTrackCard.setListener(this);
cardsContainer.addView(reverseTrackCard.build(mapActivity));
}
if (!gpxFile.hasRtePt() && !gpxFile.hasRoute()) {
cardsContainer.addView(buildDividerView(cardsContainer, showReverseCard));
cardsContainer.addView(buildDividerView(cardsContainer, true));
AttachTrackToRoadsCard attachTrackCard = new AttachTrackToRoadsCard(mapActivity);
attachTrackCard.setListener(this);
@ -584,6 +582,7 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca
MeasurementEditingContext editingContext = new MeasurementEditingContext();
editingContext.setGpxData(gpxData);
editingContext.setAppMode(app.getRoutingHelper().getAppMode());
editingContext.setSelectedSegment(app.getSettings().GPX_ROUTE_SEGMENT.get());
MeasurementToolFragment.showInstance(mapActivity.getSupportFragmentManager(), editingContext, true);
}
}
@ -721,11 +720,11 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca
@Override
public void onSegmentSelect(GPXFile gpxFile, int selectedSegment) {
selectTrackToFollow(gpxFile);
GPXRouteParamsBuilder paramsBuilder = app.getRoutingHelper().getCurrentGPXRoute();
if (paramsBuilder != null) {
paramsBuilder.setSelectedSegment(selectedSegment);
app.getSettings().GPX_ROUTE_SEGMENT.set(selectedSegment);
app.getRoutingHelper().onSettingsChanged(true);
GPXRouteParamsBuilder paramsBuilder = app.getRoutingHelper().getCurrentGPXRoute();
if (paramsBuilder != null) {
paramsBuilder.setSelectedSegment(selectedSegment);
app.getSettings().GPX_ROUTE_SEGMENT.set(selectedSegment);
app.getRoutingHelper().onSettingsChanged(true);
}
updateSelectionMode(false);
}

View file

@ -289,7 +289,11 @@ public class RouteProvider {
int selectedSegment = builder.getSelectedSegment();
if (OSMAND_ROUTER_V2.equals(file.author)) {
route = parseOsmAndGPXRoute(points, file, selectedSegment);
routePoints = file.getRoutePoints();
if (selectedSegment == -1) {
routePoints = file.getRoutePoints();
} else {
routePoints = file.getRoutePoints(selectedSegment);
}
if (reverse) {
Collections.reverse(points);
Collections.reverse(routePoints);