Exemplo n.º 1
0
        /// <summary>
        /// Renders a label to the map.
        /// </summary>
        /// <param name="graphics">Graphics reference</param>
        /// <param name="labelPoint">Label placement</param>
        /// <param name="offset">Offset of label in screen coordinates</param>
        /// <param name="font">Font used for rendering</param>
        /// <param name="forecolor">Font forecolor</param>
        /// <param name="backcolor">Background color</param>
        /// <param name="halo">Color of halo</param>
        /// <param name="rotation">Text rotation in degrees</param>
        /// <param name="text">Text to render</param>
        /// <param name="viewport"></param>
        public static void DrawLabel(Graphics graphics, Point labelPoint, Offset offset, Styles.Font font, Styles.Color forecolor, Styles.Brush backcolor, Styles.Pen halo, double rotation, string text, IViewport viewport, StyleContext context)
        {
            SizeF fontSize = graphics.MeasureString(text, font.ToGdi(context)); //Calculate the size of the text
            labelPoint.X += offset.X; labelPoint.Y += offset.Y; //add label offset
            if (Math.Abs(rotation) > Constants.Epsilon && !double.IsNaN(rotation))
            {
                graphics.TranslateTransform((float)labelPoint.X, (float)labelPoint.Y);
                graphics.RotateTransform((float)rotation);
                graphics.TranslateTransform(-fontSize.Width / 2, -fontSize.Height / 2);
                if (backcolor != null && backcolor.ToGdi(context) != Brushes.Transparent)
                    graphics.FillRectangle(backcolor.ToGdi(context), 0, 0, fontSize.Width * 0.74f + 1f, fontSize.Height * 0.74f);
                var path = new GraphicsPath();
                path.AddString(text, new FontFamily(font.FontFamily), (int)font.ToGdi(context).Style, font.ToGdi(context).Size, new System.Drawing.Point(0, 0), null);
                if (halo != null)
                    graphics.DrawPath(halo.ToGdi(context), path);
                graphics.FillPath(new SolidBrush(forecolor.ToGdi()), path);
                //g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), 0, 0);
            }
            else
            {
                if (backcolor != null && backcolor.ToGdi(context) != Brushes.Transparent)
                    graphics.FillRectangle(backcolor.ToGdi(context), (float)labelPoint.X, (float)labelPoint.Y, fontSize.Width * 0.74f + 1, fontSize.Height * 0.74f);

                var path = new GraphicsPath();

                //Arial hack
                path.AddString(text, new FontFamily("Arial"), (int)font.ToGdi(context).Style, (float)font.Size, new System.Drawing.Point((int)labelPoint.X, (int)labelPoint.Y), null);
                if (halo != null)
                    graphics.DrawPath(halo.ToGdi(context), path);
                graphics.FillPath(new SolidBrush(forecolor.ToGdi()), path);
                //g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), LabelPoint.X, LabelPoint.Y);
            }
        }
Exemplo n.º 2
0
 public static void IterateLayer(Graphics graphics, IViewport viewport, ILayer layer,
     Action<IViewport, IStyle, IFeature, StyleContext> callback)
 {
     using (var styleContext = new StyleContext())
     {
         IterateLayer(graphics, viewport, layer, styleContext, callback);
     }
 }
Exemplo n.º 3
0
        private static void IterateVectorLayer(IViewport viewport, ILayer layer, StyleContext context,
            Action<IViewport, IStyle, IFeature, StyleContext> callback)
        {
            var features = layer.GetFeaturesInView(viewport.Extent, viewport.RenderResolution)
                .Select((f, i) =>
                {
                    ReleaseLoad(i);
                    return f;
                })
                .Where(f =>
                {
                    if (f.Geometry is Mapsui.Geometries.Point) return true;
                    var boundingBox = f.Geometry.GetBoundingBox();
                    return boundingBox.Height / viewport.Resolution > 0.1 ||
                        boundingBox.Width / viewport.Resolution > 0.1;
                })
            .ToList();

            var layerStyles = layer.Style is StyleCollection ? (layer.Style as StyleCollection).ToArray() : new[] { layer.Style };
            foreach (var layerStyle in layerStyles)
            {
                var style = layerStyle; // This is the default that could be overridden by an IThemeStyle

                if ((style == null) || (style.Enabled == false) || (style.MinVisible > viewport.RenderResolution) || (style.MaxVisible < viewport.RenderResolution)) continue;

                for (int i = 0; i < features.Count; i++)
                {
                    var feature = features[i];

                    if (layerStyle is IThemeStyle) style = (layerStyle as IThemeStyle).GetStyle(feature);

                    callback(viewport, style, feature, context);

                    ReleaseLoad(i);
                }
            }

            for (int i = 0; i < features.Count; i++)
            {
                var feature = features[i];

                if (feature.Styles != null)
                {
                    foreach (var featureStyle in feature.Styles)
                    {
                        if (feature.Styles != null && featureStyle.Enabled)
                        {
                            callback(viewport, featureStyle, feature, context);
                        }
                    }
                }

                ReleaseLoad(i);
            }

            ReleaseLoad(0);
        }
Exemplo n.º 4
0
 public static void IterateLayers(Graphics graphics, IViewport viewport, IEnumerable<ILayer> layers,
     Action<IViewport, IStyle, IFeature, StyleContext> callback)
 {
     using (var styleContext = new StyleContext())
     {
         foreach (var layer in layers)
         {
             IterateLayer(graphics, viewport, layer, styleContext, callback);
         }
     }
 }
Exemplo n.º 5
0
        public static void Render(Graphics graphics, Point point, IStyle style, IViewport viewport, StyleContext context)
        {
            if (point == null) return;
            if (style == null) return;

            var symbolStyle = style as SymbolStyle;
            if ((symbolStyle == null) || (symbolStyle.BitmapId < 0))
            {
                RenderVectorPoint(graphics, point, style, viewport, context);
            }
            else
            {
                RenderBitmapPoint(graphics, point, viewport, symbolStyle);
            }
        }
Exemplo n.º 6
0
        private static void IterateLayer(Graphics graphics, IViewport viewport, ILayer layer, StyleContext context,
            Action<IViewport, IStyle, IFeature, StyleContext> callback)
        {
            if (layer.Enabled == false) return;
            if (layer.MinVisible > viewport.RenderResolution) return;
            if (layer.MaxVisible < viewport.RenderResolution) return;

            if (layer is LabelLayer)
            {
                LabellayerRenderer.Render(graphics, viewport, layer as LabelLayer, context);
            }
            else
            {
                IterateVectorLayer(viewport, layer, context, callback);
            }
        }
Exemplo n.º 7
0
        private static void RenderVectorPoint(Graphics graphics, Point point, IStyle style, IViewport viewport, StyleContext context)
        {
            var symbolStyle = ToSymbolStyle(style);
            if (symbolStyle.Fill == null) return;

            var symbolscale = symbolStyle.SymbolScale;
            var offset = symbolStyle.SymbolOffset.ToGdi();
            var rotation = symbolStyle.SymbolRotation;
            var dest = ConvertPoint(viewport.WorldToScreen(point));

            var width = symbolStyle.Width * symbolscale;
            var height = symbolStyle.Height * symbolscale;

            graphics.TranslateTransform(dest.X, dest.Y);
            graphics.RotateTransform((float)rotation);
            graphics.TranslateTransform(offset.X, -offset.Y);
            graphics.TranslateTransform((int)(-width / 2.0), (int)(-height / 2.0));

            DrawSymbol(graphics, symbolStyle, context);
            graphics.ResetTransform();
        }
Exemplo n.º 8
0
 private void RenderFeature(IViewport viewport, IStyle style, IFeature feature, StyleContext styleContext)
 {
     var vectorStyle = style as VectorStyle;
     if (feature.Geometry is Point)
         PointRenderer.Render(Graphics, (Point)feature.Geometry, vectorStyle, viewport, styleContext);
     else if (feature.Geometry is MultiPoint)
         MultiPointRenderer.Render(Graphics, (MultiPoint)feature.Geometry, vectorStyle, viewport, styleContext);
     else if (feature.Geometry is LineString)
         LineStringRenderer.Render(Graphics, (LineString)feature.Geometry, vectorStyle.Line.ToGdi(styleContext), viewport);
     else if (feature.Geometry is MultiLineString)
         MultiLineStringRenderer.Render(Graphics, (MultiLineString)feature.Geometry, vectorStyle.Line.ToGdi(styleContext), viewport);
     else if (feature.Geometry is Polygon)
         PolygonRenderer.DrawPolygon(Graphics, (Polygon)feature.Geometry, vectorStyle.Fill.ToGdi(styleContext), vectorStyle.Outline.ToGdi(styleContext), viewport);
     else if (feature.Geometry is MultiPolygon)
         MultiPolygonRenderer.Render(Graphics, (MultiPolygon)feature.Geometry, vectorStyle.Fill.ToGdi(styleContext), vectorStyle.Outline.ToGdi(styleContext), viewport);
     else if (feature.Geometry is IRaster)
         RasterRenderer.Render(Graphics, feature.Geometry, vectorStyle, viewport);
 }
