protected LocationIndex createLocationIndex(Directory dir) { LocationIndexTree tmpIndex = new LocationIndexTree(ghStorage, dir); tmpIndex.setResolution(preciseIndexResolution); tmpIndex.setMaxRegionSearch(maxRegionSearch); if (!tmpIndex.loadExisting()) { ensureWriteAccess(); tmpIndex.prepareIndex(); } return tmpIndex; }
double subqueryLonA = queryLon - iteration * deltaLon; double subqueryLonB = queryLon + iteration * deltaLon; findNetworkEntriesSingleRegion(foundEntries, subqueryLat, subqueryLonA); findNetworkEntriesSingleRegion(foundEntries, subqueryLat, subqueryLonB); double subqueryLatA = queryLat - iteration * deltaLat; double subqueryLatB = queryLat + iteration * deltaLat; findNetworkEntriesSingleRegion(foundEntries, subqueryLatA, subqueryLon); findNetworkEntriesSingleRegion(foundEntries, subqueryLatB, subqueryLon); double rMin = calculateRMin(queryLat, queryLon, iteration); double minDistance = calcMinDistance(queryLat, queryLon, foundEntries);
public final void findNetworkEntriesSingleRegion(GHIntHashSet storedNetworkEntryIds, double queryLat, double queryLon) { long keyPart = createReverseKey(queryLat, queryLon); fillIDs(keyPart, START_POINTER, storedNetworkEntryIds, 0); }
@Override public LocationIndex prepareIndex() { if (initialized) throw new IllegalStateException("Call prepareIndex only once"); StopWatch sw = new StopWatch().start(); prepareAlgo(); // in-memory preparation InMemConstructionIndex inMem = getPrepareInMemIndex(); // compact & store to dataAccess dataAccess.create(64 * 1024); try { inMem.store(inMem.root, START_POINTER); flush(); } catch (Exception ex) { throw new IllegalStateException("Problem while storing location index. " + Helper.getMemInfo(), ex); } float entriesPerLeaf = (float) inMem.size / inMem.leafs; initialized = true; logger.info("location index created in " + sw.stop().getSeconds() + "s, size:" + Helper.nf(inMem.size) + ", leafs:" + Helper.nf(inMem.leafs) + ", precision:" + minResolutionInMeter + ", depth:" + entries.length + ", checksum:" + calcChecksum() + ", entries:" + Arrays.toString(entries) + ", entriesPerLeaf:" + entriesPerLeaf); return this; }
walkNetworkIndex = new LocationIndexTree(graphHopperStorage, new RAMDirectory()).prepareIndex(); } else { walkNetworkIndex = new EmptyLocationIndex();
@Override public boolean loadExisting() { if (initialized) throw new IllegalStateException("Call loadExisting only once"); if (!dataAccess.loadExisting()) return false; if (dataAccess.getHeader(0) != MAGIC_INT) throw new IllegalStateException("incorrect location index version, expected:" + MAGIC_INT); if (dataAccess.getHeader(1 * 4) != calcChecksum()) throw new IllegalStateException("location index was opened with incorrect graph: " + dataAccess.getHeader(1 * 4) + " vs. " + calcChecksum()); setMinResolutionInMeter(dataAccess.getHeader(2 * 4)); prepareAlgo(); initialized = true; return true; }
Graph graph = createTestGraph(encodingManager); LocationIndexTree index = createIndexNoPrepare(graph, 50000); index.prepareAlgo(); LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex(); assertEquals(IntArrayList.from(new int[]{4, 4}), index.getEntries()); index.findNetworkEntries(0.5, -0.5, foundIds, 0); assertEquals(set, foundIds); set.add(2); foundIds.clear(); index.findNetworkEntries(-0.5, -0.9, foundIds, 0); index.findNetworkEntries(-0.5, -0.9, foundIds, 1); assertEquals(set, foundIds); assertEquals(2, findID(index, -0.5, -0.9));
@Test public void testInMemIndex3() { LocationIndexTree index = createIndexNoPrepare(createTestGraph(encodingManager), 10000); index.prepareAlgo(); LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex(); assertEquals(IntArrayList.from(new int[]{64, 4}), index.getEntries()); assertEquals(33, inMemIndex.getEntriesOf(0).size()); assertEquals(69, inMemIndex.getEntriesOf(1).size()); assertEquals(0, inMemIndex.getEntriesOf(2).size()); index.dataAccess.create(1024); inMemIndex.store(inMemIndex.root, LocationIndexTree.START_POINTER); assertEquals(1 << 20, index.getCapacity()); QueryResult res = index.findClosest(-.5, -.5, EdgeFilter.ALL_EDGES); assertEquals(1, res.getClosestNode()); }
@Override public QueryResult findClosest(final double queryLat, final double queryLon, final EdgeFilter edgeFilter) { if (isClosed()) throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed"); for (int iteration = 0; iteration < maxRegionSearch; iteration++) { GHIntHashSet storedNetworkEntryIds = new GHIntHashSet(); boolean earlyFinish = findNetworkEntries(queryLat, queryLon, storedNetworkEntryIds, iteration); storedNetworkEntryIds.removeAll(allCollectedEntryIds); allCollectedEntryIds.addAll(storedNetworkEntryIds);
public LocationIndexTree createIndexNoPrepare(Graph g, int resolution) { Directory dir = new RAMDirectory(location); LocationIndexTree tmpIDX = new LocationIndexTree(g, dir); tmpIDX.setResolution(resolution); return tmpIDX; }
index.prepareIndex(); index.setMaxRegionSearch(8); QueryResult qr = index.findClosest(0.03, 0.03, carFilter); assertTrue(qr.isValid()); assertEquals(33, qr.getClosestNode()); qr = index.findClosest(0.03, 0.03, bikeFilter); assertTrue(qr.isValid()); assertEquals(2, qr.getClosestNode());
@Test public void testRMin() { Graph graph = createTestGraph(encodingManager); LocationIndexTree index = createIndex(graph, 50000); //query: 0.05 | -0.3 DistanceCalc distCalc = new DistancePlaneProjection(); double rmin = index.calculateRMin(0.05, -0.3); double check = distCalc.calcDist(0.05, Math.abs(graph.getNodeAccess().getLon(2)) - index.getDeltaLon(), -0.3, -0.3); assertTrue((rmin - check) < 0.0001); double rmin2 = index.calculateRMin(0.05, -0.3, 1); double check2 = distCalc.calcDist(0.05, Math.abs(graph.getNodeAccess().getLat(0)), -0.3, -0.3); assertTrue((rmin2 - check2) < 0.0001); GHIntHashSet points = new GHIntHashSet(); assertEquals(Double.MAX_VALUE, index.calcMinDistance(0.05, -0.3, points), 1e-1); points.add(0); points.add(1); assertEquals(54757.03, index.calcMinDistance(0.05, -0.3, points), 1e-1); /*GraphVisualizer gv = new GraphVisualizer(graph, index.getDeltaLat(), index.getDeltaLon(), index.getCenter(0, 0).lat, index.getCenter(0, 0).lon); try { Thread.sleep(4000); } catch(InterruptedException ie) {}*/ }
@Override protected LocationIndex createLocationIndex(Directory dir) { return new LocationIndexTree(graph, dir); } };
findNetworkEntries(queryLat, queryLon, set, iteration);
index.prepareIndex(); List<QueryResult> result = index.findNClosest(0.0004, 0.0006, EdgeFilter.ALL_EDGES, 15); List<Integer> ids = new ArrayList<>(); for (QueryResult qr : result) {
@Override public void set(double lat, double lon) { long key = keyAlgo.encode(lat, lon); long keyPart = createReverseKey(key); // no need to feed both nodes as we search neighbors in fillIDs addNode(root, nodeA, 0, keyPart, key); } };
/** * calculate the distance to the nearest tile border for a given lat/lon coordinate in the * context of a spatial key tile. * <p> */ final double calculateRMin(double lat, double lon) { return calculateRMin(lat, lon, 0); }
index.prepareIndex(); QueryResult qr = index.findClosest(na.getLat(i), na.getLon(i), EdgeFilter.ALL_EDGES); assertEquals(i, qr.getClosestNode());
@Override public LocationIndexTree createIndex(Graph g, int resolution) { if (resolution < 0) resolution = 500000; return (LocationIndexTree) createIndexNoPrepare(g, resolution).prepareIndex(); }
@Test public void testReverseSpatialKey() { LocationIndexTree index = createIndex(createTestGraph(encodingManager), 200); assertEquals(IntArrayList.from(new int[]{64, 64, 64, 4}), index.getEntries()); // 10111110111110101010 String str44 = "00000000000000000000000000000000000000000000"; assertEquals(str44 + "01010101111101111101", BitUtil.BIG.toBitString(index.createReverseKey(1.7, 0.099))); }