Search in sources :

Example 1 with ComparableLocation

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;
}
Also used : ArrayList(java.util.ArrayList) ComparableLocation(cl.smartcities.isci.transportinspector.map.model.ComparableLocation) RouteSegment(cl.smartcities.isci.transportinspector.map.model.RouteSegment)

Example 2 with ComparableLocation

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;
}
Also used : ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) ComparableLocation(cl.smartcities.isci.transportinspector.map.model.ComparableLocation)

Example 3 with ComparableLocation

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ComparableLocation(cl.smartcities.isci.transportinspector.map.model.ComparableLocation) Pair(android.support.v4.util.Pair) ComparableLocation(cl.smartcities.isci.transportinspector.map.model.ComparableLocation) Location(android.location.Location)

Example 4 with ComparableLocation

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);
}
Also used : ArrayList(java.util.ArrayList) ComparableLocation(cl.smartcities.isci.transportinspector.map.model.ComparableLocation) LoadFarePoint(cl.smartcities.isci.transportinspector.backend.LoadFarePoint)

Example 5 with ComparableLocation

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;
    }
}
Also used : ArrayList(java.util.ArrayList) ComparableLocation(cl.smartcities.isci.transportinspector.map.model.ComparableLocation) LoadFarePoint(cl.smartcities.isci.transportinspector.backend.LoadFarePoint)

Aggregations

ComparableLocation (cl.smartcities.isci.transportinspector.map.model.ComparableLocation)9 ArrayList (java.util.ArrayList)9 Location (android.location.Location)4 Cursor (android.database.Cursor)2 LoadFarePoint (cl.smartcities.isci.transportinspector.backend.LoadFarePoint)2 Pair (android.support.v4.util.Pair)1 BusStop (cl.smartcities.isci.transportinspector.backend.BusStop)1 RouteSegment (cl.smartcities.isci.transportinspector.map.model.RouteSegment)1 BusStep (cl.smartcities.isci.transportinspector.router.googleStep.BusStep)1 GoogleStep (cl.smartcities.isci.transportinspector.router.googleStep.GoogleStep)1 NullStep (cl.smartcities.isci.transportinspector.router.googleStep.NullStep)1 SubwayStep (cl.smartcities.isci.transportinspector.router.googleStep.SubwayStep)1 WalkingStep (cl.smartcities.isci.transportinspector.router.googleStep.WalkingStep)1 Route (cl.smartcities.isci.transportinspector.router.route.Route)1 IStep (cl.smartcities.isci.transportinspector.router.transappStep.IStep)1 StepBuilder (cl.smartcities.isci.transportinspector.router.transappStep.StepBuilder)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1