Search in sources :

Example 1 with GHBitSet

use of com.graphhopper.coll.GHBitSet in project graphhopper by graphhopper.

the class AbstractEdgeElevationInterpolator method interpolateElevationsOfTowerNodes.

private void interpolateElevationsOfTowerNodes() {
    final AllEdgesIterator edge = storage.getAllEdges();
    final GHBitSet visitedEdgeIds = new GHBitSetImpl(edge.getMaxId());
    final EdgeExplorer edgeExplorer = storage.createEdgeExplorer();
    while (edge.next()) {
        final int edgeId = edge.getEdge();
        if (isInterpolatableEdge(edge)) {
            if (!visitedEdgeIds.contains(edgeId)) {
                interpolateEdge(edge, visitedEdgeIds, edgeExplorer);
            }
        }
        visitedEdgeIds.add(edgeId);
    }
}
Also used : AllEdgesIterator(com.graphhopper.routing.util.AllEdgesIterator) GHBitSetImpl(com.graphhopper.coll.GHBitSetImpl) GHBitSet(com.graphhopper.coll.GHBitSet) EdgeExplorer(com.graphhopper.util.EdgeExplorer)

Example 2 with GHBitSet

use of com.graphhopper.coll.GHBitSet in project graphhopper by graphhopper.

the class Location2IDQuadtree method fillQuadtree.

private GHBitSet fillQuadtree(int size) {
    int locs = graph.getNodes();
    if (locs <= 0) {
        throw new IllegalStateException("check your graph - it is empty!");
    }
    GHBitSet filledIndices = new GHBitSetImpl(size);
    GHPoint coord = new GHPoint();
    for (int nodeId = 0; nodeId < locs; nodeId++) {
        double lat = nodeAccess.getLatitude(nodeId);
        double lon = nodeAccess.getLongitude(nodeId);
        int key = (int) keyAlgo.encode(lat, lon);
        long bytePos = (long) key * 4;
        if (filledIndices.contains(key)) {
            int oldNodeId = index.getInt(bytePos);
            keyAlgo.decode(key, coord);
            // decide which one is closer to 'key'
            double distNew = distCalc.calcNormalizedDist(coord.lat, coord.lon, lat, lon);
            double oldLat = nodeAccess.getLatitude(oldNodeId);
            double oldLon = nodeAccess.getLongitude(oldNodeId);
            double distOld = distCalc.calcNormalizedDist(coord.lat, coord.lon, oldLat, oldLon);
            // new point is closer to quad tree point (key) so overwrite old
            if (distNew < distOld) {
                index.setInt(bytePos, nodeId);
            }
        } else {
            index.setInt(bytePos, nodeId);
            filledIndices.add(key);
        }
    }
    return filledIndices;
}
Also used : GHBitSetImpl(com.graphhopper.coll.GHBitSetImpl) GHBitSet(com.graphhopper.coll.GHBitSet) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 3 with GHBitSet

use of com.graphhopper.coll.GHBitSet in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworks method findSubnetworks.

/**
 * This method finds the double linked components according to the specified filter.
 */
