protected override void OnRender(DrawingContext drawingContext)
        {
            var projection = ParentMap?.MapProjection;

            if (projection != null && !double.IsNaN(projection.LongitudeScale))
            {
                var bounds        = projection.ViewportRectToBoundingBox(new Rect(ParentMap.RenderSize));
                var lineDistance  = GetLineDistance();
                var labelFormat   = GetLabelFormat(lineDistance);
                var latLabelStart = Math.Ceiling(bounds.South / lineDistance) * lineDistance;
                var lonLabelStart = Math.Ceiling(bounds.West / lineDistance) * lineDistance;
                var latLabels     = new List <Label>((int)((bounds.North - latLabelStart) / lineDistance) + 1);
                var lonLabels     = new List <Label>((int)((bounds.East - lonLabelStart) / lineDistance) + 1);
                var typeface      = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
                var pen           = new Pen
                {
                    Brush     = Stroke,
                    Thickness = StrokeThickness,
                    DashStyle = new DashStyle(StrokeDashArray, StrokeDashOffset),
                    DashCap   = StrokeDashCap
                };

                for (var lat = latLabelStart; lat <= bounds.North; lat += lineDistance)
                {
                    latLabels.Add(new Label(lat, new FormattedText(
                                                GetLabelText(lat, labelFormat, "NS"),
                                                CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground)));

                    drawingContext.DrawLine(pen,
                                            projection.LocationToViewportPoint(new Location(lat, bounds.West)),
                                            projection.LocationToViewportPoint(new Location(lat, bounds.East)));
                }

                for (var lon = lonLabelStart; lon <= bounds.East; lon += lineDistance)
                {
                    lonLabels.Add(new Label(lon, new FormattedText(
                                                GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
                                                CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground)));

                    drawingContext.DrawLine(pen,
                                            projection.LocationToViewportPoint(new Location(bounds.South, lon)),
                                            projection.LocationToViewportPoint(new Location(bounds.North, lon)));
                }

                foreach (var latLabel in latLabels)
                {
                    foreach (var lonLabel in lonLabels)
                    {
                        var position = projection.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));

                        drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
                        drawingContext.DrawText(latLabel.Text,
                                                new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
                        drawingContext.DrawText(lonLabel.Text,
                                                new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
                        drawingContext.Pop();
                    }
                }
            }
        }
Пример #2
0
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (ParentMap != null)
            {
                var bounds     = ParentMap.ViewportTransform.Inverse.TransformBounds(new Rect(ParentMap.RenderSize));
                var start      = ParentMap.MapTransform.Transform(new Point(bounds.X, bounds.Y));
                var end        = ParentMap.MapTransform.Transform(new Point(bounds.X + bounds.Width, bounds.Y + bounds.Height));
                var minSpacing = MinLineSpacing * 360d / (Math.Pow(2d, ParentMap.ZoomLevel) * TileSource.TileSize);
                var spacing    = LineSpacings[LineSpacings.Length - 1];

                if (spacing >= minSpacing)
                {
                    spacing = LineSpacings.FirstOrDefault(s => s >= minSpacing);
                }

                var latLabelStart = Math.Ceiling(start.Latitude / spacing) * spacing;
                var lonLabelStart = Math.Ceiling(start.Longitude / spacing) * spacing;
                var latLabels     = new List <Label>((int)((end.Latitude - latLabelStart) / spacing) + 1);
                var lonLabels     = new List <Label>((int)((end.Longitude - lonLabelStart) / spacing) + 1);
                var labelFormat   = spacing < 1d ? "{0} {1}°{2:00}'" : "{0} {1}°";

                for (var lat = latLabelStart; lat <= end.Latitude; lat += spacing)
                {
                    latLabels.Add(new Label(lat, new FormattedText(
                                                CoordinateString(lat, labelFormat, "NS"),
                                                CultureInfo.InvariantCulture, FlowDirection.LeftToRight, Typeface, FontSize, Foreground)));

                    drawingContext.DrawLine(Pen,
                                            ParentMap.LocationToViewportPoint(new Location(lat, start.Longitude)),
                                            ParentMap.LocationToViewportPoint(new Location(lat, end.Longitude)));
                }

                for (var lon = lonLabelStart; lon <= end.Longitude; lon += spacing)
                {
                    lonLabels.Add(new Label(lon, new FormattedText(
                                                CoordinateString(Location.NormalizeLongitude(lon), labelFormat, "EW"),
                                                CultureInfo.InvariantCulture, FlowDirection.LeftToRight, Typeface, FontSize, Foreground)));

                    drawingContext.DrawLine(Pen,
                                            ParentMap.LocationToViewportPoint(new Location(start.Latitude, lon)),
                                            ParentMap.LocationToViewportPoint(new Location(end.Latitude, lon)));
                }

                foreach (var latLabel in latLabels)
                {
                    foreach (var lonLabel in lonLabels)
                    {
                        var position = ParentMap.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));

                        drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
                        drawingContext.DrawText(latLabel.Text,
                                                new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
                        drawingContext.DrawText(lonLabel.Text,
                                                new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
                        drawingContext.Pop();
                    }
                }
            }
        }
Пример #3
0
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (ParentMap != null)
            {
                //ERROR HERE
                var bounds        = ParentMap.ViewportTransform.Inverse.TransformBounds(new Rect(ParentMap.RenderSize));
                var start         = ParentMap.MapTransform.Transform(new Point(bounds.X, bounds.Y));
                var end           = ParentMap.MapTransform.Transform(new Point(bounds.X + bounds.Width, bounds.Y + bounds.Height));
                var lineDistance  = GetLineDistance();
                var labelFormat   = GetLabelFormat(lineDistance);
                var latLabelStart = Math.Ceiling(start.Latitude / lineDistance) * lineDistance;
                var lonLabelStart = Math.Ceiling(start.Longitude / lineDistance) * lineDistance;

                //ERROR HERE
                if (Double.IsNaN(end.Latitude) || Double.IsNaN(end.Longitude))
                {
                    return;
                }
                var latLabels = new List <Label>((int)((end.Latitude - latLabelStart) / lineDistance) + 1);
                var lonLabels = new List <Label>((int)((end.Longitude - lonLabelStart) / lineDistance) + 1);

                for (var lat = latLabelStart; lat <= end.Latitude; lat += lineDistance)
                {
                    latLabels.Add(new Label(lat, new FormattedText(
                                                GetLabelText(lat, labelFormat, "NS"),
                                                CultureInfo.InvariantCulture, FlowDirection.LeftToRight, Typeface, FontSize, Foreground)));

                    drawingContext.DrawLine(Pen,
                                            ParentMap.LocationToViewportPoint(new Location(lat, start.Longitude)),
                                            ParentMap.LocationToViewportPoint(new Location(lat, end.Longitude)));
                }

                for (var lon = lonLabelStart; lon <= end.Longitude; lon += lineDistance)
                {
                    lonLabels.Add(new Label(lon, new FormattedText(
                                                GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
                                                CultureInfo.InvariantCulture, FlowDirection.LeftToRight, Typeface, FontSize, Foreground)));

                    drawingContext.DrawLine(Pen,
                                            ParentMap.LocationToViewportPoint(new Location(start.Latitude, lon)),
                                            ParentMap.LocationToViewportPoint(new Location(end.Latitude, lon)));
                }

                foreach (var latLabel in latLabels)
                {
                    foreach (var lonLabel in lonLabels)
                    {
                        var position = ParentMap.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));

                        drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
                        drawingContext.DrawText(latLabel.Text,
                                                new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
                        drawingContext.DrawText(lonLabel.Text,
                                                new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
                        drawingContext.Pop();
                    }
                }
            }
        }
Пример #4
0
 private void CenterPointPropertyChanged(Point centerPoint)
 {
     if (!internalPropertyChange)
     {
         centerPoint.X = Location.NormalizeLongitude(centerPoint.X);
         InternalSetValue(CenterProperty, mapTransform.Transform(centerPoint));
         UpdateTransform();
     }
 }
Пример #5
0
        private void CenterPointPropertyChanged(Point centerPoint)
        {
            if (!internalPropertyChange)
            {
                var center = MapProjection.PointToLocation(centerPoint);
                center.Longitude = Location.NormalizeLongitude(center.Longitude);

                InternalSetValue(CenterProperty, center);
                UpdateTransform();
            }
        }
Пример #6
0
        private void DrawCylindricalGraticule(DrawingContext drawingContext, MapProjection projection, double lineDistance, string labelFormat)
        {
            var boundingBox = projection.ViewportRectToBoundingBox(new Rect(ParentMap.RenderSize));

            if (boundingBox.HasValidBounds)
            {
                var latLabelStart = Math.Ceiling(boundingBox.South / lineDistance) * lineDistance;
                var lonLabelStart = Math.Ceiling(boundingBox.West / lineDistance) * lineDistance;
                var latLabels     = new List <Label>((int)((boundingBox.North - latLabelStart) / lineDistance) + 1);
                var lonLabels     = new List <Label>((int)((boundingBox.East - lonLabelStart) / lineDistance) + 1);
                var typeface      = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
                var pixelsPerDpi  = VisualTreeHelper.GetDpi(this).PixelsPerDip;
                var pen           = CreatePen();

                for (var lat = latLabelStart; lat <= boundingBox.North; lat += lineDistance)
                {
                    latLabels.Add(new Label(lat, new FormattedText(
                                                GetLabelText(lat, labelFormat, "NS"),
                                                CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDpi)));

                    drawingContext.DrawLine(pen,
                                            projection.LocationToViewportPoint(new Location(lat, boundingBox.West)),
                                            projection.LocationToViewportPoint(new Location(lat, boundingBox.East)));
                }

                for (var lon = lonLabelStart; lon <= boundingBox.East; lon += lineDistance)
                {
                    lonLabels.Add(new Label(lon, new FormattedText(
                                                GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
                                                CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDpi)));

                    drawingContext.DrawLine(pen,
                                            projection.LocationToViewportPoint(new Location(boundingBox.South, lon)),
                                            projection.LocationToViewportPoint(new Location(boundingBox.North, lon)));
                }

                foreach (var latLabel in latLabels)
                {
                    foreach (var lonLabel in lonLabels)
                    {
                        var position = projection.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));

                        drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
                        drawingContext.DrawText(latLabel.Text,
                                                new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
                        drawingContext.DrawText(lonLabel.Text,
                                                new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
                        drawingContext.Pop();
                    }
                }
            }
        }
Пример #7
0
        private void AdjustCenterProperty(DependencyProperty property, ref Location center)
        {
            if (center == null ||
                center.Longitude < -180d || center.Longitude > 180d ||
                center.Latitude < -MapProjection.MaxLatitude || center.Latitude > MapProjection.MaxLatitude)
            {
                center = (center == null)
                    ? new Location()
                    : new Location(
                    Math.Min(Math.Max(center.Latitude, -MapProjection.MaxLatitude), MapProjection.MaxLatitude),
                    Location.NormalizeLongitude(center.Longitude));

                SetValueInternal(property, center);
            }
        }
Пример #8
0
        private void UpdateTransform(bool resetTransformCenter = false, bool projectionChanged = false)
        {
            var projection = MapProjection;

            projection.Center = ProjectionCenter ?? Center;

            var center    = transformCenter ?? Center;
            var mapCenter = projection.LocationToMap(center);

            var viewScale = ViewTransform.ZoomLevelToScale(ZoomLevel);

            ViewTransform.SetTransform(mapCenter, viewCenter, viewScale, Heading);

            if (transformCenter != null)
            {
                center           = ViewToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
                center.Longitude = Location.NormalizeLongitude(center.Longitude);

                if (center.Latitude < -projection.MaxLatitude || center.Latitude > projection.MaxLatitude)
                {
                    center.Latitude      = Math.Min(Math.Max(center.Latitude, -projection.MaxLatitude), projection.MaxLatitude);
                    resetTransformCenter = true;
                }

                SetValueInternal(CenterProperty, center);

                if (centerAnimation == null)
                {
                    SetValueInternal(TargetCenterProperty, center);
                }

                if (resetTransformCenter)
                {
                    ResetTransformCenter();

                    projection.Center = ProjectionCenter ?? center;
                    mapCenter         = projection.LocationToMap(center);

                    ViewTransform.SetTransform(mapCenter, viewCenter, viewScale, Heading);
                }
            }

            ViewScale = ViewTransform.Scale;

            OnViewportChanged(new ViewportChangedEventArgs(projectionChanged, Center.Longitude - centerLongitude));

            centerLongitude = Center.Longitude;
        }
Пример #9
0
 private void AdjustCenterProperty(DependencyProperty property, ref Location center)
 {
     if (center == null)
     {
         center = new Location();
         InternalSetValue(property, center);
     }
     else if (center.Longitude < -180d || center.Longitude > 180d ||
              center.Latitude < -mapTransform.MaxLatitude || center.Latitude > mapTransform.MaxLatitude)
     {
         center = new Location(
             Math.Min(Math.Max(center.Latitude, -mapTransform.MaxLatitude), mapTransform.MaxLatitude),
             Location.NormalizeLongitude(center.Longitude));
         InternalSetValue(property, center);
     }
 }
Пример #10
0
        /// <summary>
        /// Transforms a geographic location to a cartesian coordinate point
        /// with minumum distance to the specified reference longitude value.
        /// </summary>
        public Point Transform(Location location, double referenceLongitude)
        {
            var p = Transform(location);

            p.X = Location.NormalizeLongitude(p.X);

            if (p.X > referenceLongitude + 180d)
            {
                p.X -= 360d;
            }
            else if (p.X < referenceLongitude - 180d)
            {
                p.X += 360d;
            }

            return(p);
        }
Пример #11
0
        private void UpdateTransform(bool resetTransformCenter = false, bool projectionChanged = false)
        {
            var projection = MapProjection;
            var center     = transformCenter ?? Center;

            projection.SetViewportTransform(ProjectionCenter ?? Center, center, viewportCenter, ZoomLevel, Heading);

            if (transformCenter != null)
            {
                center           = projection.ViewportPointToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
                center.Longitude = Location.NormalizeLongitude(center.Longitude);

                if (center.Latitude < -projection.MaxLatitude || center.Latitude > projection.MaxLatitude)
                {
                    center.Latitude      = Math.Min(Math.Max(center.Latitude, -projection.MaxLatitude), projection.MaxLatitude);
                    resetTransformCenter = true;
                }

                SetValueInternal(CenterProperty, center);

                if (centerAnimation == null)
                {
                    SetValueInternal(TargetCenterProperty, center);
                }

                if (resetTransformCenter)
                {
                    ResetTransformCenter();
                    projection.SetViewportTransform(ProjectionCenter ?? center, center, viewportCenter, ZoomLevel, Heading);
                }
            }

            ViewportTransform.Matrix = projection.ViewportTransform;

            var scale = projection.GetMapScale(center);

            ScaleTransform.ScaleX = scale.X;
            ScaleTransform.ScaleY = scale.Y;

            RotateTransform.Angle = Heading;

            OnViewportChanged(new ViewportChangedEventArgs(projectionChanged, Center.Longitude - centerLongitude));

            centerLongitude = Center.Longitude;
        }
Пример #12
0
        private void UpdateTransform(bool resetTransformOrigin = false)
        {
            Location center;

            if (transformOrigin == null)
            {
                center = Center;
                SetViewportTransform(center);
            }
            else
            {
                SetViewportTransform(transformOrigin);

                center           = ViewportPointToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
                center.Longitude = Location.NormalizeLongitude(center.Longitude);

                if (center.Latitude < -mapTransform.MaxLatitude || center.Latitude > mapTransform.MaxLatitude)
                {
                    center.Latitude      = Math.Min(Math.Max(center.Latitude, -mapTransform.MaxLatitude), mapTransform.MaxLatitude);
                    resetTransformOrigin = true;
                }

                InternalSetValue(CenterProperty, center);

                if (centerAnimation == null)
                {
                    InternalSetValue(TargetCenterProperty, center);
                    InternalSetValue(CenterPointProperty, mapTransform.Transform(center));
                }

                if (resetTransformOrigin)
                {
                    ResetTransformOrigin();
                    SetViewportTransform(center);
                }
            }

            CenterScale           = ViewportScale * mapTransform.RelativeScale(center) / TileSource.MetersPerDegree; // pixels per meter at center latitude
            scaleTransform.ScaleX = CenterScale;
            scaleTransform.ScaleY = CenterScale;
            rotateTransform.Angle = Heading;

            OnViewportChanged();
        }
Пример #13
0
        private void UpdateTransform(bool resetTransformOrigin = false)
        {
            var center = Center;
            var scale  = SetViewportTransform(transformOrigin ?? center);

            if (transformOrigin != null)
            {
                center           = ViewportPointToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
                center.Longitude = Location.NormalizeLongitude(center.Longitude);

                if (center.Latitude < -mapTransform.MaxLatitude || center.Latitude > mapTransform.MaxLatitude)
                {
                    center.Latitude      = Math.Min(Math.Max(center.Latitude, -mapTransform.MaxLatitude), mapTransform.MaxLatitude);
                    resetTransformOrigin = true;
                }

                InternalSetValue(CenterProperty, center);

                if (centerAnimation == null)
                {
                    InternalSetValue(TargetCenterProperty, center);
                    InternalSetValue(CenterPointProperty, MapTransform.Transform(center));
                }

                if (resetTransformOrigin)
                {
                    ResetTransformOrigin();
                    scale = SetViewportTransform(center);
                }
            }

            scale      *= mapTransform.RelativeScale(center) / MetersPerDegree; // Pixels per meter at center latitude
            CenterScale = scale;
            SetTransformMatrixes(scale);

            OnViewportChanged();
        }
Пример #14
0
        protected override void OnViewportChanged(ViewportChangedEventArgs e)
        {
            if (path == null)
            {
                path = new Path
                {
                    Data = new PathGeometry()
                };

                path.SetBinding(Shape.StrokeProperty,
                                GetBindingExpression(StrokeProperty)?.ParentBinding ??
                                new Binding
                {
                    Source = this,
                    Path   = new PropertyPath("Stroke")
                });

                path.SetBinding(Shape.StrokeThicknessProperty,
                                GetBindingExpression(StrokeThicknessProperty)?.ParentBinding ??
                                new Binding
                {
                    Source = this,
                    Path   = new PropertyPath("StrokeThickness")
                });

                Children.Add(path);
            }

            var bounds       = ParentMap.ViewportTransform.Inverse.TransformBounds(new Rect(new Point(), ParentMap.RenderSize));
            var start        = ParentMap.MapTransform.Transform(new Point(bounds.X, bounds.Y));
            var end          = ParentMap.MapTransform.Transform(new Point(bounds.X + bounds.Width, bounds.Y + bounds.Height));
            var lineDistance = GetLineDistance();

            var labelStart = new Location(
                Math.Ceiling(start.Latitude / lineDistance) * lineDistance,
                Math.Ceiling(start.Longitude / lineDistance) * lineDistance);

            var labelEnd = new Location(
                Math.Floor(end.Latitude / lineDistance) * lineDistance,
                Math.Floor(end.Longitude / lineDistance) * lineDistance);

            var lineStart = new Location(
                Math.Min(Math.Max(labelStart.Latitude - lineDistance, -ParentMap.MapTransform.MaxLatitude), ParentMap.MapTransform.MaxLatitude),
                labelStart.Longitude - lineDistance);

            var lineEnd = new Location(
                Math.Min(Math.Max(labelEnd.Latitude + lineDistance, -ParentMap.MapTransform.MaxLatitude), ParentMap.MapTransform.MaxLatitude),
                labelEnd.Longitude + lineDistance);

            if (!lineStart.Equals(graticuleStart) || !lineEnd.Equals(graticuleEnd))
            {
                graticuleStart = lineStart;
                graticuleEnd   = lineEnd;

                var geometry = (PathGeometry)path.Data;
                geometry.Figures.Clear();
                geometry.Transform = ParentMap.ViewportTransform;

                for (var lat = labelStart.Latitude; lat <= end.Latitude; lat += lineDistance)
                {
                    var figure = new PathFigure
                    {
                        StartPoint = ParentMap.MapTransform.Transform(new Location(lat, lineStart.Longitude)),
                        IsClosed   = false,
                        IsFilled   = false
                    };

                    figure.Segments.Add(new LineSegment
                    {
                        Point = ParentMap.MapTransform.Transform(new Location(lat, lineEnd.Longitude)),
                    });

                    geometry.Figures.Add(figure);
                }

                for (var lon = labelStart.Longitude; lon <= end.Longitude; lon += lineDistance)
                {
                    var figure = new PathFigure
                    {
                        StartPoint = ParentMap.MapTransform.Transform(new Location(lineStart.Latitude, lon)),
                        IsClosed   = false,
                        IsFilled   = false
                    };

                    figure.Segments.Add(new LineSegment
                    {
                        Point = ParentMap.MapTransform.Transform(new Location(lineEnd.Latitude, lon)),
                    });

                    geometry.Figures.Add(figure);
                }

                var labelFormat = GetLabelFormat(lineDistance);
                var childIndex  = 1; // 0 for Path

                for (var lat = labelStart.Latitude; lat <= end.Latitude; lat += lineDistance)
                {
                    for (var lon = labelStart.Longitude; lon <= end.Longitude; lon += lineDistance)
                    {
                        TextBlock label;

                        if (childIndex < Children.Count)
                        {
                            label = (TextBlock)Children[childIndex];
                        }
                        else
                        {
                            var renderTransform = new TransformGroup();
                            renderTransform.Children.Add(new TranslateTransform());
                            renderTransform.Children.Add(ParentMap.RotateTransform);
                            renderTransform.Children.Add(new TranslateTransform());

                            label = new TextBlock
                            {
                                RenderTransform = renderTransform
                            };

                            label.SetBinding(TextBlock.ForegroundProperty,
                                             GetBindingExpression(ForegroundProperty)?.ParentBinding ??
                                             new Binding
                            {
                                Source = this,
                                Path   = new PropertyPath("Foreground")
                            });

                            Children.Add(label);
                        }

                        childIndex++;

                        if (FontFamily != null)
                        {
                            label.FontFamily = FontFamily;
                        }

                        label.FontSize    = FontSize;
                        label.FontStyle   = FontStyle;
                        label.FontStretch = FontStretch;
                        label.FontWeight  = FontWeight;
                        label.Text        = GetLabelText(lat, labelFormat, "NS") + "\n" + GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW");
                        label.Tag         = new Location(lat, lon);
                        label.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

                        var translateTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[0];
                        translateTransform.X = StrokeThickness / 2d + 2d;
                        translateTransform.Y = -label.DesiredSize.Height / 2d;
                    }
                }

                while (Children.Count > childIndex)
                {
                    Children.RemoveAt(Children.Count - 1);
                }
            }

            // don't use MapPanel.Location because labels may be at more than 180° distance from map center

            for (int i = 1; i < Children.Count; i++)
            {
                var label             = (TextBlock)Children[i];
                var location          = (Location)label.Tag;
                var viewportTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[2];
                var viewportPosition  = ParentMap.LocationToViewportPoint(location);
                viewportTransform.X = viewportPosition.X;
                viewportTransform.Y = viewportPosition.Y;
            }

            base.OnViewportChanged(e);
        }
        protected override void OnViewportChanged()
        {
            var bounds     = ParentMap.ViewportTransform.Inverse.TransformBounds(new Rect(new Point(), ParentMap.RenderSize));
            var start      = ParentMap.MapTransform.Transform(new Point(bounds.X, bounds.Y));
            var end        = ParentMap.MapTransform.Transform(new Point(bounds.X + bounds.Width, bounds.Y + bounds.Height));
            var minSpacing = MinLineSpacing * 360d / (Math.Pow(2d, ParentMap.ZoomLevel) * 256d);
            var spacing    = LineSpacings[LineSpacings.Length - 1];

            if (spacing >= minSpacing)
            {
                spacing = LineSpacings.FirstOrDefault(s => s >= minSpacing);
            }

            var labelStart = new Location(
                Math.Ceiling(start.Latitude / spacing) * spacing,
                Math.Ceiling(start.Longitude / spacing) * spacing);

            var labelEnd = new Location(
                Math.Floor(end.Latitude / spacing) * spacing,
                Math.Floor(end.Longitude / spacing) * spacing);

            var lineStart = new Location(
                Math.Min(Math.Max(labelStart.Latitude - spacing, -ParentMap.MapTransform.MaxLatitude), ParentMap.MapTransform.MaxLatitude),
                labelStart.Longitude - spacing);

            var lineEnd = new Location(
                Math.Min(Math.Max(labelEnd.Latitude + spacing, -ParentMap.MapTransform.MaxLatitude), ParentMap.MapTransform.MaxLatitude),
                labelEnd.Longitude + spacing);

            if (!lineStart.Equals(graticuleStart) || !lineEnd.Equals(graticuleEnd))
            {
                graticuleStart = lineStart;
                graticuleEnd   = lineEnd;

                var geometry = (PathGeometry)path.Data;
                geometry.Figures.Clear();
                geometry.Transform = ParentMap.ViewportTransform;

                for (var lat = labelStart.Latitude; lat <= end.Latitude; lat += spacing)
                {
                    var figure = new PathFigure
                    {
                        StartPoint = ParentMap.MapTransform.Transform(new Location(lat, lineStart.Longitude)),
                        IsClosed   = false,
                        IsFilled   = false
                    };

                    figure.Segments.Add(new LineSegment
                    {
                        Point = ParentMap.MapTransform.Transform(new Location(lat, lineEnd.Longitude)),
                    });

                    geometry.Figures.Add(figure);
                }

                for (var lon = labelStart.Longitude; lon <= end.Longitude; lon += spacing)
                {
                    var figure = new PathFigure
                    {
                        StartPoint = ParentMap.MapTransform.Transform(new Location(lineStart.Latitude, lon)),
                        IsClosed   = false,
                        IsFilled   = false
                    };

                    figure.Segments.Add(new LineSegment
                    {
                        Point = ParentMap.MapTransform.Transform(new Location(lineEnd.Latitude, lon)),
                    });

                    geometry.Figures.Add(figure);
                }

                var childIndex = 1; // 0 for Path
                var format     = spacing < 1d ? "{0} {1}°{2:00}'" : "{0} {1}°";

                for (var lat = labelStart.Latitude; lat <= end.Latitude; lat += spacing)
                {
                    for (var lon = labelStart.Longitude; lon <= end.Longitude; lon += spacing)
                    {
                        TextBlock label;

                        if (childIndex < Children.Count)
                        {
                            label = (TextBlock)Children[childIndex];
                        }
                        else
                        {
                            var renderTransform = new TransformGroup();
                            renderTransform.Children.Add(new TranslateTransform());
                            renderTransform.Children.Add(ParentMap.RotateTransform);
                            renderTransform.Children.Add(new TranslateTransform());

                            label = new TextBlock
                            {
                                RenderTransform = renderTransform
                            };

                            label.SetBinding(TextBlock.ForegroundProperty, new Binding
                            {
                                Source = this,
                                Path   = new PropertyPath("Foreground")
                            });

                            Children.Add(label);
                        }

                        childIndex++;

                        if (FontFamily != null)
                        {
                            label.FontFamily = FontFamily;
                        }

                        label.FontSize    = FontSize;
                        label.FontStyle   = FontStyle;
                        label.FontStretch = FontStretch;
                        label.FontWeight  = FontWeight;
                        label.Text        = string.Format("{0}\n{1}", CoordinateString(lat, format, "NS"), CoordinateString(Location.NormalizeLongitude(lon), format, "EW"));
                        label.Tag         = new Location(lat, lon);
                        label.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

                        var translateTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[0];
                        translateTransform.X = StrokeThickness / 2d + 2d;
                        translateTransform.Y = -label.DesiredSize.Height / 2d;
                    }
                }

                while (Children.Count > childIndex)
                {
                    Children.RemoveAt(Children.Count - 1);
                }
            }

            // don't use MapPanel.Location because labels may be at more than 180° distance from map center

            for (int i = 1; i < Children.Count; i++)
            {
                var label             = (TextBlock)Children[i];
                var location          = (Location)label.Tag;
                var viewportTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[2];
                var viewportPosition  = ParentMap.LocationToViewportPoint(location);
                viewportTransform.X = viewportPosition.X;
                viewportTransform.Y = viewportPosition.Y;
            }

            base.OnViewportChanged();
        }
