Search in sources :

Example 51 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class MapMatching2Test method testIssue70.

@Test
public void testIssue70() throws IOException {
    GraphHopper hopper = new GraphHopper();
    hopper.setOSMFile("../map-matching/files/issue-70.osm.gz");
    hopper.setGraphHopperLocation(GH_LOCATION);
    hopper.setProfiles(new Profile("my_profile").setVehicle("car").setWeighting("fastest"));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("my_profile"));
    hopper.importOrLoad();
    MapMatching mapMatching = new MapMatching(hopper, new PMap().putObject("profile", "my_profile"));
    Gpx gpx = xmlMapper.readValue(getClass().getResourceAsStream("/issue-70.gpx"), Gpx.class);
    MatchResult mr = mapMatching.match(GpxConversions.getEntries(gpx.trk.get(0)));
    assertEquals(Arrays.asList("Милана Видака", "Бранка Радичевића", "Здравка Челара"), fetchStreets(mr.getEdgeMatches()));
    for (EdgeMatch edgeMatch : mr.getEdgeMatches()) {
        validateEdgeMatch(edgeMatch);
    }
}
Also used : MapMatching(com.graphhopper.matching.MapMatching) EdgeMatch(com.graphhopper.matching.EdgeMatch) PMap(com.graphhopper.util.PMap) LMProfile(com.graphhopper.config.LMProfile) GraphHopper(com.graphhopper.GraphHopper) MatchResult(com.graphhopper.matching.MatchResult) Profile(com.graphhopper.config.Profile) LMProfile(com.graphhopper.config.LMProfile) Gpx(com.graphhopper.jackson.Gpx) Test(org.junit.jupiter.api.Test)

Example 52 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class RoutingAdditivityTest method setup.

@BeforeAll
public static void setup() {
    Helper.removeDir(new File(GH_LOCATION));
    graphHopper = new GraphHopper();
    graphHopper.setOSMFile("../map-matching/files/leipzig_germany.osm.pbf");
    graphHopper.setGraphHopperLocation(GH_LOCATION);
    graphHopper.setProfiles(new Profile("my_profile").setVehicle("car").setWeighting("fastest"));
    graphHopper.getLMPreparationHandler().setLMProfiles(new LMProfile("my_profile"));
    graphHopper.importOrLoad();
}
Also used : LMProfile(com.graphhopper.config.LMProfile) GraphHopper(com.graphhopper.GraphHopper) File(java.io.File) Profile(com.graphhopper.config.Profile) LMProfile(com.graphhopper.config.LMProfile) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 53 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class GraphHopperStorageTest method testLoadGraph_implicitEncodedValues_issue1862.

@Test
public void testLoadGraph_implicitEncodedValues_issue1862() {
    Helper.removeDir(new File(defaultGraphLoc));
    encodingManager = new EncodingManager.Builder().add(createCarFlagEncoder()).add(new BikeFlagEncoder()).build();
    graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), false).create(defaultSize);
    NodeAccess na = graph.getNodeAccess();
    na.setNode(0, 12, 23);
    na.setNode(1, 8, 13);
    na.setNode(2, 2, 10);
    na.setNode(3, 5, 9);
    GHUtility.setSpeed(60, true, true, carEncoder, graph.edge(1, 2).setDistance(10));
    GHUtility.setSpeed(60, true, true, carEncoder, graph.edge(1, 3).setDistance(10));
    int nodes = graph.getNodes();
    int edges = graph.getAllEdges().length();
    graph.flush();
    Helper.close(graph);
    // load without configured FlagEncoders
    GraphHopper hopper = new GraphHopper();
    hopper.setProfiles(Arrays.asList(new Profile("p_car").setVehicle("car").setWeighting("fastest"), new Profile("p_bike").setVehicle("bike").setWeighting("fastest")));
    hopper.setGraphHopperLocation(defaultGraphLoc);
    assertTrue(hopper.load());
    graph = hopper.getGraphHopperStorage();
    assertEquals(nodes, graph.getNodes());
    assertEquals(edges, graph.getAllEdges().length());
    Helper.close(graph);
    hopper = new GraphHopper();
    // load via explicitly configured FlagEncoders then we can define only one profile
    hopper.getEncodingManagerBuilder().add(createCarFlagEncoder()).add(new BikeFlagEncoder());
    hopper.setProfiles(Collections.singletonList(new Profile("p_car").setVehicle("car").setWeighting("fastest")));
    hopper.setGraphHopperLocation(defaultGraphLoc);
    assertTrue(hopper.load());
    graph = hopper.getGraphHopperStorage();
    assertEquals(nodes, graph.getNodes());
    assertEquals(edges, graph.getAllEdges().length());
    Helper.close(graph);
    Helper.removeDir(new File(defaultGraphLoc));
}
Also used : BikeFlagEncoder(com.graphhopper.routing.util.BikeFlagEncoder) GraphHopper(com.graphhopper.GraphHopper) File(java.io.File) Profile(com.graphhopper.config.Profile) Test(org.junit.jupiter.api.Test)

