コード例 #1
0
ファイル: LabelLayer.cs プロジェクト: zb198/SharpMap
        /// <summary>
        /// Renders the layer
        /// </summary>
        /// <param name="g">Graphics object reference</param>
        /// <param name="map">Map which is rendered</param>
        public override void Render(Graphics g, MapViewport map)
        {
            if (DataSource == null)
            {
                throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'"));
            }

            var layerEnvelope       = ToSource(map.Envelope); //View to render
            List <BaseLabel> labels = null;

            using (var ds = new FeatureDataSet())
            {
                DataSource.Open();
                DataSource.ExecuteIntersectionQuery(layerEnvelope, ds);
                DataSource.Close();
                if (ds.Tables.Count > 0)
                {
                    g.TextRenderingHint = TextRenderingHint;
                    g.SmoothingMode     = SmoothingMode;

                    labels = CreateLabelDefinitions(g, map, ds.Tables[0]);
                }
            }

            if (labels == null || labels.Count == 0)
            {
                // Obsolete (and will cause infinite loop)
                //base.Render(g, map);
                return;
            }

            if (Style.CollisionDetection)
            {
                _labelFilter?.Invoke(labels);
            }

            var combinedArea = RectangleF.Empty;

            for (int i = 0; i < labels.Count; i++)
            {
                var baseLabel = labels[i];
                if (!baseLabel.Show)
                {
                    continue;
                }

                var font = baseLabel.Style.GetFontForGraphics(g);

                if (labels[i] is Label)
                {
                    var label      = baseLabel as Label;
                    var canvasArea = VectorRenderer.DrawLabelEx(
                        g, label.Location, label.Style.Offset,
                        font, label.Style.ForeColor, label.Style.BackColor,
                        label.Style.Halo, label.Rotation, label.Text, map,
                        label.Style.HorizontalAlignment, label.LabelPoint);

                    combinedArea = canvasArea.ExpandToInclude(combinedArea);
                }
                else if (labels[i] is PathLabel)
                {
                    var pathLabel = labels[i] as PathLabel;
                    var lblStyle  = pathLabel.Style;

                    var background = pathLabel.AffectedArea.ExteriorRing.TransformToImage(map);
                    if (lblStyle.BackColor != null && lblStyle.BackColor != Brushes.Transparent)
                    {
                        using (var gp = new GraphicsPath())
                        {
                            gp.AddPolygon(background);
                            g.FillPath(lblStyle.BackColor, gp);
                        }
                    }

                    g.DrawString(lblStyle.Halo, new SolidBrush(lblStyle.ForeColor),
                                 pathLabel.Text, font.FontFamily, (int)font.Style, font.Size,
                                 lblStyle.GetStringFormat(), lblStyle.IgnoreLength, pathLabel.Location, pathLabel.Box.Height);

                    combinedArea = background.ToRectangleF().ExpandToInclude(combinedArea);
                }
            }

            CanvasArea = combinedArea;

            // Obsolete (and will cause infinite loop)
            //base.Render(g, map);
        }