Search in sources :

Example 1 with DistanceCalc

use of com.graphhopper.util.DistanceCalc in project graphhopper by graphhopper.

the class BBoxTest method testCreate.

@Test
public void testCreate() {
    DistanceCalc c = new DistanceCalcEarth();
    BBox b = c.createBBox(52, 10, 100000);
    // The calculated bounding box has no negative values (also for southern hemisphere and negative meridians)
    // and the ordering is always the same (top to bottom and left to right)
    assertEquals(52.8993, b.maxLat, 1e-4);
    assertEquals(8.5393, b.minLon, 1e-4);
    assertEquals(51.1007, b.minLat, 1e-4);
    assertEquals(11.4607, b.maxLon, 1e-4);
}
Also used : DistanceCalc(com.graphhopper.util.DistanceCalc) DistanceCalcEarth(com.graphhopper.util.DistanceCalcEarth) Test(org.junit.jupiter.api.Test)

Example 2 with DistanceCalc

use of com.graphhopper.util.DistanceCalc in project graphhopper by graphhopper.

the class AbstractLocationIndexTester method testGrid.

@Test
public void testGrid() {
    Graph g = createSampleGraph(new EncodingManager("car"));
    int locs = g.getNodes();
    idx = createIndex(g, -1);
    // if we would use less array entries then some points gets the same key so avoid that for this test
    // e.g. for 16 we get "expected 6 but was 9" i.e 6 was overwritten by node j9 which is a bit closer to the grid center
    // go through every point of the graph if all points are reachable
    NodeAccess na = g.getNodeAccess();
    for (int i = 0; i < locs; i++) {
        double lat = na.getLatitude(i);
        double lon = na.getLongitude(i);
        assertEquals("nodeId:" + i + " " + (float) lat + "," + (float) lon, i, findID(idx, lat, lon));
    }
    // hit random lat,lon and compare result to full index
    Random rand = new Random(12);
    LocationIndex fullIndex;
    if (hasEdgeSupport())
        fullIndex = new Location2IDFullWithEdgesIndex(g);
    else
        fullIndex = new Location2IDFullIndex(g);
    DistanceCalc dist = new DistanceCalcEarth();
    for (int i = 0; i < 100; i++) {
        double lat = rand.nextDouble() * 5;
        double lon = rand.nextDouble() * 5;
        int fullId = findID(fullIndex, lat, lon);
        double fullLat = na.getLatitude(fullId);
        double fullLon = na.getLongitude(fullId);
        float fullDist = (float) dist.calcDist(lat, lon, fullLat, fullLon);
        int newId = findID(idx, lat, lon);
        double newLat = na.getLatitude(newId);
        double newLon = na.getLongitude(newId);
        float newDist = (float) dist.calcDist(lat, lon, newLat, newLon);
        if (testGridIgnore(i)) {
            continue;
        }
        assertTrue(i + " orig:" + (float) lat + "," + (float) lon + " full:" + fullLat + "," + fullLon + " fullDist:" + fullDist + " found:" + newLat + "," + newLon + " foundDist:" + newDist, Math.abs(fullDist - newDist) < 50000);
    }
    fullIndex.close();
    Helper.close((Closeable) g);
}
Also used : DistanceCalc(com.graphhopper.util.DistanceCalc) Random(java.util.Random) DistanceCalcEarth(com.graphhopper.util.DistanceCalcEarth) Test(org.junit.Test)

Example 3 with DistanceCalc

use of com.graphhopper.util.DistanceCalc in project graphhopper by graphhopper.

the class FakeWalkNetworkBuilder method buildWalkNetwork.

static void buildWalkNetwork(Collection<GTFSFeed> feeds, GraphHopperStorage graph, PtFlagEncoder encoder, DistanceCalc distCalc) {
    Collection<ConstraintVertex> sites = new ArrayList<>();
    Map<Vertex, Integer> vertex2nodeId = new HashMap<>();
    feeds.stream().flatMap(feed -> feed.stops.values().stream()).forEach(stop -> {
        int i = graph.getNodes();
        graph.getNodeAccess().setNode(i++, stop.stop_lat, stop.stop_lon);
        ConstraintVertex site = new ConstraintVertex(new Coordinate(stop.stop_lon, stop.stop_lat));
        sites.add(site);
        vertex2nodeId.put(site, i - 1);
    });
    ConformingDelaunayTriangulator conformingDelaunayTriangulator = new ConformingDelaunayTriangulator(sites, 0.0);
    conformingDelaunayTriangulator.setConstraints(new ArrayList(), new ArrayList());
    conformingDelaunayTriangulator.formInitialDelaunay();
    QuadEdgeSubdivision tin = conformingDelaunayTriangulator.getSubdivision();
    List<QuadEdge> edges = tin.getPrimaryEdges(false);
    for (QuadEdge edge : edges) {
        EdgeIteratorState ghEdge = graph.edge(vertex2nodeId.get(edge.orig()), vertex2nodeId.get(edge.dest()));
        double distance = distCalc.calcDist(edge.orig().getY(), edge.orig().getX(), edge.dest().getY(), edge.dest().getX());
        ghEdge.setDistance(distance);
        ghEdge.setFlags(encoder.setSpeed(ghEdge.getFlags(), 5.0));
        ghEdge.setFlags(encoder.setAccess(ghEdge.getFlags(), true, true));
    }
}
Also used : GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Coordinate(com.vividsolutions.jts.geom.Coordinate) QuadEdge(com.vividsolutions.jts.triangulate.quadedge.QuadEdge) java.util(java.util) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) ConformingDelaunayTriangulator(com.vividsolutions.jts.triangulate.ConformingDelaunayTriangulator) ConstraintVertex(com.vividsolutions.jts.triangulate.ConstraintVertex) Vertex(com.vividsolutions.jts.triangulate.quadedge.Vertex) GTFSFeed(com.conveyal.gtfs.GTFSFeed) DistanceCalc(com.graphhopper.util.DistanceCalc) QuadEdgeSubdivision(com.vividsolutions.jts.triangulate.quadedge.QuadEdgeSubdivision) ConstraintVertex(com.vividsolutions.jts.triangulate.ConstraintVertex) Vertex(com.vividsolutions.jts.triangulate.quadedge.Vertex) QuadEdge(com.vividsolutions.jts.triangulate.quadedge.QuadEdge) ConformingDelaunayTriangulator(com.vividsolutions.jts.triangulate.ConformingDelaunayTriangulator) QuadEdgeSubdivision(com.vividsolutions.jts.triangulate.quadedge.QuadEdgeSubdivision) ConstraintVertex(com.vividsolutions.jts.triangulate.ConstraintVertex) Coordinate(com.vividsolutions.jts.geom.Coordinate) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState)

Aggregations

DistanceCalc (com.graphhopper.util.DistanceCalc)3 DistanceCalcEarth (com.graphhopper.util.DistanceCalcEarth)2 GTFSFeed (com.conveyal.gtfs.GTFSFeed)1 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)1 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)1 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 ConformingDelaunayTriangulator (com.vividsolutions.jts.triangulate.ConformingDelaunayTriangulator)1 ConstraintVertex (com.vividsolutions.jts.triangulate.ConstraintVertex)1 QuadEdge (com.vividsolutions.jts.triangulate.quadedge.QuadEdge)1 QuadEdgeSubdivision (com.vividsolutions.jts.triangulate.quadedge.QuadEdgeSubdivision)1 Vertex (com.vividsolutions.jts.triangulate.quadedge.Vertex)1 java.util (java.util)1 Random (java.util.Random)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1