Search in sources :

Example 21 with BinaryMapIndexReader

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) LatLon(net.osmand.data.LatLon) ArrayList(java.util.ArrayList) List(java.util.List)

Example 22 with BinaryMapIndexReader

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()));
        }
    }
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) Entry(java.util.Map.Entry) RandomAccessFile(java.io.RandomAccessFile) TreeSet(java.util.TreeSet) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 23 with BinaryMapIndexReader

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;
}
Also used : TLongHashSet(gnu.trove.set.hash.TLongHashSet) NativeRouteSearchResult(net.osmand.NativeLibrary.NativeRouteSearchResult) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) RouteDataObject(net.osmand.binary.RouteDataObject) IOException(java.io.IOException)

Example 24 with BinaryMapIndexReader

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;
}
Also used : RouteSubregion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IOException(java.io.IOException) RouteDataObject(net.osmand.binary.RouteDataObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with BinaryMapIndexReader

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;
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) BeforeClass(org.junit.BeforeClass)

Aggregations

BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)48 File (java.io.File)26 RandomAccessFile (java.io.RandomAccessFile)25 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)16 List (java.util.List)7 RoutePlannerFrontEnd (net.osmand.router.RoutePlannerFrontEnd)7 RoutingConfiguration (net.osmand.router.RoutingConfiguration)7 LinkedHashMap (java.util.LinkedHashMap)6 LatLon (net.osmand.data.LatLon)6 HashMap (java.util.HashMap)5 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)5 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)5 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)5 City (net.osmand.data.City)5 RoutingContext (net.osmand.router.RoutingContext)5 Point (java.awt.Point)4 FileOutputStream (java.io.FileOutputStream)4 GeocodingResult (net.osmand.binary.GeocodingUtilities.GeocodingResult)4 TLongHashSet (gnu.trove.set.hash.TLongHashSet)3