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 Sub SomeBasicsForAllLayers()
    ' A layer combines data, symbology, and drawing functions.
    ' Some things can be controlled directly with the layer:
    Dim layer As ILayer = map1.Layers(0)
    ' Feature Layers only share some general symbology, but give
    ' full access to the vector featrueset on the dataset.
    Dim fl As IFeatureLayer = TryCast(layer, IFeatureLayer)
    ' Safe casting may produce a null value
    If fl IsNot Nothing Then
        ' The dataset at this stage is automatically an IFeatureSet.
        Dim fs As IFeatureSet = fl.DataSet
        ' Further Subtypes include Line, Point, and Polygon layers
        Dim pointl As IPointLayer = TryCast(fl, IPointLayer)
        If pointl IsNot Nothing Then
            ' point schemes control multiple categories of description with filter expressions for each category
            Dim pscheme As IPointScheme = pointl.Symbology
            ' A point symbolizers are a shortcut to Symbology.Categories[0].Symbolizer
            Dim psymb As IPointSymbolizer = pointl.Symbolizer
        End If
        ' The same works for Lines
        Dim ll As ILineLayer = TryCast(fl, ILineLayer)
        If ll IsNot Nothing Then
            Dim lscheme As ILineScheme = ll.Symbology
            Dim ls As ILineSymbolizer = ll.Symbolizer
        End If
        ' And Polygons
        Dim pl As IPolygonLayer = TryCast(fl, IPolygonLayer)
        If pl IsNot Nothing Then
            Dim pscheme As IPolygonScheme = pl.Symbology
            Dim ps As IPolygonSymbolizer = pl.Symbolizer
        End If
        ' 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 & "]"
    End If
    ' Raster Layers have an IRaster for the DataSet, instead of an IFeatureSet
    Dim rl As IRasterLayer = TryCast(layer, IRasterLayer)
    If rl IsNot Nothing Then
        ' The IRaster interface allows easy access to the data values.
        Dim r As IRaster = rl.DataSet
        ' There is only one raster symbolizer and it controls the basic properties
        Dim symbolizer As IRasterSymbolizer = rl.Symbolizer
        ' The color scheme lists the color settings
        Dim scheme As IColorScheme = symbolizer.Scheme
    End If
    ' Image layers work 
    Dim il As IImageLayer = TryCast(layer, IImageLayer)
    If il IsNot Nothing Then
        ' 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.
        Dim id As IImageData = 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.
            ' At the moment the symbolizer here is just a placeholder for potential future uses.
        Dim bmp As Bitmap = id.GetBitmap(map1.Extents, map1.ClientRectangle)
    End If
End Sub

Last edited Mar 5, 2010 at 10:05 PM by Shade1974, version 2


No comments yet.