List<IntArrayList> findSubnetworks(PrepEdgeFilter filter) {
    final FlagEncoder encoder = filter.getEncoder();
    final EdgeExplorer explorer = ghStorage.createEdgeExplorer(filter);
    int locs = ghStorage.getNodes();
    List<IntArrayList> list = new ArrayList<IntArrayList>(100);
    final GHBitSet bs = new GHBitSetImpl(locs);
    for (int start = 0; start < locs; start++) {
        if (bs.contains(start))
            continue;
        final IntArrayList intList = new IntArrayList(20);
        list.add(intList);
        new BreadthFirstSearch() {

            int tmpCounter = 0;

            @Override
            protected GHBitSet createBitSet() {
                return bs;
            }

            @Override
            protected final boolean goFurther(int nodeId) {
                if (tmpCounter > maxEdgesPerNode.get())
                    maxEdgesPerNode.set(tmpCounter);
                tmpCounter = 0;
                intList.add(nodeId);
                return true;
            }

            @Override
            protected final boolean checkAdjacent(EdgeIteratorState edge) {
                if (encoder.isForward(edge.getFlags()) || encoder.isBackward(edge.getFlags())) {
                    tmpCounter++;
                    return true;
                }
                return false;
            }
        }.start(explorer, start);
        intList.trimToSize();
    }
    return list;
}
Also used : GHBitSetImpl(com.graphhopper.coll.GHBitSetImpl) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GHBitSet(com.graphhopper.coll.GHBitSet) GHIntArrayList(com.graphhopper.coll.GHIntArrayList) ArrayList(java.util.ArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList) GHIntArrayList(com.graphhopper.coll.GHIntArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 4 with GHBitSet

use of com.graphhopper.coll.GHBitSet in project graphhopper by graphhopper.

the class Measurement method start.

// creates properties file in the format key=value
// Every value is one y-value in a separate diagram with an identical x-value for every Measurement.start call
void start(CmdArgs args) {
    String graphLocation = args.get("graph.location", "");
    String propLocation = args.get("measurement.location", "");
    if (isEmpty(propLocation))
        propLocation = "measurement" + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".properties";
    seed = args.getLong("measurement.seed", 123);
    String gitCommit = args.get("measurement.gitinfo", "");
    int count = args.getInt("measurement.count", 5000);
    GraphHopper hopper = new GraphHopperOSM() {

        @Override
        protected void prepareCH() {
            StopWatch sw = new StopWatch().start();
            super.prepareCH();
            put(Parameters.CH.PREPARE + "time", sw.stop().getTime());
            int edges = getGraphHopperStorage().getAllEdges().getMaxId();
            if (getCHFactoryDecorator().hasWeightings()) {
                Weighting weighting = getCHFactoryDecorator().getWeightings().get(0);
                int edgesAndShortcuts = getGraphHopperStorage().getGraph(CHGraph.class, weighting).getAllEdges().getMaxId();
                put(Parameters.CH.PREPARE + "shortcuts", edgesAndShortcuts - edges);
            }
        }

        @Override
        protected DataReader importData() throws IOException {
            StopWatch sw = new StopWatch().start();
            DataReader dr = super.importData();
            put("graph.import_time", sw.stop().getSeconds());
            return dr;
        }
    };
    hopper.init(args).forDesktop();
    hopper.getCHFactoryDecorator().setDisablingAllowed(true);
    hopper.getLMFactoryDecorator().setDisablingAllowed(true);
    hopper.importOrLoad();
    GraphHopperStorage g = hopper.getGraphHopperStorage();
    String vehicleStr = args.get("graph.flag_encoders", "car");
    FlagEncoder encoder = hopper.getEncodingManager().getEncoder(vehicleStr);
    StopWatch sw = new StopWatch().start();
    try {
        maxNode = g.getNodes();
        boolean isCH = false;
        boolean isLM = false;
        GHBitSet allowedEdges = printGraphDetails(g, vehicleStr);
        printMiscUnitPerfTests(g, isCH, encoder, count * 100, allowedEdges);
        printLocationIndexQuery(g, hopper.getLocationIndex(), count);
        printTimeOfRouteQuery(hopper, isCH, isLM, count / 20, "routing", vehicleStr, true, -1, true);
        if (hopper.getLMFactoryDecorator().isEnabled()) {
            System.gc();
            isLM = true;
            int activeLMCount = 12;
            for (; activeLMCount > 3; activeLMCount -= 4) {
                printTimeOfRouteQuery(hopper, isCH, isLM, count / 4, "routingLM" + activeLMCount, vehicleStr, true, activeLMCount, true);
            }
        // compareRouting(hopper, vehicleStr, count / 5);
        }
        if (hopper.getCHFactoryDecorator().isEnabled()) {
            isCH = true;
            // compareCHWithAndWithoutSOD(hopper, vehicleStr, count/5);
            if (hopper.getLMFactoryDecorator().isEnabled()) {
                isLM = true;
                System.gc();
                // try just one constellation, often ~4-6 is best
                int lmCount = 5;
                printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCHLM" + lmCount, vehicleStr, true, lmCount, true);
            }
            isLM = false;
            System.gc();
            Weighting weighting = hopper.getCHFactoryDecorator().getWeightings().get(0);
            CHGraph lg = g.getGraph(CHGraph.class, weighting);
            fillAllowedEdges(lg.getAllEdges(), allowedEdges);
            printMiscUnitPerfTests(lg, isCH, encoder, count * 100, allowedEdges);
            printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH", vehicleStr, true, -1, true);
            printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH_no_sod", vehicleStr, true, -1, false);
            printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH_no_instr", vehicleStr, false, -1, true);
        }
        logger.info("store into " + propLocation);
    } catch (Exception ex) {
        logger.error("Problem while measuring " + graphLocation, ex);
        put("error", ex.toString());
    } finally {
        put("measurement.gitinfo", gitCommit);
        put("measurement.count", count);
        put("measurement.seed", seed);
        put("measurement.time", sw.stop().getTime());
        System.gc();
        put("measurement.totalMB", getTotalMB());
        put("measurement.usedMB", getUsedMB());
        try {
            store(new FileWriter(propLocation), "measurement finish, " + new Date().toString() + ", " + Constants.BUILD_DATE);
        } catch (IOException ex) {
            logger.error("Problem while storing properties " + graphLocation + ", " + propLocation, ex);
        }
    }
}
Also used : GHBitSet(com.graphhopper.coll.GHBitSet) FileWriter(java.io.FileWriter) GraphHopperOSM(com.graphhopper.reader.osm.GraphHopperOSM) GraphHopper(com.graphhopper.GraphHopper) IOException(java.io.IOException) IOException(java.io.IOException) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) DataReader(com.graphhopper.reader.DataReader) Weighting(com.graphhopper.routing.weighting.Weighting) CHGraph(com.graphhopper.storage.CHGraph) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with GHBitSet

