We've been privately mailing with Wolf and this is the code that is doing what was requested:
private void CreateCategories()
{
const string filename = @"D:\dev\GIS-Data\MyLayer.shp";
// Check file:
if (!File.Exists(filename))
{
MessageBox.Show(@"Failed to open file: " + filename);
return;
}
// Open shapefile:
var sf = new Shapefile();
if (!sf.Open(filename, null))
{
MessageBox.Show(@"Could not load shapefile. Reason: " + sf.ErrorMsg[sf.LastErrorCode]);
return;
}
// Get the field index of the field you want to create the categories for:
var fieldIndex = sf.Table.FieldIndexByName["MyFieldName"];
if (fieldIndex == -1)
{
MessageBox.Show(@"Can't find the field");
return;
}
// No tiles:
axMap1.TileProvider = tkTileProvider.ProviderNone;
// Clear previous layers:
axMap1.RemoveAllLayers();
// adding to map
var handle = axMap1.AddLayer(sf, true);
sf = axMap1.get_Shapefile(handle); // in case a copy of shapefile was created by GlobalSettings.ReprojectLayersOnAdding
// Use utils class to make chosing colors more easy:
var utils = new Utils();
// Set default drawing options before creating categories, to make sure all shapes are colored:
sf.DefaultDrawingOptions.FillType = tkFillType.ftStandard;
sf.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.BlueViolet);
// Create visualization categories
sf.DefaultDrawingOptions.FillType = tkFillType.ftStandard;
sf.Categories.Generate(fieldIndex, tkClassificationType.ctUniqueValues, 0);
sf.Categories.ApplyExpressions();
// Apply random color scheme
var scheme = new ColorScheme();
scheme.SetColors2(tkMapColor.LightBlue, tkMapColor.LightYellow);
sf.Categories.ApplyColorScheme(tkColorSchemeType.ctSchemeRandom, scheme);
// Change to colors based on the expression
for (var i = 0; i < sf.Categories.Count; i++)
{
var cat = sf.Categories.Item[i];
switch (cat.Expression)
{
case "[MyFieldName] = -1":
cat.DrawingOptions.FillColor = utils.ColorByName(tkMapColor.Red);
break;
case "[MyFieldName] = 0":
cat.DrawingOptions.FillColor = utils.ColorByName(tkMapColor.Green);
break;
case "[MyFieldName] = 1":
cat.DrawingOptions.FillColor = utils.ColorByName(tkMapColor.Blue);
break;
case "[MyFieldName] = 2":
cat.DrawingOptions.FillColor = utils.ColorByName(tkMapColor.Black);
break;
}
}
// Redraw map to see the changes:
axMap1.Redraw();
}