Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import com.graphhopper.GHRequest;
- import com.graphhopper.GHResponse;
- import com.graphhopper.GraphHopper;
- import com.graphhopper.PathWrapper;
- import com.graphhopper.reader.osm.GraphHopperOSM;
- import com.graphhopper.routing.util.BikeFlagEncoder;
- import com.graphhopper.routing.util.CarFlagEncoder;
- import com.graphhopper.routing.util.EncodingManager;
- import com.graphhopper.routing.util.FootFlagEncoder;
- import com.graphhopper.storage.GraphHopperStorage;
- import com.graphhopper.util.Parameters;
- import com.graphhopper.util.StopWatch;
- public class TestMapCreator {
- private static final String OSM_SOURCE_DIR = "/Users/victorshcherb/osmand/maps/gh/";
- private static final String OSM_FILE = "ukraine_kiev_europe.pbf";
- public static void main(String[] args) {
- // prepareFile();
- calculateRoute();
- }
- private static void calculateRoute() {
- GraphHopper hopper = new GraphHopper().forMobile();
- hopper.load(new File(OSM_SOURCE_DIR, getOSMFolderName()).getAbsolutePath());
- GHRequest req = new GHRequest(50.439296, 30.478347, 50.449700, 30.478776)
- .setAlgorithm(Parameters.Algorithms.DIJKSTRA_BI);
- req.getHints().put(Parameters.Routing.INSTRUCTIONS, "false");
- PathWrapper pw = calculateRoute(hopper, req);
- System.out.println(pw);
- GraphHopperStorage st = hopper.getGraphHopperStorage();
- System.out.println(st.toDetailsString());
- System.out.println(st.getEdges());
- }
- public static PathWrapper calculateRoute(GraphHopper hopper, GHRequest request) {
- if (hopper != null) {
- System.out.println("calculating path ...");
- float time;
- StopWatch sw = new StopWatch().start();
- GHResponse resp = hopper.route(request);
- time = sw.stop().getSeconds();
- if (!resp.hasErrors()) {
- System.out.println("from:" + request.getPoints().get(0).lat + "," + request.getPoints().get(0).lon
- + " to:" + request.getPoints().get(request.getPoints().size() - 1).lat + ","
- + request.getPoints().get(request.getPoints().size() - 1).lon + " found path with distance:"
- + resp.getBest().getDistance() / 1000f + ", nodes:" + resp.getBest().getPoints().getSize()
- + ", time:" + time + " " + resp.getDebugInfo());
- System.out.println("the route is " + (int) (resp.getBest().getDistance() / 100) / 10f + "km long, time:"
- + resp.getBest().getTime() / 60000f + "min, debug:" + time);
- }
- return resp.getBest();
- } else {
- return null;
- }
- }
- private static String getOSMFolderName() {
- return OSM_FILE.substring(0, OSM_FILE.indexOf('.'));
- }
- public static void prepareFile() {
- GraphHopperOSM gh = new GraphHopperOSM();
- gh.setOSMFile(OSM_SOURCE_DIR + OSM_FILE).setStoreOnFlush(false);
- String output = OSM_SOURCE_DIR + getOSMFolderName();
- new File(output).mkdirs();
- gh.setGraphHopperLocation(output);
- gh.forDesktop();
- CarFlagEncoder carEncoder = new CarFlagEncoder();
- BikeFlagEncoder bikeEncoder = new BikeFlagEncoder();
- FootFlagEncoder footEncoder = new FootFlagEncoder();
- gh.setEncodingManager(new EncodingManager.Builder().add(carEncoder).add(bikeEncoder).add(footEncoder).build());
- gh.setAllowWrites(true);
- gh.setCHEnabled(true);
- gh.importAndClose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement