|
| 1 | +/** |
| 2 | + *Created by dariusaudryc on 28/2/17. |
| 3 | + */ |
| 4 | + |
| 5 | +package pygraphhopper; |
| 6 | + |
| 7 | +import com.graphhopper.GHRequest; |
| 8 | +import com.graphhopper.GHResponse; |
| 9 | +import com.opencsv.CSVReader; |
| 10 | +import com.opencsv.CSVWriter; |
| 11 | +import com.graphhopper.GraphHopper; |
| 12 | +import com.graphhopper.reader.osm.GraphHopperOSM; |
| 13 | +import com.graphhopper.routing.util.EncodingManager; |
| 14 | +import com.graphhopper.PathWrapper; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.io.FileReader; |
| 17 | +import java.io.FileWriter; |
| 18 | +import java.util.List; |
| 19 | +import static java.lang.Math.sqrt; |
| 20 | + |
| 21 | +public class route_calculation { |
| 22 | + |
| 23 | + private static GraphHopper graphHopper; |
| 24 | + |
| 25 | + public route_calculation(String workDir, String vehicle, String osmFile) { |
| 26 | + // create one GraphHopper instance |
| 27 | + graphHopper = new GraphHopperOSM().forServer(); |
| 28 | + graphHopper.setGraphHopperLocation(workDir); // "when graphhopper is calculating the route, it creates nodes. workDir is a temporary directory to store the nodes" |
| 29 | + graphHopper.setEncodingManager(new EncodingManager(vehicle)); |
| 30 | + graphHopper.setDataReaderFile(osmFile); // "osmFile is the open street map data that allows us to create nodes for routing" |
| 31 | + graphHopper.importOrLoad();//The nodes will be called if the nodes had been created in this syntax, else the nodes will be created |
| 32 | + } |
| 33 | + |
| 34 | + public static double getDistance(double orig_lat, double orig_lon, |
| 35 | + double dest_lat, double dest_lon, String vehicle) { |
| 36 | + |
| 37 | + //System.out.println("Calculating route distance for following lat/long pair:" |
| 38 | + // + orig_lat + ", " + orig_lon + " & " + dest_lat + ", " + dest_lon + " " + vehicle); |
| 39 | + |
| 40 | + |
| 41 | + GHRequest request = new GHRequest(orig_lat, orig_lon, dest_lat, dest_lon);// simple configuration of the request object |
| 42 | + request.setWeighting("fastest"); |
| 43 | + request.setVehicle(vehicle); |
| 44 | + |
| 45 | + try { |
| 46 | + GHResponse route = graphHopper.route(request); |
| 47 | + PathWrapper path = route.getBest();// use the best path, see the GHResponse class for more possibilities. |
| 48 | + return path.getDistance() / 1000; |
| 49 | + } catch(Exception e){ |
| 50 | + //TODO: find a better approach |
| 51 | + System.out.println("Exception caught: " + e.getMessage()); |
| 52 | + System.out.println("Calculating RMS value for following lat/long pair:" |
| 53 | + + orig_lat + ", " + orig_lon + " & " + dest_lat + ", " + dest_lon); |
| 54 | + |
| 55 | + double |
| 56 | + dlat = dest_lat - orig_lat, |
| 57 | + dlon = dest_lon - orig_lon, |
| 58 | + dist2 = dlat * dlat + dlon * dlon, |
| 59 | + dist = sqrt(dist2); |
| 60 | + return(dist); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static void mainHelper(String FileInput, String FileOutput, String originInputLat, String originInputLong, |
| 65 | + String destInputLat, String destInputLong, String vehicle) { |
| 66 | + |
| 67 | + |
| 68 | + CSVReader reader = null; |
| 69 | + CSVWriter writer = null; |
| 70 | + |
| 71 | + System.out.println("Going to process file: " + FileInput + "...........\n"); |
| 72 | + |
| 73 | + try { |
| 74 | + reader = new CSVReader(new FileReader(FileInput)); |
| 75 | + writer = new CSVWriter(new FileWriter(FileOutput), ','); |
| 76 | + } catch (Exception e) { |
| 77 | + |
| 78 | + System.out.println("Arguments are not defined correctly" |
| 79 | + + e.getMessage()); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + String[] entries = null; |
| 84 | + Boolean firstRow = true; |
| 85 | + String[] header = null; |
| 86 | + |
| 87 | + int originLat = -1; |
| 88 | + int originLon = -1; |
| 89 | + int destLat = -1; |
| 90 | + int destLon = -1; |
| 91 | + |
| 92 | + try { |
| 93 | + while ((entries = reader.readNext()) != null) { |
| 94 | + |
| 95 | + List<String> val = new ArrayList<String>(); |
| 96 | + |
| 97 | + if (firstRow) { |
| 98 | + firstRow = false; |
| 99 | + for (int i = 0; i < entries.length; i++) { |
| 100 | + if (entries[i].equals(originInputLat)) { |
| 101 | + originLat = i; |
| 102 | + } |
| 103 | + if (entries[i].equals(originInputLong)) { |
| 104 | + originLon = i; |
| 105 | + } |
| 106 | + if (entries[i].equals(destInputLat)) { |
| 107 | + destLat = i; |
| 108 | + } |
| 109 | + if (entries[i].equals(destInputLong)) { |
| 110 | + destLon = i; |
| 111 | + } |
| 112 | + val.add(entries[i]); |
| 113 | + } |
| 114 | + |
| 115 | + val.add("customer_route_dist"); |
| 116 | + String[] new_val = new String[val.size()]; |
| 117 | + val.toArray(new_val); |
| 118 | + |
| 119 | + writer.writeNext(new_val); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + for (int i = 0; i < entries.length; i++) |
| 124 | + val.add(entries[i]); |
| 125 | + |
| 126 | + Double mul = new Double(-1.0); |
| 127 | + |
| 128 | + if (originLat != -1 && originLon != -1 && destLat != -1 && destLon != -1) { |
| 129 | + mul = getDistance(Double.parseDouble(entries[originLat]), Double.parseDouble(entries[originLon]), |
| 130 | + Double.parseDouble(entries[destLat]), Double.parseDouble(entries[destLon]), vehicle); |
| 131 | + } |
| 132 | + |
| 133 | + val.add(mul.toString()); |
| 134 | + |
| 135 | + String[] new_val = new String[val.size()]; |
| 136 | + val.toArray(new_val); |
| 137 | + writer.writeNext(new_val); |
| 138 | + } |
| 139 | + |
| 140 | + } catch (Exception e) { |
| 141 | + System.out.println("Error in running the routing function" |
| 142 | + + e.getMessage()); |
| 143 | + } |
| 144 | + try { |
| 145 | + writer.close(); |
| 146 | + } catch (Exception e) { |
| 147 | + System.out.println("Error in Writing to csv" |
| 148 | + + e.getMessage()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + //To be used form command line. |
| 153 | + public static void main(String[] args) { |
| 154 | + |
| 155 | + String |
| 156 | + FileInput = args[0], |
| 157 | + FileOutput = args[1], |
| 158 | + originInputLat = args[2], |
| 159 | + originInputLong = args[3], |
| 160 | + destInputLat = args[4], |
| 161 | + destInputLong = args[5], |
| 162 | + workDir = args[6], |
| 163 | + osmFile = args[7], |
| 164 | + vehicle = args[8]; |
| 165 | + |
| 166 | + mainHelper(FileInput, FileOutput, originInputLat, originInputLong, |
| 167 | + destInputLat, destInputLong, vehicle); |
| 168 | + |
| 169 | + } |
| 170 | +} |
0 commit comments