Example 54 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class GraphHopperStorageLMTest method testLoad.

@Test
public void testLoad() {
    String defaultGraphLoc = "./target/ghstorage_lm";
    Helper.removeDir(new File(defaultGraphLoc));
    CarFlagEncoder carFlagEncoder = new CarFlagEncoder();
    EncodingManager encodingManager = new EncodingManager.Builder().add(carFlagEncoder).add(Subnetwork.create("my_profile")).build();
    GraphHopperStorage graph = GraphBuilder.start(encodingManager).setRAM(defaultGraphLoc, true).create();
    // 0-1
    ReaderWay way_0_1 = new ReaderWay(27l);
    way_0_1.setTag("highway", "primary");
    way_0_1.setTag("maxheight", "4.4");
    GHUtility.setSpeed(60, true, true, carFlagEncoder, graph.edge(0, 1).setDistance(1));
    updateDistancesFor(graph, 0, 0.00, 0.00);
    updateDistancesFor(graph, 1, 0.01, 0.01);
    graph.getEdgeIteratorState(0, 1).setFlags(carFlagEncoder.handleWayTags(encodingManager.createEdgeFlags(), way_0_1));
    // 1-2
    ReaderWay way_1_2 = new ReaderWay(28l);
    way_1_2.setTag("highway", "primary");
    way_1_2.setTag("maxweight", "45");
    GHUtility.setSpeed(60, true, true, carFlagEncoder, graph.edge(1, 2).setDistance(1));
    updateDistancesFor(graph, 2, 0.02, 0.02);
    graph.getEdgeIteratorState(1, 2).setFlags(carFlagEncoder.handleWayTags(encodingManager.createEdgeFlags(), way_1_2));
    graph.flush();
    graph.close();
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(defaultGraphLoc).setProfiles(new Profile("my_profile").setVehicle("car").setWeighting("fastest"));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("my_profile"));
    // does lm preparation
    hopper.importOrLoad();
    EncodingManager em = hopper.getEncodingManager();
    assertNotNull(em);
    assertEquals(1, em.fetchEdgeEncoders().size());
    assertEquals(16, hopper.getLMPreparationHandler().getLandmarks());
    hopper = new GraphHopper().setGraphHopperLocation(defaultGraphLoc).setProfiles(new Profile("my_profile").setVehicle("car").setWeighting("fastest"));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("my_profile"));
    // just loads the LM data
    hopper.importOrLoad();
    assertEquals(1, em.fetchEdgeEncoders().size());
    assertEquals(16, hopper.getLMPreparationHandler().getLandmarks());
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) LMProfile(com.graphhopper.config.LMProfile) ReaderWay(com.graphhopper.reader.ReaderWay) GraphHopper(com.graphhopper.GraphHopper) File(java.io.File) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) Profile(com.graphhopper.config.Profile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test)

Example 55 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class DefaultModule method createGraphHopper.

/**
     * @return an initialized GraphHopper instance
     */
