Beispiel #1
0
 private static Shape RenderGeometry(IViewport viewport, IStyle style, IFeature feature,
                                     BrushCache brushCache = null)
 {
     if (feature.Geometry is Geometries.Point)
     {
         return(PointRenderer.RenderPoint(feature.Geometry as Geometries.Point, style, viewport, brushCache));
     }
     if (feature.Geometry is MultiPoint)
     {
         return(GeometryRenderer.RenderMultiPoint(feature.Geometry as MultiPoint, style, viewport));
     }
     if (feature.Geometry is LineString)
     {
         return(GeometryRenderer.RenderLineString(feature.Geometry as LineString, style, viewport));
     }
     if (feature.Geometry is MultiLineString)
     {
         return(GeometryRenderer.RenderMultiLineString(feature.Geometry as MultiLineString, style, viewport));
     }
     if (feature.Geometry is Polygon)
     {
         return(GeometryRenderer.RenderPolygon(feature.Geometry as Polygon, style, viewport, brushCache));
     }
     if (feature.Geometry is MultiPolygon)
     {
         return(GeometryRenderer.RenderMultiPolygon(feature.Geometry as MultiPolygon, style, viewport));
     }
     if (feature.Geometry is IRaster)
     {
         return(GeometryRenderer.RenderRaster(feature.Geometry as IRaster, style, viewport));
     }
     return(null);
 }
Beispiel #2
0
        public static XamlShapes.Shape RenderPoint(Point point, IStyle style, IViewport viewport,
                                                   BrushCache brushCache = null)
        {
            XamlShapes.Shape symbol;
            var matrix = XamlMedia.Matrix.Identity;

            if (style is SymbolStyle)
            {
                var symbolStyle = style as SymbolStyle;

                if (symbolStyle.BitmapId < 0)
                {
                    symbol = CreateSymbolFromVectorStyle(symbolStyle, symbolStyle.Opacity, symbolStyle.SymbolType);
                }
                else
                {
                    symbol = CreateSymbolFromBitmap(symbolStyle.BitmapId, symbolStyle.Opacity, brushCache);
                }
                matrix = CreatePointSymbolMatrix(viewport.Resolution, symbolStyle);
            }
            else
            {
                symbol = CreateSymbolFromVectorStyle((style as VectorStyle) ?? new VectorStyle());
                MatrixHelper.ScaleAt(ref matrix, viewport.Resolution, viewport.Resolution);
            }

            MatrixHelper.Append(ref matrix, GeometryRenderer.CreateTransformMatrix(point, viewport));

            symbol.RenderTransform = new XamlMedia.MatrixTransform {
                Matrix = matrix
            };
            symbol.IsHitTestVisible = false;

            return(symbol);
        }
Beispiel #3
0
        private static void RenderFeature(IViewport viewport, Canvas canvas, IFeature feature, IStyle style,
                                          bool rasterizing, BrushCache brushCache = null)
        {
            if (style is LabelStyle)
            {
                var labelStyle = (LabelStyle)style;
                canvas.Children.Add(SingleLabelRenderer.RenderLabel(feature.Geometry.GetBoundingBox().GetCentroid(),
                                                                    labelStyle, viewport, labelStyle.GetLabelText(feature)));
            }
            else
            {
                var renderedGeometry = feature.RenderedGeometry.ContainsKey(style)
                    ? feature.RenderedGeometry[style] as Shape
                    : null;
                if (renderedGeometry == null)
                {
                    renderedGeometry = RenderGeometry(viewport, style, feature, brushCache);
                    if (!rasterizing)
                    {
                        feature.RenderedGeometry[style] = renderedGeometry;
                    }
                }
                else
                {
                    PositionGeometry(renderedGeometry, viewport, style, feature);
                }

                if (!canvas.Children.Contains(renderedGeometry))
                {
                    // Adding twice can happen when a single feature has two identical styles
                    canvas.Children.Add(renderedGeometry);
                }
            }
        }
