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 double CalculateDistanceBetweenReadings(Reading prev, Reading current)
        {
            if (GeographyUtils.ToRadians(current.Latitude) == 0 ||
                GeographyUtils.ToRadians(prev.Latitude) == 0)
            {
                return(0); // if either of the latitudes are 0 then return 0. 0 is an incorrect reading
            }

            return(GeographyUtils.CalculateDistanceBetweenLocations(prev.Latitude, prev.Longitude,
                                                                    current.Latitude, current.Longitude));
        }
        protected void AddPossiblePlaceToTrip(Trip trip, Reading reading, TripPossiblePlaceType type)
        {
            var ownerId        = trip.Car.OwnerId;
            var possiblePlaces = _placeService.GetPlacesNearby(reading.Latitude,
                                                               reading.Longitude, 150, ownerId);

            foreach (var place in possiblePlaces)
            {
                var possiblePlace = new TripPossiblePlace()
                {
                    Trip      = trip,
                    TripId    = trip.TripId,
                    Place     = place,
                    PlaceType = type,
                    Distance  = Convert.ToDecimal(GeographyUtils.CalculateDistanceBetweenLocations(
                                                      reading.Latitude, reading.Longitude, place.Latitude, place.Longitude)),
                    Active = true
                };

                _tripPossiblePlaceService.Create(possiblePlace);
            }

            _logger.Debug($"Added possible {type.ToString()} to trip {trip.TripId}");
        }