Exemplo n.º 1
0
        private void SetRenderTransform()
        {
            var tileScale  = (double)(1 << TileGrid.ZoomLevel);
            var scale      = Math.Pow(2d, parentMap.ZoomLevel) / tileScale;
            var tileCenter = new Point(tileScale * (0.5 + parentMap.Center.Longitude / 360d),
                                       tileScale * (0.5 - WebMercatorProjection.LatitudeToY(parentMap.Center.Latitude) / 360d));
            var tileOrigin = new Point(MapProjection.TileSize * (tileCenter.X - TileGrid.XMin),
                                       MapProjection.TileSize * (tileCenter.Y - TileGrid.YMin));
            var viewCenter = new Point(parentMap.RenderSize.Width / 2d, parentMap.RenderSize.Height / 2d);

            ((MatrixTransform)RenderTransform).Matrix =
                MapProjection.CreateTransformMatrix(tileOrigin, scale, parentMap.Heading, viewCenter);
        }
Exemplo n.º 2
0
        private void SetRenderTransform()
        {
            var tileScale   = (double)(1 << TileGrid.ZoomLevel);
            var scale       = Math.Pow(2d, parentMap.ZoomLevel) / tileScale;
            var tileCenterX = tileScale * (0.5 + parentMap.Center.Longitude / 360d);
            var tileCenterY = tileScale * (0.5 - WebMercatorProjection.LatitudeToY(parentMap.Center.Latitude) / 360d);
            var tileOriginX = MapProjection.TileSize * (tileCenterX - TileGrid.XMin);
            var tileOriginY = MapProjection.TileSize * (tileCenterY - TileGrid.YMin);
            var viewCenterX = parentMap.RenderSize.Width / 2d;
            var viewCenterY = parentMap.RenderSize.Height / 2d;

            ((MatrixTransform)RenderTransform).Matrix = MatrixEx.TranslateScaleRotateTranslate(
                -tileOriginX, -tileOriginY, scale, scale, parentMap.Heading, viewCenterX, viewCenterY);
        }
Exemplo n.º 3
0
        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)));
        }
Exemplo n.º 4
0
        private TileGrid GetTileGrid()
        {
            var tileZoomLevel = Math.Max(0, (int)Math.Round(parentMap.ZoomLevel + ZoomLevelOffset));
            var tileScale     = (double)(1 << tileZoomLevel);
            var scale         = tileScale / (Math.Pow(2d, parentMap.ZoomLevel) * MapProjection.TileSize);
            var tileCenter    = new Point(tileScale * (0.5 + parentMap.Center.Longitude / 360d),
                                          tileScale * (0.5 - WebMercatorProjection.LatitudeToY(parentMap.Center.Latitude) / 360d));
            var viewCenter = new Point(parentMap.RenderSize.Width / 2d, parentMap.RenderSize.Height / 2d);

            var transform = new MatrixTransform
            {
                Matrix = MapProjection.CreateTransformMatrix(viewCenter, scale, -parentMap.Heading, tileCenter)
            };

            var bounds = transform.TransformBounds(new Rect(0d, 0d, parentMap.RenderSize.Width, parentMap.RenderSize.Height));

            return(new TileGrid(tileZoomLevel,
                                (int)Math.Floor(bounds.X), (int)Math.Floor(bounds.Y),
                                (int)Math.Floor(bounds.X + bounds.Width), (int)Math.Floor(bounds.Y + bounds.Height)));
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public MapBase()
        {
            MapProjection = new WebMercatorProjection();
            ScaleRotateTransform.Children.Add(ScaleTransform);
            ScaleRotateTransform.Children.Add(RotateTransform);

            // set Background by Style to enable resetting by ClearValue in MapLayerPropertyChanged
            var style = new Style(typeof(MapBase));

            style.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
            Style = style;

            Clip = new RectangleGeometry();

            SizeChanged += (s, e) =>
            {
                Clip.Rect = new Rect(0d, 0d, e.NewSize.Width, e.NewSize.Height);
                ResetTransformCenter();
                UpdateTransform();
            };
        }
Exemplo n.º 7
0
 public MapBase()
 {
     MapProjection = new WebMercatorProjection();
     ScaleRotateTransform.Children.Add(ScaleTransform);
     ScaleRotateTransform.Children.Add(RotateTransform);
 }
Exemplo n.º 8
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);
        }