Beispiel #4
0
        private static Canvas RenderVectorLayer(IViewport viewport, ILayer layer, bool rasterizing = false)
        {
            // todo:
            // find solution for try catch. Sometimes this method will throw an exception
            // when clearing and adding features to a layer while rendering
            var canvas = new Canvas
            {
                Opacity          = layer.Opacity,
                IsHitTestVisible = false
            };

            try
            {
                var features    = layer.GetFeaturesInView(viewport.Extent, viewport.RenderResolution).ToList();
                var layerStyles = BaseLayer.GetLayerStyles(layer);
                var brushCache  = new BrushCache();

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

                    foreach (var feature in features)
                    {
                        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;
                        }

                        RenderFeature(viewport, canvas, feature, style, rasterizing, brushCache);
                    }
                }

                foreach (var feature in features)
                {
                    var styles = feature.Styles ?? Enumerable.Empty <IStyle>();
                    foreach (var style in styles)
                    {
                        if (feature.Styles != null && style.Enabled)
                        {
                            RenderFeature(viewport, canvas, feature, style, rasterizing, brushCache);
                        }
                    }
                }

                return(canvas);
            }
            catch (Exception)
            {
                return(canvas);
                //If exception happens inside RenderFeature function after at-least one child has been added to the canvas,
                //returning new canvas will leave the previously created (but not yet added to parent canvas) canvas abandoned, that will
                //cause the exception when resuing RenderedGeometry object, because at-least one RenderedGeometry was attached to that abandoned canvas.
                //returning the same canvas will solve this error, as it will be clear this canvas childs on next render call.
                //return new Canvas { IsHitTestVisible = false };
            }
        }
Beispiel #5
0
        public static XamlBrush MapsuiBrushToXaml(Styles.Brush brush, BrushCache brushCache = null)
        {
#if !NETFX_CORE
            if (brush == null)
            {
                return(null);
            }
            switch (brush.FillStyle)
            {
            case FillStyle.Cross:
                return(CreateHatchBrush(brush, 12, 10, new List <Geometry> {
                    Geometry.Parse("M 0 0 l 10 10"), Geometry.Parse("M 0 10 l 10 -10")
                }));

            case FillStyle.BackwardDiagonal:
                return(CreateHatchBrush(brush, 10, 10, new List <Geometry> {
                    Geometry.Parse("M 0 10 l 10 -10"), Geometry.Parse("M -0.5 0.5 l 10 -10"), Geometry.Parse("M 8 12 l 10 -10")
                }));

            case FillStyle.Bitmap:
                return(CreateImageBrush(brush, brushCache));

            case FillStyle.Dotted:
                return(DottedBrush(brush));

            case FillStyle.DiagonalCross:
                return(CreateHatchBrush(brush, 10, 10, new List <Geometry> {
                    Geometry.Parse("M 0 0 l 10 10"), Geometry.Parse("M 0 10 l 10 -10")
                }));

            case FillStyle.ForwardDiagonal:
                return(CreateHatchBrush(brush, 10, 10, new List <Geometry> {
                    Geometry.Parse("M -1 9 l 10 10"), Geometry.Parse("M 0 0 l 10 10"), Geometry.Parse("M 9 -1 l 10 10")
                }));

            case FillStyle.Hollow:
                return(new SolidColorBrush(Colors.Transparent));

            case FillStyle.Horizontal:
                return(CreateHatchBrush(brush, 10, 10, new List <Geometry> {
                    Geometry.Parse("M 0 5 h 10")
                }));

            case FillStyle.Solid:
                return(new SolidColorBrush(brush.Color != null ? brush.Color.ToXaml() : brush.Background != null ? brush.Color.ToXaml() : Colors.Transparent));

            case FillStyle.Vertical:
                return(CreateHatchBrush(brush, 10, 10, new List <Geometry> {
                    Geometry.Parse("M 5 0 l 0 10")
                }));

            default:
                return((brush.Color != null) ? new SolidColorBrush(brush.Color.ToXaml()) : null);
            }
#else
            return(new SolidColorBrush(brush.Color.ToXaml()));
#endif
        }
Beispiel #6
0
        private static Canvas RenderVectorLayer(IViewport viewport, ILayer layer, bool rasterizing = false)
        {
            // todo:
            // find solution for try catch. Sometimes this method will throw an exception
            // when clearing and adding features to a layer while rendering
            try
            {
                var canvas = new Canvas
                {
                    Opacity          = layer.Opacity,
                    IsHitTestVisible = false
                };

                var features    = layer.GetFeaturesInView(viewport.Extent, viewport.RenderResolution).ToList();
                var layerStyles = BaseLayer.GetLayerStyles(layer);
                var brushCache  = new BrushCache();

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

                    foreach (var feature in features)
                    {
                        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;
                        }

                        RenderFeature(viewport, canvas, feature, style, rasterizing, brushCache);
                    }
                }

                foreach (var feature in features)
                {
                    var styles = feature.Styles ?? Enumerable.Empty <IStyle>();
                    foreach (var style in styles)
                    {
                        if (feature.Styles != null && style.Enabled)
                        {
                            RenderFeature(viewport, canvas, feature, style, rasterizing, brushCache);
                        }
                    }
                }

                return(canvas);
            }
            catch (Exception e)
            {
                return(new Canvas {
                    IsHitTestVisible = false
                });
            }
        }
