Skip to content

Commit

Permalink
Merge pull request #19695 from osmandapp/FixRequiredMaps_v2
Browse files Browse the repository at this point in the history
Fix Required Maps after UI Review v2
  • Loading branch information
vshcherb committed May 14, 2024
2 parents 86ba0a1 + de0d1bf commit 67686ed
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 194 deletions.
22 changes: 20 additions & 2 deletions OsmAnd-java/src/main/java/net/osmand/PlatformUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@



import net.osmand.map.OsmandRegions;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;

/**
* That class is replacing of standard LogFactory due to
* problems with Android implementation of LogFactory.
Expand All @@ -17,11 +21,25 @@
* there is an intention to delegate all static methods to LogFactory.
*/
public class PlatformUtil {


private static OsmandRegions osmandRegions;

public static Log getLog(Class<?> cl){
return LogFactory.getLog(cl);
}


public static void setOsmandRegions(OsmandRegions or) {
osmandRegions = or;
}

public static OsmandRegions getOsmandRegions() throws IOException {
if (osmandRegions == null) {
osmandRegions = new OsmandRegions();
osmandRegions.prepareFile();
}
return osmandRegions;
}

public static XmlPullParser newXMLPullParser() throws XmlPullParserException{
org.kxml2.io.KXmlParser xmlParser = new org.kxml2.io.KXmlParser();
xmlParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package net.osmand.router;

import net.osmand.data.LatLon;
import net.osmand.map.OsmandRegions;
import net.osmand.map.WorldRegion;
import net.osmand.util.Algorithms;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class MissingMapsCalculationResult {

private final RoutingContext missingMapsRoutingContext;
private final List<LatLon> missingMapsPoints;

Set<String> usedMaps = new LinkedHashSet<>();
Set<String> mapsToDownload = new LinkedHashSet<>();
Set<String> missingMaps = new LinkedHashSet<>();
Set<String> mapsToUpdate = new LinkedHashSet<>();
private List<WorldRegion> regionsToDownload;
private List<WorldRegion> missingRegions;
private List<WorldRegion> regionsToUpdate;
private List<WorldRegion> usedRegions;

public MissingMapsCalculationResult(RoutingContext missingMapsRoutingContext, List<LatLon> missingMapsPoints) {
this.missingMapsRoutingContext = missingMapsRoutingContext;
this.missingMapsPoints = missingMapsPoints;
}

public void addMissingMaps(String region) {
missingMaps.add(region);
mapsToDownload.add(region);
}

public void addUsedMaps(String region) {
usedMaps.add(region);
}

public void addMapToUpdate(String region) {
mapsToUpdate.add(region);
mapsToDownload.add(region);
}

public boolean hasMissingMaps() {
return !Algorithms.isEmpty(mapsToDownload);
}

private List<WorldRegion> convert(OsmandRegions or, Set<String> maps) {
if (maps.isEmpty()) {
return null;
}
List<WorldRegion> l = new ArrayList<>();
for (String m : maps) {
WorldRegion wr = or.getRegionDataByDownloadName(m);
if (wr != null) {
l.add(wr);
}
}
return l;
}

public List<WorldRegion> getMapsToDownload() {
return regionsToDownload;
}

public List<WorldRegion> getMissingMaps() {
return missingRegions;
}

public List<WorldRegion> getMapsToUpdate() {
return regionsToUpdate;
}

public List<WorldRegion> getUsedMaps() {
return usedRegions;
}

public RoutingContext getMissingMapsRoutingContext() {
return missingMapsRoutingContext;
}

public List<LatLon> getMissingMapsPoints() {
return missingMapsPoints;
}

public String getErrorMessage() {
String msg = "";
if (mapsToUpdate != null) {
msg = mapsToUpdate + " need to be updated";
}
if (missingMaps != null) {
if (msg.length() > 0) {
msg += " and ";
}
msg = missingMaps + " need to be downloaded";
}
msg = "To calculate the route maps " + msg;
return msg;
}

public MissingMapsCalculationResult prepare(OsmandRegions or) {
regionsToDownload = convert(or, mapsToDownload);
missingRegions = convert(or, missingMaps);
regionsToUpdate = convert(or, mapsToUpdate);
usedRegions = convert(or, usedMaps);
return this;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -21,12 +20,13 @@
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.LatLon;
import net.osmand.map.OsmandRegions;
import net.osmand.map.WorldRegion;
import net.osmand.util.Algorithms;
import net.osmand.util.CollectionUtils;
import net.osmand.util.MapUtils;

public class MissingMapsCalculator {
protected static final Log log = PlatformUtil.getLog(MissingMapsCalculator.class);

protected static final Log LOG = PlatformUtil.getLog(MissingMapsCalculator.class);

public static final double DISTANCE_SPLIT = 50000;
public static final double DISTANCE_SKIP = 10000;
Expand Down Expand Up @@ -93,24 +93,26 @@ public boolean checkIfThereAreMissingMaps(RoutingContext ctx, LatLon start, List
if (end != null) {
addPoint(ctx, knownMaps, pointsToCheck, end);
}
Set<String> usedMaps = new TreeSet<String>();
Set<String> mapsToDownload = new TreeSet<String>();
Set<String> mapsToUpdate = new TreeSet<String>();

List<LatLon> points = CollectionUtils.asOneList(Collections.singletonList(start), targets);
MissingMapsCalculationResult result = new MissingMapsCalculationResult(ctx, points);
Set<Long> presentTimestamps = null;
for (Point p : pointsToCheck) {
if (p.hhEditions == null) {
if (p.regions.size() > 0) {
mapsToDownload.add(p.regions.get(0));
result.addMissingMaps(p.regions.get(0));

}
} else if (checkHHEditions) {
if (presentTimestamps == null) {
presentTimestamps = new TreeSet<Long>(p.editionsUnique);
presentTimestamps = new TreeSet<>(p.editionsUnique);
} else if (!presentTimestamps.isEmpty()) {
presentTimestamps.retainAll(p.editionsUnique);
}
} else {
if (p.regions.size() > 0) {
usedMaps.add(p.regions.get(0));
result.addUsedMaps(p.regions.get(0));

}
}
}
Expand All @@ -136,9 +138,9 @@ public boolean checkIfThereAreMissingMaps(RoutingContext ctx, LatLon start, List
}
if (region != null) {
if (!fresh) {
mapsToUpdate.add(region);
result.addMapToUpdate(region);
} else {
usedMaps.add(region);
result.addUsedMaps(region);
}
}
}
Expand All @@ -147,28 +149,26 @@ public boolean checkIfThereAreMissingMaps(RoutingContext ctx, LatLon start, List
for (Point p : pointsToCheck) {
for (int i = 0; p.hhEditions != null && i < p.hhEditions.length; i++) {
if (p.hhEditions[i] == selectedEdition ) {
usedMaps.add(p.regions.get(i));
result.addUsedMaps(p.regions.get(i));
break;
}
}
}
}

// System.out.println("Used maps: " + usedMaps);
if (mapsToDownload.isEmpty() && mapsToUpdate.isEmpty()) {
if(!result.hasMissingMaps()) {
return false;
}
ctx.calculationProgress.requestMapsToUpdate = true;
ctx.calculationProgress.missingMaps = convert(mapsToDownload);
ctx.calculationProgress.mapsToUpdate = convert(mapsToUpdate);
ctx.calculationProgress.potentiallyUsedMaps = convert(usedMaps);

ctx.calculationProgress.missingMapsCalculationResult = result.prepare(or);

log.info(String.format("Check missing maps %d points %.2f sec", pointsToCheck.size(),
LOG.info(String.format("Check missing maps %d points %.2f sec", pointsToCheck.size(),
(System.nanoTime() - tm) / 1e9));
return true;
}

private LatLon testLatLons(List<LatLon> targets) throws IOException {
protected LatLon testLatLons(List<LatLon> targets) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(MissingMapsCalculator.class.getResourceAsStream("/latlons.test.txt")));
targets.clear();
String s = null;
Expand All @@ -179,19 +179,6 @@ private LatLon testLatLons(List<LatLon> targets) throws IOException {
return targets.get(0);
}

private List<WorldRegion> convert(Set<String> mapsToDownload) {
if (mapsToDownload.isEmpty()) {
return null;
}
List<WorldRegion> l = new ArrayList<WorldRegion>();
for (String m : mapsToDownload) {
WorldRegion wr = or.getRegionDataByDownloadName(m);
if (wr != null) {
l.add(wr);
}
}
return l;
}

private void addPoint(RoutingContext ctx, Map<String, RegisteredMap> knownMaps, List<Point> pointsToCheck, LatLon loc) throws IOException {
List<BinaryMapDataObject> resList = or.getRegionsToDownload(loc.getLatitude(), loc.getLongitude());
Expand Down Expand Up @@ -269,19 +256,4 @@ public void close() throws IOException {
}
}

public String getErrorMessage(RoutingContext ctx) {
String msg = "";
if (ctx.calculationProgress.mapsToUpdate != null) {
msg = ctx.calculationProgress.mapsToUpdate + " need to be updated";
}
if (ctx.calculationProgress.missingMaps != null) {
if (msg.length() > 0) {
msg += " and ";
}
msg = ctx.calculationProgress.missingMaps + " need to be downloaded";
}
msg = "To calculate the route maps " + msg;
return msg;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package net.osmand.router;

import net.osmand.map.WorldRegion;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

Expand Down Expand Up @@ -49,10 +46,7 @@ public class RouteCalculationProgress {
public boolean requestPrivateAccessRouting;

public long routeCalculationStartTime;
public boolean requestMapsToUpdate;
public List<WorldRegion> mapsToUpdate;
public List<WorldRegion> missingMaps;
public List<WorldRegion> potentiallyUsedMaps;
public MissingMapsCalculationResult missingMapsCalculationResult;

private int hhIterationStep = HHIteration.HH_NOT_STARTED.ordinal();
private int hhTargetsDone, hhTargetsTotal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.osmand.binary.RouteDataObject;
import net.osmand.data.LatLon;
import net.osmand.data.QuadPointDouble;
import net.osmand.map.OsmandRegions;
import net.osmand.router.BinaryRoutePlanner.RouteSegment;
import net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint;
import net.osmand.router.GeneralRouter.GeneralRouterProfile;
Expand All @@ -50,7 +49,6 @@ public class RoutePlannerFrontEnd {
private boolean useOnlyHHRouting = false;
private HHRoutingConfig hhRoutingConfig = null;
private HHRoutingType hhRoutingType = HHRoutingType.JAVA;
private static MissingMapsCalculator missingMapsCalculator;


public RoutePlannerFrontEnd() {
Expand Down Expand Up @@ -377,21 +375,13 @@ public boolean isUseNativeApproximation() {
return useNativeApproximation;
}

public static void initMissingMapsCalculator(OsmandRegions osmandRegions) {
if (missingMapsCalculator == null) {
missingMapsCalculator = new MissingMapsCalculator(osmandRegions);
}
}

public GpxRouteApproximation searchGpxRoute(GpxRouteApproximation gctx, List<GpxPoint> gpxPoints,
ResultMatcher<GpxRouteApproximation> resultMatcher,
boolean useExternalTimestamps)
throws IOException, InterruptedException {
boolean useExternalTimestamps) throws IOException, InterruptedException {
GpxRouteApproximation result;
if (useGeometryBasedApproximation) {
result = searchGpxSegments(gctx, gpxPoints, resultMatcher);
}
else {
} else {
result = searchGpxRouteByRouting(gctx, gpxPoints, resultMatcher);
}
if (useExternalTimestamps) {
Expand Down Expand Up @@ -988,12 +978,9 @@ public RouteCalcResult searchRoute(final RoutingContext ctx, LatLon start, LatLo
}
targets.add(end);
if (CALCULATE_MISSING_MAPS) {
if (missingMapsCalculator == null) {
missingMapsCalculator = new MissingMapsCalculator();
}
if (missingMapsCalculator.checkIfThereAreMissingMaps(ctx, start, targets,
hhRoutingConfig != null)) {
return new RouteCalcResult(missingMapsCalculator.getErrorMessage(ctx));
MissingMapsCalculator calculator = new MissingMapsCalculator(PlatformUtil.getOsmandRegions());
if (calculator.checkIfThereAreMissingMaps(ctx, start, targets, hhRoutingConfig != null)) {
return new RouteCalcResult(ctx.calculationProgress.missingMapsCalculationResult.getErrorMessage());
}
}
if (needRequestPrivateAccessRouting(ctx, targets)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public static <T> void addAllIfNotContains(Collection<T> collection, Collection<
}
}

public static <T> void addIfNotContains(T element, Collection<T> ... collections) {
for (Collection<T> collection : collections) {
addIfNotContains(collection, element);
}
}

public static <T> void addIfNotContains(Collection<T> collection, T element) {
if (!collection.contains(element)) {
collection.add(element);
Expand Down

0 comments on commit 67686ed

Please sign in to comment.