MainController.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package com.deliveryproject.easydelivery;
  2. import com.deliveryproject.easydelivery.OSMRClass.Intersection;
  3. import com.deliveryproject.easydelivery.OSMRClass.Root;
  4. import com.deliveryproject.easydelivery.OSMRClass.Step;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import org.springframework.security.access.prepost.PreAuthorize;
  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. @CrossOrigin
  15. @RestController
  16. public class MainController {
  17. @PreAuthorize("hasRole('User')")
  18. @GetMapping("/route/nodes")
  19. @ResponseBody
  20. public ArrayList<ArrayList<Double>> getNodesBetweenTwoCoordinates(@RequestParam double lon1, @RequestParam double lat1, @RequestParam double lon2, @RequestParam double lat2) throws IOException {
  21. System.out.println(lon1);
  22. String url = "http://router.project-osrm.org/route/v1/driving/" + lon1 + "," + lat1 + ";" + lon2 + "," + lat2 + "?steps=true&geometries=geojson";
  23. System.out.println(url);
  24. URL osrmEndpoint = new URL(url);
  25. HttpURLConnection connection = (HttpURLConnection) osrmEndpoint.openConnection();
  26. connection.setRequestMethod("GET");
  27. connection.setRequestProperty("Content-Type", "application/json");
  28. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  29. String inputLine;
  30. StringBuilder response = new StringBuilder();
  31. while ((inputLine = in.readLine()) != null) {
  32. response.append(inputLine);
  33. }
  34. in.close();
  35. ObjectMapper om = new ObjectMapper();
  36. Root root = om.readValue(response.toString(), Root.class);
  37. ArrayList<Step> steps = root.routes.get(0).legs.get(0).steps;
  38. ArrayList<ArrayList<Double>> coordinates = new ArrayList<>();
  39. for (Step step : steps) {
  40. ArrayList<Intersection> intersections = step.intersections;
  41. for (Intersection intersection : intersections) {
  42. ArrayList<Double> location = new ArrayList<>();
  43. location.add(intersection.location.get(1));
  44. location.add(intersection.location.get(0));
  45. coordinates.add(location);
  46. }
  47. }
  48. System.out.println(coordinates);
  49. return coordinates;
  50. }
  51. }