Search in sources :

Example 16 with RouteSegment

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

the class MapClusterLayer method processSegment.

private void processSegment(ClusteringContext cCtx, RoutingContext ctx, RouteSegment segment, Queue<RouteSegment> queue, List<RouteSegment> result, int tileX, int tileY, boolean direction) {
    int d = 1;
    boolean directionAllowed = true;
    int prev = segment.getSegmentStart();
    while (directionAllowed) {
        int segmentEnd = segment.getSegmentStart() + (direction ? d : -d);
        d++;
        if (segmentEnd < 0 || segmentEnd >= segment.getRoad().getPointsLength()) {
            directionAllowed = false;
            continue;
        }
        int x = segment.getRoad().getPoint31XTile(segmentEnd);
        int y = segment.getRoad().getPoint31YTile(segmentEnd);
        int tX = x >> cCtx.zm;
        int tY = y >> cCtx.zm;
        cCtx.segmentsProcessed++;
        if (notClusterAtAll(cCtx, segment.getRoad())) {
            cCtx.outOfTile++;
            addSegmentResult(result, segment, prev, segmentEnd);
            return;
        }
        if (Math.abs(tX - tileX) > cCtx.LOCAL_TILE_BOUNDARIES || Math.abs(tY - tileY) > cCtx.LOCAL_TILE_BOUNDARIES) {
            // System.out.println("Res " + segment.getRoad());
            cCtx.outOfDistance++;
            addSegmentResult(result, segment, prev, segmentEnd);
            return;
        }
        RouteSegment next = ctx.loadRouteSegment(x, y, 0);
        while (next != null) {
            // System.out.println(" >> " + next.getRoad());
            queue.add(next);
            next = next.getNext();
        }
        prev = segmentEnd;
    }
}
Also used : RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment) Point(java.awt.Point)

Example 17 with RouteSegment

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

the class MapClusterLayer method clustering.

private List<RouteSegment> clustering(final ClusteringContext clusterCtx, double lat, double lon, final DataTileManager<Way> points) throws IOException {
    List<BinaryMapIndexReader> rs = new ArrayList<BinaryMapIndexReader>();
    for (File f : new File(DataExtractionSettings.getSettings().getBinaryFilesDir()).listFiles()) {
        if (f.getName().endsWith(".obf")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            RandomAccessFile raf = new RandomAccessFile(f, "r");
            rs.add(new BinaryMapIndexReader(raf, f));
        }
    }
    RoutePlannerFrontEnd router = new RoutePlannerFrontEnd(true);
    Builder builder = RoutingConfiguration.getDefault();
    RoutingConfiguration config = builder.build("car", RoutingConfiguration.DEFAULT_MEMORY_LIMIT * 3);
    RoutingContext ctx = router.buildRoutingContext(config, NativeSwingRendering.getDefaultFromSettings(), rs.toArray(new BinaryMapIndexReader[rs.size()]), clusterCtx.BASEMAP_CLUSTERING ? RouteCalculationMode.BASE : RouteCalculationMode.NORMAL);
    // find closest way
    RouteSegment st = router.findRouteSegment(lat, lon, ctx, null);
    if (st != null) {
        RouteDataObject road = st.getRoad();
        String highway = getHighway(road);
        log.info(// road.getName() + " "
        "ROAD TO START " + highway + " " + +road.id);
    }
    map.setPoints(points);
    ctx.setVisitor(new RouteSegmentVisitor() {

        private List<RouteSegment> cache = new ArrayList<RouteSegment>();

        @Override
        public void visitSegment(RouteSegment s, int endSegment, boolean poll) {
            if (!clusterCtx.ANIMATE_CLUSTERING) {
                return;
            }
            cache.add(s);
            if (cache.size() < SIZE_OF_ROUTES_TO_ANIMATE) {
                return;
            }
            for (RouteSegment segment : cache) {
                Way way = new Way(-1);
                for (int i = 0; i < segment.getRoad().getPointsLength(); i++) {
                    net.osmand.osm.edit.Node n = new net.osmand.osm.edit.Node(MapUtils.get31LatitudeY(segment.getRoad().getPoint31YTile(i)), MapUtils.get31LongitudeX(segment.getRoad().getPoint31XTile(i)), -1);
                    way.addNode(n);
                }
                way.putTag("color", "white");
                LatLon n = way.getLatLon();
                points.registerObject(n.getLatitude(), n.getLongitude(), way);
            }
            cache.clear();
            try {
                SwingUtilities.invokeAndWait(new Runnable() {

                    @Override
                    public void run() {
                        map.prepareImage();
                    }
                });
            } catch (InterruptedException e1) {
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    });
    List<RouteSegment> results = searchCluster(clusterCtx, ctx, st);
    return results;
}
Also used : Builder(net.osmand.router.RoutingConfiguration.Builder) ArrayList(java.util.ArrayList) RouteSegmentVisitor(net.osmand.router.BinaryRoutePlanner.RouteSegmentVisitor) Way(net.osmand.osm.edit.Way) RoutingContext(net.osmand.router.RoutingContext) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) Point(java.awt.Point) InvocationTargetException(java.lang.reflect.InvocationTargetException) LatLon(net.osmand.data.LatLon) RandomAccessFile(java.io.RandomAccessFile) RoutingConfiguration(net.osmand.router.RoutingConfiguration) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) RouteDataObject(net.osmand.binary.RouteDataObject) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 18 with RouteSegment

