use of cl.smartcities.isci.transportinspector.map.model.ComparableLocation in project androidApp by InspectorIncognito.
the class GridHelper method getRouteSegmentInGrid.
private List<RouteSegment> getRouteSegmentInGrid(LinkedHashSet<Integer> indexes, String serviceWithDirection) {
ArrayList<RouteSegment> routeSegments = new ArrayList<>(indexes.size());
for (Integer index : indexes) {
String segments = index + "," + (index + 1);
List<ComparableLocation> segmentPoints = getRouteSegments(serviceWithDirection, segments);
routeSegments.add(new RouteSegment(index, segmentPoints.get(0), segmentPoints.get(1)));
}
return routeSegments;
}
use of cl.smartcities.isci.transportinspector.map.model.ComparableLocation in project androidApp by InspectorIncognito.
the class GridHelper method getComparableRouteSegments.
private List<ComparableLocation> getComparableRouteSegments(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 GridHelper method getRoutePoints.
/**
* 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 Pair<List<ComparableLocation>, List<ComparableLocation>> getRoutePoints(String serviceName, List<Location> points) {
LinkedHashSet<ComparableLocation> rPoints = new LinkedHashSet<>();
LinkedHashSet<ComparableLocation> iPoints = new LinkedHashSet<>();
ArrayList<Pair<Integer, Integer>> pairArrayList = new ArrayList<>();
getRouteSegmentInGrid(serviceName, points.get(0));
for (Location point : points) {
Pair<Integer, Integer> gridPosition = getCellId(point.getLatitude(), point.getLongitude());
if (!pairArrayList.contains(gridPosition)) {
pairArrayList.add(gridPosition);
Pair<List<ComparableLocation>, List<ComparableLocation>> routesPoints = getRoutesForGridPosition(gridPosition, serviceName);
iPoints.addAll(routesPoints.first);
rPoints.addAll(routesPoints.second);
}
}
List<ComparableLocation> iList = new ArrayList<>(iPoints);
List<ComparableLocation> rList = new ArrayList<>(rPoints);
return Pair.create(iList, rList);
}
use of cl.smartcities.isci.transportinspector.map.model.ComparableLocation in project androidApp by InspectorIncognito.
the class GridHelper method getRoutesForGridPosition.
private Pair<List<ComparableLocation>, List<ComparableLocation>> getRoutesForGridPosition(Pair gridPosition, String serviceName) {
final String I_DIRECTION = "I";
final String R_DIRECTION = "R";
final int AREA = 1;
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);
}
}
String textSegments = this.getRouteSegments(gridPosition, AREA);
String[] segments = textSegments.split("/");
// Log.d(TAG, "iSegments: " + iSegments);
// Log.d(TAG, "rSegments: " + rSegments);
List<ComparableLocation> iRouteSegments = getRouteSegments(serviceDirectionIIndex, segments, serviceName, I_DIRECTION);
List<ComparableLocation> rRouteSegments = getRouteSegments(serviceDirectionRIndex, segments, serviceName, R_DIRECTION);
return Pair.create(iRouteSegments, rRouteSegments);
}
use of cl.smartcities.isci.transportinspector.map.model.ComparableLocation in project androidApp by InspectorIncognito.
the class GridHelper method getDistanceFromPointToRoute.
public double getDistanceFromPointToRoute(String route, Location point) {
Pair<Integer, Integer> gridPosition = getCellId(point.getLatitude(), point.getLongitude());
String textServices = this.getServicesInGrid(gridPosition, 1);
String[] services = textServices.split("/");
List<Integer> serviceDirectionIndex = new ArrayList<>();
for (int i = 0; i < services.length; i++) {
if (services[i].equals(route)) {
serviceDirectionIndex.add(i);
}
}
if (serviceDirectionIndex.isEmpty()) {
return Double.MAX_VALUE;
}
// service is in the grid
String textSegments = this.getRouteSegments(gridPosition, 1);
String[] segments = textSegments.split("/");
StringBuilder routeSegments = new StringBuilder();
for (Integer i : serviceDirectionIndex) {
routeSegments.append(segments[i].replace("-", ",")).append(",");
}
routeSegments = new StringBuilder((routeSegments.toString().equals("") ? "" : routeSegments.substring(0, routeSegments.length() - 1)));
List<ComparableLocation> routePoints = this.getRouteSegments(route, routeSegments.toString());
if (routePoints.size() == 1) {
return getDistanceToPoint(routePoints.get(0), point);
} else {
double minD = Double.MAX_VALUE;
for (int i = 0; i < routePoints.size() - 1; i++) {
double distance = pointToSegmentDistance(routePoints.get(i), routePoints.get(i + 1), point);
minD = minD < distance ? minD : distance;
}
return minD;
}
}
Aggregations