Ejemplo n.º 1
0
        protected void RecordPlaceVisit(Trip trip, long placeId, TripPossiblePlaceType type, bool userSelected = true)
        {
            Reading reading;

            if (type == TripPossiblePlaceType.Start)
            {
                reading = GetFirstReading(trip);
            }
            else
            {
                reading = GetLastReading(trip);
            }
            if (null == reading)
            {
                return; // No reading found
            }

            var placeVisit = new PlaceVisit()
            {
                Latitude     = reading.Latitude,
                Longitude    = reading.Longitude,
                PlaceType    = type,
                VisitDate    = type == TripPossiblePlaceType.Destination ? trip.EndDate : trip.StartDate,
                UserSelected = userSelected,
                PlaceId      = placeId,
                OwnerId      = trip.Car.OwnerId
            };

            _db.PlaceVisits.Add(placeVisit);
            _db.SaveChanges();
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 public PagedViewModel <TripPossiblePlace> GetForTripOfTypePaged(long tripId, TripPossiblePlaceType type, int skip = 0, int take = 10,
                                                                 SortParam sortParam = null)
 {
     if (string.IsNullOrWhiteSpace(sortParam?.ColumnName))
     {
         sortParam = new SortParam()
         {
             ColumnName = "Distance",
             Ascending  = true
         };
     }
     return
         (_db.TripPossiblePlaces
          .Active()
          .Build()
          .Where(x => x.TripId == tripId && x.PlaceType == type)
          .PageAndSort(skip, take, sortParam));
 }
Ejemplo n.º 4
0
        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}");
        }
Ejemplo n.º 5
0
 public IEnumerable <TripPossiblePlace> GetForTripOfType(long tripId, TripPossiblePlaceType type)
 {
     return(_db.TripPossiblePlaces.Active().Build().Where(x => x.TripId == tripId && x.PlaceType == type));
 }
Ejemplo n.º 6
0
 public static string ToString(this TripPossiblePlaceType type)
 {
     return(PlaceTypeNames[type]);
 }