Exemple #1
0
        /// <summary>
        /// see https://en.wikipedia.org/wiki/Rhumb_line
        /// </summary>
        public static LocationCollection CalculateRhumbLineLocations(this Location location1, Location location2, double resolution = 1d)
        {
            if (resolution <= 0d)
            {
                throw new ArgumentOutOfRangeException("The parameter resolution must be greater than zero.");
            }

            var y1 = WebMercatorProjection.LatitudeToY(location1.Latitude);

            if (double.IsInfinity(y1))
            {
                throw new ArgumentOutOfRangeException("The parameter location1 must have an absolute latitude value of less than 90 degrees.");
            }

            var y2 = WebMercatorProjection.LatitudeToY(location2.Latitude);

            if (double.IsInfinity(y2))
            {
                throw new ArgumentOutOfRangeException("The parameter location2 must have an absolute latitude value of less than 90 degrees.");
            }

            var x1 = location1.Longitude;
            var x2 = location2.Longitude;
            var dx = x2 - x1;
            var dy = y2 - y1;
            var s  = Math.Sqrt(dx * dx + dy * dy);
            var n  = (int)Math.Ceiling(s / resolution);

            var locations = new LocationCollection(new Location(location1.Latitude, location1.Longitude));

            for (int i = 1; i < n; i++)
            {
                double x = x1 + i * dx / n;
                double y = y1 + i * dy / n;

                locations.Add(WebMercatorProjection.YToLatitude(y), x);
            }

            locations.Add(location2.Latitude, location2.Longitude);
            return(locations);
        }
Exemple #2
0
        public static LocationCollection CalculateMeridianLocations(this Location location, double latitude2, double resolution = 1d)
        {
            if (resolution <= 0d)
            {
                throw new ArgumentOutOfRangeException("The parameter resolution must be greater than zero.");
            }

            var locations = new LocationCollection();
            var s         = latitude2 - location.Latitude;
            var n         = (int)Math.Ceiling(Math.Abs(s) / resolution);

            for (int i = 0; i <= n; i++)
            {
                locations.Add(new Location(location.Latitude + i * s / n, location.Longitude));
            }

            return(locations);
        }
Exemple #3
0
        public static void Add(this LocationCollection locations, double latitude, double longitude)
        {
            if (locations.Count > 0)
            {
                var deltaLon = longitude - locations.Last().Longitude;

                if (deltaLon < -180d)
                {
                    longitude += 360d;
                }
                else if (deltaLon > 180)
                {
                    longitude -= 360;
                }
            }

            locations.Add(new Location(latitude, longitude));
        }
Exemple #4
0
        /// <summary>
        /// Calculates a series of Locations on a rhumb line, or loxodrome, that connects the two specified Locations,
        /// with an optional angular resolution specified in degrees.
        ///
        /// See https://en.wikipedia.org/wiki/Rhumb_line
        /// </summary>
        public static LocationCollection LoxodromeLocations(Location location1, Location location2, double resolution = 1d)
        {
            if (resolution <= 0d)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(resolution), "The resolution argument must be greater than zero.");
            }

            var lat1 = location1.Latitude;
            var lon1 = location1.Longitude;
            var lat2 = location2.Latitude;
            var lon2 = location2.Longitude;

            var y1 = WebMercatorProjection.LatitudeToY(lat1);
            var y2 = WebMercatorProjection.LatitudeToY(lat2);

            if (double.IsInfinity(y1))
            {
                throw new ArgumentOutOfRangeException(
                          nameof(location1), "The location1 argument must have an absolute latitude value of less than 90.");
            }

            if (double.IsInfinity(y2))
            {
                throw new ArgumentOutOfRangeException(
                          nameof(location2), "The location2 argument must have an absolute latitude value of less than 90.");
            }

            var dlat = lat2 - lat1;
            var dlon = lon2 - lon1;
            var dy   = y2 - y1;

            // beta = atan(dlon,dy)
            // sec(beta) = 1 / cos(atan(dlon,dy)) = sqrt(1 + (dlon/dy)^2)

            var sec = Math.Sqrt(1d + dlon * dlon / (dy * dy));

            const double secLimit = 1000d; // beta approximately +/-90°

            double s12;

            if (sec > secLimit)
            {
                var lat = (lat1 + lat2) * Math.PI / 360d; // mean latitude

                s12 = Math.Abs(dlon * Math.Cos(lat));     // distance in degrees along parallel of latitude
            }
            else
            {
                s12 = Math.Abs(dlat * sec); // distance in degrees along loxodrome
            }

            var n = (int)Math.Ceiling(s12 / resolution);

            var locations = new LocationCollection(new Location(lat1, lon1));

            if (sec > secLimit)
            {
                for (var i = 1; i < n; i++)
                {
                    var lon = lon1 + i * dlon / n;
                    var lat = WebMercatorProjection.YToLatitude(y1 + i * dy / n);
                    locations.Add(lat, lon);
                }
            }
            else
            {
                for (var i = 1; i < n; i++)
                {
                    var lat = lat1 + i * dlat / n;
                    var lon = lon1 + dlon * (WebMercatorProjection.LatitudeToY(lat) - y1) / dy;
                    locations.Add(lat, lon);
                }
            }

            locations.Add(lat2, lon2);

            return(locations);
        }