use of com.graphhopper.coll.GHBitSet in project graphhopper by graphhopper.

the class BaseGraph method inPlaceNodeRemove.

/**
 * This methods disconnects all edges from removed nodes. It does no edge compaction. Then it
 * moves the last nodes into the deleted nodes, where it needs to update the node ids in every
 * edge.
 */
void inPlaceNodeRemove(int removeNodeCount) {
    // Prepare edge-update of nodes which are connected to deleted nodes
    int toMoveNodes = getNodes();
    int itemsToMove = 0;
    // sorted map when we access it via keyAt and valueAt - see below!
    final SparseIntIntArray oldToNewMap = new SparseIntIntArray(removeNodeCount);
    GHBitSet toRemoveSet = new GHBitSetImpl(removeNodeCount);
    removedNodes.copyTo(toRemoveSet);
    Logger logger = LoggerFactory.getLogger(getClass());
    if (removeNodeCount > getNodes() / 2.0)
        logger.warn("More than a half of the network should be removed!? " + "Nodes:" + getNodes() + ", remove:" + removeNodeCount);
    EdgeExplorer delExplorer = createEdgeExplorer();
    // create map of old node ids pointing to new ids
    for (int removeNode = removedNodes.next(0); removeNode >= 0; removeNode = removedNodes.next(removeNode + 1)) {
        EdgeIterator delEdgesIter = delExplorer.setBaseNode(removeNode);
        while (delEdgesIter.next()) {
            toRemoveSet.add(delEdgesIter.getAdjNode());
        }
        toMoveNodes--;
        for (; toMoveNodes >= 0; toMoveNodes--) {
            if (!removedNodes.contains(toMoveNodes))
                break;
        }
        if (toMoveNodes >= removeNode)
            oldToNewMap.put(toMoveNodes, removeNode);
        itemsToMove++;
    }
    EdgeIterable adjNodesToDelIter = (EdgeIterable) createEdgeExplorer();
    // all deleted nodes could be connected to existing. remove the connections
    for (int removeNode = toRemoveSet.next(0); removeNode >= 0; removeNode = toRemoveSet.next(removeNode + 1)) {
        // remove all edges connected to the deleted nodes
        adjNodesToDelIter.setBaseNode(removeNode);
        long prev = EdgeIterator.NO_EDGE;
        while (adjNodesToDelIter.next()) {
            int nodeId = adjNodesToDelIter.getAdjNode();
            // already invalidated
            if (nodeId != EdgeAccess.NO_NODE && removedNodes.contains(nodeId)) {
                int edgeToRemove = adjNodesToDelIter.getEdge();
                long edgeToRemovePointer = edgeAccess.toPointer(edgeToRemove);
                edgeAccess.internalEdgeDisconnect(edgeToRemove, prev, removeNode, nodeId);
                edgeAccess.invalidateEdge(edgeToRemovePointer);
            } else {
                prev = adjNodesToDelIter.edgePointer;
            }
        }
    }
    GHBitSet toMoveSet = new GHBitSetImpl(removeNodeCount * 3);
    EdgeExplorer movedEdgeExplorer = createEdgeExplorer();
    // marks connected nodes to rewrite the edges
    for (int i = 0; i < itemsToMove; i++) {
        int oldI = oldToNewMap.keyAt(i);
        EdgeIterator movedEdgeIter = movedEdgeExplorer.setBaseNode(oldI);
        while (movedEdgeIter.next()) {
            int nodeId = movedEdgeIter.getAdjNode();
            if (nodeId == EdgeAccess.NO_NODE)
                continue;
            if (removedNodes.contains(nodeId))
                throw new IllegalStateException("shouldn't happen the edge to the node " + nodeId + " should be already deleted. " + oldI);
            toMoveSet.add(nodeId);
        }
    }
    // move nodes into deleted nodes
    for (int i = 0; i < itemsToMove; i++) {
        int oldI = oldToNewMap.keyAt(i);
        int newI = oldToNewMap.valueAt(i);
        long newOffset = (long) newI * nodeEntryBytes;
        long oldOffset = (long) oldI * nodeEntryBytes;
        for (long j = 0; j < nodeEntryBytes; j += 4) {
            nodes.setInt(newOffset + j, nodes.getInt(oldOffset + j));
        }
    }
    // *rewrites* all edges connected to moved nodes
    // go through all edges and pick the necessary <- this is easier to implement than
    // a more efficient (?) breadth-first search
    EdgeIterator iter = getAllEdges();
    while (iter.next()) {
        int nodeA = iter.getBaseNode();
        int nodeB = iter.getAdjNode();
        if (!toMoveSet.contains(nodeA) && !toMoveSet.contains(nodeB))
            continue;
        // now overwrite exiting edge with new node ids
        // also flags and links could have changed due to different node order
        int updatedA = oldToNewMap.get(nodeA);
        if (updatedA < 0)
            updatedA = nodeA;
        int updatedB = oldToNewMap.get(nodeB);
        if (updatedB < 0)
            updatedB = nodeB;
        int edgeId = iter.getEdge();
        long edgePointer = edgeAccess.toPointer(edgeId);
        int linkA = edgeAccess.getEdgeRef(nodeA, nodeB, edgePointer);
        int linkB = edgeAccess.getEdgeRef(nodeB, nodeA, edgePointer);
        long flags = edgeAccess.getFlags_(edgePointer, false);
        edgeAccess.writeEdge(edgeId, updatedA, updatedB, linkA, linkB);
        edgeAccess.setFlags_(edgePointer, updatedA > updatedB, flags);
        if (updatedA < updatedB != nodeA < nodeB)
            setWayGeometry_(fetchWayGeometry_(edgePointer, true, 0, -1, -1), edgePointer, false);
    }
    // clear N_EDGE_REF
    initNodeRefs((nodeCount - removeNodeCount) * nodeEntryBytes, nodeCount * nodeEntryBytes);
    if (removeNodeCount >= nodeCount)
        throw new IllegalStateException("graph is empty after in-place removal but was " + removeNodeCount);
    // we do not remove the invalid edges => edgeCount stays the same!
    nodeCount -= removeNodeCount;
    // health check
    if (isTestingEnabled()) {
        EdgeExplorer explorer = createEdgeExplorer();
        iter = getAllEdges();
        while (iter.next()) {
            int base = iter.getBaseNode();
            int adj = iter.getAdjNode();
            String str = iter.getEdge() + ", r.contains(" + base + "):" + removedNodes.contains(base) + ", r.contains(" + adj + "):" + removedNodes.contains(adj) + ", tr.contains(" + base + "):" + toRemoveSet.contains(base) + ", tr.contains(" + adj + "):" + toRemoveSet.contains(adj) + ", base:" + base + ", adj:" + adj + ", nodeCount:" + nodeCount;
            if (adj >= nodeCount)
                throw new RuntimeException("Adj.node problem with edge " + str);
            if (base >= nodeCount)
                throw new RuntimeException("Base node problem with edge " + str);
            try {
                explorer.setBaseNode(adj).toString();
            } catch (Exception ex) {
                org.slf4j.LoggerFactory.getLogger(getClass()).error("adj:" + adj);
            }
            try {
                explorer.setBaseNode(base).toString();
            } catch (Exception ex) {
                org.slf4j.LoggerFactory.getLogger(getClass()).error("base:" + base);
            }
        }
        // access last node -> no error
        explorer.setBaseNode(nodeCount - 1).toString();
    }
    removedNodes = null;
}
Also used : GHBitSet(com.graphhopper.coll.GHBitSet) Logger(org.slf4j.Logger) GHBitSetImpl(com.graphhopper.coll.GHBitSetImpl) SparseIntIntArray(com.graphhopper.coll.SparseIntIntArray)

Aggregations

GHBitSet (com.graphhopper.coll.GHBitSet)17 GHBitSetImpl (com.graphhopper.coll.GHBitSetImpl)10 GHPoint (com.graphhopper.util.shapes.GHPoint)7 IntArrayList (com.carrotsearch.hppc.IntArrayList)4 GHTBitSet (com.graphhopper.coll.GHTBitSet)2 CHProfile (com.graphhopper.config.CHProfile)2 LMProfile (com.graphhopper.config.LMProfile)2 Profile (com.graphhopper.config.Profile)2 AllEdgesIterator (com.graphhopper.routing.util.AllEdgesIterator)2 Weighting (com.graphhopper.routing.weighting.Weighting)2 CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)2 BBox (com.graphhopper.util.shapes.BBox)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 IntArrayDeque (com.carrotsearch.hppc.IntArrayDeque)1 IntPredicate (com.carrotsearch.hppc.predicates.IntPredicate)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1