Search in sources :

Example 1 with Coordinate

use of de.opendiabetes.vault.plugin.importer.googlecrawler.models.Coordinate in project BachelorPraktikum by lucasbuschlinger.

the class LocationHistory method refineLocations.

/**
 * Populates the activity history by fetching the location names of coordinates
 * from the Google Places API.
 */
public void refineLocations() {
    for (Map.Entry<Long, List<Activity>> entry : activityHistory.entrySet()) {
        List<Coordinate> coords = getLocationsPerDay(entry.getKey());
        for (Activity act : entry.getValue()) {
            if (act.getActivityId() == ActivityTypes.STILL || act.getActivityId() == ActivityTypes.UNKNOWN || act.getActivityId() == ActivityTypes.SLEEPING || act.getActivityId() == ActivityTypes.LIGHT_SLEEP || act.getActivityId() == ActivityTypes.DEEP_SLEEP || act.getActivityId() == ActivityTypes.REM_SLEEP || act.getActivityId() == ActivityTypes.MEDITATION) {
                List<Coordinate> activityCoords = new ArrayList<>();
                long startTime = act.getStartTime();
                long endTime = act.getEndTime();
                for (Coordinate c : coords) {
                    if (c.getTimestamp() >= startTime && c.getTimestamp() <= endTime) {
                        activityCoords.add(c);
                    }
                }
                if (activityCoords.size() > 0) {
                    activityCoords.sort(Comparator.comparing(Coordinate::getAccuracy));
                    int threshold = activityCoords.get((int) (activityCoords.size() * 0.75)).getAccuracy();
                    activityCoords = activityCoords.stream().filter(c -> c.getAccuracy() <= threshold).collect(Collectors.toList());
                    double weightedLatitude = 0;
                    double weightedLongitude = 0;
                    double weight = 0;
                    for (Coordinate c : activityCoords) {
                        weightedLatitude += c.getLatitude() * c.getAccuracy();
                        weightedLongitude += c.getLongitude() * c.getAccuracy();
                        weight += c.getAccuracy();
                    }
                    final double lat = weightedLatitude / weight;
                    final double lng = weightedLongitude / weight;
                    int searchRadius = DEFAULT_RADIUS;
                    if (searchRadius < threshold) {
                        searchRadius = threshold;
                    }
                    String place = GooglePlaces.getInstance().atOwnPlaces(lat, lng, searchRadius);
                    if (place.equals("AWAY")) {
                        place = GooglePlaces.getInstance().isAtContact(lat, lng, searchRadius);
                        if (place.equals("AWAY")) {
                            PlacesSearchResult[] results = GooglePlaces.getInstance().getPlaces(lat, lng, searchRadius);
                            if (results != null) {
                                if (results.length == 1) {
                                    place = results[0].name;
                                } else {
                                    List<PlacesSearchResult> places = extractRealLocations(results);
                                    places.sort((PlacesSearchResult o1, PlacesSearchResult o2) -> {
                                        double o1Distance = GooglePlaces.getInstance().calculateDistance(o1.geometry.location.lat, o1.geometry.location.lng, lat, lng);
                                        double o2Distance = GooglePlaces.getInstance().calculateDistance(o2.geometry.location.lat, o2.geometry.location.lng, lat, lng);
                                        return Double.compare(o1Distance, o2Distance);
                                    });
                                    List<PlacesSearchResult> sportRelatedPlaces = getGymOrSportClub(places);
                                    GoogleMapsPlot.getInstance().addLocation(new LatLng(lat, lng));
                                    if (sportRelatedPlaces.size() == 1) {
                                        place = sportRelatedPlaces.get(0).name;
                                    } else {
                                        Location location = checkForResolvedLocations(lat, lng, searchRadius * 2);
                                        if (location != null) {
                                            place = location.getName();
                                        } else if (sportRelatedPlaces.size() > 1) {
                                            conflictLocations.put(new ConflictedLocationIdentifier(act.getStartTime(), new LatLng(lat, lng)), sportRelatedPlaces);
                                            place = "CONFLICT";
                                        } else if (places.size() > 0) {
                                            results = GooglePlaces.getInstance().getPlaces(lat, lng, searchRadius * 2);
                                            places = extractRealLocations(results);
                                            places.sort((PlacesSearchResult o1, PlacesSearchResult o2) -> {
                                                double o1Distance = GooglePlaces.getInstance().calculateDistance(o1.geometry.location.lat, o1.geometry.location.lng, lat, lng);
                                                double o2Distance = GooglePlaces.getInstance().calculateDistance(o2.geometry.location.lat, o2.geometry.location.lng, lat, lng);
                                                return Double.compare(o1Distance, o2Distance);
                                            });
                                            conflictLocations.put(new ConflictedLocationIdentifier(act.getStartTime(), new LatLng(lat, lng)), places);
                                            place = "CONFLICT";
                                        } else {
                                            place = "UNKNOWN";
                                        }
                                    }
                                }
                            } else {
                                place = "UNKNOWN";
                            }
                        }
                    }
                    act.setLocation(place);
                }
                GoogleMapsPlot.getInstance().addLocationNames(act.getLocation());
            }
        }
    }
}
Also used : ConflictedLocationIdentifier(de.opendiabetes.vault.plugin.importer.googlecrawler.models.ConflictedLocationIdentifier) ArrayList(java.util.ArrayList) Activity(de.opendiabetes.vault.plugin.importer.googlecrawler.models.Activity) Coordinate(de.opendiabetes.vault.plugin.importer.googlecrawler.models.Coordinate) ArrayList(java.util.ArrayList) List(java.util.List) PlacesSearchResult(com.google.maps.model.PlacesSearchResult) LatLng(com.google.maps.model.LatLng) HashMap(java.util.HashMap) Map(java.util.Map) Location(de.opendiabetes.vault.plugin.importer.googlecrawler.models.Location)

