Map Suite has the ability to edit existing shapefiles, but what if you want to create your own?
The code below should get you started, but if you want to explore more shapefile editing check out our EditShapeFile sample application included with the evaluation download.
Below is a very simple example of creating a point based shapefile from scratch. It adds three records and fills the value for the field “Name”.
C#
Private DatabaseColumns Columns = new DatabaseColumns();
private DatabaseColumn Column1;
Column1.FieldType = FieldDataType.fdtString;Column1.Length = 15;
Column1.DecimalLength = 0;
Column1.Name = "Name";
Columns.Add(Column1);
Map1.CreateNewShapefile(ShapeTypeEnum.Point, "C:\\thinkgeo\\temp", "Test", Columns);
Layer Layer = new Layer("C:\\thinkgeo\\temp\\test.shp", false);
Layer.BeginEdit();
int Shape1 = Layer.AddShape(new PointShape(-98, 19));
Layer.UpdateTableData(1, "Name", "Record1");
int Shape2 = Layer.AddShape(new PointShape(-97, 15));
Layer.UpdateTableData(2, "Name", "Record2");
int Shape3 = Layer.AddShape(new PointShape(-98, 20));
Layer.UpdateTableData(3, "Name", "Record3");
Layer.CommitEdit();
VB.NET
Dim Columns As New DatabaseColumns
Dim Column1 As DatabaseColumn
Column1.FieldType = FieldDataType.fdtString
Column1.Length = 10
Column1.DecimalLength = 0
Column1.Name = "Name"
Columns.Add(Column1)
Dim Column2 As DatabaseColumn
Column2.FieldType = FieldDataType.fdtDouble
Column2.Length = 5
Column2.DecimalLength = 2
Column2.Name = "Acres"
Columns.Add(Column2)
Dim strPath As String = "C:\Projects\ThinkGeo\"
Dim strName As String = "Steve"
Map1.CreateNewShapefile(ShapeTypeEnum.Polygon, strPath, strName, Columns)
Map1.MapUnit = MapSuite.Geometry.MapLengthUnits.DecimalDegrees
Dim Layer As New Layer(strPath & strName, False)
Layer.BeginEdit()
Dim Shape As New Polygon()
Dim dblacres As Double = 90.31
Dim shape1 As Integer = Layer.AddShape(Shape)
Layer.UpdateTableData(shape1, "Acres", dblacres.ToString)
Layer.UpdateTableData(shape1, "Name", "Record1")
Layer.CommitEdit()
Layer.BuildIndex()Layer.BuildRecID("Recid", True)
ThinkGeo • 1617 St. Andrews Drive, Lawrence, KS, 66047 Toll-Free: 1-866-847-7510 • Internat'l: 1-785-727-4133 • sales@thinkgeo.com
Copyright 2008 ThinkGeo LLC