use of net.osmand.router.BinaryRoutePlanner.RouteSegment in project Osmand by osmandapp.

the class BinaryRoutePlannerOld method proccessRestrictions.

private boolean proccessRestrictions(RoutingContext ctx, RouteDataObject road, RouteSegment inputNext, boolean reverseWay) {
    ctx.segmentsToVisitPrescripted.clear();
    ctx.segmentsToVisitNotForbidden.clear();
    boolean exclusiveRestriction = false;
    RouteSegment next = inputNext;
    if (!reverseWay && road.getRestrictionLength() == 0) {
        return false;
    }
    if (!ctx.getRouter().restrictionsAware()) {
        return false;
    }
    while (next != null) {
        int type = -1;
        if (!reverseWay) {
            for (int i = 0; i < road.getRestrictionLength(); i++) {
                if (road.getRestrictionId(i) == next.road.id) {
                    type = road.getRestrictionType(i);
                    break;
                }
            }
        } else {
            for (int i = 0; i < next.road.getRestrictionLength(); i++) {
                int rt = next.road.getRestrictionType(i);
                long restrictedTo = next.road.getRestrictionId(i);
                if (restrictedTo == road.id) {
                    type = rt;
                    break;
                }
                // Check if there is restriction only to the other than current road
                if (rt == MapRenderingTypes.RESTRICTION_ONLY_RIGHT_TURN || rt == MapRenderingTypes.RESTRICTION_ONLY_LEFT_TURN || rt == MapRenderingTypes.RESTRICTION_ONLY_STRAIGHT_ON) {
                    // check if that restriction applies to considered junk
                    RouteSegment foundNext = inputNext;
                    while (foundNext != null) {
                        if (foundNext.getRoad().id == restrictedTo) {
                            break;
                        }
                        foundNext = foundNext.next;
                    }
                    if (foundNext != null) {
                        // special constant
                        type = REVERSE_WAY_RESTRICTION_ONLY;
                    }
                }
            }
        }
        if (type == REVERSE_WAY_RESTRICTION_ONLY) {
        // next = next.next; continue;
        } else if (type == -1 && exclusiveRestriction) {
        // next = next.next; continue;
        } else if (type == MapRenderingTypes.RESTRICTION_NO_LEFT_TURN || type == MapRenderingTypes.RESTRICTION_NO_RIGHT_TURN || type == MapRenderingTypes.RESTRICTION_NO_STRAIGHT_ON || type == MapRenderingTypes.RESTRICTION_NO_U_TURN) {
        // next = next.next; continue;
        } else if (type == -1) {
            // case no restriction
            ctx.segmentsToVisitNotForbidden.add(next);
        } else {
            // 2. in case we are going forward we have one "in" and many "out"
            if (!reverseWay) {
                exclusiveRestriction = true;
                ctx.segmentsToVisitNotForbidden.clear();
                ctx.segmentsToVisitPrescripted.add(next);
            } else {
                ctx.segmentsToVisitNotForbidden.add(next);
            }
        }
        next = next.next;
    }
    ctx.segmentsToVisitPrescripted.addAll(ctx.segmentsToVisitNotForbidden);
    return true;
}
Also used : RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment) FinalRouteSegment(net.osmand.router.BinaryRoutePlanner.FinalRouteSegment)

Example 19 with RouteSegment

use of net.osmand.router.BinaryRoutePlanner.RouteSegment in project Osmand by osmandapp.

the class RoutePlannerFrontEnd method searchRoute.

