Search in sources :

Example 1 with RouteSegment

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

the class MapRouterLayer method createSegmentVisitor.

private RouteSegmentVisitor createSegmentVisitor(final boolean animateRoutingCalculation, final DataTileManager<Entity> points) {
    return new RouteSegmentVisitor() {

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

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

        private List<Integer> cacheInt = new ArrayList<Integer>();

        @Override
        public void visitSegment(RouteSegment s, int endSegment, boolean poll) {
            if (stop) {
                throw new RuntimeException("Interrupted");
            }
            if (!animateRoutingCalculation) {
                return;
            }
            if (!poll && pause) {
                pollCache.add(s);
                return;
            }
            cache.add(s);
            cacheInt.add(endSegment);
            if (cache.size() < steps) {
                return;
            }
            if (pause) {
                registerObjects(points, poll, pollCache, null);
                pollCache.clear();
            }
            registerObjects(points, !poll, cache, cacheInt);
            cache.clear();
            cacheInt.clear();
            redraw();
            if (pause) {
                waitNextPress();
            }
        }

        private void registerObjects(final DataTileManager<Entity> points, boolean white, List<RouteSegment> registerCache, List<Integer> cacheInt) {
            for (int l = 0; l < registerCache.size(); l++) {
                RouteSegment segment = registerCache.get(l);
                Way way = new Way(-1);
                way.putTag(OSMTagKey.NAME.getValue(), segment.getTestName());
                if (white) {
                    way.putTag("color", "white");
                }
                int from = cacheInt != null ? segment.getSegmentStart() : segment.getSegmentStart() - 2;
                int to = cacheInt != null ? cacheInt.get(l) : segment.getSegmentStart() + 2;
                if (from > to) {
                    int x = from;
                    from = to;
                    to = x;
                }
                for (int i = from; i <= to; i++) {
                    if (i >= 0 && i < segment.getRoad().getPointsLength()) {
                        net.osmand.osm.edit.Node n = createNode(segment, i);
                        way.addNode(n);
                    }
                }
                LatLon n = way.getLatLon();
                points.registerObject(n.getLatitude(), n.getLongitude(), way);
            }
        }
    };
}
Also used : RouteSegmentVisitor(net.osmand.router.BinaryRoutePlanner.RouteSegmentVisitor) Point(java.awt.Point) Way(net.osmand.osm.edit.Way) LatLon(net.osmand.data.LatLon) DataTileManager(net.osmand.data.DataTileManager) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment)

Example 2 with RouteSegment

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

the class CheckRoadConnectivity method processSegment.

private RouteSegment processSegment(RoutingContext ctx, RouteSegment segment, PriorityQueue<RouteSegment> queue, TLongHashSet visited, TLongObjectHashMap<List<RouteDataObject>> all, boolean direction, boolean start) {
    int ind = segment.getSegmentStart();
    RouteDataObject road = segment.getRoad();
    final long pid = calcPointIdUnique(segment.getRoad(), ind);
    if (visited.contains(pid)) {
        return null;
    }
    visited.add(pid);
    double distFromStart = segment.getDistanceFromStart();
    while (true) {
        int py = road.getPoint31YTile(ind);
        int px = road.getPoint31XTile(ind);
        if (direction) {
            ind++;
        } else {
            ind--;
        }
        if (ind < 0 || ind >= segment.getRoad().getPointsLength()) {
            break;
        }
        if (all.contains(calcPointId(segment.getRoad(), ind)) && !start) {
            return segment;
        }
        visited.add(calcPointIdUnique(segment.getRoad(), ind));
        int x = road.getPoint31XTile(ind);
        int y = road.getPoint31YTile(ind);
        distFromStart += MapUtils.squareDist31TileMetric(px, py, x, y) / ctx.getRouter().defineRoutingSpeed(road) * ctx.getRouter().defineSpeedPriority(road);
        RouteSegment rs = ctx.loadRouteSegment(x, y, 0);
        while (rs != null) {
            if (!visited.contains(calcPointIdUnique(rs.getRoad(), rs.getSegmentStart()))) {
                if (!queue.contains(rs) || rs.getDistanceFromStart() > distFromStart) {
                    rs.setDistanceFromStart((float) distFromStart);
                    rs.setParentSegmentEnd(ind);
                    rs.setParentRoute(segment);
                    queue.remove(rs);
                    queue.add(rs);
                }
            }
            rs = rs.getNext();
        }
    }
    return null;
}
Also used : RouteDataObject(net.osmand.binary.RouteDataObject) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment)

