Search in sources :

Example 11 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class ShapeFileReaderTest method testTravelTimesBetweenRandomLocations.

@Test
public void testTravelTimesBetweenRandomLocations() {
    int nTests = 200;
    final Random random = new Random(123);
    final GHPoint min = new GHPoint(35.882931, 14.403076);
    final GHPoint max = new GHPoint(35.913523, 14.448566);
    class RandPointGenerator {

        double rand(double min, double max) {
            return min + random.nextDouble() * (max - min);
        }

        GHPoint randPoint() {
            return new GHPoint(rand(min.lat, max.lat), rand(min.lon, max.lon));
        }
    }
    RandPointGenerator pointGenerator = new RandPointGenerator();
    int nbFails = 0;
    DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
    for (int i = 0; i < nTests; i++) {
        FromToPair pair = new FromToPair(pointGenerator.randPoint(), pointGenerator.randPoint());
        // paths from random points can fail to don't assert on failure
        PathWrapper shpPath = pair.getPath(hopperShp, false);
        PathWrapper pbfPath = pair.getPath(hopperPbf, false);
        // the road network)
        if (shpPath == null || pbfPath == null) {
            nbFails++;
            continue;
        }
        double shpSecs = getSecondsTravel(shpPath);
        double pbfSecs = getSecondsTravel(pbfPath);
        double frac = shpSecs / pbfSecs;
        double percentageDeviation = Math.abs(1.0 - frac) * 100;
        stats.accept(percentageDeviation);
    }
    assertTrue("Number of fails should be small for the chosen box", nbFails < nTests / 3);
    // Test mean fraction. There will be some deviation as not all tags are
    // considered etc,
    // but we expect it to be small for a large number of tests
    double mean = stats.getAverage();
    assertTrue("Should have a mean deviation in travel times of less than 1%", mean < 1.0);
}
Also used : Random(java.util.Random) PathWrapper(com.graphhopper.PathWrapper) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 12 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class GraphHopperIT method testPointHint.

@Test
public void testPointHint() {
    GraphHopper tmpHopper = new GraphHopperOSM().setOSMFile(DIR + "/Laufamholzstrasse.osm.xml").setCHEnabled(false).setGraphHopperLocation(tmpGraphFile).setEncodingManager(new EncodingManager("car"));
    tmpHopper.importOrLoad();
    GHRequest req = new GHRequest(49.46553, 11.154669, 49.465244, 11.152577).setVehicle("car").setWeighting("fastest");
    req.setPointHints(new ArrayList<>(Arrays.asList("Laufamholzstraße, 90482, Nürnberg, Deutschland", "")));
    GHResponse rsp = tmpHopper.route(req);
    assertFalse(rsp.getErrors().toString(), rsp.hasErrors());
    GHPoint snappedPoint = rsp.getBest().getWaypoints().toGHPoint(0);
    assertEquals(49.465686, snappedPoint.getLat(), .000001);
    assertEquals(11.154605, snappedPoint.getLon(), .000001);
    req.setPointHints(new ArrayList<>(Arrays.asList("", "")));
    rsp = tmpHopper.route(req);
    assertFalse(rsp.getErrors().toString(), rsp.hasErrors());
    snappedPoint = rsp.getBest().getWaypoints().toGHPoint(0);
    assertEquals(49.465502, snappedPoint.getLat(), .000001);
    assertEquals(11.154498, snappedPoint.getLon(), .000001);
    // Match to closest edge, since hint was not found
    req.setPointHints(new ArrayList<>(Arrays.asList("xy", "")));
    rsp = tmpHopper.route(req);
    assertFalse(rsp.getErrors().toString(), rsp.hasErrors());
    snappedPoint = rsp.getBest().getWaypoints().toGHPoint(0);
    assertEquals(49.465502, snappedPoint.getLat(), .000001);
    assertEquals(11.154498, snappedPoint.getLon(), .000001);
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) GraphHopperOSM(com.graphhopper.reader.osm.GraphHopperOSM) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 13 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class OSMReaderTest method testEstimatedCenter.