public List<RouteSegmentResult> searchRoute(final RoutingContext ctx, LatLon start, LatLon end, List<LatLon> intermediates, PrecalculatedRouteDirection routeDirection) throws IOException, InterruptedException {
    if (ctx.calculationProgress == null) {
        ctx.calculationProgress = new RouteCalculationProgress();
    }
    boolean intermediatesEmpty = intermediates == null || intermediates.isEmpty();
    List<LatLon> targets = new ArrayList<>();
    targets.add(end);
    if (!intermediatesEmpty) {
        targets.addAll(intermediates);
    }
    if (needRequestPrivateAccessRouting(ctx, targets)) {
        ctx.calculationProgress.requestPrivateAccessRouting = true;
    }
    double maxDistance = MapUtils.getDistance(start, end);
    if (!intermediatesEmpty) {
        LatLon b = start;
        for (LatLon l : intermediates) {
            maxDistance = Math.max(MapUtils.getDistance(b, l), maxDistance);
            b = l;
        }
    }
    if (ctx.calculationMode == RouteCalculationMode.COMPLEX && routeDirection == null && maxDistance > ctx.config.DEVIATION_RADIUS * 6) {
        RoutingContext nctx = buildRoutingContext(ctx.config, ctx.nativeLib, ctx.getMaps(), RouteCalculationMode.BASE);
        nctx.calculationProgress = ctx.calculationProgress;
        List<RouteSegmentResult> ls = searchRoute(nctx, start, end, intermediates);
        routeDirection = PrecalculatedRouteDirection.build(ls, ctx.config.DEVIATION_RADIUS, ctx.getRouter().getMaxDefaultSpeed());
    }
    if (intermediatesEmpty && ctx.nativeLib != null) {
        ctx.startX = MapUtils.get31TileNumberX(start.getLongitude());
        ctx.startY = MapUtils.get31TileNumberY(start.getLatitude());
        ctx.targetX = MapUtils.get31TileNumberX(end.getLongitude());
        ctx.targetY = MapUtils.get31TileNumberY(end.getLatitude());
        RouteSegment recalculationEnd = getRecalculationEnd(ctx);
        if (recalculationEnd != null) {
            ctx.initTargetPoint(recalculationEnd);
        }
        if (routeDirection != null) {
            ctx.precalculatedRouteDirection = routeDirection.adopt(ctx);
        }
        List<RouteSegmentResult> res = runNativeRouting(ctx, recalculationEnd);
        if (res != null) {
            new RouteResultPreparation().printResults(ctx, start, end, res);
        }
        makeStartEndPointsPrecise(res, start, end, intermediates);
        return res;
    }
    int indexNotFound = 0;
    List<RouteSegmentPoint> points = new ArrayList<RouteSegmentPoint>();
    if (!addSegment(start, ctx, indexNotFound++, points)) {
        return null;
    }
    if (intermediates != null) {
        for (LatLon l : intermediates) {
            if (!addSegment(l, ctx, indexNotFound++, points)) {
                return null;
            }
        }
    }
    if (!addSegment(end, ctx, indexNotFound++, points)) {
        return null;
    }
    List<RouteSegmentResult> res = searchRoute(ctx, points, routeDirection);
    // make start and end more precise
    makeStartEndPointsPrecise(res, start, end, intermediates);
    if (res != null) {
        new RouteResultPreparation().printResults(ctx, start, end, res);
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) RouteSegmentPoint(net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint) QuadPoint(net.osmand.data.QuadPoint) LatLon(net.osmand.data.LatLon) RouteSegmentPoint(net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment)

Example 20 with RouteSegment

use of net.osmand.router.BinaryRoutePlanner.RouteSegment in project Osmand by osmandapp.

the class TestRouting method calculateRoute.

private static void calculateRoute(double startLat, double startLon, double endLat, double endLon, BinaryMapIndexReader[] rs) throws IOException, InterruptedException {
    long ts = System.currentTimeMillis();
    Builder config = RoutingConfiguration.getDefault();
    RoutingConfiguration rconfig = config.build(vehicle, MEMORY_TEST_LIMIT);
    RoutePlannerFrontEnd router = new RoutePlannerFrontEnd(oldRouting);
    RoutingContext ctx = router.buildRoutingContext(rconfig, lib, rs);
    RouteSegment startSegment = router.findRouteSegment(startLat, startLon, ctx, null);
    RouteSegment endSegment = router.findRouteSegment(endLat, endLon, ctx, null);
    if (startSegment == null) {
        throw new IllegalArgumentException("Start segment is not found ");
    }
    if (endSegment == null) {
        throw new IllegalArgumentException("End segment is not found ");
    }
    // Clear ctx
    ctx = router.buildRoutingContext(rconfig, lib, rs);
    List<RouteSegmentResult> route = router.searchRoute(ctx, new LatLon(startLat, startLon), new LatLon(endLat, endLon), null);
    System.out.println("Route is " + route.size() + " segments " + (System.currentTimeMillis() - ts) + " ms ");
}
Also used : LatLon(net.osmand.data.LatLon) Builder(net.osmand.router.RoutingConfiguration.Builder) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment) FinalRouteSegment(net.osmand.router.BinaryRoutePlanner.FinalRouteSegment)

Aggregations

RouteSegment (net.osmand.router.BinaryRoutePlanner.RouteSegment)22 ArrayList (java.util.ArrayList)10 FinalRouteSegment (net.osmand.router.BinaryRoutePlanner.FinalRouteSegment)9 RouteDataObject (net.osmand.binary.RouteDataObject)8 Point (java.awt.Point)5 LatLon (net.osmand.data.LatLon)5 PriorityQueue (java.util.PriorityQueue)3 Way (net.osmand.osm.edit.Way)3 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 TLongHashSet (gnu.trove.set.hash.TLongHashSet)2 DataTileManager (net.osmand.data.DataTileManager)2 QuadPoint (net.osmand.data.QuadPoint)2 RouteSegmentPoint (net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint)2 RouteSegmentVisitor (net.osmand.router.BinaryRoutePlanner.RouteSegmentVisitor)2 Builder (net.osmand.router.RoutingConfiguration.Builder)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1