Example 3 with RouteSegment

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

the class CheckRoadConnectivity method findConnectedRoads.

private List<RouteDataObject> findConnectedRoads(RoutingContext ctx, RouteDataObject initial, boolean begin, TLongObjectHashMap<List<RouteDataObject>> all) {
    PriorityQueue<RouteSegment> queue = new PriorityQueue<RouteSegment>(10, new Comparator<RouteSegment>() {

        @Override
        public int compare(RouteSegment o1, RouteSegment o2) {
            return Double.compare(o1.getDistanceFromStart(), o2.getDistanceFromStart());
        }
    });
    VehicleRouter router = ctx.getRouter();
    ArrayList<RouteDataObject> next = new ArrayList<RouteDataObject>();
    ctx.loadTileData(initial.getPoint31XTile(0), initial.getPoint31YTile(0), 17, next);
    for (RouteDataObject n : next) {
        if (n.id == initial.id) {
            initial = n;
            break;
        }
    }
    queue.add(new RouteSegment(initial, begin ? 1 : initial.getPointsLength() - 2));
    TLongHashSet visited = new TLongHashSet();
    RouteSegment finalSegment = null;
    while (!queue.isEmpty() && finalSegment == null) {
        RouteSegment segment = queue.poll();
        int oneWay = router.isOneWay(segment.getRoad());
        boolean start = initial.id == segment.getRoad().id;
        if (start) {
            oneWay = begin ? -1 : 1;
        }
        if (oneWay >= 0) {
            finalSegment = processSegment(ctx, segment, queue, visited, all, true, start);
        }
        if (oneWay <= 0) {
            finalSegment = processSegment(ctx, segment, queue, visited, all, false, start);
        }
    }
    if (finalSegment == null) {
        if (TRACE) {
            System.out.println("Isolated " + initial.id);
        }
    } else {
        StringBuilder b = new StringBuilder("Route for " + initial.id + " : ");
        RouteSegment s = finalSegment;
        List<RouteDataObject> rdoToAdd = new ArrayList<RouteDataObject>();
        while (s != null) {
            if (s.getRoad().id != initial.id) {
                b.append(s.getRoad().id).append(", ");
                rdoToAdd.add(s.getRoad());
            }
            s = s.getParentRoute();
        }
        if (TRACE) {
            System.out.println(b);
        }
        return rdoToAdd;
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) PriorityQueue(java.util.PriorityQueue) TLongHashSet(gnu.trove.set.hash.TLongHashSet) RouteDataObject(net.osmand.binary.RouteDataObject) VehicleRouter(net.osmand.router.VehicleRouter) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment)

Example 4 with RouteSegment

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

the class MapClusterLayer method searchCluster.