Exemplo n.º 9
0
        public static void Render(Graphics graphics, IViewport viewport, LabelLayer labelLayer, StyleContext styleContext)
        {
            var layerStyles = BaseLayer.GetLayerStyles(labelLayer);
            foreach (var layerStyle in layerStyles)
            {
                if (layerStyle.Enabled && labelLayer.MaxVisible >= viewport.Resolution && labelLayer.MinVisible < viewport.Resolution)
                {
                    if (labelLayer.DataSource == null)
                        throw (new ApplicationException("DataSource property not set"));

                    graphics.SmoothingMode = SmoothingMode.AntiAlias;

                    var features = labelLayer.GetFeaturesInView(viewport.Extent, viewport.Resolution);

                    //Initialize label collection
                    var labels = new List<Label>();

                    var style = layerStyle as LabelStyle;

                    //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection
                    //Render labels
                    foreach (IFeature feature in features)
                    {
                        if (layerStyle is IThemeStyle) style = (layerStyle as IThemeStyle).GetStyle(feature) as LabelStyle;

                        float rotation = 0;
                        if (!String.IsNullOrEmpty(labelLayer.RotationColumn))
                            rotation = float.Parse(feature[labelLayer.RotationColumn].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture);

                        int priority = labelLayer.Priority;
                        if (labelLayer.PriorityDelegate != null)
                            priority = labelLayer.PriorityDelegate(feature);
                        else if (!String.IsNullOrEmpty(labelLayer.PriorityColumn))
                            priority = int.Parse(feature[labelLayer.PriorityColumn].ToString(), NumberStyles.Any, CultureInfo.InvariantCulture);

                        string text;

                        if (labelLayer.LabelStringDelegate != null)
                        {
                            text = labelLayer.LabelStringDelegate(feature);
                        }
                        else
                        {
                            text = feature[labelLayer.LabelColumn].ToString();
                        }

                        if (!string.IsNullOrEmpty(text))
                        {
                            if (feature.Geometry is GeometryCollection)
                            {
                                var geometryCollection = feature.Geometry as GeometryCollection;
                                if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.All)
                                {
                                    foreach (var geometry in geometryCollection)
                                    {
                                        var label = CreateLabel(geometry, text, rotation, priority, style, viewport, graphics, styleContext);
                                        if (label != null) labels.Add(label);
                                    }
                                }
                                else if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.CommonCenter)
                                {
                                    var label = CreateLabel(feature.Geometry, text, rotation, priority, style, viewport, graphics, styleContext);
                                    if (label != null) labels.Add(label);
                                }
                                else if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.First)
                                {
                                    if ((feature.Geometry as GeometryCollection).Collection.Count > 0)
                                    {
                                        Label label = CreateLabel(geometryCollection.Collection[0], text, rotation, 0,
                                            style, viewport, graphics, styleContext);
                                        if (label != null) labels.Add(label);
                                    }
                                }
                                else if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.Largest)
                                {
                                    var coll = (feature.Geometry as GeometryCollection);
                                    if (coll.NumGeometries > 0)
                                    {
                                        double largestVal = 0;
                                        int idxOfLargest = 0;
                                        for (var j = 0; j < coll.NumGeometries; j++)
                                        {
                                            Geometry geom = coll.Geometry(j);
                                            if (geom is LineString && ((LineString)geom).Length > largestVal)
                                            {
                                                largestVal = ((LineString)geom).Length;
                                                idxOfLargest = j;
                                            }
                                            if (geom is MultiLineString && ((MultiLineString)geom).Length > largestVal)
                                            {
                                                largestVal = ((MultiLineString)geom).Length;
                                                idxOfLargest = j;
                                            }
                                            if (geom is Polygon && ((Polygon)geom).Area > largestVal)
                                            {
                                                largestVal = ((Polygon)geom).Area;
                                                idxOfLargest = j;
                                            }
                                            if (geom is MultiPolygon && ((MultiPolygon)geom).Area > largestVal)
                                            {
                                                largestVal = ((MultiPolygon)geom).Area;
                                                idxOfLargest = j;
                                            }
                                        }

                                        var label = CreateLabel(coll.Geometry(idxOfLargest), text, rotation, priority, style,
                                                                viewport, graphics, styleContext);
                                        if (label != null) labels.Add(label);
                                    }
                                }
                            }
                            else
                            {
                                var label = CreateLabel(feature.Geometry, text, rotation, priority, style, viewport, graphics, styleContext);
                                if (label != null) labels.Add(label);
                            }
                        }
                    }

                    if (labels.Count > 0) //We have labels to render...
                    {
                        if ((layerStyle is LabelStyle) && (layerStyle as LabelStyle).CollisionDetection && labelLayer.LabelFilter != null)
                            labelLayer.LabelFilter(labels);
                        foreach (Label label in labels)
                        {
                            if (!label.Show) continue;
                            LabelRenderer.DrawLabel(graphics, label.LabelPoint, label.Style.Offset, label.Style.Font,
                                label.Style.ForeColor, label.Style.BackColor, label.Halo, label.Rotation, label.Text, viewport, styleContext);
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        private static Label CreateLabel(IGeometry feature, string text, float rotation, int priority, LabelStyle style, IViewport viewport,
                                  Graphics g, StyleContext context)
        {
            var gdiSize = g.MeasureString(text, style.Font.ToGdi(context));
            var size = new Styles.Size { Width = gdiSize.Width, Height = gdiSize.Height };

            var position = viewport.WorldToScreen(feature.GetBoundingBox().GetCentroid());
            position.X = position.X - size.Width * (short)style.HorizontalAlignment * 0.5f;
            position.Y = position.Y - size.Height * (short)style.VerticalAlignment * 0.5f;
            if (position.X - size.Width > viewport.Width || position.X + size.Width < 0 ||
                position.Y - size.Height > viewport.Height || position.Y + size.Height < 0)
                return null;

            Label label;

            if (!style.CollisionDetection)
                label = new Label(text, position, rotation, priority, null, style);
            else
            {
                //Collision detection is enabled so we need to measure the size of the string
                label = new Label(text, position, rotation, priority,
                                new LabelBox(position.X - size.Width * 0.5f - style.CollisionBuffer.Width,
                                             position.Y + size.Height * 0.5f + style.CollisionBuffer.Height,
                                             size.Width + 2f * style.CollisionBuffer.Width,
                                             size.Height + style.CollisionBuffer.Height * 2f), style);
            }

            if (!(feature is LineString)) return label;
            var line = feature as LineString;

            if (line.Length / viewport.Resolution > size.Width) //Only label feature if it is long enough
                CalculateLabelOnLinestring(line, ref label, viewport);
            else
                return null;

            return label;
        }
Exemplo n.º 11
0
 public static void Render(Graphics graphics, MultiPoint points, IStyle style, IViewport viewport, StyleContext styleContext)
 {
     foreach (var point in points.Points) PointRenderer.Render(graphics, point, style, viewport, styleContext);
 }
Exemplo n.º 12
0
        private static void DrawSymbol(Graphics graphics, SymbolStyle symbolStyle, StyleContext context)
        {
            using (var fill = symbolStyle.Fill.ToGdi(context))
            {
                if (symbolStyle.SymbolType == SymbolType.Rectangle)
                {
                    graphics.FillRectangle(fill, 0, 0, (int)symbolStyle.Width, (int)symbolStyle.Height);
                }
                else
                {
                    graphics.FillEllipse(fill, 0, 0, (int)symbolStyle.Width, (int)symbolStyle.Height);

                    if (symbolStyle.Outline != null)
                    {
                        using (var outline = symbolStyle.Outline.ToGdi(context))
                        {
                            graphics.DrawEllipse(outline, 0, 0, (int)symbolStyle.Width, (int)symbolStyle.Height);
                        }
                    }
                }
            }
        }
Exemplo n.º 13
0
        private static void IterateVectorLayer(IViewport viewport, ILayer layer, StyleContext context,
                                               Action <IViewport, IStyle, IFeature, StyleContext> callback)
        {
            var features = layer.GetFeaturesInView(viewport.Extent, viewport.RenderResolution)
                           .Select((f, i) =>
            {
                ReleaseLoad(i);
                return(f);
            })
                           .Where(f =>
            {
                if (f.Geometry is Mapsui.Geometries.Point)
                {
                    return(true);
                }
                var boundingBox = f.Geometry.GetBoundingBox();
                return(boundingBox.Height / viewport.Resolution > 0.1 ||
                       boundingBox.Width / viewport.Resolution > 0.1);
            })
                           .ToList();

            var layerStyles = layer.Style is StyleCollection ? (layer.Style as StyleCollection).ToArray() : new[] { layer.Style };

            foreach (var layerStyle in layerStyles)
            {
                var style = layerStyle; // This is the default that could be overridden by an IThemeStyle

                if ((style == null) || (style.Enabled == false) || (style.MinVisible > viewport.RenderResolution) || (style.MaxVisible < viewport.RenderResolution))
                {
                    continue;
                }

                for (int i = 0; i < features.Count; i++)
                {
                    var feature = features[i];

                    if (layerStyle is IThemeStyle)
                    {
                        style = (layerStyle as IThemeStyle).GetStyle(feature);
                    }

                    callback(viewport, style, feature, context);

                    ReleaseLoad(i);
                }
            }

            for (int i = 0; i < features.Count; i++)
            {
                var feature = features[i];

                if (feature.Styles != null)
                {
                    foreach (var featureStyle in feature.Styles)
                    {
                        if (feature.Styles != null && featureStyle.Enabled)
                        {
                            callback(viewport, featureStyle, feature, context);
                        }
                    }
                }

                ReleaseLoad(i);
            }

            ReleaseLoad(0);
        }
Exemplo n.º 14
0
        private static void IterateLayer(Graphics graphics, IViewport viewport, ILayer layer, StyleContext context,
                                         Action <IViewport, IStyle, IFeature, StyleContext> callback)
        {
            if (layer.Enabled == false)
            {
                return;
            }
            if (layer.MinVisible > viewport.RenderResolution)
            {
                return;
            }
            if (layer.MaxVisible < viewport.RenderResolution)
            {
                return;
            }

            if (layer is LabelLayer)
            {
                LabellayerRenderer.Render(graphics, viewport, layer as LabelLayer, context);
            }
            else
            {
                IterateVectorLayer(viewport, layer, context, callback);
            }
        }