From 2b55f1dcb38c173309c730dfe9af5d56bf379261 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Tue, 25 Aug 2020 23:10:34 +0300 Subject: [PATCH] Fix save route point as line. Do not calculate straight segments always. --- .../MeasurementEditingContext.java | 39 ++++++++++++++++++- .../MeasurementToolFragment.java | 33 ++++++++++------ .../command/ApplyGpxApproximationCommand.java | 2 +- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java index d4f538faba..941c680502 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java @@ -191,6 +191,10 @@ public class MeasurementEditingContext { return gpxData != null && gpxData.getGpxFile() != null && gpxData.getGpxFile().hasRtePt(); } + public boolean hasSavedRoute() { + return gpxData != null && gpxData.getGpxFile() != null && gpxData.getGpxFile().hasRoute(); + } + public CalculationMode getCalculationMode() { return calculationMode; } @@ -321,6 +325,7 @@ public class MeasurementEditingContext { public void clearSegments() { clearBeforeSegments(); clearAfterSegments(); + clearSnappedToRoadPoints(); } public void clearBeforeSegments() { @@ -378,6 +383,16 @@ public class MeasurementEditingContext { } private void recreateCacheForSnap(TrkSegment cache, TrkSegment original, boolean calculateIfNeeded) { + boolean hasDefaultModeOnly = true; + if (original.points.size() > 1) { + for (int i = 0; i < original.points.size(); i++) { + String profileType = original.points.get(i).getProfileType(); + if (profileType != null && !profileType.equals(DEFAULT_APP_MODE.getStringKey())) { + hasDefaultModeOnly = false; + break; + } + } + } if (original.points.size() > 1) { for (int i = 0; i < original.points.size() - 1; i++) { Pair pair = new Pair<>(original.points.get(i), original.points.get(i + 1)); @@ -386,7 +401,7 @@ public class MeasurementEditingContext { if (pts != null) { cache.points.addAll(pts); } else { - if (calculateIfNeeded) { + if (calculateIfNeeded && !hasDefaultModeOnly) { scheduleRouteCalculateIfNotEmpty(); } cache.points.addAll(Arrays.asList(pair.first, pair.second)); @@ -450,7 +465,7 @@ public class MeasurementEditingContext { } } - public void setPoints(GpxRouteApproximation gpxApproximation) { + public void setPoints(GpxRouteApproximation gpxApproximation, ApplicationMode mode) { if (gpxApproximation == null || Algorithms.isEmpty(gpxApproximation.finalPoints) || Algorithms.isEmpty(gpxApproximation.result)) { return; } @@ -463,12 +478,14 @@ public class MeasurementEditingContext { 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); List points = new ArrayList<>(); @@ -640,6 +657,24 @@ public class MeasurementEditingContext { return params; } + public List getDistinctRoutePoints() { + List res = new ArrayList<>(); + List points = new ArrayList<>(before.points); + points.addAll(after.points); + int size = points.size(); + for (int i = 0; i < size - 1; i++) { + Pair pair = new Pair<>(points.get(i), points.get(i + 1)); + RoadSegmentData data = this.roadSegmentData.get(pair); + if (data != null) { + res.addAll(data.points); + if (i < size - 2) { + res.remove(res.size() - 1); + } + } + } + return res; + } + @Nullable public GPXFile exportRouteAsGpx(@NonNull String gpxName) { if (application == null || before.points.isEmpty() || !hasRoute()) { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index 7e577a19cc..8b76a93e68 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -429,7 +429,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route if (editingCtx.isNewData() && planRouteMode) { StartPlanRouteBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), createStartPlanRouteListener()); - } else if (!editingCtx.isNewData() && !editingCtx.hasRoutePoints() && !editingCtx.hasRoute()) { + } else if (!editingCtx.isNewData() && !editingCtx.hasRoutePoints() && !editingCtx.hasRoute() && editingCtx.getPointsCount() > 1) { SnapTrackWarningBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), this); } } @@ -578,7 +578,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route toolBarController.setTitle(getString(R.string.route_between_points)); mapActivity.refreshMap(); - if (editingCtx.isNewData() || editingCtx.hasRoutePoints() || editingCtx.hasRoute()) { + if (editingCtx.isNewData() || editingCtx.hasRoutePoints() || editingCtx.hasRoute() || editingCtx.getPointsCount() < 2) { RouteBetweenPointsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), this, editingCtx.getCalculationMode(), editingCtx.getAppMode()); @@ -594,7 +594,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route if (editingCtx.getPointsCount() > 0) { GpxData gpxData = editingCtx.getGpxData(); if (editingCtx.isNewData()) { - saveAsGpx(SaveType.ROUTE_POINT); + saveAsGpx(SaveType.ROUTE_POINT, close); } else if (isInEditMode() && gpxData.getActionType() == ActionType.EDIT_SEGMENT) { openSaveAsNewTrackMenu(mapActivity); } else { @@ -799,6 +799,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route if (measurementLayer != null) { editingCtx.getCommandManager().execute(new ChangeRouteModeCommand(measurementLayer, mode, calculationMode)); updateUndoRedoButton(false, redoBtn); + updateUndoRedoButton(true, undoBtn); disable(upDownBtn); updateSnapToRoadControls(); updateDistancePointsText(); @@ -912,12 +913,12 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route return new SaveAsNewTrackFragmentListener() { @Override public void saveAsRoutePointOnClick() { - saveAsGpx(SaveType.ROUTE_POINT); + saveAsGpx(SaveType.ROUTE_POINT, false); } @Override public void saveAsLineOnClick() { - saveAsGpx(SaveType.LINE); + saveAsGpx(SaveType.LINE, false); } }; } @@ -1326,7 +1327,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } } - private void saveAsGpx(final SaveType saveType) { + private void saveAsGpx(final SaveType saveType, final boolean close) { MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { final File dir = mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR); @@ -1365,7 +1366,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route fout = new File(dir, fileName); } } - saveNewGpx(dir, fileName, showOnMapToggle.isChecked(), saveType, false); + saveNewGpx(dir, fileName, showOnMapToggle.isChecked(), saveType, close); } }) .setNegativeButton(R.string.shared_string_cancel, null); @@ -1471,8 +1472,12 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route if (measurementLayer != null) { if (saveType == SaveType.LINE) { TrkSegment segment = new TrkSegment(); - segment.points.addAll(before.points); - segment.points.addAll(after.points); + if (editingCtx.hasRoute()) { + segment.points.addAll(editingCtx.getDistinctRoutePoints()); + } else { + segment.points.addAll(before.points); + segment.points.addAll(after.points); + } Track track = new Track(); track.name = trackName; track.segments.add(segment); @@ -1501,8 +1506,12 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route if (planRouteMode) { if (saveType == SaveType.LINE) { TrkSegment segment = new TrkSegment(); - segment.points.addAll(before.points); - segment.points.addAll(after.points); + if (editingCtx.hasRoute()) { + segment.points.addAll(editingCtx.getDistinctRoutePoints()); + } else { + segment.points.addAll(before.points); + segment.points.addAll(after.points); + } Track track = new Track(); track.name = trackName; track.segments.add(segment); @@ -1673,7 +1682,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } } if (!editMode && editingCtx.getPointsCount() > 1) { - toolBarController.setTitle(fileName); + toolBarController.setTitle(fileName.replace('_', ' ')); toolBarController.setDescription(actionStr); } else { toolBarController.setTitle(actionStr); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/ApplyGpxApproximationCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/ApplyGpxApproximationCommand.java index ef99bd3758..f9a79c96b6 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/ApplyGpxApproximationCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/ApplyGpxApproximationCommand.java @@ -66,6 +66,6 @@ public class ApplyGpxApproximationCommand extends MeasurementModeCommand { public void applyApproximation() { getEditingCtx().setAppMode(mode); getEditingCtx().clearSegments(); - getEditingCtx().setPoints(approximation); + getEditingCtx().setPoints(approximation, mode); } }