Kaynağa Gözat

Created an endpoint that allows getting intersection coordinates between two points

wpfat23-5 3 yıl önce
ebeveyn
işleme
e14375ef8b

+ 13 - 0
pom.xml

@@ -28,6 +28,10 @@
 			<artifactId>spring-boot-starter-tomcat</artifactId>
 			<scope>provided</scope>
 		</dependency>
+
+
+
+
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-test</artifactId>
@@ -37,6 +41,15 @@
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-actuator</artifactId>
 		</dependency>
+
+		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
+		<dependency>
+			<groupId>javax.servlet</groupId>
+			<artifactId>javax.servlet-api</artifactId>
+			<version>4.0.1</version>
+			<scope>provided</scope>
+		</dependency>
+
 	</dependencies>
 
 	<build>

+ 54 - 5
src/main/java/com/deliveryproject/easydelivery/MainController.java

@@ -1,12 +1,61 @@
 package com.deliveryproject.easydelivery;
 
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RestController;
+import com.deliveryproject.easydelivery.OSMRClass.Intersection;
+import com.deliveryproject.easydelivery.OSMRClass.Root;
+import com.deliveryproject.easydelivery.OSMRClass.Step;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.web.bind.annotation.*;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+
 
 @RestController
 public class MainController {
-    @GetMapping("/")
-    public String index() {
-        return "Greetings from Spring Boot!";
+
+    @GetMapping("/route/nodes")
+    @ResponseBody
+    @CrossOrigin(origins = "http://localhost:3000", maxAge = 3600)
+    public ArrayList<ArrayList<Double>> getNodesBetweenTwoCoordinates(@RequestParam double lon1, @RequestParam double lat1, @RequestParam double lon2, @RequestParam double lat2) throws IOException {
+        System.out.println(lon1);
+        String url = "http://router.project-osrm.org/route/v1/driving/" + lon1 + "," + lat1 + ";" + lon2 + "," + lat2 + "?steps=true&geometries=geojson";
+        URL osrmEndpoint = new URL(url);
+        HttpURLConnection connection = (HttpURLConnection) osrmEndpoint.openConnection();
+        connection.setRequestMethod("GET");
+        connection.setRequestProperty("Content-Type", "application/json");
+
+        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+        String inputLine;
+        StringBuilder response = new StringBuilder();
+        while ((inputLine = in.readLine()) != null) {
+            response.append(inputLine);
+        }
+        in.close();
+
+        ObjectMapper om = new ObjectMapper();
+        Root root = om.readValue(response.toString(), Root.class);
+
+        ArrayList<Step> steps = root.routes.get(0).legs.get(0).steps;
+
+        ArrayList<ArrayList<Double>> coordinates = new ArrayList<>();
+
+        for (Step step : steps) {
+            ArrayList<Intersection> intersections = step.intersections;
+            for (Intersection intersection : intersections) {
+                ArrayList<Double> location = new ArrayList<>();
+                location.add(intersection.location.get(1));
+                location.add(intersection.location.get(0));
+                coordinates.add(location);
+            }
+
+        }
+        System.out.println(coordinates);
+        return coordinates;
     }
+
+
 }

+ 0 - 1
src/main/resources/application.properties

@@ -1 +0,0 @@
-