use of cl.smartcities.isci.transportinspector.map.model.ComparableLocation in project androidApp by InspectorIncognito.
the class GridHelper method getRouteSegments.
/**
* Calculate the bus direction based on list of points.
*
* @param serviceName ex: 506, 506e, ...
* @param points list of user points, the last point is the most recent
* @return bus direction ("I" or "R")
*/
/*
public String calculateDirection(String serviceName, List<GeoPoint> points) {
final String I_DIRECTION = "I";
final String R_DIRECTION = "R";
final int AREA = 1;
int size = points.size();
GeoPoint lastPoint = points.get(size-1);
GeoPoint penultimatePoint = points.get(size-2);
//Log.d(TAG, "penultimate point: " + penultimatePoint.getLatitude() + "," +
// penultimatePoint.getLongitude());
//Log.d(TAG, "last point: " + lastPoint.getLatitude() + "," + lastPoint.getLongitude());
Pair gridPosition = getCellId(lastPoint.getLatitude(), lastPoint.getLongitude());
String textServices = this.getServicesInGrid(gridPosition, AREA);
String[] services = textServices.split("/");
List<Integer> serviceDirectionIIndex = new ArrayList<>();
List<Integer> serviceDirectionRIndex = new ArrayList<>();
for (int i=0; i < services.length; i++) {
if (services[i].equals(serviceName + I_DIRECTION)) {
serviceDirectionIIndex.add(i);
} else if (services[i].equals(serviceName + R_DIRECTION)) {
serviceDirectionRIndex.add(i);
}
}
// if the service is not present in the cell areas, or if it exist only one direction
if (serviceDirectionIIndex.size() == 0 && serviceDirectionRIndex.size() == 0) {
return null;
} else if (serviceDirectionIIndex.size() > 0 && serviceDirectionRIndex.size() == 0) {
return I_DIRECTION;
} else if (serviceDirectionIIndex.size() == 0 && serviceDirectionRIndex.size() > 0) {
return R_DIRECTION;
}
// else, calculate the angle
String textSegments = this.getRouteSegments(gridPosition, AREA);
String[] segments = textSegments.split("/");
String iSegments = "";
for (Integer i: serviceDirectionIIndex) {
iSegments += segments[i].replace("-", ",") + ",";
}
iSegments = (iSegments.equals("")?"":iSegments.substring(0, iSegments.length()-1));
String rSegments = "";
for (Integer i: serviceDirectionRIndex) {
rSegments += segments[i].replace("-", ",") + ",";
}
rSegments = (rSegments.equals("")?"":rSegments.substring(0, rSegments.length()-1));
//Log.d(TAG, "iSegments: " + iSegments);
//Log.d(TAG, "rSegments: " + rSegments);
List<GeoPoint> iRouteSegments = new ArrayList<>();//this.getRouteSegments(serviceName+I_DIRECTION, iSegments);
List<GeoPoint> rRouteSegments = new ArrayList<>();//this.getRouteSegments(serviceName+R_DIRECTION, rSegments);
// in meters
double iMinDistance = 1000000;
double rMinDistance = 1000000;
int iMinRoutepointIndex = 0;
int rMinRoutepointIndex = 0;
GeoPoint p;
double distance;
for (int i=0; i < iRouteSegments.size(); i++) {
p = iRouteSegments.get(i);
distance = Math.sqrt(Math.pow(lastPoint.getLatitude()-p.getLatitude(), 2) +
Math.pow(lastPoint.getLongitude()-p.getLongitude(), 2));
//Log.d(TAG, "(IDA): " + distance +" mts (" + p.getLatitude() + "," + p.getLongitude() +")");
if (distance < iMinDistance) {
iMinDistance = distance;
iMinRoutepointIndex = i;
}
}
for (int i=0; i < rRouteSegments.size(); i++) {
p = rRouteSegments.get(i);
distance = Math.sqrt(Math.pow(lastPoint.getLatitude()-p.getLatitude(), 2) +
Math.pow(lastPoint.getLongitude()-p.getLongitude(), 2));
//Log.d(TAG, "(VUELTA): " + distance +" mts (" + p.getLatitude() + "," + p.getLongitude() +")");
if (distance < rMinDistance) {
rMinDistance = distance;
rMinRoutepointIndex = i;
}
}
GeoPoint nearestPointI1, nearestPointI2;
if (iRouteSegments.size() < 2) {
return R_DIRECTION;
} if (iMinRoutepointIndex == iRouteSegments.size()-1) {
nearestPointI1 = iRouteSegments.get(iMinRoutepointIndex - 1);
nearestPointI2 = iRouteSegments.get(iMinRoutepointIndex);
} else {
nearestPointI1 = iRouteSegments.get(iMinRoutepointIndex);
nearestPointI2 = iRouteSegments.get(iMinRoutepointIndex + 1);
}
GeoPoint nearestPointR1, nearestPointR2;
if (rRouteSegments.size() < 2) {
return I_DIRECTION;
} if (rMinRoutepointIndex == rRouteSegments.size()-1) {
nearestPointR1 = rRouteSegments.get(rMinRoutepointIndex - 1);
nearestPointR2 = rRouteSegments.get(rMinRoutepointIndex);
} else {
nearestPointR1 = rRouteSegments.get(rMinRoutepointIndex);
nearestPointR2 = rRouteSegments.get(rMinRoutepointIndex + 1);
}
//Log.d(TAG, "punto cercano de ida1: " + nearestPointI1.getLatitude() + "," + nearestPointI1.getLongitude());
//Log.d(TAG, "punto cercano de ida2: " + nearestPointI2.getLatitude() + "," + nearestPointI2.getLongitude());
//Log.d(TAG, "punto cercano de vuelta1: " + nearestPointR1.getLatitude() + "," + nearestPointR1.getLongitude());
//Log.d(TAG, "punto cercano de vuelta2: " + nearestPointR2.getLatitude() + "," + nearestPointR2.getLongitude());
Pair<Double,Double> userVector = new Pair<>(
lastPoint.getLatitude()-penultimatePoint.getLatitude(),
lastPoint.getLongitude()-penultimatePoint.getLongitude());
Pair<Double,Double> iVector = new Pair<>(
nearestPointI2.getLatitude()-nearestPointI1.getLatitude(),
nearestPointI2.getLongitude()-nearestPointI1.getLongitude());
Pair<Double,Double> rVector = new Pair<>(
nearestPointR2.getLatitude()-nearestPointR1.getLatitude(),
nearestPointR2.getLongitude()-nearestPointR1.getLongitude());
double userMag = Math.sqrt(Math.pow(userVector.first, 2) + Math.pow(userVector.second, 2));
double iMag = Math.sqrt(Math.pow(iVector.first, 2) + Math.pow(iVector.second, 2));
double rMag = Math.sqrt(Math.pow(rVector.first, 2) + Math.pow(rVector.second, 2));
// dot product
double dotProductUserI = userVector.first*iVector.first + userVector.second*iVector.second;
double dotProductUserR = userVector.first*rVector.first + userVector.second*rVector.second;
// cos angle
double cosI = dotProductUserI / (userMag*iMag);
double cosR = dotProductUserR / (userMag*rMag);
//Log.d(TAG, "degreeI: " + Math.toDegrees(Math.acos(cosI)));
//Log.d(TAG, "degreeR: " + Math.toDegrees(Math.acos(cosR)));
if (Math.toDegrees(Math.acos(cosI)) < Math.toDegrees(Math.acos(cosR))) {
return I_DIRECTION;
} else {
return R_DIRECTION;
}
}*/
/**
* get a list of GeoPoint related with a part of bus route
*
* @param serviceCode service with direction, ex: 506I, 506R
* @param segments groups of route segments needed
* @return list of GeoPoint
*/
private List<ComparableLocation> getRouteSegments(String serviceCode, String segments) {
String query = "SELECT " + DataBaseContract.Route.LATITUDE + ", " + DataBaseContract.Route.LONGITUDE + " FROM " + DataBaseContract.Route.TABLE_NAME + " WHERE " + DataBaseContract.Route.SERVICE + " = ? AND " + DataBaseContract.Route.SEQUENCE + " IN (" + segments + ") " + " ORDER BY " + DataBaseContract.Route.SEQUENCE + " ASC";
String[] params = new String[] { serviceCode };
Cursor cursor = this.getReadableDatabase().rawQuery(query, params);
List<ComparableLocation> route = new ArrayList<>();
while (cursor.moveToNext()) {
double lat = cursor.getDouble(0);
double lon = cursor.getDouble(1);
ComparableLocation location = new ComparableLocation(lat, lon);
route.add(location);
}
cursor.close();
this.close();
return route;
}
use of cl.smartcities.isci.transportinspector.map.model.ComparableLocation in project androidApp by InspectorIncognito.
the class PolyUtil method decode.
public static List<Location> decode(final String encodedPath) {
int len = encodedPath.length();
// For speed we preallocate to an upper bound on the final length, then
// truncate the array before returning.
final List<Location> path = new ArrayList<>();
int index = 0;
int lat = 0;
int lng = 0;
while (index < len) {
int result = 1;
int shift = 0;
int b;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
result = 1;
shift = 0;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
path.add(new ComparableLocation(lat * 1e-5, lng * 1e-5));
}
return path;
}
use of cl.smartcities.isci.transportinspector.map.model.ComparableLocation in project androidApp by InspectorIncognito.
the class StepJsonBuilder method getRoute.
private Route getRoute(JSONObject route, Context context) throws JSONException {
List<IStep> steps = new ArrayList<>();
List<GoogleStep> googleSteps = new ArrayList<>();
List<InnerPolyline> polyline = new ArrayList<>();
List<BusStop> busStops = new ArrayList<>();
String title = null;
String firstTitle = null;
JSONObject leg = route.getJSONArray("legs").getJSONObject(0);
JSONObject northeast = route.getJSONObject("bounds").getJSONObject("northeast");
JSONObject southwest = route.getJSONObject("bounds").getJSONObject("southwest");
Location northeastPoint = new ComparableLocation(northeast.getDouble("lat"), northeast.getDouble("lng"));
Location southwestPoint = new ComparableLocation(southwest.getDouble("lat"), southwest.getDouble("lng"));
String start = leg.getString("start_address");
String end = leg.getString("end_address");
String departureTime = leg.getJSONObject("departure_time").getString("text");
String arrivalTime = leg.getJSONObject("arrival_time").getString("text");
String duration = leg.getJSONObject("duration").getString("text");
GoogleStep prevState = new NullStep("Tu UbicaciĆ³n", start, departureTime);
GoogleStep currentState;
JSONArray rawSteps = leg.getJSONArray("steps");
for (int i = 0; i < rawSteps.length(); i++) {
JSONObject step = rawSteps.getJSONObject(i);
Location startPoint = new ComparableLocation(step.getJSONObject("start_location").getDouble("lat"), step.getJSONObject("start_location").getDouble("lng"));
Location endPoint = new ComparableLocation(step.getJSONObject("end_location").getDouble("lat"), step.getJSONObject("end_location").getDouble("lng"));
String travel_mode = step.getString("travel_mode");
String stepDuration = step.getJSONObject("duration").getString("text");
String actualRoute = step.getJSONObject("polyline").getString("points");
if (travel_mode.equals("TRANSIT")) {
JSONObject details = step.getJSONObject("transit_details");
String mode = details.getJSONObject("line").getJSONObject("vehicle").getString("type");
String line = details.getJSONObject("line").getString("short_name");
String departure = details.getJSONObject("departure_stop").getString("name");
String arrival = details.getJSONObject("arrival_stop").getString("name");
if (mode.equals("BUS")) {
try {
currentState = new BusStep(departure, arrival, stepDuration, line, actualRoute, startPoint, endPoint);
} catch (IllegalStateException exception) {
return null;
}
BusStop arrivalStop = getBusStop(arrival, context, endPoint);
if (arrivalStop == null) {
return null;
}
busStops.add(arrivalStop);
if (!prevState.isBusStep()) {
BusStop departureStop = getBusStop(departure, context, startPoint);
if (departureStop == null) {
return null;
}
busStops.add(departureStop);
}
} else {
String color = details.getJSONObject("line").getString("color");
currentState = new SubwayStep(departure, arrival, stepDuration, line, color, actualRoute, startPoint, endPoint);
}
if (title == null) {
title = currentState.getTitle();
}
} else {
int rawStepDuration = step.getJSONObject("duration").getInt("value");
currentState = new WalkingStep(stepDuration, rawStepDuration, actualRoute, startPoint, endPoint);
}
if (firstTitle == null) {
firstTitle = currentState.getTitle();
}
polyline.add(currentState.getPolyline());
googleSteps.add(currentState);
steps.add(prevState.dispatch(currentState, new StepBuilder(context)));
prevState = currentState;
}
currentState = new NullStep("Tu destino", end, arrivalTime);
steps.add(prevState.dispatch(currentState, new StepBuilder(context)));
title = title == null ? firstTitle : title;
return new Route(steps, googleSteps, duration, northeastPoint, southwestPoint, polyline, busStops, title);
}
use of cl.smartcities.isci.transportinspector.map.model.ComparableLocation in project androidApp by InspectorIncognito.
the class VectorMapFragment method get506ILineLocations.
private ArrayList<Location> get506ILineLocations() {
ArrayList<Location> locations = new ArrayList<>();
locations.add(new ComparableLocation(-33.456898, -70.661552));
locations.add(new ComparableLocation(-33.456974, -70.661103));
locations.add(new ComparableLocation(-33.456914, -70.660752));
locations.add(new ComparableLocation(-33.457051, -70.660644));
locations.add(new ComparableLocation(-33.457644, -70.660490));
locations.add(new ComparableLocation(-33.457898, -70.660609));
locations.add(new ComparableLocation(-33.458375, -70.660376));
locations.add(new ComparableLocation(-33.458279, -70.659341));
locations.add(new ComparableLocation(-33.458189, -70.657581));
locations.add(new ComparableLocation(-33.459151, -70.657546));
locations.add(new ComparableLocation(-33.460203, -70.657764));
return locations;
}
Aggregations