[How-to] Colorize individual shapes

Coordinator
Aug 2, 2013 at 3:50 PM
A lot of questions on the MapWindow/MapWinGIS forums are about the new categories. They are very powerful but for some simple tasks it was not so easy.

A few days ago Sergei introduced a few new methods to make it easier to assign a custom category to a shape.
These new methods made it in the Stable Release which I published just a minute ago. Go to the download section of this site.

We've created a C# example and a VB.NET example.
The C# version can be downloaded here: http://svn.mapwindow.org/svnroot/TestingScripts/TestColorUniqueShapes.cs
The VB.NET code is:
    Dim sf As Shapefile = New Shapefile()

    If sf.Open("C:\Users\Meems\Desktop\United States\Shapefiles\lakes.shp") Then
      Dim utls As Utils = New Utils()

      Dim ctOrange As ShapefileCategory = sf.Categories.Add("Orange")
      ctOrange.DrawingOptions.FillColor = utls.ColorByName(tkMapColor.Orange)

      Dim ctBlue As ShapefileCategory = sf.Categories.Add("Blue")
      ctBlue.DrawingOptions.FillColor = utls.ColorByName(tkMapColor.Blue)

      For i As Integer = 0 To sf.NumShapes - 1
        If i Mod 2 = 0 Then
          sf.ShapeCategory2(i) = "Orange"
        Else
          sf.ShapeCategory3(i) = ctBlue
        End If
      Next i

      For i As Integer = 0 To sf.NumShapes - 1
        System.Diagnostics.Debug.Print("Shape category: {0}, {1}", i, sf.ShapeCategory2(i))
      Next i

      For i As Integer = 0 To sf.NumShapes - 1
        System.Diagnostics.Debug.Print("Shape category: {0}, {1}", i, sf.ShapeCategory3(i).Name)
      Next i

      ' Add the data to the map:
      AxMap1.AddLayer(sf, True)
    End If
Please download the latest installer and update your project.
If you have questions, please post them here and we'll try to answer them.
Aug 7, 2013 at 12:07 AM
Edited Aug 7, 2013 at 12:22 AM
To further Paul's email and to answer my own question on the Forum re changing a single shape's outline:

The documentation reports the C# call Shapefile.set_ShapeCategory (int ShapeIndex, int pVal ) .

It is good to know however, that in vb.net you skip the set_ en get_ prefixes of the functions (or at least in VS2008) and that the call format is different as well. Btw, there are now two more options to change a single shape visualisation attribute: set_ShapeCategory2 and set_ShapeCategory3 with different call lists.
So, to set/change the outline of a poly in VB.Net, you could use the following code:
........
Dim shpfile As New MapWinGIS.Shapefile
shpfile.Open(ShpPath, Me)

Dim ShapeIndex as integer = 5  'pick a shape in the shapefile
Dim utils As MapWinGIS.Utils = New MapWinGIS.Utils
Dm ct ShapefileCategory = sf.Categories.Add("SomeName")
   
 ct.DrawingOptions.FillColor = utils.ColorByName(MapWinGIS.tkMapColor.Blue)
 ct.DrawingOptions.LineColor = utils.ColorByName(MapWinGIS.tkMapColor.Red)
 ct.DrawingOptions.LineWidth = 4

shpfile.ShapeCategory3(ShapeIndex) = ct
AxMap1.AddLayer(shpfile, True)
AxMap1.Redraw()
Please note that the category name ("SomeName" in the code example) is not checked for uniqueness and this could cause problems if used in the ShapeCategory2 call.

Thanks to Paul and Sergei for their help in resolving this issue.