Ejemplo n.º 1
0
        public static Canvas RenderLabelLayer(IViewport viewport, LabelLayer layer)
        {
            var canvas = new Canvas {
                Opacity = layer.Opacity
            };

            // todo: take into account the priority
            var features = layer.GetFeaturesInView(viewport.Extent, viewport.Resolution).ToList();

            var layerStyles = BaseLayer.GetLayerStyles(layer);

            foreach (var layerStyle in layerStyles)
            {
                foreach (var feature in features)
                {
                    var style = (layerStyle is IThemeStyle) ? (layerStyle as IThemeStyle).GetStyle(feature) : layerStyle;

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

                    if (!(style is LabelStyle))
                    {
                        throw new Exception("Style of label is not a LabelStyle");
                    }
                    var labelStyle = style as LabelStyle;

                    labelStyle.Text = layer.GetLabelText(feature);

                    var postion   = feature.Geometry.GetBoundingBox().GetCentroid();
                    var labelText = labelStyle.GetLabelText(feature);
                    canvas.Children.Add(SingleLabelRenderer.RenderLabel(postion, labelStyle, viewport, labelText));
                }
            }
            return(canvas);
        }
Ejemplo n.º 2
0
        public static CALayer RenderLabelLayer(IViewport viewport, LabelLayer layer, List <IFeature> features)
        {
            var canvas = new CALayer();

            canvas.Opacity = (float)layer.Opacity;

            //todo: take into account the priority
            var stackOffset = new Offset();

            if (layer.Style != null)
            {
                var style = layer.Style;

                foreach (var feature in features)
                {
                    if (style is IThemeStyle)
                    {
                        style = (style as IThemeStyle).GetStyle(feature);
                    }

                    if ((style == null) || (style.Enabled == false) || (style.MinVisible > viewport.Resolution) || (style.MaxVisible < viewport.Resolution))
                    {
                        continue;
                    }
                    if (!(style is LabelStyle))
                    {
                        throw new Exception("Style of label is not a LabelStyle");
                    }
                    //var labelStyle = style as LabelStyle;
                    string labelText = layer.GetLabelText(feature);

                    var label = RenderLabel(feature.Geometry as Mapsui.Geometries.Point,
                                            style as LabelStyle, feature, viewport, labelText);

                    canvas.AddSublayer(label);
                }
            }

            return(canvas);
        }
Ejemplo n.º 3
0
        public static List <IFeature> RenderStackedLabelLayer(IViewport viewport, LabelLayer layer)
        {
            var renderedFeatures = new List <IFeature> ();
            var canvas           = new CALayer();

            canvas.Opacity = (float)layer.Opacity;

            //todo: take into account the priority
            var features = layer.GetFeaturesInView(viewport.Extent, viewport.Resolution);
            var margin   = viewport.Resolution * 50;

            if (layer.Style != null)
            {
                var clusters = new List <Cluster>();
                //todo: repeat until there are no more merges
                ClusterFeatures(clusters, features, margin, layer.Style, viewport.Resolution);

                foreach (var cluster in clusters)
                {
                    var feature = cluster.Features.OrderBy(f => f.Geometry.GetBoundingBox().GetCentroid().Y).FirstOrDefault();
                    //SetFeatureOutline (feature, layer.Name, cluster.Features.Count);
                    //var bb = RenderBox(cluster.Box, viewport);

                    //Zorg dat dit ALTIJD decimal zelfde ISet als ViewChanged is
                    //var feature = cluster.Features.FirstOrDefault ();

                    var styles = feature.Styles ?? Enumerable.Empty <IStyle>();
                    foreach (var style in styles)
                    {
                        if (feature.Styles != null && style.Enabled)
                        {
                            var styleKey         = layer.Name;                     //feature.GetHashCode ().ToString ();
                            var renderedGeometry = (feature[styleKey] != null) ? (CALayer)feature[styleKey] : null;
                            var labelText        = layer.GetLabelText(feature);

                            if (renderedGeometry == null)
                            {
                                //Mapsui.Geometries.Point point, Offset stackOffset, LabelStyle style, IFeature feature, IViewport viewport, string text)
                                renderedGeometry = RenderLabel(feature.Geometry as Mapsui.Geometries.Point,
                                                               style as LabelStyle, feature, viewport, labelText);

                                feature [styleKey] = renderedGeometry;
                                feature ["first"]  = true;
                            }
                            else
                            {
                                feature ["first"] = false;
                            }
                        }
                    }
                    renderedFeatures.Add(feature);

                    /*
                     * Offset stackOffset = null;
                     *
                     * foreach (var feature in cluster.Features.OrderBy(f => f.Geometry.GetBoundingBox().GetCentroid().Y))
                     * {
                     *      if (layerStyle is IThemeStyle) style = (layerStyle as IThemeStyle).GetStyle(feature);
                     *      if ((style == null) || (style.Enabled == false) || (style.MinVisible > viewport.Resolution) || (style.MaxVisible < viewport.Resolution)) continue;
                     *
                     *      if (stackOffset == null) //first time
                     *      {
                     *              stackOffset = new Offset();
                     *              if (cluster.Features.Count > 1)
                     *                      canvas.AddSublayer (RenderBox(cluster.Box, viewport));
                     *      }
                     *      else stackOffset.Y += 18; //todo: get size from text, (or just pass stack nr)
                     *
                     *      if (!(style is LabelStyle)) throw new Exception("Style of label is not a LabelStyle");
                     *      var labelStyle = style as LabelStyle;
                     *      string labelText = layer.GetLabel(feature);
                     *      var position = new Mapsui.Geometries.Point(cluster.Box.GetCentroid().X, cluster.Box.Bottom);
                     *      canvas.AddSublayer(RenderLabel(position, stackOffset, labelStyle, feature, viewport, labelText));
                     * }
                     */
                }
            }

            return(renderedFeatures);
        }