protected GraphHopper createGraphHopper(CmdArgs args) {
    GraphHopper tmp = new GraphHopperOSM() {

        @Override
        protected void loadOrPrepareLM() {
            if (!getLMFactoryDecorator().isEnabled() || getLMFactoryDecorator().getPreparations().isEmpty())
                return;
            try {
                String location = args.get(Parameters.Landmark.PREPARE + "split_area_location", "");
                Reader reader = location.isEmpty() ? new InputStreamReader(LandmarkStorage.class.getResource("map.geo.json").openStream()) : new FileReader(location);
                JsonFeatureCollection jsonFeatureCollection = new GHJsonBuilder().create().fromJson(reader, JsonFeatureCollection.class);
                if (!jsonFeatureCollection.getFeatures().isEmpty()) {
                    SpatialRuleLookup ruleLookup = new SpatialRuleLookupBuilder().build("country", new SpatialRuleLookupBuilder.SpatialRuleDefaultFactory(), jsonFeatureCollection, getGraphHopperStorage().getBounds(), 0.1, true);
                    for (PrepareLandmarks prep : getLMFactoryDecorator().getPreparations()) {
                        prep.setSpatialRuleLookup(ruleLookup);
                    }
                }
            } catch (IOException ex) {
                logger.error("Problem while reading border map GeoJSON. Skipping this.", ex);
            }
            super.loadOrPrepareLM();
        }
    }.forServer().init(args);
    String location = args.get("spatial_rules.location", "");
    if (!location.isEmpty()) {
        if (!tmp.getEncodingManager().supports(("generic"))) {
            logger.warn("spatial_rules.location was specified but 'generic' encoder is missing to utilize the index");
        } else
            try {
                SpatialRuleLookup index = buildIndex(new FileReader(location), tmp.getGraphHopperStorage().getBounds());
                if (index != null) {
                    logger.info("Set spatial rule lookup with " + index.size() + " rules");
                    ((DataFlagEncoder) tmp.getEncodingManager().getEncoder("generic")).setSpatialRuleLookup(index);
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
    }
    tmp.importOrLoad();
    logger.info("loaded graph at:" + tmp.getGraphHopperLocation() + ", data_reader_file:" + tmp.getDataReaderFile() + ", flag_encoders:" + tmp.getEncodingManager() + ", " + tmp.getGraphHopperStorage().toDetailsString());
    return tmp;
}
Also used : GHJsonBuilder(com.graphhopper.json.GHJsonBuilder) InputStreamReader(java.io.InputStreamReader) GraphHopperOSM(com.graphhopper.reader.osm.GraphHopperOSM) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) GraphHopper(com.graphhopper.GraphHopper) SpatialRuleLookup(com.graphhopper.routing.util.spatialrules.SpatialRuleLookup) IOException(java.io.IOException) LandmarkStorage(com.graphhopper.routing.lm.LandmarkStorage) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) FileReader(java.io.FileReader) SpatialRuleLookupBuilder(com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)

Aggregations

GraphHopper (com.graphhopper.GraphHopper)101 Test (org.junit.jupiter.api.Test)62 Profile (com.graphhopper.config.Profile)52 LMProfile (com.graphhopper.config.LMProfile)44 CHProfile (com.graphhopper.config.CHProfile)39 GraphHopperTest (com.graphhopper.GraphHopperTest)25 ArrayList (java.util.ArrayList)24 File (java.io.File)16 Test (org.junit.Test)11 SRTMProvider (com.graphhopper.reader.dem.SRTMProvider)10 GHRequest (com.graphhopper.GHRequest)7 GHResponse (com.graphhopper.GHResponse)7 GraphHopperConfig (com.graphhopper.GraphHopperConfig)6 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)6 EncodingManager (com.graphhopper.routing.util.EncodingManager)6 PMap (com.graphhopper.util.PMap)6 GHPoint (com.graphhopper.util.shapes.GHPoint)5 IOException (java.io.IOException)5 Gpx (com.graphhopper.jackson.Gpx)4 MapMatching (com.graphhopper.matching.MapMatching)4