Search in sources :

Example 11 with BusStop

use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.

the class BusStopPlugin method setupData.

private void setupData() {
    if (mapboxMap == null) {
        return;
    }
    GridHelper helper = new GridHelper(getContext());
    List<BusStop> busStops = helper.getNearBusStops(mapboxMap.getCameraPosition().target.getLatitude(), mapboxMap.getCameraPosition().target.getLongitude());
    helper.close();
    onBusEngine.setBusStops(busStops);
    List<Feature> markerCoordinates = new ArrayList<>();
    for (BusStop busStop : busStops) {
        markerCoordinates.add(new BusStopMarker(busStop));
    }
    busStopFeatures = FeatureCollection.fromFeatures(markerCoordinates);
    busesFeatures = FeatureCollection.fromFeatures(new Feature[] {});
    busRouteStartFeature = FeatureCollection.fromFeatures(new Feature[] {});
    busRouteEndFeature = FeatureCollection.fromFeatures(new Feature[] {});
    setupBusRouteSource();
    setupBusRouteLayer();
    setupBusStopSource();
    setupBusStopLayer();
    setupBusSource();
    setupBusLayer();
}
Also used : GridHelper(cl.smartcities.isci.transportinspector.database.GridHelper) ArrayList(java.util.ArrayList) BusStopMarker(cl.smartcities.isci.transportinspector.map.model.busStop.BusStopMarker) BusStop(cl.smartcities.isci.transportinspector.backend.BusStop) Feature(com.mapbox.services.commons.geojson.Feature)

Example 12 with BusStop

use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.

the class BusStopPlugin method busStopFeatureClick.

private void busStopFeatureClick(Feature busStopFeature, BusStopClickHandler handler) {
    if (selectedBus != null) {
        Log.d(BusStopPlugin.class.getSimpleName(), "selectedBus != null");
        deselectSelectedBusFeature();
    }
    final Feature newSelectedFeature = handler.handleMarkerClick(busStopFeature, busStopFeatures);
    if (newSelectedFeature != null) {
        Log.d(BusStopPlugin.class.getSimpleName(), "newSelectedFeature != null");
        if (busStopServiceCall != null && !busStopServiceCall.isCanceled()) {
            busStopServiceCall.cancel();
        }
        busesFeatures = FeatureCollection.fromFeatures(new Feature[] {});
        if (periodicRequestHandler != null) {
            periodicRequestHandler.stopPeriodicRequest();
            periodicRequestHandler = null;
        }
        refreshBusSource();
        Log.d(BusStopPlugin.class.getSimpleName(), "selectedBusStop assign");
        if (selectedBusStop != null) {
            deselectSelectedBusStop();
        }
        selectedBusStop = newSelectedFeature;
        final BusStop busStop = ((BusStopMarker) newSelectedFeature).getBusStop();
        final BusStopLoadingLayerTask loaderTask = new BusStopLoadingLayerTask(newSelectedFeature, new BusStopLoadingLayerTask.LoadingListener() {

            @Override
            public void refresh() {
                refreshBusStopSource();
            }
        });
        final Thread thread = new Thread(loaderTask);
        thread.start();
        busStopServiceCall = busStopInfoService.getBusStopData(Installation.getPhoneId(), newSelectedFeature.getStringProperty(BusStopMarker.PROPERTY_ID));
        /*Log the URL called*/
        Log.d(BusStopPlugin.class.getSimpleName(), busStopServiceCall.request().url().toString());
        busStopServiceCall.enqueue(new Callback<ServerSentBusStopInfo>() {

            @Override
            public void onResponse(Call<ServerSentBusStopInfo> call, Response<ServerSentBusStopInfo> response) {
                Log.d(BusStopPlugin.class.getSimpleName(), "onResponse");
                if (response == null || response.body() == null) {
                    if (response != null)
                        Log.e("BusStopPlugin", response.toString());
                    return;
                }
                periodicRequestHandler = new PeriodicRequestHandler(getContext(), BusStopPlugin.this, busStop, response.body().getEventsList());
                BusStopPlugin.this.onResponse(response.body().getBusList(), response.body().getEventsList(), loaderTask, thread);
            }

            @Override
            public void onFailure(Call<ServerSentBusStopInfo> call, Throwable t) {
                Log.d(BusStopPlugin.class.getSimpleName(), "onFailure");
                if (!call.isCanceled()) {
                    Toast.makeText(getContext(), "Error de conexión", Toast.LENGTH_SHORT).show();
                }
                loaderTask.stop();
                thread.interrupt();
                deselectSelectedBusStop(newSelectedFeature);
            }
        });
        listener.onBusStopFocusGained();
        animateCameraToSelection(newSelectedFeature);
    } else {
        refreshBusSource();
        centerCameraInSelection(busStopFeature);
    }
    refreshBusStopSource();
}
Also used : PeriodicRequestHandler(cl.smartcities.isci.transportinspector.periodicRequest.PeriodicRequestHandler) BusStopMarker(cl.smartcities.isci.transportinspector.map.model.busStop.BusStopMarker) BusStopLoadingLayerTask(cl.smartcities.isci.transportinspector.map.tasks.BusStopLoadingLayerTask) Feature(com.mapbox.services.commons.geojson.Feature) BusStop(cl.smartcities.isci.transportinspector.backend.BusStop) ServerSentBusStopInfo(cl.smartcities.isci.transportinspector.model.gson.ServerSentBusStopInfo)

