Fix colorization when speed was derived from timestamps

This commit is contained in:
cepprice 2021-04-03 01:26:33 +05:00
parent b36d5a92d8
commit 241a2387b1
3 changed files with 45 additions and 31 deletions

View file

@ -4,6 +4,7 @@ import net.osmand.GPXUtilities;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.osm.edit.Node; import net.osmand.osm.edit.Node;
import net.osmand.osm.edit.OsmMapUtils; import net.osmand.osm.edit.OsmMapUtils;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -30,6 +31,8 @@ public class RouteColorize {
public static final int RED = rgbaToDecimal(243, 55, 77, 255); public static final int RED = rgbaToDecimal(243, 55, 77, 255);
public static final int[] colors = new int[] {GREEN, YELLOW, RED}; public static final int[] colors = new int[] {GREEN, YELLOW, RED};
private static final int MAX_SLOPE_VALUE = 25;
public enum ColorizationType { public enum ColorizationType {
ELEVATION, ELEVATION,
SPEED, SPEED,
@ -75,7 +78,7 @@ public class RouteColorize {
/** /**
* @param type ELEVATION, SPEED, SLOPE * @param type ELEVATION, SPEED, SLOPE
*/ */
public RouteColorize(int zoom, GPXUtilities.GPXFile gpxFile, ColorizationType type) { public RouteColorize(int zoom, GPXUtilities.GPXFile gpxFile, ColorizationType type, float maxProfileSpeed) {
if (!gpxFile.hasTrkPt()) { if (!gpxFile.hasTrkPt()) {
LOG.warn("GPX file is not consist of track points"); LOG.warn("GPX file is not consist of track points");
@ -85,21 +88,27 @@ public class RouteColorize {
List<Double> latList = new ArrayList<>(); List<Double> latList = new ArrayList<>();
List<Double> lonList = new ArrayList<>(); List<Double> lonList = new ArrayList<>();
List<Double> valList = new ArrayList<>(); List<Double> valList = new ArrayList<>();
GPXUtilities.GPXTrackAnalysis analysis = Algorithms.isEmpty(gpxFile.path) ?
gpxFile.getAnalysis(System.currentTimeMillis()) : gpxFile.getAnalysis(gpxFile.modifiedTime);
int wptIdx = 0;
for (GPXUtilities.Track t : gpxFile.tracks) { for (GPXUtilities.Track t : gpxFile.tracks) {
for (GPXUtilities.TrkSegment ts : t.segments) { for (GPXUtilities.TrkSegment ts : t.segments) {
for (GPXUtilities.WptPt p : ts.points) { for (GPXUtilities.WptPt p : ts.points) {
latList.add(p.lat); latList.add(p.lat);
lonList.add(p.lon); lonList.add(p.lon);
if (type == ColorizationType.SPEED) { if (type == ColorizationType.SPEED) {
valList.add(p.speed); valList.add((double) analysis.speedData.get(wptIdx).speed);
} else { } else {
valList.add(p.ele); valList.add((double) analysis.elevationData.get(wptIdx).elevation);
} }
wptIdx++;
} }
} }
} }
this.zoom = zoom; this.zoom = zoom;
colorizationType = type;
latitudes = listToArray(latList); latitudes = listToArray(latList);
longitudes = listToArray(lonList); longitudes = listToArray(lonList);
@ -108,9 +117,8 @@ public class RouteColorize {
} else { } else {
values = listToArray(valList); values = listToArray(valList);
} }
calculateMinMaxValue(); calculateMinMaxValue();
colorizationType = type; maxValue = getMaxValue(colorizationType, analysis, minValue, maxProfileSpeed);
checkPalette(); checkPalette();
sortPalette(); sortPalette();
} }
@ -194,7 +202,7 @@ public class RouteColorize {
return rgbaToDecimal((int) resultRed, (int) resultGreen, (int) resultBlue, (int) resultAlpha); return rgbaToDecimal((int) resultRed, (int) resultGreen, (int) resultBlue, (int) resultAlpha);
} }
} }
return getDefaultColor(); return getTransparentColor();
} }
public void setPalette(double[][] palette) { public void setPalette(double[][] palette) {
@ -209,12 +217,12 @@ public class RouteColorize {
} }
setPalette(new double[][] { setPalette(new double[][] {
{minValue, gradientPalette[0]}, {minValue, gradientPalette[0]},
{colorizationType == ColorizationType.SLOPE ? 0 : (minValue + maxValue) / 2, gradientPalette[1]}, {(minValue + maxValue) / 2, gradientPalette[1]},
{maxValue, gradientPalette[2]} {maxValue, gradientPalette[2]}
}); });
} }
private int getDefaultColor() { private int getTransparentColor() {
return rgbaToDecimal(0, 0, 0, 0); return rgbaToDecimal(0, 0, 0, 0);
} }
@ -295,7 +303,7 @@ public class RouteColorize {
double[][] defaultPalette = { double[][] defaultPalette = {
{minValue, GREEN}, {minValue, GREEN},
{colorizationType == ColorizationType.SLOPE ? 0 : (minValue + maxValue) / 2, YELLOW}, {(minValue + maxValue) / 2, YELLOW},
{maxValue, RED} {maxValue, RED}
}; };
palette = defaultPalette; palette = defaultPalette;
@ -397,6 +405,20 @@ public class RouteColorize {
return result; return result;
} }
public static double getMinValue(ColorizationType type, GPXUtilities.GPXTrackAnalysis analysis) {
return type == ColorizationType.ELEVATION ? analysis.minElevation : 0.0;
}
public static double getMaxValue(ColorizationType type, GPXUtilities.GPXTrackAnalysis analysis, double minValue, double maxProfileSpeed) {
if (type == ColorizationType.SPEED) {
return Math.max(analysis.maxSpeed, maxProfileSpeed);
} else if (type == ColorizationType.ELEVATION) {
return Math.max(analysis.maxElevation, minValue + 50);
} else {
return MAX_SLOPE_VALUE;
}
}
private void calculateMinMaxValue() { private void calculateMinMaxValue() {
if (values.length == 0) if (values.length == 0)
return; return;
@ -457,5 +479,4 @@ public class RouteColorize {
this.val = val; this.val = val;
} }
} }
} }

