protected Place GuessPlaceForTrip(Trip trip, Reading reading, TripPossiblePlaceType type) { var possiblePlaces = _db.TripPossiblePlaces.Active() .Where(x => x.PlaceType == type && x.TripId == trip.TripId); var ownerId = trip.Car.OwnerId; // Bounding box around reading location with a 500m side var boundingBox = GeographyUtils.CalculateBoxAroundPoint(reading.Latitude, reading.Longitude, 250); var guessedPlaceVisit = _db.PlaceVisits.Include(pv => pv.Place) .Where(x => x.OwnerId == ownerId && x.PlaceType == type && x.UserSelected && x.Place.Latitude >= boundingBox.MinPoint.Latitude && x.Place.Latitude <= boundingBox.MaxPoint.Latitude && x.Place.Longitude >= boundingBox.MinPoint.Longitude && x.Place.Longitude <= boundingBox.MaxPoint.Longitude) .Join(possiblePlaces, pv => pv.PlaceId, pp => pp.PlaceId, (pv, pp) => pv) .ToList() .Select(pv => new { Distance = GeographyUtils.CalculateDistanceBetweenLocations(new GeographyUtils.LocationModel() { Latitude = reading.Latitude, Longitude = reading.Longitude }, new GeographyUtils.LocationModel() { Latitude = pv.Latitude, Longitude = pv.Longitude }), pv.PlaceId, pv.Latitude, pv.Longitude }) .GroupBy(x => x.PlaceId, (key, v) => new { PlaceId = key, Count = v.Count(), AverageDistance = v.Sum(pv => pv.Distance) / v.Count() }) .OrderBy(x => (1 - x.AverageDistance) * x.Count) .FirstOrDefault(); // we have the place visit and the distance of that place visit to the current reading // get the one that is a factor of the closests and most selected // distance is in KM? if it is.. it will never be greater than 1 (500 technically?) // (1 - distance) * count if (null != guessedPlaceVisit) { return(_db.Places.First(x => x.PlaceId == guessedPlaceVisit.PlaceId)); } return(null); }
protected IEnumerable <Place> GetUserPlaces(double latitude, double longitude, int range, long userId) { var boundingBox = GeographyUtils.CalculateBoxAroundPoint(latitude, longitude, range); return(_db.UserPlaces.Build().Active().Where(p => p.OwnerId == userId && p.Place.Latitude >= boundingBox.MinPoint.Latitude && p.Place.Latitude <= boundingBox.MaxPoint.Latitude && p.Place.Longitude >= boundingBox.MinPoint.Longitude && p.Place.Longitude <= boundingBox.MaxPoint.Longitude) .Select(up => up.Place)); }