CreatePolygonShapefile.cs

This example demonstrates how to create a polygon shapefile by placing 100 circles randomly. Here is a screenshot with the results of the code execution.

using System;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
using System.Diagnostics;
namespace Examples
{
public partial class MapExamples
{
public void CreatePolygonShapefile(AxMap axMap1)
{
axMap1.Projection = tkMapProjection.PROJECTION_NONE;
var sf = new Shapefile();
bool result = sf.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON);
if (!result)
{
MessageBox.Show(sf.ErrorMsg[sf.LastErrorCode]);
}
else
{
double xMin = 0.0;
double yMin = 0.0;
double xMax = 1000.0;
double yMax = 1000.0;
var rnd = new Random(DateTime.Now.Millisecond);
int fldX = sf.EditAddField("x", FieldType.DOUBLE_FIELD, 9, 12);
int fldY = sf.EditAddField("y", FieldType.DOUBLE_FIELD, 9, 12);
int fldArea = sf.EditAddField("area", FieldType.DOUBLE_FIELD, 9, 12);
// In a loop we are creating 100 different points using the box established above.
for (int i = 0; i < 100; i++)
{
double xCenter = xMin + (xMax - xMin) * rnd.NextDouble();
double yCenter = yMin + (yMax - yMin) * rnd.NextDouble();
// random radius from 10 to 100
double radius = 10 + rnd.NextDouble() * 90;
// polygons must be clockwise
Shape shp = new Shape();
shp.Create(ShpfileType.SHP_POLYGON);
for (int j = 0; j < 37; j++)
{
Point pnt = new Point();
pnt.x = xCenter + radius * Math.Cos(j * Math.PI / 18);
pnt.y = yCenter - radius * Math.Sin(j * Math.PI / 18);
shp.InsertPoint(pnt, ref j);
}
Debug.Print(shp.Extents.ToDebugString());
sf.EditInsertShape(shp, ref i);
sf.EditCellValue(fldX, i, xCenter.ToString());
sf.EditCellValue(fldY, i, yCenter.ToString());
sf.EditCellValue(fldArea, i, Math.PI * radius * radius);
}
int handle = axMap1.AddLayer(sf, true);
string extents = sf.Extents.ToDebugString();
axMap1.ZoomToLayer(handle);
sf.Categories.Generate(fldArea, tkClassificationType.ctNaturalBreaks, 7);
ColorScheme scheme = new ColorScheme();
scheme.SetColors2(tkMapColor.Wheat, tkMapColor.Salmon);
axMap1.Redraw();
// save if needed
//sf.SaveAs(@"c:\polygons.shp", null);
}
}
}
}
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
ShpfileType
The type of the shapefile.
Definition: Enumerations.cs:169
FieldType
The available types of fields of dbf table.
Definition: Enumerations.cs:34
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
void SetColors2(tkMapColor Color1, tkMapColor Color2)
Clears all the existing breaks and creates 2 breaks with the specified colours.
Definition: ColorScheme.cs:156
string ToDebugString()
Gets serialized contents of the extents for debug purposes.
Definition: Extents.cs:172
A point object represents a point with x, y, Z, and M values. Shapes created by adding point objects ...
Definition: PointClass.cs:38
double y
Gets or sets the y value of the point.
Definition: PointClass.cs:122
double x
Gets or sets the x value of the point.
Definition: PointClass.cs:113
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
Extents Extents
Gets the extents of the shape.
Definition: Shape.cs:286
bool Create(ShpfileType shpType)
Creates a new shape of the specified type.
Definition: Shape.cs:154
bool InsertPoint(Point newPoint, ref int pointIndex)
Inserts the specified point object into the shape using the desired point index if possible.
Definition: Shape.cs:372
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
void ZoomToLayer(int layerHandle)
Zooms the map display to the specified layer.
Definition: AxMap.cs:643
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342