Пример #16
0
        protected override void OnViewportChanged(ViewportChangedEventArgs e)
        {
            var projection = ParentMap.MapProjection;

            if (!projection.IsAzimuthal)
            {
                if (path == null)
                {
                    path = new Path {
                        Data = new PathGeometry()
                    };
                    path.SetBinding(Shape.StrokeProperty, GetBinding(StrokeProperty, nameof(Stroke)));
                    path.SetBinding(Shape.StrokeThicknessProperty, GetBinding(StrokeThicknessProperty, nameof(StrokeThickness)));
                    path.SetBinding(Shape.StrokeDashArrayProperty, GetBinding(StrokeDashArrayProperty, nameof(StrokeDashArray)));
                    path.SetBinding(Shape.StrokeDashOffsetProperty, GetBinding(StrokeDashOffsetProperty, nameof(StrokeDashOffset)));
                    path.SetBinding(Shape.StrokeDashCapProperty, GetBinding(StrokeDashCapProperty, nameof(StrokeDashCap)));
                    Children.Add(path);
                }

                var bounds       = projection.ViewportRectToBoundingBox(new Rect(0d, 0d, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height));
                var lineDistance = GetLineDistance();

                var labelStart = new Location(
                    Math.Ceiling(bounds.South / lineDistance) * lineDistance,
                    Math.Ceiling(bounds.West / lineDistance) * lineDistance);

                var labelEnd = new Location(
                    Math.Floor(bounds.North / lineDistance) * lineDistance,
                    Math.Floor(bounds.East / lineDistance) * lineDistance);

                var lineStart = new Location(
                    Math.Min(Math.Max(labelStart.Latitude - lineDistance, -projection.MaxLatitude), projection.MaxLatitude),
                    labelStart.Longitude - lineDistance);

                var lineEnd = new Location(
                    Math.Min(Math.Max(labelEnd.Latitude + lineDistance, -projection.MaxLatitude), projection.MaxLatitude),
                    labelEnd.Longitude + lineDistance);

                var geometry = (PathGeometry)path.Data;
                geometry.Figures.Clear();

                for (var lat = labelStart.Latitude; lat <= bounds.North; lat += lineDistance)
                {
                    var figure = new PathFigure
                    {
                        StartPoint = projection.LocationToViewportPoint(new Location(lat, lineStart.Longitude)),
                        IsClosed   = false,
                        IsFilled   = false
                    };

                    figure.Segments.Add(new LineSegment
                    {
                        Point = projection.LocationToViewportPoint(new Location(lat, lineEnd.Longitude))
                    });

                    geometry.Figures.Add(figure);
                }

                for (var lon = labelStart.Longitude; lon <= bounds.East; lon += lineDistance)
                {
                    var figure = new PathFigure
                    {
                        StartPoint = projection.LocationToViewportPoint(new Location(lineStart.Latitude, lon)),
                        IsClosed   = false,
                        IsFilled   = false
                    };

                    figure.Segments.Add(new LineSegment
                    {
                        Point = projection.LocationToViewportPoint(new Location(lineEnd.Latitude, lon))
                    });

                    geometry.Figures.Add(figure);
                }

                var labelFormat = GetLabelFormat(lineDistance);
                var childIndex  = 1; // 0 for Path

                for (var lat = labelStart.Latitude; lat <= bounds.North; lat += lineDistance)
                {
                    for (var lon = labelStart.Longitude; lon <= bounds.East; lon += lineDistance)
                    {
                        TextBlock label;

                        if (childIndex < Children.Count)
                        {
                            label = (TextBlock)Children[childIndex];
                        }
                        else
                        {
                            var renderTransform = new TransformGroup();
                            renderTransform.Children.Add(new TranslateTransform());
                            renderTransform.Children.Add(ParentMap.RotateTransform);
                            renderTransform.Children.Add(new TranslateTransform());

                            label = new TextBlock {
                                RenderTransform = renderTransform
                            };
                            if (FontFamily != null)
                            {
                                label.SetBinding(TextBlock.FontFamilyProperty, GetBinding(FontFamilyProperty, nameof(FontFamily)));
                            }
                            label.SetBinding(TextBlock.FontSizeProperty, GetBinding(FontSizeProperty, nameof(FontSize)));
                            label.SetBinding(TextBlock.FontStyleProperty, GetBinding(FontStyleProperty, nameof(FontStyle)));
                            label.SetBinding(TextBlock.FontStretchProperty, GetBinding(FontStretchProperty, nameof(FontStretch)));
                            label.SetBinding(TextBlock.FontWeightProperty, GetBinding(FontWeightProperty, nameof(FontWeight)));
                            label.SetBinding(TextBlock.ForegroundProperty, GetBinding(ForegroundProperty, nameof(Foreground)));

                            Children.Add(label);
                        }

                        childIndex++;

                        label.Text = GetLabelText(lat, labelFormat, "NS") + "\n" + GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW");
                        label.Tag  = new Location(lat, lon);
                        label.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

                        var translateTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[0];
                        translateTransform.X = StrokeThickness / 2d + 2d;
                        translateTransform.Y = -label.DesiredSize.Height / 2d;
                    }

                    while (Children.Count > childIndex)
                    {
                        Children.RemoveAt(Children.Count - 1);
                    }
                }

                // don't use MapPanel.Location because labels may be at more than 180° distance from map center

                for (int i = 1; i < Children.Count; i++)
                {
                    var label             = (TextBlock)Children[i];
                    var location          = (Location)label.Tag;
                    var viewportTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[2];
                    var viewportPosition  = projection.LocationToViewportPoint(location);
                    viewportTransform.X = viewportPosition.X;
                    viewportTransform.Y = viewportPosition.Y;
                }
            }
            else if (path != null)
            {
                path = null;
                Children.Clear();
            }

            base.OnViewportChanged(e);
        }
