Search in sources :

Example 1 with PrecalculatedRouteDirection

use of net.osmand.router.PrecalculatedRouteDirection in project OsmAnd-tools by osmandapp.

the class MapRouterLayer method selfRoute.

public List<Way> selfRoute(LatLon start, LatLon end, List<LatLon> intermediates, List<RouteSegmentResult> previousRoute, RouteCalculationMode rm) {
    List<Way> res = new ArrayList<Way>();
    long time = System.currentTimeMillis();
    List<File> files = new ArrayList<File>();
    for (File f : Algorithms.getSortedFilesVersions(new File(DataExtractionSettings.getSettings().getBinaryFilesDir()))) {
        if (f.getName().endsWith(".obf")) {
            files.add(f);
        }
    }
    final boolean animateRoutingCalculation = DataExtractionSettings.getSettings().isAnimateRouting();
    if (animateRoutingCalculation) {
        nextTurn.setVisible(true);
        playPauseButton.setVisible(true);
        stopButton.setVisible(true);
        pause = true;
        playPauseButton.setText("Play");
    }
    stop = false;
    if (files.isEmpty()) {
        JOptionPane.showMessageDialog(OsmExtractionUI.MAIN_APP.getFrame(), "Please specify obf file in settings", "Obf file not found", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    System.out.println("Self made route from " + start + " to " + end);
    if (start != null && end != null) {
        try {
            BinaryMapIndexReader[] rs = new BinaryMapIndexReader[files.size()];
            int it = 0;
            for (File f : files) {
                // $NON-NLS-1$ //$NON-NLS-2$
                RandomAccessFile raf = new RandomAccessFile(f, "r");
                rs[it++] = new BinaryMapIndexReader(raf, f);
            }
            String m = DataExtractionSettings.getSettings().getRouteMode();
            String[] props = m.split("\\,");
            RoutePlannerFrontEnd router = new RoutePlannerFrontEnd(USE_OLD_ROUTING);
            Map<String, String> paramsR = new LinkedHashMap<String, String>();
            for (String p : props) {
                if (p.contains("=")) {
                    paramsR.put(p.split("=")[0], p.split("=")[1]);
                } else {
                    paramsR.put(p, "true");
                }
            }
            RoutingConfiguration config = DataExtractionSettings.getSettings().getRoutingConfig().build(props[0], /*RoutingConfiguration.DEFAULT_MEMORY_LIMIT*/
            1000, paramsR);
            PrecalculatedRouteDirection precalculatedRouteDirection = null;
            // Test gpx precalculation
            // LatLon[] lts = parseGPXDocument("/home/victor/projects/osmand/temp/esya.gpx");
            // start = lts[0];
            // end = lts[lts.length - 1];
            // System.out.println("Start " + start + " end " + end);
            // precalculatedRouteDirection = PrecalculatedRouteDirection.build(lts, config.router.getMaxDefaultSpeed());
            // precalculatedRouteDirection.setFollowNext(true);
            // config.planRoadDirection = 1;
            // Test initial direction
            // config.initialDirection = 90d / 180d * Math.PI; // EAST
            // config.initialDirection = 180d / 180d * Math.PI; // SOUTH
            // config.initialDirection = -90d / 180d * Math.PI; // WEST
            // config.initialDirection = 0 / 180d * Math.PI; // NORTH
            // config.NUMBER_OF_DESIRABLE_TILES_IN_MEMORY = 300;
            // config.ZOOM_TO_LOAD_TILES = 14;
            final RoutingContext ctx = router.buildRoutingContext(config, DataExtractionSettings.getSettings().useNativeRouting() ? NativeSwingRendering.getDefaultFromSettings() : null, rs, rm);
            ctx.leftSideNavigation = false;
            ctx.previouslyCalculatedRoute = previousRoute;
            log.info("Use " + config.routerName + "mode for routing");
            final DataTileManager<Entity> points = new DataTileManager<Entity>(11);
            map.setPoints(points);
            ctx.setVisitor(createSegmentVisitor(animateRoutingCalculation, points));
            // Choose native or not native
            long nt = System.nanoTime();
            startProgressThread(ctx);
            try {
                List<RouteSegmentResult> searchRoute = router.searchRoute(ctx, start, end, intermediates, precalculatedRouteDirection);
                throwExceptionIfRouteNotFound(ctx, searchRoute);
                System.out.println("External native time " + (System.nanoTime() - nt) / 1.0e9f);
                if (animateRoutingCalculation) {
                    playPauseButton.setVisible(false);
                    nextTurn.setText("FINISH");
                    waitNextPress();
                    nextTurn.setText(">>");
                }
                this.previousRoute = searchRoute;
                calculateResult(res, searchRoute);
            } finally {
                ctx.calculationProgress.isCancelled = true;
            }
        } catch (Exception e) {
            ExceptionHandler.handle(e);
        } finally {
            playPauseButton.setVisible(false);
            nextTurn.setVisible(false);
            stopButton.setVisible(false);
            if (map.getPoints() != null) {
                map.getPoints().clear();
            }
        }
        System.out.println("Finding self routes " + res.size() + " " + (System.currentTimeMillis() - time) + " ms");
    }
    return res;
}
Also used : Entity(net.osmand.osm.edit.Entity) ArrayList(java.util.ArrayList) Way(net.osmand.osm.edit.Way) LinkedHashMap(java.util.LinkedHashMap) RoutingContext(net.osmand.router.RoutingContext) DataTileManager(net.osmand.data.DataTileManager) RouteSegmentResult(net.osmand.router.RouteSegmentResult) PrecalculatedRouteDirection(net.osmand.router.PrecalculatedRouteDirection) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) Point(java.awt.Point) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) RoutingConfiguration(net.osmand.router.RoutingConfiguration) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with PrecalculatedRouteDirection

