Ejemplo n.º 1
0
        public void RenderLayer(ILabelLayer layer, Map map, Graphics g)
        {
            if (layer.Style.Enabled &&
                layer.Style.MaxVisible >= map.Zoom &&
                layer.Style.MinVisible < map.Zoom)
            {
                if (layer.DataSource == null)
                {
                    throw (new ApplicationException("DataSource property not set on layer '" + layer.LayerName + "'"));
                }

                g.TextRenderingHint = layer.TextRenderingHint;
                g.SmoothingMode     = layer.SmoothingMode;

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

                FeatureDataSet ds = new FeatureDataSet();
                layer.DataSource.Open();
                layer.DataSource.ExecuteIntersectionQuery(envelope, ds);
                layer.DataSource.Close();
                if (ds.Tables.Count == 0)
                {
                    //base.Render(g, map);
                    return;
                }
                FeatureDataTable features = (FeatureDataTable)ds.Tables[0];

                //Initialize label collection
                List <Label> labels = new List <Label>();
                LabelLayer.GetLabelMethod lblDelegate = layer.LabelStringDelegate;

                //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection
                //Render labels
                for (int i = 0; i < features.Count; i++)
                {
                    FeatureDataRow feature = features[i];
                    if (layer.CoordinateTransformation != null)
                    {
                        features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, layer.CoordinateTransformation.MathTransform);
                    }

                    ILabelStyle style = null;
                    if (layer.Theme != null) //If thematics is enabled, lets override the style
                    {
                        style = layer.Theme.GetStyle(feature);
                    }
                    else
                    {
                        style = layer.Style;
                    }

                    float rotation = 0;
                    if (!String.IsNullOrEmpty(layer.RotationColumn))
                    {
                        float.TryParse(feature[layer.RotationColumn].ToString(), NumberStyles.Any, Map.numberFormat_EnUS, out rotation);
                    }

                    string text;
                    if (lblDelegate != null)
                    {
                        text = lblDelegate(feature);
                    }
                    else
                    {
                        text = feature[layer.LabelColumn].ToString();
                    }

                    if (text != null && text != String.Empty)
                    {
                        if (feature.Geometry is GeometryCollection)
                        {
                            if (layer.MultipartGeometryBehaviour == SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.All)
                            {
                                foreach (SharpMap.Geometries.Geometry geom in (feature.Geometry as GeometryCollection))
                                {
                                    SharpMap.Rendering.Label lbl = CreateLabel(layer, geom, text, rotation, style, map, g);
                                    if (lbl != null)
                                    {
                                        labels.Add(lbl);
                                    }
                                }
                            }
                            else if (layer.MultipartGeometryBehaviour == SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.CommonCenter)
                            {
                                Label lbl = CreateLabel(layer, feature.Geometry, text, rotation, style, map, g);
                                if (lbl != null)
                                {
                                    labels.Add(lbl);
                                }
                            }
                            else if (layer.MultipartGeometryBehaviour == SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.First)
                            {
                                if ((feature.Geometry as GeometryCollection).Collection.Count > 0)
                                {
                                    SharpMap.Rendering.Label lbl = CreateLabel(layer, (feature.Geometry as GeometryCollection).Collection[0], text, rotation, style, map, g);
                                    if (lbl != null)
                                    {
                                        labels.Add(lbl);
                                    }
                                }
                            }
                            else if (layer.MultipartGeometryBehaviour == SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest)
                            {
                                GeometryCollection coll = (feature.Geometry as GeometryCollection);
                                if (coll.NumGeometries > 0)
                                {
                                    double largestVal   = 0;
                                    int    idxOfLargest = 0;
                                    for (int j = 0; j < coll.NumGeometries; j++)
                                    {
                                        SharpMap.Geometries.Geometry geom = coll.Geometry(j);
                                        if (geom is Geometries.LineString && ((Geometries.LineString)geom).Length > largestVal)
                                        {
                                            largestVal   = ((Geometries.LineString)geom).Length;
                                            idxOfLargest = j;
                                        }
                                        if (geom is Geometries.MultiLineString && ((Geometries.MultiLineString)geom).Length > largestVal)
                                        {
                                            largestVal   = ((Geometries.LineString)geom).Length;
                                            idxOfLargest = j;
                                        }
                                        if (geom is Geometries.Polygon && ((Geometries.Polygon)geom).Area > largestVal)
                                        {
                                            largestVal   = ((Geometries.Polygon)geom).Area;
                                            idxOfLargest = j;
                                        }
                                        if (geom is Geometries.MultiPolygon && ((Geometries.MultiPolygon)geom).Area > largestVal)
                                        {
                                            largestVal   = ((Geometries.MultiPolygon)geom).Area;
                                            idxOfLargest = j;
                                        }
                                    }

                                    SharpMap.Rendering.Label lbl = CreateLabel(layer, coll.Geometry(idxOfLargest), text, rotation, style, map, g);
                                    if (lbl != null)
                                    {
                                        labels.Add(lbl);
                                    }
                                }
                            }
                        }
                        else
                        {
                            SharpMap.Rendering.Label lbl = CreateLabel(layer, feature.Geometry, text, rotation, style, map, g);
                            if (lbl != null)
                            {
                                labels.Add(lbl);
                            }
                        }
                    }
                }
                if (labels.Count > 0) //We have labels to render...
                {
                    if (layer.Style.CollisionDetection && layer.LabelFilter != null)
                    {
                        layer.LabelFilter(labels);
                    }
                    for (int i = 0; i < labels.Count; i++)
                    {
                        VectorRenderer.DrawLabel(g, labels[i].LabelPoint, labels[i].Style.Offset, labels[i].Style.Font, labels[i].Style.ForeColor, labels[i].Style.BackColor, layer.Style.Halo, labels[i].Rotation, labels[i].Text, map);
                    }
                }
                labels = null;
            }
            //base.Render(g, map);
        }
Ejemplo n.º 2
0
        /// <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 + "'"));
            }
            g.TextRenderingHint = TextRenderingHint;
            g.SmoothingMode     = SmoothingMode;

            var mapEnvelope   = map.Envelope;
            var layerEnvelope = ToSource(mapEnvelope); //View to render
            var lineClipping  = new CohenSutherlandLineClipping(mapEnvelope.MinX, mapEnvelope.MinY,
                                                                mapEnvelope.MaxX, mapEnvelope.MaxY);

            var ds = new FeatureDataSet();

            DataSource.Open();
            DataSource.ExecuteIntersectionQuery(layerEnvelope, ds);
            DataSource.Close();
            if (ds.Tables.Count == 0)
            {
                base.Render(g, map);
                return;
            }

            var features = ds.Tables[0];

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

            //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection
            //Render labels

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

                LabelStyle style;
                if (Theme != null) //If thematics is enabled, lets override the style
                {
                    style = Theme.GetStyle(feature) as LabelStyle;
                }
                else
                {
                    style = Style;
                }

                // Do we need to render at all?
                if (!style.Enabled)
                {
                    continue;
                }

                // Rotation
                float rotationStyle  = style != null ? style.Rotation : 0f;
                float rotationColumn = 0f;
                if (!String.IsNullOrEmpty(RotationColumn))
                {
                    Single.TryParse(feature[RotationColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs,
                                    out rotationColumn);
                }
                float rotation = rotationStyle + rotationColumn;

                // Priority
                int priority = Priority;
                if (_getPriorityMethod != null)
                {
                    priority = _getPriorityMethod(feature);
                }
                else if (!String.IsNullOrEmpty(PriorityColumn))
                {
                    Int32.TryParse(feature[PriorityColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs,
                                   out priority);
                }

                // Text
                string text;
                if (_getLabelMethod != null)
                {
                    text = _getLabelMethod(feature);
                }
                else
                {
                    text = feature[LabelColumn].ToString();
                }

                if (!String.IsNullOrEmpty(text))
                {
                    // for lineal geometries, try clipping to ensure proper labeling
                    if (feature.Geometry is ILineal)
                    {
                        if (feature.Geometry is ILineString)
                        {
                            feature.Geometry = lineClipping.ClipLineString(feature.Geometry as ILineString);
                        }
                        else if (feature.Geometry is IMultiLineString)
                        {
                            feature.Geometry = lineClipping.ClipLineString(feature.Geometry as IMultiLineString);
                        }
                    }

                    if (feature.Geometry is IGeometryCollection)
                    {
                        if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.All)
                        {
                            foreach (var geom in (feature.Geometry as IGeometryCollection))
                            {
                                BaseLabel lbl = CreateLabel(feature, geom, text, rotation, priority, style, map, g, _getLocationMethod);
                                if (lbl != null)
                                {
                                    labels.Add(lbl);
                                }
                            }
                        }
                        else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.CommonCenter)
                        {
                            BaseLabel lbl = CreateLabel(feature, feature.Geometry, text, rotation, priority, style, map, g, _getLocationMethod);
                            if (lbl != null)
                            {
                                labels.Add(lbl);
                            }
                        }
                        else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.First)
                        {
                            if ((feature.Geometry as IGeometryCollection).NumGeometries > 0)
                            {
                                BaseLabel lbl = CreateLabel(feature, (feature.Geometry as IGeometryCollection).GetGeometryN(0), text,
                                                            rotation, style, map, g);
                                if (lbl != null)
                                {
                                    labels.Add(lbl);
                                }
                            }
                        }
                        else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.Largest)
                        {
                            var coll = (feature.Geometry as IGeometryCollection);
                            if (coll.NumGeometries > 0)
                            {
                                var largestVal   = 0d;
                                var idxOfLargest = 0;
                                for (var j = 0; j < coll.NumGeometries; j++)
                                {
                                    var geom = coll.GetGeometryN(j);
                                    if (geom is ILineString && ((ILineString)geom).Length > largestVal)
                                    {
                                        largestVal   = ((ILineString)geom).Length;
                                        idxOfLargest = j;
                                    }
                                    if (geom is IMultiLineString && ((IMultiLineString)geom).Length > largestVal)
                                    {
                                        largestVal   = ((IMultiLineString)geom).Length;
                                        idxOfLargest = j;
                                    }
                                    if (geom is IPolygon && ((IPolygon)geom).Area > largestVal)
                                    {
                                        largestVal   = ((IPolygon)geom).Area;
                                        idxOfLargest = j;
                                    }
                                    if (geom is IMultiPolygon && ((IMultiPolygon)geom).Area > largestVal)
                                    {
                                        largestVal   = ((IMultiPolygon)geom).Area;
                                        idxOfLargest = j;
                                    }
                                }

                                BaseLabel lbl = CreateLabel(feature, coll.GetGeometryN(idxOfLargest), text, rotation, priority, style,
                                                            map, g, _getLocationMethod);
                                if (lbl != null)
                                {
                                    labels.Add(lbl);
                                }
                            }
                        }
                    }
                    else
                    {
                        BaseLabel lbl = CreateLabel(feature, feature.Geometry, text, rotation, priority, style, map, g, _getLocationMethod);
                        if (lbl != null)
                        {
                            labels.Add(lbl);
                        }
                    }
                }
            }
            if (labels.Count > 0) //We have labels to render...
            {
                if (Style.CollisionDetection && _labelFilter != null)
                {
                    _labelFilter(labels);
                }

                for (int i = 0; i < labels.Count; i++)
                {
                    // Don't show the label if not necessary
                    if (!labels[i].Show)
                    {
                        continue;
                    }

                    if (labels[i] is Label)
                    {
                        var label = labels[i] as Label;
                        if (label.Style.IsTextOnPath == false || label.TextOnPathLabel == null)
                        {
                            VectorRenderer.DrawLabel(g, label.Location, label.Style.Offset,
                                                     label.Style.GetFontForGraphics(g), label.Style.ForeColor,
                                                     label.Style.BackColor, label.Style.Halo, label.Rotation,
                                                     label.Text, map, label.Style.HorizontalAlignment,
                                                     label.LabelPoint);
                        }
                        else
                        {
                            if (label.Style.BackColor != null && label.Style.BackColor != System.Drawing.Brushes.Transparent)
                            {
                                //draw background
                                if (label.TextOnPathLabel.RegionList.Count > 0)
                                {
                                    g.FillRectangles(labels[i].Style.BackColor, labels[i].TextOnPathLabel.RegionList.ToArray());
                                    //g.FillPolygon(labels[i].Style.BackColor, labels[i].TextOnPathLabel.PointsText.ToArray());
                                }
                            }
                            label.TextOnPathLabel.DrawTextOnPath();
                        }
                    }
                    else if (labels[i] is PathLabel)
                    {
                        var plbl     = labels[i] as PathLabel;
                        var lblStyle = plbl.Style;
                        g.DrawString(lblStyle.Halo, new SolidBrush(lblStyle.ForeColor), plbl.Text,
                                     lblStyle.Font.FontFamily, (int)lblStyle.Font.Style, lblStyle.Font.Size,
                                     lblStyle.GetStringFormat(), lblStyle.IgnoreLength, plbl.Location);
                    }
                }
            }
            base.Render(g, map);
        }
Ejemplo n.º 3
0
        /// <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, Map map)
        {
            if (Style.Enabled && Style.MaxVisible >= map.Zoom && Style.MinVisible < map.Zoom)
            {
                if (DataSource == null)
                {
                    throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'"));
                }
                g.TextRenderingHint = TextRenderingHint;
                g.SmoothingMode     = SmoothingMode;

                BoundingBox envelope     = map.Envelope; //View to render
                var         lineClipping = new CohenSutherlandLineClipping(envelope.Min.X, envelope.Min.Y,
                                                                           envelope.Max.X, envelope.Max.Y);

                if (CoordinateTransformation != null)
                {
#if !DotSpatialProjections
                    CoordinateTransformation.MathTransform.Invert();
                    envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.MathTransform);
                    CoordinateTransformation.MathTransform.Invert();
#else
                    envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source);
#endif
                }
                FeatureDataSet ds = new FeatureDataSet();
                DataSource.Open();
                DataSource.ExecuteIntersectionQuery(envelope, ds);
                DataSource.Close();
                if (ds.Tables.Count == 0)
                {
                    base.Render(g, map);
                    return;
                }

                FeatureDataTable features = ds.Tables[0];


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

                //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection
                //Render labels
                for (int i = 0; i < features.Count; i++)
                {
                    FeatureDataRow feature = features[i];
                    if (CoordinateTransformation != null)
#if !DotSpatialProjections
                    { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry,
                                                                                 CoordinateTransformation.
                                                                                 MathTransform); }
#else
                    { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry,
                                                                                 CoordinateTransformation.Source,
                                                                                 CoordinateTransformation.Target); }
#endif
                    LabelStyle style;
                    if (Theme != null) //If thematics is enabled, lets override the style
                    {
                        style = Theme.GetStyle(feature) as LabelStyle;
                    }
                    else
                    {
                        style = Style;
                    }

                    float rotationStyle  = style != null ? style.Rotation : 0f;
                    float rotationColumn = 0f;
                    if (!String.IsNullOrEmpty(RotationColumn))
                    {
                        Single.TryParse(feature[RotationColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs,
                                        out rotationColumn);
                    }
                    float rotation = rotationStyle + rotationColumn;

                    int priority = Priority;
                    if (_getPriorityMethod != null)
                    {
                        priority = _getPriorityMethod(feature);
                    }
                    else if (!String.IsNullOrEmpty(PriorityColumn))
                    {
                        Int32.TryParse(feature[PriorityColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs,
                                       out priority);
                    }

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

                    if (!String.IsNullOrEmpty(text))
                    {
                        // for lineal geometries, try clipping to ensure proper labeling
                        if (feature.Geometry is ILineal)
                        {
                            if (feature.Geometry is LineString)
                            {
                                feature.Geometry = lineClipping.ClipLineString(feature.Geometry as LineString);
                            }
                            else if (feature.Geometry is MultiLineString)
                            {
                                feature.Geometry = lineClipping.ClipLineString(feature.Geometry as MultiLineString);
                            }
                        }

                        if (feature.Geometry is GeometryCollection)
                        {
                            if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.All)
                            {
                                foreach (Geometry geom in (feature.Geometry as GeometryCollection))
                                {
                                    BaseLabel lbl = CreateLabel(geom, text, rotation, priority, style, map, g);
                                    if (lbl != null)
                                    {
                                        labels.Add(lbl);
                                    }
                                }
                            }
                            else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.CommonCenter)
                            {
                                BaseLabel lbl = CreateLabel(feature.Geometry, text, rotation, priority, style, map, g);
                                if (lbl != null)
                                {
                                    labels.Add(lbl);
                                }
                            }
                            else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.First)
                            {
                                if ((feature.Geometry as GeometryCollection).Collection.Count > 0)
                                {
                                    BaseLabel lbl = CreateLabel((feature.Geometry as GeometryCollection).Collection[0], text,
                                                                rotation, style, map, g);
                                    if (lbl != null)
                                    {
                                        labels.Add(lbl);
                                    }
                                }
                            }
                            else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.Largest)
                            {
                                GeometryCollection 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   = ((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;
                                        }
                                    }

                                    BaseLabel lbl = CreateLabel(coll.Geometry(idxOfLargest), text, rotation, priority, style,
                                                                map, g);
                                    if (lbl != null)
                                    {
                                        labels.Add(lbl);
                                    }
                                }
                            }
                        }
                        else
                        {
                            BaseLabel lbl = CreateLabel(feature.Geometry, text, rotation, priority, style, map, g);
                            if (lbl != null)
                            {
                                labels.Add(lbl);
                            }
                        }
                    }
                }
                if (labels.Count > 0) //We have labels to render...
                {
                    if (Style.CollisionDetection && _labelFilter != null)
                    {
                        _labelFilter(labels);
                    }

                    for (int i = 0; i < labels.Count; i++)
                    {
                        // Don't show the label if not necessary
                        if (!labels[i].Show)
                        {
                            continue;
                        }

                        if (labels[i] is Label)
                        {
                            var label = labels[i] as Label;
                            VectorRenderer.DrawLabel(g, label.Location, label.Style.Offset,
                                                     label.Style.Font, label.Style.ForeColor,
                                                     label.Style.BackColor, Style.Halo, label.Rotation,
                                                     label.Text, map);
                        }
                        else if (labels[i] is PathLabel)
                        {
                            var plbl     = labels[i] as PathLabel;
                            var lblStyle = plbl.Style;
                            g.DrawString(lblStyle.Halo, new SolidBrush(lblStyle.ForeColor), plbl.Text,
                                         lblStyle.Font.FontFamily, (int)lblStyle.Font.Style, lblStyle.Font.Size,
                                         lblStyle.GetStringFormat(), lblStyle.IgnoreLength, plbl.Location);
                        }
                    }
                }
            }
            base.Render(g, map);
        }