Cache simplified colored tracks

This commit is contained in:
cepprice 2021-04-11 23:34:01 +05:00
parent 74a534c4f3
commit 430ff544b3

View file

@ -118,6 +118,9 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
private MapMarkersHelper mapMarkersHelper; private MapMarkersHelper mapMarkersHelper;
private GpxSelectionHelper selectedGpxHelper; private GpxSelectionHelper selectedGpxHelper;
private final Map<String, Long> cachedModifiedTime = new HashMap<>();
private final Map<String, List<TrkSegment>> cachedZoomedSegments = new HashMap<>();
private List<WptPt> cache = new ArrayList<>(); private List<WptPt> cache = new ArrayList<>();
private Map<WptPt, SelectedGpxFile> pointFileMap = new HashMap<>(); private Map<WptPt, SelectedGpxFile> pointFileMap = new HashMap<>();
private MapTextLayer textLayer; private MapTextLayer textLayer;
@ -460,7 +463,10 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
float trackWidth = getTrackWidth(width, defaultTrackWidth); float trackWidth = getTrackWidth(width, defaultTrackWidth);
int trackColor = getTrackColor(selectedGpxFile.getGpxFile(), cachedColor); int trackColor = getTrackColor(selectedGpxFile.getGpxFile(), cachedColor);
int arrowColor = UiUtilities.getContrastColor(view.getApplication(), trackColor, false); int arrowColor = UiUtilities.getContrastColor(view.getApplication(), trackColor, false);
for (TrkSegment segment : selectedGpxFile.getPointsToDisplay()) { GradientScaleType scaleType = getGradientScaleType(selectedGpxFile.getGpxFile());
List<TrkSegment> segments = scaleType != null ?
getCachedSegments(selectedGpxFile, scaleType) : selectedGpxFile.getPointsToDisplay();
for (TrkSegment segment : segments) {
if (segment.renderer instanceof Renderable.RenderableSegment) { if (segment.renderer instanceof Renderable.RenderableSegment) {
((Renderable.RenderableSegment) segment.renderer) ((Renderable.RenderableSegment) segment.renderer)
.drawGeometry(canvas, tileBox, correctedQuadRect, arrowColor, trackColor, trackWidth); .drawGeometry(canvas, tileBox, correctedQuadRect, arrowColor, trackColor, trackWidth);
@ -687,7 +693,6 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
return; return;
} }
OsmandApplication app = view.getApplication();
GPXFile gpxFile = selectedGpxFile.getGpxFile(); GPXFile gpxFile = selectedGpxFile.getGpxFile();
GradientScaleType scaleType = getGradientScaleType(gpxFile); GradientScaleType scaleType = getGradientScaleType(gpxFile);
List<TrkSegment> segments = new ArrayList<>(); List<TrkSegment> segments = new ArrayList<>();
@ -695,11 +700,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
if (scaleType == null) { if (scaleType == null) {
segments.addAll(selectedGpxFile.getModifiablePointsToDisplay()); segments.addAll(selectedGpxFile.getModifiablePointsToDisplay());
} else { } else {
RouteColorize colorize = new RouteColorize(view.getZoom(), gpxFile, selectedGpxFile.getTrackAnalysis(app), segments.addAll(getCachedSegments(selectedGpxFile, scaleType));
scaleType.toColorizationType(), app.getSettings().getApplicationMode().getMaxSpeed());
colorize.setPalette(getColorizationPalette(gpxFile, scaleType));
List<RouteColorizationPoint> colorsOfPoints = colorize.getResult(true);
segments.addAll(createSimplifiedSegmentsFromColorizedPoints(gpxFile, colorsOfPoints, scaleType));
} }
for (TrkSegment ts : segments) { for (TrkSegment ts : segments) {
@ -725,6 +726,43 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
} }
} }
private List<TrkSegment> getCachedSegments(SelectedGpxFile selectedGpxFile, GradientScaleType scaleType) {
GPXFile gpxFile = selectedGpxFile.getGpxFile();
String fileName = gpxFile.path;
String trackId = view.getZoom() + "_" + scaleType.toString() + "_" + fileName;
if (cachedModifiedTime.get(fileName) != null && cachedModifiedTime.get(fileName) == gpxFile.modifiedTime) {
List<TrkSegment> segments = cachedZoomedSegments.get(trackId);
if (segments == null) {
segments = calculateGradientTrack(selectedGpxFile, scaleType);
cachedZoomedSegments.put(trackId, segments);
}
return segments;
} else {
if (cachedModifiedTime.get(fileName) != null) {
for (String key : cachedZoomedSegments.keySet()) {
if (key.contains("_" + fileName)) {
cachedZoomedSegments.remove(key);
}
}
}
cachedModifiedTime.put(fileName, gpxFile.modifiedTime);
List<TrkSegment> segments = calculateGradientTrack(selectedGpxFile, scaleType);
cachedZoomedSegments.put(trackId, segments);
return segments;
}
}
private List<TrkSegment> calculateGradientTrack(SelectedGpxFile selectedGpxFile, GradientScaleType scaleType) {
OsmandApplication app = view.getApplication();
GPXFile gpxFile = selectedGpxFile.getGpxFile();
RouteColorize colorize = new RouteColorize(view.getZoom(), gpxFile, selectedGpxFile.getTrackAnalysis(app),
scaleType.toColorizationType(), app.getSettings().getApplicationMode().getMaxSpeed());
colorize.setPalette(getColorizationPalette(gpxFile, scaleType));
List<RouteColorizationPoint> colorsOfPoints = colorize.getResult(true);
return createSimplifiedSegmentsFromColorizedPoints(selectedGpxFile.getGpxFile(), colorsOfPoints, scaleType);
}
private List<TrkSegment> createSimplifiedSegmentsFromColorizedPoints(GPXFile gpxFile, private List<TrkSegment> createSimplifiedSegmentsFromColorizedPoints(GPXFile gpxFile,
List<RouteColorizationPoint> colorizationPoints, List<RouteColorizationPoint> colorizationPoints,
GradientScaleType scaleType) { GradientScaleType scaleType) {