[How-to] Colorize individual shapes

Coordinator
Aug 2, 2013 at 3:49 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 5, 2013 at 5:26 PM
Beautiful!!! I was saving the index of the category after I created that and using it for individual shape assignment. With this method I just need the category name. Great work.
Aug 7, 2013 at 6:26 AM
thank you, these new methods are very usefull for me. I've been looking this for weeks.
I am new for MapWinGis and VB.Net.
Now I'm working on final project.
This project is about vulnerability areas to the disaster.
I have .mdb stores information about the each area.
there will be query to calculate the areas to get the final result whether those area are vulnerable or not using 3 categories : High, moderate and low.
High = 3
Moderate = 2
Low = 1
The vulnerable values will be stored in .dbf or .mdb

My questions are:
  1. can I display the categories on map using data from .db
  2. how to display categories by call the values from .dbf, for example: Category 1 colored by blue, 2 by yellow, 3 by red.
I really hope that you can help me on this. I really appreciate for your help.

sincerely

-winid-
Coordinator
Aug 7, 2013 at 1:18 PM
You can join your mdb with your shapefile and create categories based on fields of the joined table.
I justed show these methods are so new the documentation isn't up-to-date yet, but you can see an implementation in the table editor:
http://svn.mapwindow.org/svnroot/MapWindow4Dev/TableEditorNew
You need to look for sf.table.join

Paul
Aug 7, 2013 at 3:25 PM
thx for your suggestion, Paul.
I don't know where to find it on your link.
I still dont understand how to make it.
Can you show me an example, please?

-winid-
Aug 19, 2013 at 11:27 PM
Thank You for the code example Paul. I'm using Version 4.8.8 SR on my PC, and trying to get to grips with the categories side of things, and this is the best VB example I could find in order to transpose the code for use in a Microsoft Access form's VBA code

The problem I get is, when getting to the line below, I get a runtime error 438, Object doesn't support this property or method. Am I missing an obvious transposition between VB.Net & VBA, or is there a compatibility issue?
        ctOrange = SfWaypoints.Categories.Add("Orange")
The complete code as I've edited it on my system is as follows :
    Dim utls As New Utils
    Dim i as integer
    Dim ctOrange As New ShapefileCategory
        ctOrange = SfWaypoints.Categories.Add("Orange")
        ctOrange.DrawingOptions.FillColor = utls.ColorByName(tkMapColor.Orange)
        Dim ctBlue As New ShapefileCategory
        ctBlue = SfWaypoints.Categories.Add("Blue")
        ctBlue.DrawingOptions.FillColor = utls.ColorByName(tkMapColor.Blue)

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


    AxMap.AddLayer SfWaypoints, True
Thanks in advance,
PG
Aug 20, 2013 at 4:22 PM

Try this:

Set ctOrange = SfWaypoints.Categories.Add("Orange")

Douglas J. Kelly L.G., L.HG.

Hydrogeologist

Island County Environmental Health

(360) 678-7885

Aug 20, 2013 at 5:53 PM
IslandHydro wrote:
Try this: Set ctOrange = SfWaypoints.Categories.Add("Orange") Douglas J. Kelly L.G., L.HG. Hydrogeologist Island County Environmental Health (360) 678-7885
You, Sir, are a true gent.. One word and my problem is solved :) Thanks for the pointer ;)