Layer Inheritance and Casting

The most basic idea of a layer is that it combines the data being displayed with the symbology (or scheme) that should be used to represent that dataset. MapWindow 4.x just placed all the properties and methods directly on a single class, but this can create confusion from the fact that most of the methods available on the class would not actually do anything with the specific layer in hand. The model we are using in MapWindow 6.0 uses inheritance to allow the layer classes to share some of the content while the methods, properties or events that only work in some cases are placed in sub-classes. The diagram below illustrates the hierarchy for layers and also shows some example code for casting a layer from the map to the appropriate subclass.


private void SomeBasicsForAllLayers()
    // A layer combines data, symbology, and drawing functions.
    // Some things can be controlled directly with the layer:
    ILayer layer = map1.Layers[0];

    // Feature Layers only share some general symbology, but give
    // full access to the vector featrueset on the dataset.
    IFeatureLayer fl = layer as IFeatureLayer; 
    // Safe casting may produce a null value
    if(fl != null)
        // The dataset at this stage is automatically an IFeatureSet.
        IFeatureSet fs = fl.DataSet;

        // Further Subtypes include Line, Point, and Polygon layers
        IPointLayer pointl = fl as IPointLayer;
        if(pointl != null)
            // point schemes control multiple categories of description with filter expressions for each category
            IPointScheme pscheme = pointl.Symbology;
            // A point symbolizers are a shortcut to Symbology.Categories[0].Symbolizer
            IPointSymbolizer psymb = pointl.Symbolizer;
        // The same works for Lines
        ILineLayer ll = fl as ILineLayer;
        if(ll != null)
            ILineScheme lscheme = ll.Symbology;
            ILineSymbolizer ls = ll.Symbolizer;
        // And Polygons
        IPolygonLayer pl = fl as IPolygonLayer;
        if(pl != null)
            IPolygonScheme pscheme = pl.Symbology;
            IPolygonSymbolizer ps = pl.Symbolizer;
        // You will not find label layers directly in the map layers collection.
        // Instead they belong directly to a feature layer.
        fl.LabelLayer.Symbology.Categories[0].Expression = "[" + fs.DataTable.Columns[0].ColumnName + "]";
    // Raster Layers have an IRaster for the DataSet, instead of an IFeatureSet
    IRasterLayer rl = layer as IRasterLayer;
    if(rl != null)
        // The IRaster interface allows easy access to the data values.
        IRaster r = rl.DataSet;
        // There is only one raster symbolizer and it controls the basic properties
        IRasterSymbolizer symbolizer = rl.Symbolizer;
        // The color scheme lists the color settings
        IColorScheme scheme = symbolizer.Scheme;

    // Image layers work 
    IImageLayer il = layer as IImageLayer;
    if(il != null)
        // Image data controls the data access for the colors at a pixel location,
        // or else provides direct access to a byte array representing the image.
        IImageData id = il.DataSet;

        // A bitmap retrieval method automatically scales the representation
        // for the specified geographic extent so that it works with the pixel
        // size of the specified client rectangle.
        Bitmap bmp = id.GetBitmap(map1.Extents, map1.ClientRectangle);

        // At the moment the symbolizer here is just a placeholder for potential future uses.

Last edited Mar 5, 2010 at 9:04 PM by Shade1974, version 4


No comments yet.