Ejemplo n.º 4
0
        public static Canvas Render(IViewport viewport, LabelLayer layer)
        {
            // todo: Move stack functionality to Mapsui core.
            // step 1) Split RenderStackedLabelLayer into a method
            // GetFeaturesInViewStacked en a RenderStackedLabel
            // which can later be replace by normal label rendering.
            // The method GetFeaturesInViewStacked
            // returns a style with an offset determined by the stackoffset
            // and a position determined by CenterX en Cluster.Box.Bottom.
            // step 2) Move GetFeaturesInViewStacked to a GetFeaturesInView
            // method of a new StackedLabelLayed.

            var canvas = new Canvas {
                Opacity = layer.Opacity
            };

            // todo: take into account the priority
            var features = layer.GetFeaturesInView(viewport.Extent, viewport.Resolution).ToArray();
            var margin   = viewport.Resolution * 50;

            const int symbolSize = 32; // todo: determine margin by symbol size
            const int boxMargin  = symbolSize / 2;

            var clusters = new List <Cluster>();

            //todo: repeat until there are no more merges
            ClusterFeatures(clusters, features, margin, layer.Style, viewport.Resolution);
            const int textHeight = 18;

            foreach (var cluster in clusters)
            {
                var stackOffsetY = double.NaN;

                var orderedFeatures = cluster.Features.OrderBy(f => f.Geometry.GetBoundingBox().GetCentroid().Y);

                if (cluster.Features.Count > 1)
                {
                    canvas.Children.Add(RenderBox(cluster.Box, viewport));
                }

                foreach (var feature in orderedFeatures)
                {
                    if (double.IsNaN(stackOffsetY)) // first time
                    {
                        stackOffsetY = textHeight * 0.5 + boxMargin;
                    }
                    else
                    {
                        stackOffsetY += textHeight; //todo: get size from text (or just pass stack nr)
                    }
                    LabelStyle style;
                    if (layer.Style is IThemeStyle)
                    {
                        style = (LabelStyle)((IThemeStyle)layer.Style).GetStyle(feature);
                    }
                    else
                    {
                        style = (LabelStyle)layer.Style;
                    }

                    var labelStyle = new LabelStyle(style)
                    {
                        Text = layer.GetLabelText(feature)
                               //we only use the layer for the text, this should be returned by style
                    };
                    labelStyle.Offset.Y += stackOffsetY;

                    // Since the box can be rotated, find the minimal Y value of all 4 corners
                    var rotatedBox = cluster.Box.Rotate(-viewport.Rotation);
                    var minY       = rotatedBox.Vertices.Select(v => v.Y).Min();
                    var position   = new Point(cluster.Box.GetCentroid().X, minY);

                    var labelText = labelStyle.GetLabelText(feature);
                    canvas.Children.Add(SingleLabelRenderer.RenderLabel(position, labelStyle, viewport, labelText));
                }
            }
            return(canvas);
        }