@Test
public void testEstimatedCenter() {
    final CarFlagEncoder encoder = new CarFlagEncoder() {

        private EncodedValue objectEncoder;

        @Override
        public int defineNodeBits(int index, int shift) {
            shift = super.defineNodeBits(index, shift);
            objectEncoder = new EncodedValue("oEnc", shift, 2, 1, 0, 3, true);
            return shift + 2;
        }

        @Override
        public long handleNodeTags(ReaderNode node) {
            if (node.hasTag("test", "now"))
                return -objectEncoder.setValue(0, 1);
            return 0;
        }
    };
    EncodingManager manager = new EncodingManager(encoder);
    GraphHopperStorage ghStorage = newGraph(dir, manager, false, false);
    final Map<Integer, Double> latMap = new HashMap<Integer, Double>();
    final Map<Integer, Double> lonMap = new HashMap<Integer, Double>();
    latMap.put(1, 1.1d);
    latMap.put(2, 1.2d);
    lonMap.put(1, 1.0d);
    lonMap.put(2, 1.0d);
    OSMReader osmreader = new OSMReader(ghStorage) {

        // mock data access
        @Override
        double getTmpLatitude(int id) {
            return latMap.get(id);
        }

        @Override
        double getTmpLongitude(int id) {
            return lonMap.get(id);
        }

        @Override
        Collection<EdgeIteratorState> addOSMWay(LongIndexedContainer osmNodeIds, long wayFlags, long osmId) {
            return Collections.emptyList();
        }
    };
    // save some node tags for first node
    ReaderNode osmNode = new ReaderNode(1, 1.1d, 1.0d);
    osmNode.setTag("test", "now");
    osmreader.getNodeFlagsMap().put(1, encoder.handleNodeTags(osmNode));
    ReaderWay way = new ReaderWay(1L);
    way.getNodes().add(1);
    way.getNodes().add(2);
    way.setTag("highway", "motorway");
    osmreader.getNodeMap().put(1, 1);
    osmreader.getNodeMap().put(2, 2);
    osmreader.processWay(way);
    GHPoint p = way.getTag("estimated_center", null);
    assertEquals(1.15, p.lat, 1e-3);
    assertEquals(1.0, p.lon, 1e-3);
    Double d = way.getTag("estimated_distance", null);
    assertEquals(11119.5, d, 1e-1);
}
Also used : LongIndexedContainer(com.carrotsearch.hppc.LongIndexedContainer) ReaderNode(com.graphhopper.reader.ReaderNode) ReaderWay(com.graphhopper.reader.ReaderWay) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 14 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class QueryGraphTest method testEdgesShareOneNode.

@Test
public void testEdgesShareOneNode() {
    initGraph(g);
    EdgeIteratorState iter = GHUtility.getEdge(g, 0, 2);
    QueryResult res1 = createLocationResult(0.5, 0, iter, 0, EDGE);
    iter = GHUtility.getEdge(g, 1, 0);
    QueryResult res2 = createLocationResult(1.5, 2, iter, 0, EDGE);
    QueryGraph queryGraph = new QueryGraph(g);
    queryGraph.lookup(Arrays.asList(res1, res2));
    assertEquals(new GHPoint(0.5, 0), res1.getSnappedPoint());
    assertEquals(new GHPoint(1.300019, 1.899962), res2.getSnappedPoint());
    assertNotNull(GHUtility.getEdge(queryGraph, 0, 4));
    assertNotNull(GHUtility.getEdge(queryGraph, 0, 3));
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 15 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class QueryGraphTest method testAvoidDuplicateVirtualNodesIfIdentical.

@Test
public void testAvoidDuplicateVirtualNodesIfIdentical() {
    initGraph(g);
    EdgeIteratorState edgeState = GHUtility.getEdge(g, 0, 2);
    QueryResult res1 = createLocationResult(0.5, 0, edgeState, 0, EDGE);
    QueryResult res2 = createLocationResult(0.5, 0, edgeState, 0, EDGE);
    QueryGraph queryGraph = new QueryGraph(g);
    queryGraph.lookup(Arrays.asList(res1, res2));
    assertEquals(new GHPoint(0.5, 0), res1.getSnappedPoint());
    assertEquals(new GHPoint(0.5, 0), res2.getSnappedPoint());
    assertEquals(3, res1.getClosestNode());
    assertEquals(3, res2.getClosestNode());
    // force skip due to **tower** node snapping in phase 2, but no virtual edges should be created for res1
    edgeState = GHUtility.getEdge(g, 0, 1);
    res1 = createLocationResult(1, 0, edgeState, 0, EDGE);
    // now create virtual edges
    edgeState = GHUtility.getEdge(g, 0, 2);
    res2 = createLocationResult(0.5, 0, edgeState, 0, EDGE);
    queryGraph = new QueryGraph(g);
    queryGraph.lookup(Arrays.asList(res1, res2));
    // make sure only one virtual node was created
    assertEquals(queryGraph.getNodes(), g.getNodes() + 1);
    EdgeIterator iter = queryGraph.createEdgeExplorer().setBaseNode(0);
    assertEquals(GHUtility.asSet(1, 3), GHUtility.getNeighbors(iter));
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Aggregations

GHPoint (com.graphhopper.util.shapes.GHPoint)206 Test (org.junit.jupiter.api.Test)62 GHRequest (com.graphhopper.GHRequest)57 GHResponse (com.graphhopper.GHResponse)50 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)50 Test (org.junit.Test)45 LMProfile (com.graphhopper.config.LMProfile)27 Profile (com.graphhopper.config.Profile)27 CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)27 CHProfile (com.graphhopper.config.CHProfile)26 Snap (com.graphhopper.storage.index.Snap)17 QueryResult (com.graphhopper.storage.index.QueryResult)15 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)13 ResponsePath (com.graphhopper.ResponsePath)11 ArrayList (java.util.ArrayList)11 EnumSource (org.junit.jupiter.params.provider.EnumSource)11 PathWrapper (com.graphhopper.PathWrapper)8 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)8 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)8 IntArrayList (com.carrotsearch.hppc.IntArrayList)6