Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1.  
  2.  
  3. import java.io.File;
  4.  
  5. import com.graphhopper.GHRequest;
  6. import com.graphhopper.GHResponse;
  7. import com.graphhopper.GraphHopper;
  8. import com.graphhopper.PathWrapper;
  9. import com.graphhopper.reader.osm.GraphHopperOSM;
  10. import com.graphhopper.routing.util.BikeFlagEncoder;
  11. import com.graphhopper.routing.util.CarFlagEncoder;
  12. import com.graphhopper.routing.util.EncodingManager;
  13. import com.graphhopper.routing.util.FootFlagEncoder;
  14. import com.graphhopper.storage.GraphHopperStorage;
  15. import com.graphhopper.util.Parameters;
  16. import com.graphhopper.util.StopWatch;
  17.  
  18. public class TestMapCreator {
  19.  
  20. private static final String OSM_SOURCE_DIR = "/Users/victorshcherb/osmand/maps/gh/";
  21. private static final String OSM_FILE = "ukraine_kiev_europe.pbf";
  22.  
  23. public static void main(String[] args) {
  24. // prepareFile();
  25. calculateRoute();
  26. }
  27.  
  28. private static void calculateRoute() {
  29. GraphHopper hopper = new GraphHopper().forMobile();
  30. hopper.load(new File(OSM_SOURCE_DIR, getOSMFolderName()).getAbsolutePath());
  31. GHRequest req = new GHRequest(50.439296, 30.478347, 50.449700, 30.478776)
  32. .setAlgorithm(Parameters.Algorithms.DIJKSTRA_BI);
  33. req.getHints().put(Parameters.Routing.INSTRUCTIONS, "false");
  34. PathWrapper pw = calculateRoute(hopper, req);
  35. System.out.println(pw);
  36. GraphHopperStorage st = hopper.getGraphHopperStorage();
  37. System.out.println(st.toDetailsString());
  38. System.out.println(st.getEdges());
  39. }
  40.  
  41. public static PathWrapper calculateRoute(GraphHopper hopper, GHRequest request) {
  42. if (hopper != null) {
  43. System.out.println("calculating path ...");
  44. float time;
  45. StopWatch sw = new StopWatch().start();
  46. GHResponse resp = hopper.route(request);
  47. time = sw.stop().getSeconds();
  48. if (!resp.hasErrors()) {
  49. System.out.println("from:" + request.getPoints().get(0).lat + "," + request.getPoints().get(0).lon
  50. + " to:" + request.getPoints().get(request.getPoints().size() - 1).lat + ","
  51. + request.getPoints().get(request.getPoints().size() - 1).lon + " found path with distance:"
  52. + resp.getBest().getDistance() / 1000f + ", nodes:" + resp.getBest().getPoints().getSize()
  53. + ", time:" + time + " " + resp.getDebugInfo());
  54. System.out.println("the route is " + (int) (resp.getBest().getDistance() / 100) / 10f + "km long, time:"
  55. + resp.getBest().getTime() / 60000f + "min, debug:" + time);
  56. }
  57. return resp.getBest();
  58. } else {
  59. return null;
  60. }
  61. }
  62.  
  63. private static String getOSMFolderName() {
  64. return OSM_FILE.substring(0, OSM_FILE.indexOf('.'));
  65. }
  66.  
  67. public static void prepareFile() {
  68. GraphHopperOSM gh = new GraphHopperOSM();
  69. gh.setOSMFile(OSM_SOURCE_DIR + OSM_FILE).setStoreOnFlush(false);
  70. String output = OSM_SOURCE_DIR + getOSMFolderName();
  71. new File(output).mkdirs();
  72. gh.setGraphHopperLocation(output);
  73. gh.forDesktop();
  74.  
  75. CarFlagEncoder carEncoder = new CarFlagEncoder();
  76. BikeFlagEncoder bikeEncoder = new BikeFlagEncoder();
  77. FootFlagEncoder footEncoder = new FootFlagEncoder();
  78. gh.setEncodingManager(new EncodingManager.Builder().add(carEncoder).add(bikeEncoder).add(footEncoder).build());
  79. gh.setAllowWrites(true);
  80. gh.setCHEnabled(true);
  81. gh.importAndClose();
  82. }
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement