Search in sources :

Example 1 with RouteSegmentResult

use of net.osmand.router.RouteSegmentResult 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 RouteSegmentResult

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

the class RoutingHelper method getCurrentName.

// protected boolean isDistanceLess(float currentSpeed, double dist, double etalon, float defSpeed){
// if(dist < etalon || ((dist / currentSpeed) < (etalon / defSpeed))){
// return true;
// }
// return false;
// }
public synchronized String getCurrentName(TurnType[] next) {
    NextDirectionInfo n = getNextRouteDirectionInfo(new NextDirectionInfo(), true);
    Location l = lastFixedLocation;
    float speed = 0;
    if (l != null && l.hasSpeed()) {
        speed = l.getSpeed();
    }
    if (n.distanceTo > 0 && n.directionInfo != null && !n.directionInfo.getTurnType().isSkipToSpeak() && voiceRouter.isDistanceLess(speed, n.distanceTo, voiceRouter.PREPARE_DISTANCE * 0.75f, 0f)) {
        String nm = n.directionInfo.getStreetName();
        String rf = n.directionInfo.getRef();
        String dn = n.directionInfo.getDestinationName();
        if (next != null) {
            next[0] = n.directionInfo.getTurnType();
        }
        return formatStreetName(nm, rf, dn, "»");
    }
    RouteSegmentResult rs = getCurrentSegmentResult();
    if (rs != null) {
        String nm = rs.getObject().getName(settings.MAP_PREFERRED_LOCALE.get(), settings.MAP_TRANSLITERATE_NAMES.get());
        String rf = rs.getObject().getRef(settings.MAP_PREFERRED_LOCALE.get(), settings.MAP_TRANSLITERATE_NAMES.get(), rs.isForwardDirection());
        String dn = rs.getObject().getDestinationName(settings.MAP_PREFERRED_LOCALE.get(), settings.MAP_TRANSLITERATE_NAMES.get(), rs.isForwardDirection());
        return formatStreetName(nm, rf, dn, "»");
    }
    return null;
}
Also used : NextDirectionInfo(net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo) RouteSegmentResult(net.osmand.router.RouteSegmentResult) Location(net.osmand.Location)

Example 3 with RouteSegmentResult

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

the class RouteCalculationResult method getOriginalRoute.

public List<RouteSegmentResult> getOriginalRoute() {
    if (segments.size() == 0) {
        return null;
    }
    List<RouteSegmentResult> list = new ArrayList<RouteSegmentResult>();
    list.add(segments.get(0));
    for (int i = 1; i < segments.size(); i++) {
        if (segments.get(i - 1) != segments.get(i)) {
            list.add(segments.get(i));
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) RouteSegmentResult(net.osmand.router.RouteSegmentResult) LocationPoint(net.osmand.data.LocationPoint)

Example 4 with RouteSegmentResult

use of net.osmand.router.RouteSegmentResult 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)

Example 5 with RouteSegmentResult

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

the class MapRouterLayer method calculateResult.

private void calculateResult(List<Way> res, List<RouteSegmentResult> searchRoute) {
    net.osmand.osm.edit.Node prevWayNode = null;
    for (RouteSegmentResult s : searchRoute) {
        // double dist = MapUtils.getDistance(s.startPoint, s.endPoint);
        Way way = new Way(-1);
        // String name = String.format("time %.2f ", s.getSegmentTime());
        String name = s.getDescription();
        if (s.getTurnType() != null) {
            name += " (TA " + s.getTurnType().getTurnAngle() + ") ";
        }
        // String name = String.format("beg %.2f end %.2f ", s.getBearingBegin(), s.getBearingEnd());
        way.putTag(OSMTagKey.NAME.getValue(), name);
        boolean plus = s.getStartPointIndex() < s.getEndPointIndex();
        int i = s.getStartPointIndex();
        while (true) {
            LatLon l = s.getPoint(i);
            net.osmand.osm.edit.Node n = new net.osmand.osm.edit.Node(l.getLatitude(), l.getLongitude(), -1);
            if (prevWayNode != null) {
                if (OsmMapUtils.getDistance(prevWayNode, n) > 0) {
                    System.out.println("Warning not connected road " + " " + s.getObject().id + " dist " + OsmMapUtils.getDistance(prevWayNode, n));
                }
                prevWayNode = null;
            }
            way.addNode(n);
            if (i == s.getEndPointIndex()) {
                break;
            }
            if (plus) {
                i++;
            } else {
                i--;
            }
        }
        if (way.getNodes().size() > 0) {
            prevWayNode = way.getNodes().get(way.getNodes().size() - 1);
        }
        res.add(way);
    }
}
Also used : LatLon(net.osmand.data.LatLon) Node(org.w3c.dom.Node) RouteSegmentResult(net.osmand.router.RouteSegmentResult) Way(net.osmand.osm.edit.Way) Point(java.awt.Point)

Aggregations

RouteSegmentResult (net.osmand.router.RouteSegmentResult)9 ArrayList (java.util.ArrayList)5 LatLon (net.osmand.data.LatLon)4 LocationPoint (net.osmand.data.LocationPoint)4 Location (net.osmand.Location)3 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)3 RoutePlannerFrontEnd (net.osmand.router.RoutePlannerFrontEnd)3 RoutingConfiguration (net.osmand.router.RoutingConfiguration)3 RoutingContext (net.osmand.router.RoutingContext)3 Point (java.awt.Point)2 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 Way (net.osmand.osm.edit.Way)2 NextDirectionInfo (net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo)2 PrecalculatedRouteDirection (net.osmand.router.PrecalculatedRouteDirection)2 Builder (net.osmand.router.RoutingConfiguration.Builder)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 LinkedHashMap (java.util.LinkedHashMap)1