private List<RouteSegment> searchCluster(ClusteringContext cCtx, RoutingContext ctx, RouteSegment st) throws IOException {
    List<RouteSegment> result = new ArrayList<BinaryRoutePlanner.RouteSegment>();
    TLongHashSet visitedIds = new TLongHashSet();
    RouteDataObject startRoad = st.getRoad();
    final int stx = startRoad.getPoint31XTile(st.getSegmentStart());
    final int sty = startRoad.getPoint31YTile(st.getSegmentStart());
    int tileX = startRoad.getPoint31XTile(st.getSegmentStart()) >> cCtx.zm;
    int tileY = startRoad.getPoint31YTile(st.getSegmentStart()) >> cCtx.zm;
    Queue<RouteSegment> queue = new PriorityQueue<RouteSegment>(50, new Comparator<RouteSegment>() {

        @Override
        public int compare(RouteSegment o1, RouteSegment o2) {
            double d1 = MapUtils.squareDist31TileMetric(stx, sty, o1.getRoad().getPoint31XTile(o1.getSegmentStart()), o1.getRoad().getPoint31YTile(o1.getSegmentStart()));
            double d2 = MapUtils.squareDist31TileMetric(stx, sty, o2.getRoad().getPoint31XTile(o2.getSegmentStart()), o2.getRoad().getPoint31YTile(o2.getSegmentStart()));
            return Double.compare(d1, d2);
        }
    });
    queue.add(st);
    while (!queue.isEmpty()) {
        RouteSegment segment = queue.poll();
        if (visitedIds.contains(calculateId(segment))) {
            // System.out.println("contains " + segment.getRoad());
            continue;
        }
        // System.out.println(segment.getRoad());
        visitedIds.add(calculateId(segment));
        // Visualization of steps !
        if (ctx.getVisitor() != null) {
            ctx.getVisitor().visitSegment(segment, -1, true);
        }
        cCtx.roadProcessed++;
        if (cCtx.roadProcessed > 50) {
            float ratio = (float) (queue.size() + cCtx.outOfTile + cCtx.outOfDistance) / cCtx.segmentsProcessed;
            if (ratio < cCtx.minRatio) {
                cCtx.minRatio = ratio;
                cCtx.roadMinProcessed = cCtx.roadProcessed;
            }
        }
        processSegment(cCtx, ctx, segment, queue, result, tileX, tileY, true);
        processSegment(cCtx, ctx, segment, queue, result, tileX, tileY, false);
    }
    System.out.println("Current ratio " + ((float) (queue.size() + cCtx.outOfTile + cCtx.outOfDistance) / cCtx.segmentsProcessed) + " min ratio " + cCtx.minRatio + " min segments procesed " + cCtx.roadMinProcessed);
    String res = "Processed " + cCtx.roadProcessed + " / " + cCtx.segmentsProcessed + " and borders are " + (cCtx.outOfTile + cCtx.outOfDistance) + " out because of distance " + cCtx.outOfDistance;
    log.info(res);
    return result;
}
Also used : BinaryRoutePlanner(net.osmand.router.BinaryRoutePlanner) ArrayList(java.util.ArrayList) PriorityQueue(java.util.PriorityQueue) Point(java.awt.Point) TLongHashSet(gnu.trove.set.hash.TLongHashSet) RouteDataObject(net.osmand.binary.RouteDataObject) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment)

Example 5 with RouteSegment

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

the class BinaryRoutePlannerOld method updateCalculationProgress.

private void updateCalculationProgress(final RoutingContext ctx, PriorityQueue<RouteSegment> graphDirectSegments, PriorityQueue<RouteSegment> graphReverseSegments) {
    if (ctx.calculationProgress != null) {
        ctx.calculationProgress.reverseSegmentQueueSize = graphReverseSegments.size();
        ctx.calculationProgress.directSegmentQueueSize = graphDirectSegments.size();
        RouteSegment dirPeek = graphDirectSegments.peek();
        if (dirPeek != null) {
            ctx.calculationProgress.distanceFromBegin = Math.max(dirPeek.distanceFromStart, ctx.calculationProgress.distanceFromBegin);
        }
        RouteSegment revPeek = graphReverseSegments.peek();
        if (revPeek != null) {
            ctx.calculationProgress.distanceFromEnd = Math.max(revPeek.distanceFromStart, ctx.calculationProgress.distanceFromEnd);
        }
    }
}
Also used : 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