View file

@ -12,6 +12,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import net.osmand.router.RouteColorize;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -42,8 +43,9 @@ public class GradientCard extends BaseCard {
AndroidUiHelper.updateVisibility(view, true); AndroidUiHelper.updateVisibility(view, true);
TextView minValue = view.findViewById(R.id.min_value); TextView minValue = view.findViewById(R.id.min_value);
TextView maxValue = view.findViewById(R.id.max_value); TextView maxValue = view.findViewById(R.id.max_value);
float min = getMinValue(); double min = RouteColorize.getMinValue(selectedScaleType.toColorizationType(), gpxTrackAnalysis);
float max = getMaxValue(min); double max = RouteColorize.getMaxValue(selectedScaleType.toColorizationType(),
gpxTrackAnalysis, min, app.getSettings().getApplicationMode().getMaxSpeed());
minValue.setText(formatValue(min)); minValue.setText(formatValue(min));
maxValue.setText(formatValue(max)); maxValue.setText(formatValue(max));
} }
@ -53,27 +55,13 @@ public class GradientCard extends BaseCard {
updateContent(); updateContent();
} }
private float getMinValue() { private CharSequence formatValue(double value) {
return (float) (selectedScaleType == GradientScaleType.ALTITUDE ? gpxTrackAnalysis.minElevation : 0.0);
}
private float getMaxValue(float minValue) {
if (selectedScaleType == GradientScaleType.SPEED) {
return (Math.max(gpxTrackAnalysis.maxSpeed, app.getSettings().getApplicationMode().getMaxSpeed()));
} else if (selectedScaleType == GradientScaleType.ALTITUDE) {
return (float) Math.max(gpxTrackAnalysis.maxElevation, minValue + 50);
} else {
return 25;
}
}
private CharSequence formatValue(float value) {
if (selectedScaleType == GradientScaleType.ALTITUDE) { if (selectedScaleType == GradientScaleType.ALTITUDE) {
return OsmAndFormatter.getFormattedAlt(value, app); return OsmAndFormatter.getFormattedAlt(value, app);
} else if (selectedScaleType == GradientScaleType.SLOPE) { } else if (selectedScaleType == GradientScaleType.SLOPE) {
return (int) value + " %"; return (int) value + " %";
} }
String speed = OsmAndFormatter.getFormattedSpeed(value, app); String speed = OsmAndFormatter.getFormattedSpeed((float) value, app);
String speedUnit = app.getSettings().SPEED_SYSTEM.get().toShortString(app); String speedUnit = app.getSettings().SPEED_SYSTEM.get().toShortString(app);
Spannable formattedSpeed = new SpannableString(speed); Spannable formattedSpeed = new SpannableString(speed);
formattedSpeed.setSpan( formattedSpeed.setSpan(

View file

@ -684,11 +684,16 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
List<TrkSegment> segments = selectedGpxFile.getPointsToDisplay(); List<TrkSegment> segments = selectedGpxFile.getPointsToDisplay();
GradientScaleType scaleType = getGradientScaleType(gpxFile); GradientScaleType scaleType = getGradientScaleType(gpxFile);
List<RouteColorize.RouteColorizationPoint> colorsOfPoints = null; List<RouteColorize.RouteColorizationPoint> colorsOfPoints = null;
if (scaleType != null) { boolean needCalculateColors = scaleType != null && segments.get(0).points.get(0)
RouteColorize colorize = new RouteColorize(view.getZoom(), gpxFile, scaleType.toColorizationType()); .getColor(scaleType.toColorizationType()) == 0;
if (scaleType != null && currentTrack || needCalculateColors) {
RouteColorize colorize = new RouteColorize(view.getZoom(), gpxFile,
scaleType.toColorizationType(), view.getApplication().getSettings().getApplicationMode().getMaxSpeed());
colorize.setPalette(getColorizationPalette(gpxFile, scaleType)); colorize.setPalette(getColorizationPalette(gpxFile, scaleType));
colorsOfPoints = colorize.getResult(false); colorsOfPoints = colorize.getResult(false);
} }
int startIdx = 0; int startIdx = 0;
for (TrkSegment ts : segments) { for (TrkSegment ts : segments) {
String width = getTrackWidthName(gpxFile, defaultTrackWidthPref.get()); String width = getTrackWidthName(gpxFile, defaultTrackWidthPref.get());