Example 2 with Coordinate

use of de.opendiabetes.vault.plugin.importer.googlecrawler.models.Coordinate in project BachelorPraktikum by lucasbuschlinger.

the class GoogleFitness method fetchLocationsPerDay.

/**
 * Fetches all locations at a specific day.
 * @param day - a date as unix timestamp
 */
private void fetchLocationsPerDay(final long day) {
    long[] startEnd = getStartEndDay(day);
    AggregateBy aggregate = new AggregateBy();
    aggregate.setDataTypeName("com.google.location.sample");
    AggregateRequest agg = new AggregateRequest();
    agg.setStartTimeMillis(startEnd[0]);
    agg.setEndTimeMillis(startEnd[1]);
    agg.setAggregateBy(Arrays.asList(aggregate));
    try {
        Fitness.Users.Dataset.Aggregate request = fitnessService.users().dataset().aggregate("me", agg);
        AggregateResponse rep = request.execute();
        List<DataPoint> activitiesByDataSource = rep.getBucket().get(0).getDataset().get(0).getPoint();
        List<Coordinate> locations = new ArrayList<>();
        for (DataPoint dp : activitiesByDataSource) {
            Coordinate coord = new Coordinate(dp.getStartTimeNanos() / NANO_TO_MILLISECONDS_DIVISOR, dp.getValue().get(1).getFpVal(), dp.getValue().get(0).getFpVal(), (int) Math.ceil(dp.getValue().get(2).getFpVal()));
            if (dp.getValue().size() > 3) {
                coord.setAltitude(dp.getValue().get(3).getFpVal().intValue());
            }
            locations.add(coord);
        }
        LocationHistory.getInstance().addLocations(startEnd[0], locations);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : AggregateBy(com.google.api.services.fitness.model.AggregateBy) AggregateRequest(com.google.api.services.fitness.model.AggregateRequest) AggregateResponse(com.google.api.services.fitness.model.AggregateResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataPoint(com.google.api.services.fitness.model.DataPoint) Coordinate(de.opendiabetes.vault.plugin.importer.googlecrawler.models.Coordinate) Fitness(com.google.api.services.fitness.Fitness)

Aggregations

Coordinate (de.opendiabetes.vault.plugin.importer.googlecrawler.models.Coordinate)2 ArrayList (java.util.ArrayList)2 Fitness (com.google.api.services.fitness.Fitness)1 AggregateBy (com.google.api.services.fitness.model.AggregateBy)1 AggregateRequest (com.google.api.services.fitness.model.AggregateRequest)1 AggregateResponse (com.google.api.services.fitness.model.AggregateResponse)1 DataPoint (com.google.api.services.fitness.model.DataPoint)1 LatLng (com.google.maps.model.LatLng)1 PlacesSearchResult (com.google.maps.model.PlacesSearchResult)1 Activity (de.opendiabetes.vault.plugin.importer.googlecrawler.models.Activity)1 ConflictedLocationIdentifier (de.opendiabetes.vault.plugin.importer.googlecrawler.models.ConflictedLocationIdentifier)1 Location (de.opendiabetes.vault.plugin.importer.googlecrawler.models.Location)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1