public IEnumerable <ScooterLocation> GetBy(
            int scootersNumber,
            int searchingRadiusKilometer,
            float landmarkLongitude,
            float landmarkLatitude)
        {
            int earthRadiusKilometer = 6371;
            var radiansLongitude     = GeoLocationHelper.ConvertDegreesToRadians(landmarkLongitude);
            var radiansLatitude      = GeoLocationHelper.ConvertDegreesToRadians(landmarkLatitude);

            // The great circle distance
            // d = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
            return(_context.ScooterLocations
                   .Select(location => new
            {
                Location = location,
                Distance = earthRadiusKilometer *
                           Math.Acos(
                    Math.Sin(radiansLatitude) * Math.Sin(location.RadiansLatitude)
                    +
                    (
                        Math.Cos(radiansLatitude) * Math.Cos(location.RadiansLatitude)
                        * Math.Cos(radiansLongitude - location.RadiansLongitude)
                    )
                    ),
            })
                   .Where(location => location.Distance < searchingRadiusKilometer)
                   .OrderBy(location => location.Distance)
                   .Take(scootersNumber)
                   .Select(s => s.Location));
        }
Ejemplo n.º 2
0
        public void Seed(IServiceProvider serviceProvider)
        {
            var polygonService = serviceProvider.GetService <PolygonService>();
            var cityMapService = serviceProvider.GetService <ICityMapService>();
            var context        = serviceProvider.GetService <MobilityDbContext>();

            var cityPolygons = cityMapService.GetPolygonPoints();
            var points       = polygonService.GetRandomPointsInsidePolygon(
                cityPolygons,
                _scooterNumbersToGenerate);

            foreach (Point point in points)
            {
                context.ScooterLocations.Add(new ScooterLocation
                {
                    Longitude        = point.X,
                    Latitude         = point.Y,
                    RadiansLongitude = (float)GeoLocationHelper.ConvertDegreesToRadians(point.X),
                    RadiansLatitude  = (float)GeoLocationHelper.ConvertDegreesToRadians(point.Y)
                });
            }

            context.SaveChanges();
        }