Example #1
0
        public static async Task <BusStop[]> GetStopsForArea(BasicGeoposition topLeft, BasicGeoposition bottomRight, CancellationToken cancellationToken)
        {
            List <int>     hashes       = new List <int>();
            GeoboundingBox box          = new GeoboundingBox(topLeft, bottomRight);
            Rect           intersection = box.ToRect();

            intersection.Intersect(DowntownSeattleZone.ToRect());
            if (intersection.Width > 0 && intersection.Height > 0)
            {
                hashes.Add(16);
            }
            if (intersection != box.ToRect())
            {
                double minLat = bottomRight.Latitude;
                double minLon = topLeft.Longitude;
                double maxLat = topLeft.Latitude;
                double maxLon = bottomRight.Longitude;
                for (double lat = minLat; lat < maxLat + 0.1; lat += 0.1)
                {
                    if (lat > maxLat)
                    {
                        lat = maxLat;
                    }
                    for (double lon = minLon; lon < maxLon + 0.2; lon += 0.2)
                    {
                        if (lon > maxLon)
                        {
                            lon = maxLon;
                        }
                        int hash = HashLocation(lat, lon);
                        if (!hashes.Contains(hash))
                        {
                            hashes.Add(hash);
                        }
                    }
                }
            }
            cancellationToken.ThrowIfCancellationRequested();

            List <BusStop> result = new List <BusStop>();

            foreach (var hash in hashes)
            {
                await AccessStopCache(hash, delegate(List <BusStop> stops)
                {
                    foreach (var stop in stops)
                    {
                        if (box.ContainsPoint(stop.Position) && !result.Contains(stop))
                        {
                            result.Add(stop);
                        }
                    }
                    return(false);
                });

                cancellationToken.ThrowIfCancellationRequested();
            }

            return(result.ToArray());
        }
Example #2
0
        public static int HashLocation(double latitude, double longitude)
        {
            if (DowntownSeattleZone.ContainsPoint(new BasicGeoposition()
            {
                Latitude = latitude, Longitude = longitude
            }))
            {
                return(16);
            }
            int latHash = (int)(Abs(latitude) / 0.1) % 4;
            int lonHash = (int)(Abs(longitude) / 0.2) % 4;

            return(latHash + 4 * lonHash);
        }
Example #3
0
        public static async Task<BusStop[]> GetStopsForArea(BasicGeoposition topLeft, BasicGeoposition bottomRight, CancellationToken cancellationToken)
        {
            List<int> hashes = new List<int>();
            GeoboundingBox box = new GeoboundingBox(topLeft, bottomRight);
            Rect intersection = box.ToRect();
            intersection.Intersect(DowntownSeattleZone.ToRect());
            if (intersection.Width > 0 && intersection.Height > 0)
                hashes.Add(16);
            if (intersection != box.ToRect())
            {
                double minLat = bottomRight.Latitude;
                double minLon = topLeft.Longitude;
                double maxLat = topLeft.Latitude;
                double maxLon = bottomRight.Longitude;
                for (double lat = minLat; lat < maxLat + 0.1; lat += 0.1)
                {
                    if (lat > maxLat)
                        lat = maxLat;
                    for (double lon = minLon; lon < maxLon + 0.2; lon += 0.2)
                    {
                        if (lon > maxLon)
                            lon = maxLon;
                        int hash = HashLocation(lat, lon);
                        if (!hashes.Contains(hash))
                            hashes.Add(hash);
                    }
                }
            }
            cancellationToken.ThrowIfCancellationRequested();

            List<BusStop> result = new List<BusStop>();

            foreach (var hash in hashes)
            {
                await AccessStopCache(hash, delegate (List<BusStop> stops)
                {
                    foreach (var stop in stops)
                    {
                        if (box.ContainsPoint(stop.Position) && !result.Contains(stop))
                            result.Add(stop);
                    }
                    return false;
                });
                cancellationToken.ThrowIfCancellationRequested();
            }

            return result.ToArray();
        }