Beispiel #7
0
        private static XamlShapes.Path CreatePolygonPath(VectorStyle style, double resolution, BrushCache brushCache = null)
        {
            var path = new XamlShapes.Path();

            if (style.Outline != null)
            {
                path.Stroke          = new XamlMedia.SolidColorBrush(style.Outline.Color.ToXaml());
                path.StrokeThickness = style.Outline.Width * resolution;
                path.StrokeDashArray = style.Outline.PenStyle.ToXaml();
                path.Tag             = style.Outline.Width; // see #linewidthhack
            }

            path.Fill             = style.Fill.ToXaml(brushCache);
            path.IsHitTestVisible = false;
            return(path);
        }
Beispiel #8
0
        public static XamlShapes.Shape RenderPolygon(Polygon polygon, IStyle style, IViewport viewport, BrushCache brushCache = null)
        {
            if (!(style is VectorStyle))
            {
                throw new ArgumentException("Style is not of type VectorStyle");
            }
            var vectorStyle = style as VectorStyle;

            XamlShapes.Path path = CreatePolygonPath(vectorStyle, viewport.Resolution, brushCache);
            path.Data = polygon.ToXaml();

            var matrixTransform = new XamlMedia.MatrixTransform {
                Matrix = CreateTransformMatrix1(viewport)
            };

            path.RenderTransform = matrixTransform;

            if (path.Fill != null)
            {
                path.Fill.Transform = matrixTransform.Inverse as XamlMedia.MatrixTransform;
            }
            path.UseLayoutRounding = true;
            return(path);
        }
Beispiel #9
0
        private static XamlShapes.Shape CreateSymbolFromBitmap(int bmpId, double opacity, BrushCache brushCache = null)
        {
            XamlMedia.ImageBrush imageBrush;

            if (brushCache == null)
            {
                var data        = BitmapRegistry.Instance.Get(bmpId);
                var bitmapImage = data.CreateBitmapImage();
                imageBrush = new XamlMedia.ImageBrush {
                    ImageSource = bitmapImage
                };
            }
            else
            {
                imageBrush = brushCache.GetImageBrush(bmpId);
            }

            var width  = imageBrush.ImageSource.Width;
            var height = imageBrush.ImageSource.Height;

            var path = new XamlShapes.Path
            {
                Data = new XamlMedia.RectangleGeometry
                {
                    Rect = new Rect(-width * 0.5, -height * 0.5, width, height)
                },
                Fill    = imageBrush,
                Opacity = opacity
            };

            return(path);
        }
 public static Media.Brush ToXaml(this Brush brush, BrushCache brushCache = null)
 {
     return(StyleConverter.MapsuiBrushToXaml(brush, brushCache));
 }
Beispiel #11
0
        private static XamlShapes.Shape CreateSymbolFromBitmap(int bmpId, double opacity, BrushCache brushCache = null)
        {
            XamlMedia.ImageBrush imageBrush;

            if (brushCache == null)
            {
                var data        = BitmapRegistry.Instance.Get(bmpId);
                var bitmapImage = data.CreateBitmapImage();
                imageBrush = new XamlMedia.ImageBrush {
                    ImageSource = bitmapImage
                };
            }
            else
            {
                imageBrush = brushCache.GetImageBrush(bmpId);
            }

#if NETFX_CORE
            var width  = ((BitmapImage)imageBrush.ImageSource).PixelWidth;
            var height = ((BitmapImage)imageBrush.ImageSource).PixelHeight;
#else
// note: It probably makes more sense to use PixelWith here:
            var width  = imageBrush.ImageSource.Width;
            var height = imageBrush.ImageSource.Height;
#endif

            var path = new XamlShapes.Path
            {
                Data = new XamlMedia.RectangleGeometry
                {
                    Rect = new Rect(-width * 0.5, -height * 0.5, width, height)
                },
                Fill    = imageBrush,
                Opacity = opacity
            };

            return(path);
        }
Beispiel #12
0
 private static ImageBrush CreateImageBrush(Styles.Brush brush, BrushCache brushCache = null)
 {
     return(brushCache != null?brushCache.GetImageBrush(brush.BitmapId, CreateImageBrush) : CreateImageBrush(BitmapRegistry.Instance.Get(brush.BitmapId)));
 }