AddCategoryRange.cs

This example demonstrates how to calculate area of polygons and to set 3 ranges of categories with different symbology. This example demonstrates quite elaborate specification of categories. In most cases much easier approaches can be used (see ShapefileCategories.Generate). Here is a screenshot with the results of the code execution.

// ReSharper disable ArrangeAccessorOwnerBody
// ReSharper disable DelegateSubtraction
// ReSharper disable PossibleInvalidCastExceptionInForeachLoop
// ReSharper disable CheckNamespace
using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
// ReSharper disable SuggestVarOrType_BuiltInTypes
// ReSharper disable SuggestVarOrType_SimpleTypes
namespace Examples
{
public partial class MapExamples
{
// <summary>
// Calculates area of polygons and sets 3 range of categories with different symbology
// </summary>
public void AddCategoryRange(AxMap axMap, string dataPath)
{
axMap.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR;
string filename = dataPath + "landuse.shp";
if (File.Exists(filename) == false)
{
MessageBox.Show(@"Failed to open file: " + filename);
return;
}
Shapefile sf = new Shapefile();
if (!sf.Open(filename)) return;
if (!sf.StartEditingTable())
{
MessageBox.Show(@"Failed to open editing mode.");
return;
}
int fieldIndex = sf.Table.FieldIndexByName["Area"];
if (fieldIndex == -1)
fieldIndex = sf.EditAddField("Area", FieldType.DOUBLE_FIELD, 15, 18);
for (int i = 0; i < sf.NumShapes; i++)
{
double area = sf.Shape[i].Area * 100000.0;
sf.EditCellValue(fieldIndex, i, area);
}
// adding to map
int handle = axMap.AddLayer(sf, true);
sf = axMap.get_Shapefile(handle); // in case a copy of shapefile was created by GlobalSettings.ReprojectLayersOnAdding
double mean = sf.Table.MeanValue[fieldIndex];
double stDev = sf.Table.StandardDeviation[fieldIndex];
double min = (double)sf.Table.MinValue[fieldIndex];
double max = (double)sf.Table.MaxValue[fieldIndex];
var scheme = new ColorScheme();
// 1. the first range [min; mean - stDev]
Utils utils = new Utils();
sf.Categories.AddRange(fieldIndex, tkClassificationType.ctNaturalBreaks, 5, min, mean);
scheme.SetColors2(tkMapColor.Red, tkMapColor.Yellow);
// apply colors 0 and 4 are indices of categories, since 5 categories were added - from 0 to 4
sf.Categories.ApplyColorScheme3(tkColorSchemeType.ctSchemeRandom, scheme, tkShapeElements.shElementFill, 0, 4);
// 2. the second range [mean - stDev; mean + stDev]
// the default drawing options will be copied to the new categories
sf.Categories.AddRange(fieldIndex, tkClassificationType.ctEqualIntervals, 5, mean, mean + stDev);
scheme.SetColors2(tkMapColor.Green, tkMapColor.Blue);
sf.Categories.ApplyColorScheme3(tkColorSchemeType.ctSchemeGraduated, scheme, tkShapeElements.shElementFill, 5, 9);
// 3. the third range [mean + stDev; max]
// the default drawing options will be copied to the new categories
sf.Categories.AddRange(fieldIndex, tkClassificationType.ctEqualIntervals, 5, mean + stDev, max);
scheme.SetColors2(tkMapColor.Pink, tkMapColor.Violet);
sf.Categories.ApplyColorScheme3(tkColorSchemeType.ctSchemeGraduated, scheme, tkShapeElements.shElementFill, 10, 14);
// apply expresions should be called exlicitly
axMap.Redraw();
// saving options to see categories desription in XML
axMap.SaveLayerOptions(handle, "categories_sample", true, "");
}
// <summary>
// To apply the same options on the next loading
// </summary>
private void RestoreCategories(AxMap axMap, string dataPath)
{
string filename = dataPath + "landuse.shp";
Shapefile sf = new Shapefile();
if (!sf.Open(filename)) return;
int handle = axMap.AddLayer(sf, true);
string description = "";
axMap.LoadLayerOptions(handle, "categories_sample", ref description);
}
}
}
tkShapeElements
The elements of the symbology for a single shape with individual colors.
Definition: Enumerations.cs:1323
tkClassificationType
The type of the classification available for ShapefileCategories.Generate and Labels....
Definition: Enumerations.cs:227
tkMapColor
A list of named constants for some of the well-known colors.
Definition: Enumerations.cs:951
tkColorSchemeType
The type of color scheme. Determines how colors will be extracted from the color scheme (see Shapefil...
Definition: Enumerations.cs:305
tkFillType
The type of shape fill.
Definition: Enumerations.cs:521
FieldType
The available types of fields of dbf table.
Definition: Enumerations.cs:34
tkGDIPlusHatchStyle
The hatch style available for drawing of fill for polygons and point symbols.
Definition: Enumerations.cs:567
tkMapProjection
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
Map component for visualization of vector, raster or grid data.
Definition: AxMap.cs:56
void Redraw()
Redraws all layers in the map if the map is not locked.
Definition: AxMap.cs:183
Provides methods for random colour generation and colour interpolation based on the specific set of c...
Definition: ColorScheme.cs:65
tkGDIPlusHatchStyle FillHatchStyle
The hatch style of the fill for point symbols and polygon shapes.
Definition: ShapeDrawingOptions.cs:510
tkFillType FillType
The type of the polygon fill. See the enumeration for details.
Definition: ShapeDrawingOptions.cs:539
uint FillColor2
Gets or sets the end color of the gradient fill.
Definition: ShapeDrawingOptions.cs:481
void ApplyColorScheme3(tkColorSchemeType Type, ColorScheme ColorScheme, tkShapeElements ShapeElement, int CategoryStartIndex, int CategoryEndIndex)
Applies color scheme to the visualization categories.
Definition: ShapefileCategories.cs:118
bool AddRange(int FieldIndex, tkClassificationType ClassificationType, int numClasses, object MinValue, object MaxValue)
Creates the specified number of visualization categories and expressions for them.
Definition: ShapefileCategories.cs:84
void ApplyExpressions()
Maps shapes to the visualization categories based in ShapefileCategory.Expression.
Definition: ShapefileCategories.cs:143
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
int NumShapes
Gets the number of shapes in the shapefile.
Definition: Shapefile.cs:254
ShapeDrawingOptions DefaultDrawingOptions
Gets or sets an instance of ShapeDrawingOptions class which holds default drawing options.
Definition: Shapefile.cs:111
ShapefileCategories Categories
Gets or sets an instance of ShapefileCategories class associated with the shapefile.
Definition: Shapefile.cs:81
A utils object provides access to a set of utility functions to perform a variety of tasks on other o...
Definition: Utils.cs:20
uint ColorByName(tkMapColor Name)
Returns the numeric representation for the specified color.
Definition: Utils.cs:40
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
Shapefile get_Shapefile(int layerHandle)
Gets shapefile object associated with the layer.
Definition: AxMap.cs:1546
bool SaveLayerOptions(int layerHandle, string optionsName, bool overwrite, string description)
Saves settings of the layer to the file.
Definition: AxMap.cs:896
bool LoadLayerOptions(int layerHandle, string optionsName, ref string description)
Applies to the layer a set of settings from file with the specified name.
Definition: AxMap.cs:868
bool Open(string shapefileName, ICallback cBack)
Opens shapefile from the disk.
Definition: Shapefile.cs:1430
int EditAddField(string name, FieldType type, int precision, int width)
Adds a field to the attribute table of the shapefile. The table must be in editing mode.
Definition: Shapefile.cs:708
Table Table
Gets the reference to the attribute table associated with the shapefile.
Definition: Shapefile.cs:677
bool EditCellValue(int fieldIndex, int shapeIndex, object newVal)
Sets the new value for particular cell in attribute table. The table must be in editing mode.
Definition: Shapefile.cs:633
bool StartEditingTable(ICallback cBack)
Opens editing mode for the attribute table.
Definition: Shapefile.cs:684