changed routeSequences to vector of string arrays to fit new direction format. updated calculateRoutes to handle the new formatting. removed distanceBetweenPoints function as it's no longer needed
This commit is contained in:
parent
72f82fb425
commit
1612ae5978
1 changed files with 20 additions and 46 deletions
66
Route.java
66
Route.java
|
@ -1,11 +1,13 @@
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
public class Route {
|
public class Route {
|
||||||
private double[] currentLocation; //Lat-Long coordinates for current location
|
private double[] currentLocation; //Lat-Long coordinates for current location
|
||||||
private double[][][] routeSequences; //3D array for routes. 1st dim selects route, 2nd dim selects route point, 3rd dim selects lat/long/speed/tolls (indices 0-2)
|
private Vector<Vector<String[]>> routeSequences; //3D array for routes. 1st dim selects route, 2nd dim selects route point, 3rd dim selects lat/long/speed/tolls (indices 0-2)
|
||||||
private int routeSelected;
|
private int routeSelected;
|
||||||
private boolean tollsFlag; //stores user setting for whether to avoid tolls
|
private boolean tollsFlag; //stores user setting for whether to avoid tolls
|
||||||
private boolean hwyFlag; //stores user preference for whether to avoid major highways
|
private boolean hwyFlag; //stores user preference for whether to avoid major highways
|
||||||
public double[] timeEstimate; //time estimate in seconds
|
public Vector<Double> timeEstimate; //time estimate in seconds
|
||||||
public float[] tollsAmount; //stores toll amount if toll roads are used
|
public Vector<Float> tollsAmount; //stores toll amount if toll roads are used
|
||||||
public Destination currentDestination; //uses user-created class Destination
|
public Destination currentDestination; //uses user-created class Destination
|
||||||
|
|
||||||
private void updateCurrentLocation() {
|
private void updateCurrentLocation() {
|
||||||
|
@ -20,15 +22,18 @@ public class Route {
|
||||||
//ideally a query would go out using multiple pathfiding algorithms to get route options
|
//ideally a query would go out using multiple pathfiding algorithms to get route options
|
||||||
updateCurrentLocation();
|
updateCurrentLocation();
|
||||||
HelperFunctions apiCallToRouting = new HelperFunctions();
|
HelperFunctions apiCallToRouting = new HelperFunctions();
|
||||||
routeSequences[0] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 1
|
//route option 1
|
||||||
timeEstimate[0] = estimateTime(routeSequences[0]);
|
routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag));
|
||||||
estimateTolls(routeSequences[0]);
|
timeEstimate[0] = estimateTime(routeSequences.get(0));
|
||||||
routeSequences[1] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 2
|
estimateTolls(routeSequences.get(0));
|
||||||
timeEstimate[1] = estimateTime(routeSequences[1]);
|
//route option 2
|
||||||
estimateTolls(routeSequences[1]);
|
routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag));
|
||||||
routeSequences[2] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 3
|
timeEstimate[1] = estimateTime(routeSequences.get(1));
|
||||||
timeEstimate[2] = estimateTime(routeSequences[1]);
|
estimateTolls(routeSequences.get(1));
|
||||||
estimateTolls(routeSequences[1]);
|
//route option 3
|
||||||
|
routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag));
|
||||||
|
timeEstimate[2] = estimateTime(routeSequences.get(2));
|
||||||
|
estimateTolls(routeSequences.get(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int enumRoutes() {
|
public int enumRoutes() {
|
||||||
|
@ -49,45 +54,14 @@ public class Route {
|
||||||
return runningTolls;
|
return runningTolls;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double estimateTime(double[][] route) {
|
public double estimateTime(Vector<String[]> route) {
|
||||||
//calculates distances between route segments, divided by speed in MPH
|
//calculates distances between route segments, divided by speed in MPH
|
||||||
//the API would return expected speed due to traffic conditions as part of the speed (index 2) in the route array
|
//the API would return expected speed due to traffic conditions as part of the speed (index 2) in the route array
|
||||||
double runningTime = 0;
|
double runningTime = 0;
|
||||||
//steps through subsequence route points to get distances, calculates time based on speed estimate, and accumulates on runningTime
|
//steps through subsequence route points to get distances, calculates time based on speed estimate, and accumulates on runningTime
|
||||||
for (int i = 0; i < route.length; i++) {
|
for (String[] el : route) {
|
||||||
runningTime += distanceBetweenPoints(route[i], route[i+1]) / route[i][2];
|
runningTime += Double.parseDouble(el[1])/Double.parseDouble(el[2]);
|
||||||
}
|
}
|
||||||
return runningTime;
|
return runningTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
private double distanceBetweenPoints(double[] pointA, double[] pointB) {
|
|
||||||
/**
|
|
||||||
* Code excerpt by StackOverflow user Dommer, adapted for this application's use
|
|
||||||
* https://stackoverflow.com/questions/3694380/calculating-distance-between-two-points-using-latitude-longitude/3694410#3694410
|
|
||||||
*
|
|
||||||
* Calculate distance between two points in latitude and longitude taking
|
|
||||||
* into account height difference. If you are not interested in height
|
|
||||||
* difference pass 0.0. Uses Haversine method as its base.
|
|
||||||
*
|
|
||||||
* lat1, lon1 Start point lat2, lon2 End point el1 Start altitude in miles
|
|
||||||
* el2 End altitude in miles
|
|
||||||
* @returns Distance
|
|
||||||
*/
|
|
||||||
final int R = 6371; // Radius of the earth
|
|
||||||
int el1 = 0, el2 = 0;
|
|
||||||
|
|
||||||
double latDistance = Math.toRadians(pointA[0] - pointB[0]);
|
|
||||||
double lonDistance = Math.toRadians(pointA[1] - pointB[1]);
|
|
||||||
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
|
|
||||||
+ Math.cos(Math.toRadians(pointA[0])) * Math.cos(Math.toRadians(pointB[0]))
|
|
||||||
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
|
|
||||||
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
||||||
double distance = R * c * 0.6213711922; // convert to miles
|
|
||||||
|
|
||||||
double height = el1 - el2;
|
|
||||||
|
|
||||||
distance = Math.pow(distance, 2) + Math.pow(height, 2);
|
|
||||||
|
|
||||||
return Math.sqrt(distance);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue