Skip to content

Commit

Permalink
Update default classes
Browse files Browse the repository at this point in the history
  • Loading branch information
vshcherb committed May 14, 2024
1 parent 09d3b74 commit de0d1bf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,80 @@
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;

public boolean requestMapsToUpdate;
public List<WorldRegion> mapsToDownload; // both missing and update
public List<WorldRegion> mapsToUpdate;
public List<WorldRegion> missingMaps;
public List<WorldRegion> potentiallyUsedMaps;
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 mapsToDownload;
return regionsToDownload;
}

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

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

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

public RoutingContext getMissingMapsRoutingContext() {
Expand All @@ -64,4 +99,14 @@ public String getErrorMessage() {
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 @@ -6,7 +6,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -21,7 +20,6 @@
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;
Expand Down Expand Up @@ -95,17 +93,15 @@ public boolean checkIfThereAreMissingMaps(RoutingContext ctx, LatLon start, List
if (end != null) {
addPoint(ctx, knownMaps, pointsToCheck, end);
}
Set<String> usedMaps = new LinkedHashSet<>();
Set<String> mapsToDownload = new LinkedHashSet<>();
Set<String> missingMaps = new LinkedHashSet<>();
Set<String> mapsToUpdate = new LinkedHashSet<>();

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) {
String region = p.regions.get(0);
missingMaps.add(region);
mapsToDownload.add(region);
result.addMissingMaps(p.regions.get(0));

}
} else if (checkHHEditions) {
if (presentTimestamps == null) {
Expand All @@ -115,7 +111,8 @@ public boolean checkIfThereAreMissingMaps(RoutingContext ctx, LatLon start, List
}
} else {
if (p.regions.size() > 0) {
usedMaps.add(p.regions.get(0));
result.addUsedMaps(p.regions.get(0));

}
}
}
Expand All @@ -141,10 +138,9 @@ public boolean checkIfThereAreMissingMaps(RoutingContext ctx, LatLon start, List
}
if (region != null) {
if (!fresh) {
mapsToUpdate.add(region);
mapsToDownload.add(region);
result.addMapToUpdate(region);
} else {
usedMaps.add(region);
result.addUsedMaps(region);
}
}
}
Expand All @@ -153,32 +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;
}
List<LatLon> points = CollectionUtils.asOneList(Collections.singletonList(start), targets);
MissingMapsCalculationResult result = new MissingMapsCalculationResult(ctx, points);
result.requestMapsToUpdate = true;
result.mapsToDownload = convert(mapsToDownload);
result.missingMaps = convert(missingMaps);
result.mapsToUpdate = convert(mapsToUpdate);
result.potentiallyUsedMaps = convert(usedMaps);
ctx.calculationProgress.missingMapsCalculationResult = result;

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

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 @@ -189,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<>();
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

0 comments on commit de0d1bf

Please sign in to comment.