예제 #1
0
        public async Task <AirportNearByDTOList> GetAirportsNearBy(double latitude, double longitude, double radius)
        {
            /*
             * We make a bounding square to search for airports.  The corners of the square may contain
             * airports just outside of the radius.  We will exclude those when we calculate the distance.
             */

            AirportNearByDTOList nearByList = new AirportNearByDTOList();

            var geolocation = GeoLocation.FromDegrees(latitude, longitude);
            var bounding    = geolocation.BoundingCoordinates(radius);

            double minLatitude  = bounding[0].getLatitudeInDegrees();
            double minLongitude = bounding[0].getLongitudeInDegrees();
            double maxLatitude  = bounding[1].getLatitudeInDegrees();
            double maxLongitude = bounding[1].getLongitudeInDegrees();

            var airportList = await DataServices.GetAirportsNearBy(minLatitude, minLongitude, maxLatitude, maxLongitude, AirportDTO.Includes);

            nearByList.TotalRecords = airportList.TotalRecords;
            nearByList.Airports     = new List <AirportNearByDTO>();

            GeoLocation center = GeoLocation.FromDegrees(latitude, longitude);

            foreach (var airport in airportList.Airports)
            {
                double distance = CalculateDistance(airport, center);
                if (distance <= radius)
                {
                    nearByList.Airports.Add(new AirportNearByDTO(airport, distance));
                }
            }

            return(nearByList);
        }
예제 #2
0
        public IList <Airport> GetAirportsNearBy(double latitude, double longitude, double radius)
        {
            var geolocation = GeoLocation.FromDegrees(latitude, longitude);
            var bounding    = geolocation.BoundingCoordinates(radius);

            double minLatitude  = bounding[0].getLatitudeInDegrees();
            double minLongitude = bounding[0].getLongitudeInDegrees();
            double maxLatitude  = bounding[1].getLatitudeInDegrees();
            double maxLongitude = bounding[1].getLongitudeInDegrees();

            List <Airport> locations = new List <Airport>();

            using (StandingDataContainer context = new StandingDataContainer())
            {
                IQueryable <Airport> query = context.Airports
                                             .Where(l => l.Latitude >= minLatitude && l.Latitude <= maxLatitude);

                if (minLongitude <= maxLongitude)
                {
                    query = query
                            .Where(l => l.Longitude >= minLongitude && l.Longitude <= maxLongitude);
                }
                else
                {
                    query = query
                            .Where(l => l.Longitude >= minLongitude || l.Longitude <= maxLongitude);
                }

                locations.AddRange(query.Select(s => s).ToList());
            }

            return(locations);
        }
예제 #3
0
        public double CalculateDistance(Airport airport, GeoLocation center)
        {
            double distance = -1;

            if (airport.Latitude.HasValue && airport.Longitude.HasValue)
            {
                GeoLocation airportLocation = GeoLocation.FromDegrees(airport.Latitude.Value, airport.Longitude.Value);
                distance = center.DistanceTo(airportLocation);
            }
            return(distance);
        }
예제 #4
0
        private static ScoreDoc[] Query(QueryDto queryDto)
        {
            var         searcher    = WebApiApplication.Searcher;
            GeoLocation geoLocation = GeoLocation.FromDegrees(queryDto.Latitude, queryDto.Longitude);

            GeoLocation[] bounds  = geoLocation.BoundingCoordinates(queryDto.Radius);
            double        latFrom = bounds[0].getLatitudeInDegrees();
            double        latTo   = bounds[1].getLatitudeInDegrees();
            double        lngFrom = bounds[0].getLongitudeInDegrees();
            double        lngTo   = bounds[1].getLongitudeInDegrees();
            NumericRangeQuery <double> latQuery = NumericRangeQuery.NewDoubleRange("latitude", latFrom, latTo, true, true);

            latQuery.Boost = 1.5f;
            NumericRangeQuery <double> lngQuery = NumericRangeQuery.NewDoubleRange("longitude", lngFrom, lngTo, true,
                                                                                   true);

            lngQuery.Boost = 1.5f;
            var query = new BooleanQuery
            {
                { latQuery, Occur.MUST },
                { lngQuery, Occur.MUST }
            };

            if (!string.IsNullOrEmpty(queryDto.Query))
            {
                var standardAnalyzer = new StandardAnalyzer(Version.LUCENE_30);
                var innerQuery       = new BooleanQuery
                {
                    {
                        new QueryParser(Version.LUCENE_30, "product_name", standardAnalyzer).Parse(queryDto.Query),
                        Occur.SHOULD
                    },
                    {
                        new QueryParser(Version.LUCENE_30, "product_description", standardAnalyzer).Parse(queryDto.Query),
                        Occur.SHOULD
                    },
                };
                if (!queryDto.Query.Contains("~"))
                {
                    Query q = new QueryParser(Version.LUCENE_30, "product_name", standardAnalyzer).Parse(queryDto.Query + "~");
                    q.Boost = 0.75f;
                    innerQuery.Add(q, Occur.SHOULD);
                    Query q2 = new QueryParser(Version.LUCENE_30, "product_description", standardAnalyzer).Parse(queryDto.Query + "~");
                    q2.Boost = 0.75f;
                    innerQuery.Add(q2, Occur.SHOULD);
                }
                query.Add(innerQuery, Occur.MUST);
            }
            TopDocs topDocs = searcher.Search(query, 100);

            ScoreDoc[] hits = topDocs.ScoreDocs;
            return(hits);
        }
예제 #5
0
        public IEnumerable <WorkshopShortViewModel> GetByLocation(double latitude, double longitute, double distance)
        {
            var geoLocation         = GeoLocation.FromDegrees(latitude, longitute);
            var boundingCoordinates = geoLocation.BoundingCoordinates(distance);
            var radius = distance / EarthRadius;

            using (var scope = _dbContextScope.CreateReadOnly())
            {
                var locations = _workshopRepo.GetClosestLocations(boundingCoordinates,
                                                                  geoLocation.getLatitudeInRadians(),
                                                                  geoLocation.getLongitudeInRadians(),
                                                                  radius);

                return(Mapper.Map <IEnumerable <WorkshopShortViewModel> >(locations));
            }
        }
        public List<Restaurant> GetAllNearByRestaurants(string state, string city)
        {
            var coordinates = (from g in db.GeoDatas
                               where g.State == state && g.City == city
                               select g).FirstOrDefault();
            var lat = (double)coordinates.Latitude;
            var lon = (double)coordinates.Longitude;
            var location = GeoLocation.FromDegrees(lat, lon);
            GeoLocation[] boundingCoordinates = location.BoundingCoordinates(10);
            var distance = location.DistanceTo(boundingCoordinates[1]);
            var minlat = boundingCoordinates[0].getLatitudeInDegrees();
            var maxlat = boundingCoordinates[1].getLatitudeInDegrees();
            var minlon = boundingCoordinates[0].getLongitudeInDegrees();
            var maxlon = boundingCoordinates[1].getLongitudeInDegrees();

            var restaurants = (from a in db.Restaurants
                               where (a.Address.Latitude >= minlat && a.Address.Latitude <= maxlat) && (a.Address.Longitude >= minlon && a.Address.Longitude <= maxlon) &&
        Math.Acos(Math.Sin(lat) * Math.Sin((Double)a.Address.Latitude) + Math.Cos(lat) * Math.Cos((Double)a.Address.Latitude) * Math.Cos((Double)a.Address.Longitude - (lon))) <= distance
                               select a).ToList();

            return restaurants;
        }