Search in sources :

Example 1 with RouteSegmentVisitor

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

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

Aggregations

Point (java.awt.Point)2 ArrayList (java.util.ArrayList)2 LatLon (net.osmand.data.LatLon)2 Way (net.osmand.osm.edit.Way)2 RouteSegment (net.osmand.router.BinaryRoutePlanner.RouteSegment)2 RouteSegmentVisitor (net.osmand.router.BinaryRoutePlanner.RouteSegmentVisitor)2 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)1 RouteDataObject (net.osmand.binary.RouteDataObject)1 DataTileManager (net.osmand.data.DataTileManager)1 RoutePlannerFrontEnd (net.osmand.router.RoutePlannerFrontEnd)1 RoutingConfiguration (net.osmand.router.RoutingConfiguration)1 Builder (net.osmand.router.RoutingConfiguration.Builder)1 RoutingContext (net.osmand.router.RoutingContext)1 NodeList (org.w3c.dom.NodeList)1