0

I have a local instance of graphhopper running on the back-end using java spring boot. this is the code:

package com.example.greenpathsbe.Services;

import com.graphhopper.GraphHopper;
import com.graphhopper.GraphHopperConfig;
import com.graphhopper.ResponsePath;
import com.graphhopper.reader.osm.GraphHopperOSM;
import com.graphhopper.config.Profile;
import com.graphhopper.util.Instruction;
import com.graphhopper.util.PointList;
import com.graphhopper.util.shapes.GHPoint;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.*;

@Service
public class GraphHopperService {

    @Value("${graphhopper.data.path}")
    private String dataPath;

    @Value("${graphhopper.data.pathToCsv}")
    private String path;
    private String graphFolder = "./graphFolder";

    private List<Profile> profiles;
    private GraphHopper graphHopper;



    @PostConstruct
    public void init() {
        Profile profile = new Profile("my_profile")
                .setVehicle("foot")
                
                .setWeighting("shortest");
        profile.getHints().put("instructions", "true");
        profiles = new ArrayList<>();
        profiles.add(profile);
        graphHopper = new GraphHopperOSM();
        graphHopper.setOSMFile(dataPath + "eindhoven.osm.pbf");
        graphHopper.init(new GraphHopperConfig().setProfiles(profiles));
        graphHopper.setGraphHopperLocation(graphFolder);
        graphHopper.getCHPreparationHandler();
        graphHopper.importOrLoad();
    }

    public GraphHopper getGraphHopper() {
        return graphHopper;
    }
    public List<CustomPath> createCustomPaths(List<ResponsePath> responsePaths) {
        List<CustomPath> customPaths = new ArrayList<>();
        for (ResponsePath responsePath : responsePaths) {
            CustomPath customPath = new CustomPath();
            List<Coordinate> coordinates = new ArrayList<>();
            for (GHPoint point : responsePath.getPoints()) {
                Coordinate coordinate = new Coordinate();
                coordinate.setLatitude(point.getLat());
                coordinate.setLongitude(point.getLon());
                coordinates.add(coordinate);
            }
            customPath.setNodes(coordinates.size());
            customPath.setCoordinates(coordinates);
            List<PathSegment> pathSegments = new ArrayList<>();
            for (Instruction instruction : responsePath.getInstructions()) {
                PathSegment pathSegment = new PathSegment();
                pathSegment.setId(instruction.getSign());
                pathSegment.setName(instruction.getName());
                pathSegment.setDistance(instruction.getDistance());
                pathSegment.setTime(instruction.getTime());
                pathSegments.add(pathSegment);
            }
            customPath.setPathSegments(pathSegments);
            customPaths.add(customPath);
        }
        return customPaths;
    }
    public CustomPath createCustomPath(ResponsePath responsePath) {
        CustomPath customPath = new CustomPath();
        List<Coordinate> coordinates = new ArrayList<>();
        for (GHPoint point : responsePath.getPoints()) {
            Coordinate coordinate = new Coordinate();
            coordinate.setLatitude(point.getLat());
            coordinate.setLongitude(point.getLon());
            coordinates.add(coordinate);
        }
        customPath.setNodes(coordinates.size());
        customPath.setCoordinates(coordinates);
        List<PathSegment> pathSegments = new ArrayList<>();
        for (Instruction instruction : responsePath.getInstructions()) {
            PathSegment pathSegment = new PathSegment();
            pathSegment.setId(instruction.getSign());
            pathSegment.setName(instruction.getName());
            pathSegment.setDistance(instruction.getDistance());
            pathSegment.setTime(instruction.getTime());
            pathSegments.add(pathSegment);
        }
        customPath.setPathSegments(pathSegments);
        return customPath;
    }
}


This is the controller which I call in the front-end:

package com.example.greenpathsbe.controller;

import com.example.greenpathsbe.Services.CustomPath;
import com.example.greenpathsbe.Services.ExtendedResponsePath;
import com.example.greenpathsbe.Services.GraphHopperService;
import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
import com.graphhopper.ResponsePath;
import com.graphhopper.util.Parameters;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.bind.annotation.*;
import com.graphhopper.routing.Path;

import java.lang.reflect.Parameter;
import java.util.List;

@RestController
@RequestMapping("/routes")
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:3000/")
public class RouteController {
    @Autowired
    private GraphHopperService graphHopperService;

    @GetMapping
    public List<CustomPath> getRoute(
            @RequestParam("fromLat") double fromLat,
            @RequestParam("fromLng") double fromLng,
            @RequestParam("toLat") double toLat,
            @RequestParam("toLng") double toLng) {

            System.out.println("fromLat: " + fromLat);
            System.out.println("fromLng: " + fromLng);
            System.out.println("toLat: " + toLat);
            System.out.println("toLng: " + toLng);
            GHRequest request = new GHRequest(fromLat, fromLng, toLat, toLng).setProfile("my_profile").setAlgorithm(Parameters.Algorithms.ALT_ROUTE);
            request.getHints().put("instructions", "true");  // Might be useless
            GHResponse response = graphHopperService.getGraphHopper().route(request);
            List<ResponsePath> responsePaths = response.getAll();
            System.out.println("response: " + response);

          //  return graphHopperService.createCustomPath(response.getBest());
            return graphHopperService.createCustomPaths(responsePaths);

    }


}

I am trying to make it so it will return multiple routes between point A and B. Right now I get a maximum of 2 routes which is not sufficient for my needs. Any ideas what can I do?

SVG
  • 1
  • 1

0 Answers0