Example 13 with BusStop

use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.

the class BusStopHelper method getNearBusStops.

/**
 * Open the database, does a query, close the database and return a list of BusStop that
 * contains all the near bus stops in a square
 *
 * @param pLat    The latitude from the point where we want to get near bus stops
 * @param pLon    The longitude from the point where we want to get near bus stops
 * @param pRadius The radius of the square (from center to side)
 * @return A list of BusStops containing the BusStops in the square
 */
private List<BusStop> getNearBusStops(double pLat, double pLon, double pRadius) {
    String query = "SELECT * FROM " + DataBaseContract.Stop.TABLE_NAME + " WHERE " + DataBaseContract.Stop.LATITUDE + " > ? AND " + DataBaseContract.Stop.LATITUDE + " < ? AND " + DataBaseContract.Stop.LONGITUDE + " > ? AND " + DataBaseContract.Stop.LONGITUDE + " < ?";
    double latMin = this.destinationPoint(pLat, pLon, pRadius, 0)[0], latMax = this.destinationPoint(pLat, pLon, pRadius, 180)[0], lonMin = this.destinationPoint(pLat, pLon, pRadius, 90)[1], lonMax = this.destinationPoint(pLat, pLon, pRadius, -90)[1];
    String[] params = new String[] { Double.toString(latMin), Double.toString(latMax), Double.toString(lonMin), Double.toString(lonMax) };
    Cursor cursor = this.getReadableDatabase().rawQuery(query, params);
    List<BusStop> busStopList = this.cursorToBusStopList(cursor);
    this.close();
    return busStopList;
}
Also used : Cursor(android.database.Cursor) BusStop(cl.smartcities.isci.transportinspector.backend.BusStop)

Example 14 with BusStop

use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.

the class BusStopHelper method getNearestBusStops.

/**
 * Obtains the top N nearest BusStop in 1km radius, sorted by the distance from the given
 * starting point. If there are no BusStops in the radius, returns an empty list.
 *
 * @param pLat Latitude from the starting point
 * @param pLon Longitude from the staring point
 * @param pN   Indicates the amount of desired BusStops
 * @return A list of BusStops containing the top N nearest BusStops
 */
public List<BusStop> getNearestBusStops(double pLat, double pLon, int pN) {
    // Query to obtain the BusStops inside a 1km radius
    // 1km
    double radius = 1000;
    List<BusStop> nearBusStops = this.getNearBusStops(pLat, pLon, radius);
    // From the obtained BusStops, create a TreeMap
    TreeMap<Float, BusStop> treeMap = new TreeMap<>();
    for (BusStop busStop : nearBusStops) {
        // float[] distance = new float[3];
        // Location.distanceBetween(pLat, pLon, busStop.getLat(), busStop.getLon(), distance);
        // treeMap.put(distance[0],busStop);
        float distance = busStop.getDistance(pLat, pLon);
        treeMap.put(distance, busStop);
    }
    // From the TreeMap we get the pN closest BusStops
    List<BusStop> nearestBusStops = new ArrayList<>();
    int count = 0;
    for (Float key : treeMap.keySet()) {
        if (count < pN) {
            nearestBusStops.add(treeMap.get(key));
            count++;
        } else
            break;
    }
    return nearestBusStops;
}
Also used : ArrayList(java.util.ArrayList) BusStop(cl.smartcities.isci.transportinspector.backend.BusStop) TreeMap(java.util.TreeMap)

Example 15 with BusStop

use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.

the class BusStopHelper method reorderBusStops.

private List<BusStop> reorderBusStops(String[] originalOrder, List<BusStop> busStops) {
    List<BusStop> result = new ArrayList<>();
    for (String code : originalOrder) {
        int index = busStops.indexOf(new BusStop(code));
        if (index == -1) {
            Log.e("ERROR", code);
        } else {
            result.add(busStops.get(index));
            busStops.remove(index);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BusStop(cl.smartcities.isci.transportinspector.backend.BusStop)

Aggregations

BusStop (cl.smartcities.isci.transportinspector.backend.BusStop)27 ArrayList (java.util.ArrayList)13 Feature (com.mapbox.services.commons.geojson.Feature)8 BusStopMarker (cl.smartcities.isci.transportinspector.map.model.busStop.BusStopMarker)6 BusStopHelper (cl.smartcities.isci.transportinspector.database.BusStopHelper)5 Cursor (android.database.Cursor)4 View (android.view.View)3 Bus (cl.smartcities.isci.transportinspector.backend.Bus)3 Test (org.junit.Test)3 Bitmap (android.graphics.Bitmap)2 Nullable (android.support.annotation.Nullable)2 TextView (android.widget.TextView)2 FavoriteHelper (cl.smartcities.isci.transportinspector.database.FavoriteHelper)2 GridHelper (cl.smartcities.isci.transportinspector.database.GridHelper)2 DetectedActivity (com.google.android.gms.location.DetectedActivity)2 Context (android.content.Context)1 Intent (android.content.Intent)1 Typeface (android.graphics.Typeface)1 Location (android.location.Location)1 Pair (android.support.v4.util.Pair)1