예제 #1
0
        public async Task <List <GeoCoordinate> > FillAltitude(List <GeoCoordinate> points, string key = "")
        {
            Logger.Write("Using MapzenAPI to obtian Altitude based on Longitude and Latitude.");
            if (key != "")
            {
                ApiKey = key;
            }

            if (!ApiKey.Equals(""))
            {
                List <GeoCoordinate> pointsToRequest = new List <GeoCoordinate>();
                foreach (var point in points)
                {
                    if (pointsToRequest.Any(x => LocationUtils.CalculateDistanceInMeters(x, point) <= 30))
                    {
                        continue;
                    }
                    if (CheckForExistingAltitude(point.Latitude, point.Longitude))
                    {
                        continue;
                    }

                    pointsToRequest.Add(point);
                }
                var heights = await RequestHeights(pointsToRequest);

                for (int index = 0; index < pointsToRequest.Count; index++)
                {
                    if (index < heights.Count)
                    {
                        pointsToRequest[index].Altitude = heights[index];
                        _knownAltitude.Add(new GeoLatLonAlt()
                        {
                            Lat = pointsToRequest[index].Latitude,
                            Lon = pointsToRequest[index].Longitude,
                            Alt = pointsToRequest[index].Altitude
                        });
                    }
                }
                foreach (var point in points)
                {
                    if (CheckForExistingAltitude(point.Latitude, point.Longitude))
                    {
                        point.Altitude = GetExistingAltitude(point.Latitude, point.Longitude);
                    }
                    else
                    {
                        point.Altitude = _session != null?RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax) : R.Next(10, 120);
                    }
                }
            }
            else
            {
                foreach (var point in points)
                {
                    point.Altitude = _session != null?RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax) : R.Next(10, 120);
                }
            }
            return(points);
        }
예제 #2
0
        public double GetExistingAltitude(double lat, double lon)
        {
            var knownTemp = DbHandler.GetAltitudeForCoords(lat, lon, 30).ToArray();

            if (!knownTemp.Any())
            {
                return(RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin,
                                                    _session.Settings.DefaultAltitudeMax));
            }
            var geoLatLonAlt = knownTemp.FirstOrDefault();

            return(geoLatLonAlt?.Alt ?? RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax));
        }
예제 #3
0
        protected double GetHeight(string[] data)
        {
            if (data[2].Equals("ERROR"))
            {
                Logger.Write("There was an error grabbing Altitude from Mapzen API! Check your Elevation API Key!",
                             LogLevel.Warning);
                return(_session != null?RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax) : R.Next(10, 120));
            }
            Logger.Write("Successfully grabbed new Mapzen Elevation: " + data[2] + " Meters.");
            var latLonAlt = new GeoLatLonAlt()
            {
                Lat = double.Parse(data[0], NumberStyles.Any, CultureInfo.InvariantCulture),
                Lon = double.Parse(data[1], NumberStyles.Any, CultureInfo.InvariantCulture),
                Alt = double.Parse(data[2], NumberStyles.Any, CultureInfo.InvariantCulture) + 0.8 + Math.Round(RandomExtensions.NextInRange(R, 0, 0.2), 7)
            };

            _knownAltitude.Add(latLonAlt);
            return(latLonAlt.Alt);
        }
예제 #4
0
        public async Task <double> GetAltitude(double lat, double lon, string key = "")
        {
            Logger.Write("Using MapzenAPI to obtian Altitude based on Longitude and Latitude.");
            if (key != "")
            {
                ApiKey = key;
            }

            if (CheckForExistingAltitude(lat, lon))
            {
                return(GetExistingAltitude(lat, lon));
            }
            if (!Equals(lat, default(double)) && !Equals(lon, default(double)) && !ApiKey.Equals(""))
            {
                return(GetHeight(new[]
                {
                    lat.ToString(CultureInfo.InvariantCulture),
                    lon.ToString(CultureInfo.InvariantCulture),
                    await Request(Url(lat.ToString(CultureInfo.InvariantCulture), lon.ToString(CultureInfo.InvariantCulture),
                                      key))
                }));
            }
            return(_session != null?RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax) : R.Next(10, 120));
        }
예제 #5
0
 public double GetExistingAltitude(double lat, double lon)
 {
     //trying to find points closer then 5m from search loc
     return(_knownAltitude.FirstOrDefault(x => LocationUtils.CalculateDistanceInMeters(x.Lat, x.Lon, lat, lon) < 30)?.Alt ?? RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax));
 }