use of net.osmand.router.PrecalculatedRouteDirection in project Osmand by osmandapp.

the class RouteProvider method findVectorMapsRoute.

protected RouteCalculationResult findVectorMapsRoute(final RouteCalculationParams params, boolean calcGPXRoute) throws IOException {
    BinaryMapIndexReader[] files = params.ctx.getResourceManager().getRoutingMapFiles();
    RoutePlannerFrontEnd router = new RoutePlannerFrontEnd(false);
    OsmandSettings settings = params.ctx.getSettings();
    router.setUseFastRecalculation(settings.USE_FAST_RECALCULATION.get());
    RoutingConfiguration.Builder config = params.ctx.getDefaultRoutingConfig();
    GeneralRouter generalRouter = SettingsNavigationActivity.getRouter(config, params.mode);
    if (generalRouter == null) {
        return applicationModeNotSupported(params);
    }
    RoutingConfiguration cf = initOsmAndRoutingConfig(config, params, settings, generalRouter);
    if (cf == null) {
        return applicationModeNotSupported(params);
    }
    PrecalculatedRouteDirection precalculated = null;
    if (calcGPXRoute) {
        ArrayList<Location> sublist = findStartAndEndLocationsFromRoute(params.gpxRoute.points, params.start, params.end, null, null);
        LatLon[] latLon = new LatLon[sublist.size()];
        for (int k = 0; k < latLon.length; k++) {
            latLon[k] = new LatLon(sublist.get(k).getLatitude(), sublist.get(k).getLongitude());
        }
        precalculated = PrecalculatedRouteDirection.build(latLon, generalRouter.getMaxDefaultSpeed());
        precalculated.setFollowNext(true);
    // cf.planRoadDirection = 1;
    }
    // BUILD context
    NativeOsmandLibrary lib = settings.SAFE_MODE.get() ? null : NativeOsmandLibrary.getLoadedLibrary();
    // check loaded files
    int leftX = MapUtils.get31TileNumberX(params.start.getLongitude());
    int rightX = leftX;
    int bottomY = MapUtils.get31TileNumberY(params.start.getLatitude());
    int topY = bottomY;
    if (params.intermediates != null) {
        for (LatLon l : params.intermediates) {
            leftX = Math.min(MapUtils.get31TileNumberX(l.getLongitude()), leftX);
            rightX = Math.max(MapUtils.get31TileNumberX(l.getLongitude()), rightX);
            bottomY = Math.max(MapUtils.get31TileNumberY(l.getLatitude()), bottomY);
            topY = Math.min(MapUtils.get31TileNumberY(l.getLatitude()), topY);
        }
    }
    LatLon l = params.end;
    leftX = Math.min(MapUtils.get31TileNumberX(l.getLongitude()), leftX);
    rightX = Math.max(MapUtils.get31TileNumberX(l.getLongitude()), rightX);
    bottomY = Math.max(MapUtils.get31TileNumberY(l.getLatitude()), bottomY);
    topY = Math.min(MapUtils.get31TileNumberY(l.getLatitude()), topY);
    params.ctx.getResourceManager().getRenderer().checkInitialized(15, lib, leftX, rightX, bottomY, topY);
    RoutingContext ctx = router.buildRoutingContext(cf, lib, files, RouteCalculationMode.NORMAL);
    RoutingContext complexCtx = null;
    boolean complex = params.mode.isDerivedRoutingFrom(ApplicationMode.CAR) && !settings.DISABLE_COMPLEX_ROUTING.get() && precalculated == null;
    ctx.leftSideNavigation = params.leftSide;
    ctx.calculationProgress = params.calculationProgress;
    if (params.previousToRecalculate != null && params.onlyStartPointChanged) {
        int currentRoute = params.previousToRecalculate.getCurrentRoute();
        List<RouteSegmentResult> originalRoute = params.previousToRecalculate.getOriginalRoute();
        if (originalRoute != null && currentRoute < originalRoute.size()) {
            ctx.previouslyCalculatedRoute = originalRoute.subList(currentRoute, originalRoute.size());
        }
    }
    if (complex && router.getRecalculationEnd(ctx) != null) {
        complex = false;
    }
    if (complex) {
        complexCtx = router.buildRoutingContext(cf, lib, files, RouteCalculationMode.COMPLEX);
        complexCtx.calculationProgress = params.calculationProgress;
        complexCtx.leftSideNavigation = params.leftSide;
        complexCtx.previouslyCalculatedRoute = ctx.previouslyCalculatedRoute;
    }
    LatLon st = new LatLon(params.start.getLatitude(), params.start.getLongitude());
    LatLon en = new LatLon(params.end.getLatitude(), params.end.getLongitude());
    List<LatLon> inters = new ArrayList<LatLon>();
    if (params.intermediates != null) {
        inters = new ArrayList<LatLon>(params.intermediates);
    }
    return calcOfflineRouteImpl(params, router, ctx, complexCtx, st, en, inters, precalculated);
}
Also used : PrecalculatedRouteDirection(net.osmand.router.PrecalculatedRouteDirection) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) GeneralRouter(net.osmand.router.GeneralRouter) OsmandSettings(net.osmand.plus.OsmandSettings) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) LocationPoint(net.osmand.data.LocationPoint) LatLon(net.osmand.data.LatLon) RoutingContext(net.osmand.router.RoutingContext) Builder(net.osmand.router.RoutingConfiguration.Builder) RoutingConfiguration(net.osmand.router.RoutingConfiguration) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) NativeOsmandLibrary(net.osmand.plus.render.NativeOsmandLibrary) RouteSegmentResult(net.osmand.router.RouteSegmentResult) Location(net.osmand.Location)

Aggregations

ArrayList (java.util.ArrayList)2 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)2 PrecalculatedRouteDirection (net.osmand.router.PrecalculatedRouteDirection)2 RoutePlannerFrontEnd (net.osmand.router.RoutePlannerFrontEnd)2 RouteSegmentResult (net.osmand.router.RouteSegmentResult)2 RoutingConfiguration (net.osmand.router.RoutingConfiguration)2 RoutingContext (net.osmand.router.RoutingContext)2 Point (java.awt.Point)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 LinkedHashMap (java.util.LinkedHashMap)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Location (net.osmand.Location)1 DataTileManager (net.osmand.data.DataTileManager)1 LatLon (net.osmand.data.LatLon)1 LocationPoint (net.osmand.data.LocationPoint)1 Entity (net.osmand.osm.edit.Entity)1 Way (net.osmand.osm.edit.Way)1