private string GetLatLonBoundingBoxUri(int x, int y, int zoomLevel) { var tileSize = 360d / (1 << zoomLevel); // tile width in degrees var west = x * tileSize - 180d; var east = (x + 1) * tileSize - 180d; var south = WebMercatorProjection.YToLatitude(180d - (y + 1) * tileSize); var north = WebMercatorProjection.YToLatitude(180d - y * tileSize); return(uriFormat .Replace("{w}", west.ToString(CultureInfo.InvariantCulture)) .Replace("{s}", south.ToString(CultureInfo.InvariantCulture)) .Replace("{e}", east.ToString(CultureInfo.InvariantCulture)) .Replace("{n}", north.ToString(CultureInfo.InvariantCulture))); }
/// <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); }
/// <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); }