Search in sources :

Example 1 with ConflictedLocationIdentifier

use of de.opendiabetes.vault.plugin.importer.googlecrawler.models.ConflictedLocationIdentifier 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)

Aggregations

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 Coordinate (de.opendiabetes.vault.plugin.importer.googlecrawler.models.Coordinate)1 Location (de.opendiabetes.vault.plugin.importer.googlecrawler.models.Location)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1