use of net.osmand.binary.BinaryMapIndexReader in project Osmand by osmandapp.
the class SearchPhrase method sortFiles.
public void sortFiles() {
if (indexes == null) {
indexes = new ArrayList<>(getOfflineIndexes());
}
Map<String, List<BinaryMapIndexReader>> diffsByRegion = getDiffsByRegion();
final LatLon ll = getLastTokenLocation();
if (ll != null) {
Collections.sort(indexes, new Comparator<BinaryMapIndexReader>() {
Map<BinaryMapIndexReader, LatLon> locations = new HashMap<>();
@Override
public int compare(BinaryMapIndexReader o1, BinaryMapIndexReader o2) {
LatLon rc1 = o1 == null ? null : getLocation(o1);
LatLon rc2 = o2 == null ? null : getLocation(o2);
double d1 = rc1 == null ? 10000000d : MapUtils.getDistance(rc1, ll);
double d2 = rc2 == null ? 10000000d : MapUtils.getDistance(rc2, ll);
return Double.compare(d1, d2);
}
private LatLon getLocation(BinaryMapIndexReader o1) {
if (locations.containsKey(o1)) {
return locations.get(o1);
}
LatLon rc1 = null;
if (o1.containsMapData()) {
rc1 = o1.getMapIndexes().get(0).getCenterLatLon();
} else {
rc1 = o1.getRegionCenter();
}
locations.put(o1, rc1);
return rc1;
}
});
if (!diffsByRegion.isEmpty()) {
List<BinaryMapIndexReader> finalSort = new ArrayList<>();
for (int i = 0; i < indexes.size(); i++) {
BinaryMapIndexReader currFile = indexes.get(i);
if (diffsByRegion.get(currFile.getRegionName()) != null) {
finalSort.addAll(diffsByRegion.get(currFile.getRegionName()));
finalSort.add(currFile);
} else {
finalSort.add(currFile);
}
}
indexes.clear();
indexes.addAll(finalSort);
}
}
}
use of net.osmand.binary.BinaryMapIndexReader in project Osmand by osmandapp.
the class RouteTestingTest method testRouting.
@Test
public void testRouting() throws Exception {
String fl = "../../resources/test-resources/Routing_test.obf";
RandomAccessFile raf = new RandomAccessFile(fl, "r");
RoutePlannerFrontEnd fe = new RoutePlannerFrontEnd(false);
BinaryMapIndexReader[] binaryMapIndexReaders = { new BinaryMapIndexReader(raf, new File(fl)) };
RoutingConfiguration.Builder builder = RoutingConfiguration.getDefault();
Map<String, String> params = te.getParams();
RoutingConfiguration config = builder.build(params.containsKey("vehicle") ? params.get("vehicle") : "car", RoutingConfiguration.DEFAULT_MEMORY_LIMIT * 3, params);
RoutingContext ctx = fe.buildRoutingContext(config, null, binaryMapIndexReaders, RoutePlannerFrontEnd.RouteCalculationMode.NORMAL);
ctx.leftSideNavigation = false;
List<RouteSegmentResult> routeSegments = fe.searchRoute(ctx, te.getStartPoint(), te.getEndPoint(), te.getTransitPoint());
Set<Long> reachedSegments = new TreeSet<Long>();
Assert.assertNotNull(routeSegments);
int prevSegment = -1;
for (int i = 0; i <= routeSegments.size(); i++) {
if (i == routeSegments.size() || routeSegments.get(i).getTurnType() != null) {
if (prevSegment >= 0) {
String name = routeSegments.get(prevSegment).getDescription();
long segmentId = routeSegments.get(prevSegment).getObject().getId() >> (BinaryInspector.SHIFT_ID);
System.out.println("segmentId: " + segmentId + " description: " + name);
}
prevSegment = i;
}
if (i < routeSegments.size()) {
reachedSegments.add(routeSegments.get(i).getObject().getId() >> (BinaryInspector.SHIFT_ID));
}
}
Map<Long, String> expectedResults = te.getExpectedResults();
Iterator<Entry<Long, String>> it = expectedResults.entrySet().iterator();
while (it.hasNext()) {
Entry<Long, String> es = it.next();
if (es.getValue().equals("false")) {
Assert.assertTrue("Expected segment " + (es.getKey()) + " was wrongly reached in route segments " + reachedSegments.toString(), !reachedSegments.contains(es.getKey()));
} else {
Assert.assertTrue("Expected segment " + (es.getKey()) + " weren't reached in route segments " + reachedSegments.toString(), reachedSegments.contains(es.getKey()));
}
}
}
use of net.osmand.binary.BinaryMapIndexReader in project Osmand by osmandapp.
the class RoutingContext method loadSubregionTile.
public void loadSubregionTile(final RoutingSubregionTile ts, boolean loadObjectsInMemory, List<RouteDataObject> toLoad, TLongHashSet excludeNotAllowed) {
boolean wasUnloaded = ts.isUnloaded();
int ucount = ts.getUnloadCont();
if (nativeLib == null) {
long now = System.nanoTime();
try {
BinaryMapIndexReader reader = reverseMap.get(ts.subregion.routeReg);
ts.setLoadedNonNative();
List<RouteDataObject> res = reader.loadRouteIndexData(ts.subregion);
// System.out.println(ts.subregion.shiftToData + " " + res);
if (toLoad != null) {
toLoad.addAll(res);
} else {
for (RouteDataObject ro : res) {
if (ro != null) {
if (config.router.acceptLine(ro)) {
if (excludeNotAllowed != null && !excludeNotAllowed.contains(ro.getId())) {
ts.add(ro);
}
} else if (excludeNotAllowed != null && ro.getId() > 0) {
excludeNotAllowed.add(ro.getId());
if (ts.excludedIds == null) {
ts.excludedIds = new TLongHashSet();
}
ts.excludedIds.add(ro.getId());
}
}
}
}
} catch (IOException e) {
throw new RuntimeException("Loading data exception", e);
}
timeToLoad += (System.nanoTime() - now);
} else {
long now = System.nanoTime();
NativeRouteSearchResult ns = nativeLib.loadRouteRegion(ts.subregion, loadObjectsInMemory);
// System.out.println(ts.subregion.shiftToData + " " + Arrays.toString(ns.objects));
ts.setLoadedNative(ns, this);
timeToLoad += (System.nanoTime() - now);
}
loadedTiles++;
if (wasUnloaded) {
if (ucount == 1) {
loadedPrevUnloadedTiles++;
}
} else {
if (global != null) {
global.allRoutes += ts.tileStatistics.allRoutes;
global.coordinates += ts.tileStatistics.coordinates;
}
distinctLoadedTiles++;
}
global.size += ts.tileStatistics.size;
}
use of net.osmand.binary.BinaryMapIndexReader in project Osmand by osmandapp.
the class RoutingContext method loadTileHeaders.
public List<RoutingSubregionTile> loadTileHeaders(final int zoomToLoadM31, int tileX, int tileY) {
SearchRequest<RouteDataObject> request = BinaryMapIndexReader.buildSearchRouteRequest(tileX << zoomToLoadM31, (tileX + 1) << zoomToLoadM31, tileY << zoomToLoadM31, (tileY + 1) << zoomToLoadM31, null);
List<RoutingSubregionTile> collection = null;
for (Entry<BinaryMapIndexReader, List<RouteSubregion>> r : map.entrySet()) {
// NOTE: load headers same as we do in non-native (it is not native optimized)
try {
if (r.getValue().size() > 0) {
long now = System.nanoTime();
// int rg = r.getValue().get(0).routeReg.regionsRead;
List<RouteSubregion> subregs = r.getKey().searchRouteIndexTree(request, r.getValue());
if (subregs.size() > 0) {
checkOldRoutingFiles(r.getKey());
}
for (RouteSubregion sr : subregs) {
int ind = searchSubregionTile(sr);
RoutingSubregionTile found;
if (ind < 0) {
found = new RoutingSubregionTile(sr);
subregionTiles.add(-(ind + 1), found);
} else {
found = subregionTiles.get(ind);
}
if (collection == null) {
collection = new ArrayList<RoutingContext.RoutingSubregionTile>(4);
}
collection.add(found);
}
timeToLoadHeaders += (System.nanoTime() - now);
}
} catch (IOException e) {
throw new RuntimeException("Loading data exception", e);
}
}
return collection;
}
use of net.osmand.binary.BinaryMapIndexReader in project Osmand by osmandapp.
the class RouteResultPreparationTest method setUp.
@BeforeClass
public static void setUp() throws Exception {
String fileName = "../../resources/test-resources/Turn_lanes_test.obf";
File fl = new File(fileName);
RandomAccessFile raf = new RandomAccessFile(fl, "r");
fe = new RoutePlannerFrontEnd(false);
RoutingConfiguration.Builder builder = RoutingConfiguration.getDefault();
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("car", "true");
RoutingConfiguration config = builder.build("car", RoutingConfiguration.DEFAULT_MEMORY_LIMIT * 3, params);
BinaryMapIndexReader[] binaryMapIndexReaders = { new BinaryMapIndexReader(raf, fl) };
ctx = fe.buildRoutingContext(config, null, binaryMapIndexReaders, RoutePlannerFrontEnd.RouteCalculationMode.NORMAL);
ctx.leftSideNavigation = false;
RouteResultPreparation.PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST = true;
}
Aggregations