Example #1
0
        public static void Render(Graphics g, IViewport viewport, LabelLayer labelLayer)
        {
            foreach (var layerStyle in labelLayer.Styles)
            {
                if (layerStyle.Enabled && labelLayer.MaxVisible >= viewport.Resolution && labelLayer.MinVisible < viewport.Resolution)
                {
                    if (labelLayer.DataSource == null)
                    {
                        throw (new ApplicationException("DataSource property not set"));
                    }

#if !PocketPC
                    g.SmoothingMode = SmoothingMode.AntiAlias;
#endif

                    //!!!BoundingBox envelope = map.Envelope; //View to render
                    //!!!if (CoordinateTransformation != null)
                    //!!!    envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.MathTransform.Inverse());

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

                    //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 (CoordinateTransformation != null)
                        //!!!    features[i].Geometry = GeometryTransform.TransformGeometry(
                        //!!!   features[i].Geometry, CoordinateTransformation. MathTransform);

                        if (layerStyle is IThemeStyle)
                        {
                            style = (layerStyle as ThemeStyle).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 (text != null && text != String.Empty)
                        {
                            if (feature.Geometry is GeometryCollection)
                            {
                                if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.All)
                                {
                                    foreach (Geometry geom in (feature.Geometry as GeometryCollection))
                                    {
                                        Label lbl = CreateLabel(geom, text, rotation, priority, style, viewport, g, labelLayer);
                                        if (lbl != null)
                                        {
                                            labels.Add(lbl);
                                        }
                                    }
                                }
                                else if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.CommonCenter)
                                {
                                    Label lbl = CreateLabel(feature.Geometry, text, rotation, priority, style, viewport, g, labelLayer);
                                    if (lbl != null)
                                    {
                                        labels.Add(lbl);
                                    }
                                }
                                else if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.First)
                                {
                                    //!!!
                                    //if ((feature.Geometry as GeometryCollection).Collection.Count > 0)
                                    //{
                                    //    Label lbl = CreateLabel((feature.Geometry as GeometryCollection).Collection[0], text,
                                    //                            rotation, style, map, g, labelTheme);
                                    //    if (lbl != null)
                                    //        labels.Add(lbl);
                                    //}
                                }
                                else if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.Largest)
                                {
                                    var coll = (feature.Geometry as GeometryCollection);
                                    if (coll.NumGeometries > 0)
                                    {
                                        double largestVal   = 0;
                                        int    idxOfLargest = 0;
                                        for (int 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   = ((LineString)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;
                                            }
                                        }

                                        Label lbl = CreateLabel(coll.Geometry(idxOfLargest), text, rotation, priority, style,
                                                                viewport, g, labelLayer);
                                        if (lbl != null)
                                        {
                                            labels.Add(lbl);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Label lbl = CreateLabel(feature.Geometry, text, rotation, priority, style, viewport, g, labelLayer);
                                if (lbl != null)
                                {
                                    labels.Add(lbl);
                                }
                            }
                        }
                    }

                    if (labels.Count > 0) //We have labels to render...
                    {
                        if ((layerStyle as LabelStyle).CollisionDetection && labelLayer.LabelFilter != null)
                        {
                            labelLayer.LabelFilter(labels);
                        }
                        for (int i = 0; i < labels.Count; i++)
                        {
                            if (labels[i].Show)
                            {
                                GdiGeometryRenderer.DrawLabel(g, labels[i].LabelPoint, labels[i].Style.Offset,
                                                              labels[i].Style.Font, labels[i].Style.ForeColor,
                                                              labels[i].Style.BackColor, (layerStyle as LabelStyle).Halo, labels[i].Rotation,
                                                              labels[i].Text, viewport);
                            }
                        }
                    }
                    labels = null;
                }
            }
        }
Example #2
0
        public static void Render(Graphics graphics, IViewport viewport, LabelLayer labelLayer)
        {
            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);
                                        if (label != null)
                                        {
                                            labels.Add(label);
                                        }
                                    }
                                }
                                else if (labelLayer.MultipartGeometryBehaviour == LabelLayer.MultipartGeometryBehaviourEnum.CommonCenter)
                                {
                                    var label = CreateLabel(feature.Geometry, text, rotation, priority, style, viewport, graphics);
                                    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);
                                        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);
                                        if (label != null)
                                        {
                                            labels.Add(label);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var label = CreateLabel(feature.Geometry, text, rotation, priority, style, viewport, graphics);
                                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);
                        }
                    }
                }
            }
        }