예제 #6
0
        protected async Task <List <double> > RequestHeights(List <GeoCoordinate> points)
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            var resList = new List <double>();

            try
            {
                var httpUrl = PrepareUrlForList(points);

                var get     = "";
                var request = (HttpWebRequest)WebRequest.Create(httpUrl);
                request.Proxy = _session == null?WebRequest.GetSystemWebProxy() : _session.Proxy;

                if (_session == null)
                {
                    request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                }

                request.AutomaticDecompression = DecompressionMethods.GZip;
                using (var response = (HttpWebResponse)request.GetResponse())
                    using (var stream = response.GetResponseStream())
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                get = reader.ReadToEnd();
                            }
                        }
                var json = JObject.Parse(get);
                stopWatch.Stop();
                if (stopWatch.ElapsedMilliseconds < 10000)
                {
                    await Task.Delay(10000 - (int)stopWatch.ElapsedMilliseconds);
                }

                foreach (var h in json["height"])
                {
                    if (h.ToString().Equals("ERROR"))
                    {
                        resList.Add(_session != null ? RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax) : R.Next(10, 120));
                    }
                    double hVal = 0;
                    if (!double.TryParse(h.ToString(), out hVal))
                    {
                        hVal = _session != null
                            ? RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax) + 0.8 + Math.Round(RandomExtensions.NextInRange(R, 0, 0.2), 5)
                            : R.Next(10, 120);
                    }
                    resList.Add(hVal);
                }
            }
            catch (Exception ex)
            {
                Logger.Write("ERROR: " + ex.Message, LogLevel.Error);
                return(resList);
            }
            finally
            {
                if (stopWatch.IsRunning)
                {
                    stopWatch.Stop();
                }
            }
            while (points.Count < resList.Count)
            {
                resList.Add(_session != null
                    ? RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax)
                    : R.Next(10, 120));
            }
            return(resList);
        }
예제 #7
0
        public async Task <List <GeoCoordinate> > FillAltitude(List <GeoCoordinate> points, string key = "", CancellationToken token = default(CancellationToken))
        {
            Logger.Write("Using MapzenAPI to obtian Altitude based on Longitude and Latitude.");
            if (key != "")
            {
                ApiKey = key;
            }

            if (!ApiKey.Equals(""))
            {
                var pointsToRequest = new List <GeoCoordinate>();
                foreach (var point in points)
                {
                    if (pointsToRequest.Any(x => LocationUtils.CalculateDistanceInMeters(x, point) <= 30))
                    {
                        continue;
                    }
                    if (CheckForExistingAltitude(point.Latitude, point.Longitude))
                    {
                        continue;
                    }

                    pointsToRequest.Add(point);
                }
                if (pointsToRequest.Any())
                {
                    if (pointsToRequest.Count <= 50)
                    {
                        var heights = await RequestHeights(pointsToRequest);

                        var altToAdd = new List <GeoLatLonAlt>();
                        for (var index = 0; index < pointsToRequest.Count; index++)
                        {
                            if (index >= heights.Count)
                            {
                                continue;
                            }
                            pointsToRequest[index].Altitude = heights[index];
                            altToAdd.Add(new GeoLatLonAlt
                            {
                                Lat = pointsToRequest[index].Latitude,
                                Lon = pointsToRequest[index].Longitude,
                                Alt = pointsToRequest[index].Altitude
                            });
                        }
                        DbHandler.PushMapzenAltToDb(altToAdd);
                    }
                    else
                    {
                        while (pointsToRequest.Any())
                        {
                            var next50 = pointsToRequest.Take(50).ToList();
                            foreach (var rem in next50)
                            {
                                pointsToRequest.Remove(rem);
                            }
                            token.ThrowIfCancellationRequested();
                            var heights = await RequestHeights(next50);

                            var altToAdd = new List <GeoLatLonAlt>();
                            for (var index = 0; index < next50.Count; index++)
                            {
                                if (index >= heights.Count)
                                {
                                    continue;
                                }
                                next50[index].Altitude = heights[index] + R.NextInRange(0.1, 0.8);
                                altToAdd.Add(new GeoLatLonAlt
                                {
                                    Lat = next50[index].Latitude,
                                    Lon = next50[index].Longitude,
                                    Alt = next50[index].Altitude
                                });
                            }
                            await Task.Delay(10000, token);

                            DbHandler.PushMapzenAltToDb(altToAdd);
                        }
                    }
                }
                foreach (var point in points)
                {
                    if (CheckForExistingAltitude(point.Latitude, point.Longitude))
                    {
                        point.Altitude = GetExistingAltitude(point.Latitude, point.Longitude);
                    }
                    else
                    {
                        point.Altitude = _session != null?RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax) : R.Next(10, 120);
                    }
                }
            }
            else
            {
                foreach (var point in points)
                {
                    point.Altitude = _session != null?RandomExtensions.NextInRange(R, _session.Settings.DefaultAltitudeMin, _session.Settings.DefaultAltitudeMax) : R.Next(10, 120);
                }
            }
            return(points);
        }