Пример #17
0
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (ParentMap != null)
            {
                var bounds     = ParentMap.ViewportTransform.Inverse.TransformBounds(new Rect(ParentMap.RenderSize));
                var start      = ParentMap.MapTransform.Transform(new Point(bounds.X, bounds.Y));
                var end        = ParentMap.MapTransform.Transform(new Point(bounds.X + bounds.Width, bounds.Y + bounds.Height));
                var minSpacing = MinLineSpacing * 360d / (Math.Pow(2d, ParentMap.ZoomLevel) * TileSource.TileSize);
                var spacing    = LineSpacings[LineSpacings.Length - 1];

                if (spacing >= minSpacing)
                {
                    spacing = LineSpacings.FirstOrDefault(s => s >= minSpacing);
                }

                var labelFormat = spacing < 1d ? "{0} {1}°{2:00}'" : "{0} {1}°";
                var labelStart  = new Location(
                    Math.Ceiling(start.Latitude / spacing) * spacing,
                    Math.Ceiling(start.Longitude / spacing) * spacing);

                var latLabels = new List <Label>((int)((end.Latitude - labelStart.Latitude) / spacing) + 1);
                var lonLabels = new List <Label>((int)((end.Longitude - labelStart.Longitude) / spacing) + 1);

                for (var lat = labelStart.Latitude; lat <= end.Latitude; lat += spacing)
                {
                    latLabels.Add(new Label(lat, CoordinateString(lat, labelFormat, "NS")));

                    drawingContext.DrawLine(Pen,
                                            ParentMap.LocationToViewportPoint(new Location(lat, start.Longitude)),
                                            ParentMap.LocationToViewportPoint(new Location(lat, end.Longitude)));
                }

                for (var lon = labelStart.Longitude; lon <= end.Longitude; lon += spacing)
                {
                    lonLabels.Add(new Label(lon, CoordinateString(Location.NormalizeLongitude(lon), labelFormat, "EW")));

                    drawingContext.DrawLine(Pen,
                                            ParentMap.LocationToViewportPoint(new Location(start.Latitude, lon)),
                                            ParentMap.LocationToViewportPoint(new Location(end.Latitude, lon)));
                }

                if (Foreground != null && Foreground != Brushes.Transparent && latLabels.Count > 0 && lonLabels.Count > 0)
                {
                    var latLabelOrigin = new Point(StrokeThickness / 2d + 2d, -StrokeThickness / 2d - FontSize / 4d);
                    var lonLabelOrigin = new Point(StrokeThickness / 2d + 2d, StrokeThickness / 2d + FontSize);
                    var transform      = Matrix.Identity;
                    transform.Rotate(ParentMap.Heading);

                    foreach (var latLabel in latLabels)
                    {
                        foreach (var lonLabel in lonLabels)
                        {
                            GlyphRun latGlyphRun;
                            GlyphRun lonGlyphRun;

                            if (!glyphRuns.TryGetValue(latLabel.Text, out latGlyphRun))
                            {
                                latGlyphRun = GlyphRunText.Create(latLabel.Text, Typeface, FontSize, latLabelOrigin);
                                glyphRuns.Add(latLabel.Text, latGlyphRun);
                            }

                            if (!glyphRuns.TryGetValue(lonLabel.Text, out lonGlyphRun))
                            {
                                lonGlyphRun = GlyphRunText.Create(lonLabel.Text, Typeface, FontSize, lonLabelOrigin);
                                glyphRuns.Add(lonLabel.Text, lonGlyphRun);
                            }

                            var position = ParentMap.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));

                            drawingContext.PushTransform(new MatrixTransform(
                                                             transform.M11, transform.M12, transform.M21, transform.M22, position.X, position.Y));

                            drawingContext.DrawGlyphRun(Foreground, latGlyphRun);
                            drawingContext.DrawGlyphRun(Foreground, lonGlyphRun);
                            drawingContext.Pop();
                        }
                    }

                    var removeKeys = glyphRuns.Keys.Where(k => !latLabels.Any(l => l.Text == k) && !lonLabels.Any(l => l.Text == k));

                    foreach (var key in removeKeys.ToList())
                    {
                        glyphRuns.Remove(key);
                    }
                }
                else
                {
                    glyphRuns.Clear();
                }
            }
        }