MainController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.deliveryproject.easydelivery;
  2. import com.deliveryproject.easydelivery.Models.Coordinate;
  3. import com.deliveryproject.easydelivery.Models.OSMRClass.Intersection;
  4. import com.deliveryproject.easydelivery.Models.OSMRClass.Root;
  5. import com.deliveryproject.easydelivery.Models.OSMRClass.Step;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import org.springframework.web.bind.annotation.*;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. @CrossOrigin
  16. @RestController
  17. public class MainController {
  18. //@PreAuthorize("hasRole('User')")
  19. @GetMapping("/route/nodes")
  20. @ResponseBody
  21. public ArrayList<ArrayList<Double>> getNodesBetweenTwoCoordinates(@RequestParam double lon1, @RequestParam double lat1, @RequestParam double lon2, @RequestParam double lat2) throws IOException {
  22. System.out.println(lon1);
  23. String url = "http://router.project-osrm.org/route/v1/driving/" + lon1 + "," + lat1 + ";" + lon2 + "," + lat2 + "?steps=true&geometries=geojson";
  24. System.out.println(url);
  25. URL osrmEndpoint = new URL(url);
  26. HttpURLConnection connection = (HttpURLConnection) osrmEndpoint.openConnection();
  27. connection.setRequestMethod("GET");
  28. connection.setRequestProperty("Content-Type", "application/json");
  29. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  30. String inputLine;
  31. StringBuilder response = new StringBuilder();
  32. while ((inputLine = in.readLine()) != null) {
  33. response.append(inputLine);
  34. }
  35. in.close();
  36. ObjectMapper om = new ObjectMapper();
  37. Root root = om.readValue(response.toString(), Root.class);
  38. ArrayList<Step> steps = root.routes.get(0).legs.get(0).steps;
  39. ArrayList<ArrayList<Double>> coordinates = new ArrayList<>();
  40. for (Step step : steps) {
  41. ArrayList<Intersection> intersections = step.intersections;
  42. for (Intersection intersection : intersections) {
  43. ArrayList<Double> location = new ArrayList<>();
  44. location.add(intersection.location.get(1));
  45. location.add(intersection.location.get(0));
  46. coordinates.add(location);
  47. }
  48. }
  49. System.out.println(coordinates);
  50. return coordinates;
  51. }
  52. @GetMapping("/optimize-delivery-route")
  53. @ResponseBody
  54. public List<Coordinate> optimizeDeliveryRoute(List<Coordinate> coordinates) {
  55. List<Integer> optimizedRoute = new ArrayList<>();
  56. List<Coordinate> optimizedRouteCoordinates = new ArrayList<>();
  57. boolean[] visited = new boolean[coordinates.size()];
  58. int currentCoordinateIndex = 0;
  59. optimizedRoute.add(currentCoordinateIndex);
  60. visited[currentCoordinateIndex] = true;
  61. while (optimizedRoute.size() < coordinates.size()) {
  62. double minDistance = Double.MAX_VALUE;
  63. int nearestCoordinateIndex = -1;
  64. for (int i = 0; i < coordinates.size(); i++) {
  65. if (!visited[i]) {
  66. double distance = calculateDistance(coordinates.get(currentCoordinateIndex), coordinates.get(i));
  67. if (distance < minDistance) {
  68. minDistance = distance;
  69. nearestCoordinateIndex = i;
  70. }
  71. }
  72. }
  73. optimizedRoute.add(nearestCoordinateIndex);
  74. visited[nearestCoordinateIndex] = true;
  75. currentCoordinateIndex = nearestCoordinateIndex;
  76. }
  77. for (Integer integer : optimizedRoute) {
  78. optimizedRouteCoordinates.add(coordinates.get(integer));
  79. }
  80. return optimizedRouteCoordinates;
  81. }
  82. private double calculateDistance(Coordinate c1, Coordinate c2) {
  83. ArrayList<ArrayList<Double>> nodes;
  84. try {
  85. nodes = getNodesBetweenTwoCoordinates(c1.lat, c1.lon, c2.lat, c2.lon);
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. return Double.MAX_VALUE;
  89. }
  90. double sumDistance = 0.0;
  91. for (int i = 0; i < nodes.size() - 1; i++) {
  92. ArrayList<Double> current = nodes.get(i);
  93. ArrayList<Double> next = nodes.get(i + 1);
  94. double dx = next.get(0) - current.get(0);
  95. double dy = next.get(1) - current.get(1);
  96. double distance = Math.sqrt(dx * dx + dy * dy);
  97. sumDistance += distance;
  98. }
  99. return sumDistance;
  100. }
  101. }