This example demonstrates how to split a shapefile into several shapefiles according the values of the given attribute. For each unique value of the specified field a new shapefile will be created. Then each shape of the input shapefile will be copied to the one of the newly created shapefiles. To determine the list of the unique value generation of shapefile categories is made. Finally all new shapefiles are added to the map. A common color scheme is applied to them using ColorScheme class. Note: generally it is not needed split a shapefile into parts for applying color scheme. More likely use of such approach is splitting large shapefiles into several parts for faster rendering. Here is a screenshot with the results of the code execution.

using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
public partial class MapExamples
// <summary>
// Split a shapefile into several ones according the values the specified attribute.
// </summary>
public void SplitByAttribute(AxMap axMap1, string dataPath)
axMap1.Projection = tkMapProjection.PROJECTION_NONE;
axMap1.GrabProjectionFromData = true;
string filename = dataPath + "natural.shp";
if (!File.Exists(filename))
MessageBox.Show("Couldn't file the file: " + filename);
Shapefile sf = new Shapefile();
sf.Open(filename, null);
int fieldIndex = sf.Table.FieldIndexByName["type"];
sf.Categories.Generate(fieldIndex, tkClassificationType.ctUniqueValues, 0);
ColorScheme scheme = new ColorScheme();
scheme.SetColors2(tkMapColor.White, tkMapColor.Black);
for (int i = 0; i < sf.Categories.Count; i++)
Shapefile sfNew = sf.Clone();
int layerHandle = axMap1.AddLayer(sfNew, true);
for (int shapeIndex = 0; shapeIndex < sf.NumShapes; shapeIndex++)
if (sf.ShapeCategory[shapeIndex] == i)
Shape shape = sf.Shape[shapeIndex].Clone();
int index = sfNew.NumShapes;
sfNew.EditInsertShape(shape, ref index);
ShapefileCategory category = sf.Categories.Item[i];
string name = category.Name.Substring(category.Name.IndexOf("=") + 1);
uint color = scheme.get_RandomColor((i + 1) / sf.Categories.Count);
axMap1.set_LayerName(layerHandle, name);
//sfNew.SaveAs(path + name + ".shp", null); // saves shapefile
// <summary>
// Shows the names of layers on the drawing layer
// </summary>
private void ShowLegend()
int width = 40;
int height = 20;
int padding = 5;
int drawHandle = axMap1.NewDrawing(tkDrawReferenceList.dlScreenReferencedList);
Labels labels = axMap1.get_DrawingLabels(drawHandle);
if (labels != null)
labels.Alignment = tkLabelAlignment.laBottomRight;
Shapefile sf = new Shapefile();
for (int i = 0; i < axMap1.NumLayers; i++)
int layerHandle = axMap1.get_LayerHandle(i);
sf = axMap1.get_Shapefile(layerHandle);
// adds rectangle
object x, y;
int top = padding + i * (height + padding);
this.getRectange(padding, top, width, height, out x, out y);
axMap1.DrawPolygonEx(drawHandle, ref x, ref y, 4, sf.DefaultDrawingOptions.FillColor, true);
// adds text
string text = axMap1.get_LayerName(layerHandle) + ".shp";
var dlbls = axMap1.get_DrawingLabels(drawHandle);
if (dlbls != null)
dlbls.AddLabel(text, padding * 2 + width, top + padding);
// the position of text (for debugging)
axMap1.DrawPointEx(drawHandle, padding * 2 + width, top, 2, 255);
// <summary>
// Returns coordinates of the rectangle with specified size and position
// </summary>
private void getRectange(int left, int top, int width, int height, out object xArr, out object yArr)
double[] x = new double[4];
double[] y = new double[4];
x[0] = left;
x[1] = left;
x[2] = left + width;
x[3] = left + width;
y[0] = top;
y[1] = top + height;
y[2] = top + height;
y[3] = top;
xArr = x; yArr = y;
The available alignments for the labels. Defines both horizontal and vertical alignment.
Definition: Enumerations.cs:786
The type of the classification available for ShapefileCategories.Generate and Labels....
Definition: Enumerations.cs:227
A list of named constants for some of the well-known colors.
Definition: Enumerations.cs:951
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
The type of spatial reference for the drawing layer.
Definition: Enumerations.cs:484
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
uint get_RandomColor(double Value)
Generates random colour based on the input value.
Definition: ColorScheme.cs:236
void SetColors2(tkMapColor Color1, tkMapColor Color2)
Clears all the existing breaks and creates 2 breaks with the specified colours.
Definition: ColorScheme.cs:156
Represents a list of labels of the map layer and their visualization options.
Definition: Labels.cs:51
uint FillColor
Gets or sets the fill color of the shape.
Definition: ShapeDrawingOptions.cs:471
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
int Count
Returns the number of the categories in the list.
Definition: ShapefileCategories.cs:169
void ApplyExpressions()
Maps shapes to the visualization categories based in ShapefileCategory.Expression.
Definition: ShapefileCategories.cs:143
bool Generate(int FieldIndex, tkClassificationType ClassificationType, int numClasses)
Generates visualization categories by certain attribute
Definition: ShapefileCategories.cs:190
Represents a set of visualization options for shapefile layer.
Definition: ShapefileCategory.cs:47
string Name
Gets or sets the name of the category. The names must not be unique.
Definition: ShapefileCategory.cs:70
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
void AddLabel(string Text, double x, double y, double offsetX, double offsetY, double Rotation, int Category)
Adds a new label as the last one in the list.
Definition: Labels.cs:438
tkLabelAlignment Alignment
Sets horizontal and vertical alignment of labels.
Definition: Labels.cs:744
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
bool GrabProjectionFromData
Gets or sets a value indicating whether projection for will be taken from the first datasource added ...
Definition: AxMap.cs:2701
Labels get_DrawingLabels(int drawingLayerIndex)
Gets labels associated with the drawing layer.
Definition: AxMap.cs:1659
void DrawPointEx(int layerHandle, double x, double y, int pixelSize, uint color, byte alpha=255)
Draws a point on the specified drawing layer.
Definition: AxMap.cs:1766
int NewDrawing(tkDrawReferenceList projection)
Creates a new drawing layer on the map returning its handle.
Definition: AxMap.cs:1868
void DrawPolygonEx(int layerHandle, ref object xPoints, ref object yPoints, int numPoints, uint color, bool fill, byte alpha=255)
Draws a polygon on the specified drawing layer.
Definition: AxMap.cs:1795
void ZoomToMaxExtents()
Zooms the map to the maximum extents of all loaded layers.
Definition: AxMap.cs:652
int get_LayerHandle(int layerPosition)
Gets the handle of the layer at the given position in the map. Returns -1 if there is no layer at the...
Definition: AxMap.cs:1352
int NumLayers
Gets the number of layers loaded in the map.
Definition: AxMap.cs:1422
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
string get_LayerName(int layerHandle)
Gets the name of the specified layer.
Definition: AxMap.cs:1158
void set_LayerName(int layerHandle, string newValue)
Sets the name of the specified layer.
Definition: AxMap.cs:1168
bool EditInsertShape(Shape shape, ref int shapeIndex)
Inserts a new shape in the shapefile.
Definition: Shapefile.cs:819
Shapefile Clone()
Creates a copy of the shapefile.
Definition: Shapefile.cs:1376
bool Open(string shapefileName, ICallback cBack)
Opens shapefile from the disk.
Definition: Shapefile.cs:1430
Table Table
Gets the reference to the attribute table associated with the shapefile.
Definition: Shapefile.cs:677