Documente Academic
Documente Profesional
Documente Cultură
In the following example it is assumed that you have dragged an instance of the ShapeFile Control
from the IDE over to your Visual Basic project and renamed the OCX instance as ShapeIn.
In this case the ShapeFile Control is used to read in a polygon ShapeFile consisting of a series of
squares, one of which has a hole in it.
VB 6 VB .Net C#
Sub ReadShape()
.ReadDataOnMove
.OpenShape "C:\Temp\Polygon.shp", shpOpen
' Print out the name and values of the ShapeFile attribute data.
For ThisData = 1 To .ShapeFields.Count
Next ThisData
' Print out the coordinates that make up this shape file
' Because we’re dealing with a polygon file, it is best to output by Parts
' You can also just output from 1 To .Vertice.Count
For ThisPart = 1 To .Parts.Count
Next VertCount
Next ThisPart
.MoveNext
Next ThisRecord
End With
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 2 of 43
End Sub
VB .NET Example:
Sub ReadShape()
With ShapeIn
.ReadDataOnMove = FullRead This is the default
OpenShape automatically moves to the first record and loads it
.OpenShape("C:\Temp\Polygon.shp", shpOpen)
Print out the name and values of the ShapeFile attribute data.
For Each mF As crpShape.ShapeField In ShapeIn.ShapeFields
Console.WriteLine(mF.FieldName + " " + mF.Value.ToString())
Next
Next
End With
End Sub
C# Example:
{
ShapeIn.ReadDataOnMove = FullRead;
// This is the default
// OpenShape automatically moves to the first record and loads it
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 3 of 43
ShapeIn.OpenShape("C:\\Temp\\Polygon.shp", shpOpen);
In the following example it is assumed that you have dragged an instance of the ShapeFile Control
from the IDE over to your Visual Basic project and renamed the OCX instance as ShapeOut.
In this case the ShapeFile Control is used to write a series of ShapeFiles. The polygon shapes
consist of a series of squares, one of which has a hole in it.
VB 6 VB .Net C#
Sub WriteShapes()
With ShapeOut
' *******************************
' * Point *
' *******************************
.OpenShape "C:\Shapes\Point_create.shp", shpCreate, shpPoint
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 4 of 43
' *******************************
' * PointM *
' *******************************
.OpenShape "C:\Shapes\PointM_create.shp", shpCreate, shpPointM
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
' *******************************
' * PointZ *
' *******************************
.OpenShape "C:\Shapes\PointZ_create.shp", shpCreate, shpPointZ
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 5 of 43
' *******************************
' * MultiPoint *
' *******************************
.OpenShape "C:\Shapes\MultiPoint_create.shp", shpCreate, shpMultiPoint
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
' *******************************
' * MultiPointM *
' *******************************
.OpenShape "C:\Shapes\MultiPointM_create.shp", shpCreate, shpMultiPointM
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 6 of 43
.ShapeFields(3).Value = CDate(Now)
.CreateShape
Set NewVert = .Vertices.AddVertice(1, 11, , 20)
Set NewVert = .Vertices.AddVertice(1, 12)
Set NewVert = .Vertices.AddVertice(1, 13, , 21)
.ShapeFields(1).Value = "ROSS" + Chr(190)
.ShapeFields(2).Value = 2.34
.ShapeFields(3).Value = CDate(DateAdd("d", 2, Now))
.CreateShape
Set NewVert = .Vertices.AddVertice(10, 10, , 30)
Set NewVert = .Vertices.AddVertice(10, 11)
Set NewVert = .Vertices.AddVertice(10, 12, , 31)
.ShapeFields(1).Value = "ROSS" + Chr(191)
.ShapeFields(2).Value = 3.45
.ShapeFields(3).Value = CDate(DateAdd("d", 2, Now))
.CreateShape
' *******************************
' * MultiPointZ *
' *******************************
.OpenShape "C:\Shapes\MultiPointZ_create.shp", shpCreate, shpMultiPointZ
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
' *******************************
' * PolyLine *
' *******************************
.OpenShape "C:\Shapes\PolyLine_create.shp", shpCreate, shpPolyLine
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 7 of 43
' *******************************
' * PolyLineM *
' *******************************
.OpenShape "C:\Shapes\PolyLineM_create.shp", shpCreate, shpPolyLineM
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 8 of 43
' *******************************
' * PolyLineZ *
' *******************************
.OpenShape "C:\Shapes\PolyLineZ_create.shp", shpCreate, shpPolyLineZ
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 9 of 43
.CreateShape
' *******************************
' * Polygon *
' *******************************
.OpenShape "C:\Shapes\Polygon_create.shp", shpCreate, shpPolygon
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 10 of 43
' *******************************
' * PolygonM *
' *******************************
.OpenShape "C:\Shapes\PolygonM_create.shp", shpCreate, shpPolygonM
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
' *******************************
' * PolygonZ *
' *******************************
.OpenShape "C:\Shapes\PolygonZ_create.shp", shpCreate, shpPolygonZ
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 11 of 43
' *******************************
' * MultiPatch *
' *******************************
.OpenShape "C:\Shapes\MultiPatch_create.shp", shpCreate, shpMultiPatch
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 12 of 43
End With
End Sub
VB .NET Example:
Module Examples
Sub CreateShapes()
Dim ShapeOut As New crpShape.ShapeFiles
Dim NewFld As crpShape.ShapeField
Dim NewVert As crpShape.Vertice
With ShapeOut
' *******************************
' * Point *
' *******************************
.OpenShape("C:\Shapes\Point_Net_create.shp", shpCreate, shpPoint)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
NewVert = .Vertices.AddVertice(1, 1)
.ShapeFields(1).Value = "Ross" + Chr(189)
.ShapeFields(2).Value = 1.23
.ShapeFields(3).Value = CDate(Now)
.CreateShape()
NewVert = .Vertices.AddVertice(1, 10)
.ShapeFields(1).Value = "Ross" + Chr(190)
.ShapeFields(2).Value = 2.34
.ShapeFields(3).Value = CDate(DateAdd("d", 2, Now))
.CreateShape()
NewVert = .Vertices.AddVertice(10, 10)
.ShapeFields(1).Value = "Ross" + Chr(191)
.ShapeFields(2).Value = 3.45
.ShapeFields(3).Value = CDate(DateAdd("d", 2, Now))
.CreateShape()
' *******************************
' * PointM *
' *******************************
.OpenShape("C:\Shapes\PointM_Net_create.shp", shpCreate, shpPointM)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 13 of 43
.AppendFieldDefs()
' *******************************
' * PointZ *
' *******************************
.OpenShape("C:\Shapes\PointZ_Net_create.shp", shpCreate, shpPointZ)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
' *******************************
' * MultiPoint *
' *******************************
.OpenShape("C:\Shapes\MultiPoint_Net_create.shp", shpCreate, shpMultiPoint)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
NewVert = .Vertices.AddVertice(1, 1)
NewVert = .Vertices.AddVertice(1, 2)
NewVert = .Vertices.AddVertice(1, 3)
.ShapeFields(1).Value = "Ross" + Chr(189)
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 14 of 43
.ShapeFields(2).Value = 1.23
.ShapeFields(3).Value = CDate(Now)
.CreateShape()
NewVert = .Vertices.AddVertice(1, 11)
NewVert = .Vertices.AddVertice(1, 12)
NewVert = .Vertices.AddVertice(1, 13)
.ShapeFields(1).Value = "Ross" + Chr(190)
.ShapeFields(2).Value = 2.34
.ShapeFields(3).Value = CDate(DateAdd("d", 2, Now))
.CreateShape()
NewVert = .Vertices.AddVertice(10, 10)
NewVert = .Vertices.AddVertice(10, 11)
NewVert = .Vertices.AddVertice(10, 12)
.ShapeFields(1).Value = "Ross" + Chr(191)
.ShapeFields(2).Value = 3.45
.ShapeFields(3).Value = CDate(DateAdd("d", 2, Now))
.CreateShape()
' *******************************
' * MultiPointM *
' *******************************
.OpenShape("C:\Shapes\MultiPointM_Net_create.shp", shpCreate, shpMultiPointM)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
' *******************************
' * MultiPointZ *
' *******************************
.OpenShape("C:\Shapes\MultiPointZ_Net_create.shp", shpCreate, shpMultiPointZ)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 15 of 43
.AppendFieldDefs()
' *******************************
' * PolyLine *
' *******************************
.OpenShape("C:\Shapes\PolyLine_Net_create.shp", shpCreate, shpPolyLine)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
NewVert = .Vertices.AddVertice(1, 1)
NewVert = .Vertices.AddVertice(1, 10)
NewVert = .Vertices.AddVertice(10, 10)
NewVert = .Vertices.AddVertice(10, 1)
NewVert = .Vertices.AddVertice(1, 1)
.ShapeFields(1).Value = "Ross" + Chr(189)
.ShapeFields(2).Value = 1.23
.ShapeFields(3).Value = CDate(Now)
.CreateShape()
NewVert = .Vertices.AddVertice(10, 1, 1)
NewVert = .Vertices.AddVertice(10, 10, 1)
NewVert = .Vertices.AddVertice(20, 10, 1)
NewVert = .Vertices.AddVertice(20, 1, 1)
NewVert = .Vertices.AddVertice(10, 1, 1)
NewVert = .Vertices.AddVertice(14, 4, 2)
NewVert = .Vertices.AddVertice(14, 6, 2)
NewVert = .Vertices.AddVertice(16, 6, 2)
NewVert = .Vertices.AddVertice(16, 4, 2)
NewVert = .Vertices.AddVertice(14, 4, 2)
.ShapeFields(1).Value = "Ross" + Chr(190)
.ShapeFields(2).Value = 2.34
.ShapeFields(3).Value = CDate(DateAdd("d", 1, Now))
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 16 of 43
.CreateShape()
NewVert = .Vertices.AddVertice(20, 1)
NewVert = .Vertices.AddVertice(20, 10)
NewVert = .Vertices.AddVertice(30, 10)
NewVert = .Vertices.AddVertice(30, 1)
NewVert = .Vertices.AddVertice(20, 1)
.ShapeFields(1).Value = "Ross" + Chr(191)
.ShapeFields(2).Value = 3.45
.ShapeFields(3).Value = CDate(DateAdd("d", 2, Now))
.CreateShape()
' *******************************
' * PolyLineM *
' *******************************
.OpenShape("C:\Shapes\PolyLineM_Net_create.shp", shpCreate, shpPolyLineM)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 17 of 43
' *******************************
' * PolyLineZ *
' *******************************
.OpenShape("C:\Shapes\PolyLineZ_Net_create.shp", shpCreate, shpPolyLineZ)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
' *******************************
' * Polygon *
' *******************************
.OpenShape("C:\Shapes\Polygon_Net_create.shp", shpCreate, shpPolygon)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
NewVert = .Vertices.AddVertice(1, 1)
NewVert = .Vertices.AddVertice(1, 10)
NewVert = .Vertices.AddVertice(10, 10)
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 18 of 43
NewVert = .Vertices.AddVertice(10, 1)
NewVert = .Vertices.AddVertice(1, 1)
.ShapeFields(1).Value = "Ross" + Chr(189)
.ShapeFields(2).Value = 1.23
.ShapeFields(3).Value = CDate(Now)
.CreateShape()
NewVert = .Vertices.AddVertice(10, 1, 1)
NewVert = .Vertices.AddVertice(10, 10, 1)
NewVert = .Vertices.AddVertice(20, 10, 1)
NewVert = .Vertices.AddVertice(20, 1, 1)
NewVert = .Vertices.AddVertice(10, 1, 1)
NewVert = .Vertices.AddVertice(14, 4, 2)
NewVert = .Vertices.AddVertice(14, 6, 2)
NewVert = .Vertices.AddVertice(16, 6, 2)
NewVert = .Vertices.AddVertice(16, 4, 2)
NewVert = .Vertices.AddVertice(14, 4, 2)
.ShapeFields(1).Value = "Ross" + Chr(190)
.ShapeFields(2).Value = 2.34
.ShapeFields(3).Value = CDate(DateAdd("d", 1, Now))
.CreateShape()
NewVert = .Vertices.AddVertice(20, 1)
NewVert = .Vertices.AddVertice(20, 10)
NewVert = .Vertices.AddVertice(30, 10)
NewVert = .Vertices.AddVertice(30, 1)
NewVert = .Vertices.AddVertice(20, 1)
.ShapeFields(1).Value = "Ross" + Chr(191)
.ShapeFields(2).Value = 3.45
.ShapeFields(3).Value = CDate(DateAdd("d", 2, Now))
.CreateShape()
' *******************************
' * PolygonM *
' *******************************
.OpenShape("C:\Shapes\PolygonM_Net_create.shp", shpCreate, shpPolygonM)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 19 of 43
' *******************************
' * PolygonZ *
' *******************************
.OpenShape("C:\Shapes\PolygonZ_Net_create.shp", shpCreate, shpPolygonZ)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 20 of 43
' *******************************
' * MultiPatch *
' *******************************
.OpenShape("C:\Shapes\MultiPatch_Net_create.shp", shpCreate, shpMultiPatch)
NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs()
End With
End Sub
End Module
C# Example:
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 21 of 43
// *******************************
// * Point *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\Point_Net_create.shp", shpCreate, shpPoint);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
// *******************************
// * PointM *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\PointM_Net_create.shp", shpCreate, shpPointM);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
// *******************************
// * PointZ *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\PointZ_Net_create.shp", shpCreate, shpPointZ);
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 22 of 43
// *******************************
// * MultiPoint *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\MultiPoint_Net_create.shp", shpCreate,
shpMultiPoint);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
// *******************************
// * MultiPointM *
// *******************************
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 23 of 43
ShapeOut.OpenShape("C:\\Shapes\\MultiPointM_Net_create.shp", shpCreate,
shpMultiPointM);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
// *******************************
// * MultiPointZ *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\MultiPointZ_Net_create.shp", shpCreate,
shpMultiPointZ);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 24 of 43
// *******************************
// * PolyLine *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\PolyLine_Net_create.shp", shpCreate, shpPolyLine);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
// *******************************
// * PolyLineM *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\PolyLineM_Net_create.shp", shpCreate,
shpPolyLineM);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 25 of 43
// *******************************
// * PolyLineM *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\PolyLineZ_Net_create.shp", shpCreate,
shpPolyLineZ);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 26 of 43
ShapeOut.CreateShape();
// *******************************
// * Polygon *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\Polygon_Net_create.shp", shpCreate, shpPolygon);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 27 of 43
// *******************************
// * PolygonM *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\PolygonM_Net_create.shp", shpCreate, shpPolygonM);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 28 of 43
// *******************************
// * PolygonZ *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\PolygonZ_Net_create.shp", shpCreate, shpPolygonZ);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
// *******************************
// * MultiPatch *
// *******************************
ShapeOut.OpenShape("C:\\Shapes\\MultiPatch_Net_create.shp", shpCreate,
shpMultiPatch);
NewFld = ShapeOut.ShapeFields.CreateField("TextField", shpText, 10);
NewFld = ShapeOut.ShapeFields.CreateField("NumField", shpNumeric, 5, 2);
NewFld = ShapeOut.ShapeFields.CreateField("DateField", shpDate);
ShapeOut.AppendFieldDefs();
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 29 of 43
In the following example it is assumed that you have dragged an instance of the ShapeFile Control
from the IDE over to your Visual Basic project and renamed the OCX instance as ShapeOut.
In this case the ShapeFile Control is used to write a series of ShapeFiles. The first shape is written,
then the file reopened to add the extra shapes. A point to note is that you have to clear the
Vertices collection before adding any new records when appending to an existing ShapeFile.
Sub WriteShapes()
With ShapeOut
' *******************************
' * Create New Point Shape *
' *******************************
.OpenShape "C:\Shapes\Point_append.shp", shpCreate, shpPoint
Set NewFld = .ShapeFields.CreateField("TextField", shpText, 10)
Set NewFld = .ShapeFields.CreateField("NumField", shpNumeric, 5, 2)
Set NewFld = .ShapeFields.CreateField("DateField", shpDate)
.AppendFieldDefs
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 30 of 43
End With
With ShapeOut
' *******************************
' * Append to Existing Point Shape *
' *******************************
.OpenShape "C:\Shapes\Pointappend.shp", shpOpen
' Clear the Vertice structure that is automatically populated by .OpenShape
If .ReadDataOnMove <> HeaderOnly Then
.Vertices.Clear
' You may also want to clear the records in the ShapeFields structure
.ShapeFields.Clear
End If
End With
End Sub
VB .NET Example:
Module Examples
Sub AppendShapes()
Dim ShapeOut As New crpShape.ShapeFiles
Dim NewFld As crpShape.ShapeField
Dim NewVert As crpShape.Vertice
With ShapeOut
' *******************************
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 31 of 43
NewVert = .Vertices.AddVertice(1, 1)
.ShapeFields(1).Value = "Ross" + Chr(189)
.ShapeFields(2).Value = 1.23
.ShapeFields(3).Value = CDate(Now)
.CreateShape()
End With
With ShapeOut
' *******************************
' * Append to Existing Point Shape *
' *******************************
.OpenShape("C:\Shapes\Point_Net_append.shp", shpOpen)
' Clear the Vertice structure that is automatically populated by .OpenShape
If .ReadDataOnMove <> HeaderOnly Then
.Vertices.Clear()
' You may also want to clear the records in the ShapeFields structure
.ShapeFields.Clear()
End If
End With
End Sub
End Module
In this example a new ShapeFile is created from attribute and vertice info held in an Access
database. The Access database (Points.mdb) Contains some point data with the fields X and Y
containing the X & Y coordinates.
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 32 of 43
On the form are the ShapeFile Control and MicroSofts' Data Control. The Data Control is called
DataIn and the ShapeFile Control is called ShapeFiles1. Here is a note of warning ... the data
type definitions in ADO and DAO do differ from the data types available in the ShapeFile DBF
format so I've included a bit of code to get you started. This example may not be the most efficient
code wise ... but here we go ...
Sub EGDAOInput()
Dim NewVert As Vertices
Dim NewField As ShapeField
Dim dbAccess As DAO.Database
Dim rcAccess As DAO.Recordset
Dim fdDefn As Field
Dim i As Long
With ShapeFiles1
' in ADODB you can use NumericScale to returns the number of decimal places
' allowed for numeric values otherwise you're guessing
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 33 of 43
' For each record in the database copy the field values to the ShapeFile
For Each fdDefn In rcAccess.Fields
.ShapeFields(fdDefn.Name).Value = fdDefn.Value
Next fdDefn
Loop
rcAccess.Close
End With
End Sub
Both the OCX and DLL are Active X COM objects so you can use either of them in ASP and HTML
pages. The trick is that each reference to a ShapeFiles, ShapeField and Vertice object needs to be
created using the CreateObject command and any enumerations parsed have to be converted to
integers. Huh??? Here is an VBScript ASP sample that creates a polygon, maybe that will make it
clearer. Oh, and remember that the ArcViewShapeFileDLL must be properly registered using
REGSVR32 before you can use it.
<%
' Dimension the object references required
%>
Dim NewVert As Vertices
Dim MyShape
Set MyShape = Server.CreateObject("ArcViewShapeFileDLL.ShapeFiles")
Dim NewVert
Set NewVert = Server.CreateObject("ArcViewShapeFileDLL.Vertice")
Dim ShapeField
Set ShapeField = Server.CreateObject("ArcViewShapeFileDLL.ShapeField")
<%
' Create a new polygon ShapeFile. The enumeration for shpCreate = 1, and shpPolygon = 5
' Note that the ShapeFields types in the CreateField statements are also converted from the
enumeration
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 34 of 43
%>
MyShape.OpenShape "C:/Temp/PolyOut.shp",1,5
Set NewField = MyShape.ShapeFields.CreateField("Poly_id",4
Set NewField = MyShape.ShapeFields.CreateField("Name",10,20)
Set NewField = MyShape.ShapeFields.CreateField("Date_Mod",8)
MyShape.AppendFieldDefs
<%
' Add the record values and create the new shape
%>
MyShape.ShapeFields("Poly_id").Value = 1
MyShape.ShapeFields("Name").Value = "The First Polygon"
MyShape.ShapeFields("Date_Mod").Value = CDate("19/11/2004")
Set NewVert = MyShape.Vertices.AddVertice(1,1)
Set NewVert = MyShape.Vertices.AddVertice(1,10)
Set NewVert = MyShape.Vertices.AddVertice(10,10)
Set NewVert = MyShape.Vertices.AddVertice(10,1)
Set NewVert = MyShape.Vertices.AddVertice(1,1)
MyShape.CreateShape
<%
' Tidy Up
%>
Set NewField = Nothing
Set NewVert = Nothing
Set ShapeOut = Nothing
The following example shows how you can use the two different find methods. The first
searches the ShapeFile database for a match against a SQL style query. The second does a
point in polygon search.
Drag an instance of the ShapeFile Control from the IDE over to your Visual Basic project to
get the ShapeFiles1 instance or dimension a new instance of the DLL with the same name.
The example uses the polygon ShapeFile created in the Creating Shapes example.
Sub Find()
' Find Examples
With ShapeFiles1
.ReadDataOnMove = FastRead
.OpenShape "C:\Shapes\Polygons_base.shp", shpOpen
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 35 of 43
Loop
Debug.Print ""
.FindbyXY 10, 5
Loop
End With
End Sub
The following example provided by Marc Miller shows how you can use the COM DLL in
Visual Studio .NET (see ... it can be done). The DLL has been referenced under the
WindowsApplication References. On the form is a Picture box (PictureBox1), a List box
(ListBox1) and two buttons (btnReadVertices and btnReadAttributes). In this case the
application loads a ShapeFile of the USA. The first button will display the ShapeFile and
list the Vertice info and the second button lists the attribute data. The application looks
like this:
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 36 of 43
Imports System
Imports System.IO
Imports System.Text
Imports System.Drawing
Imports Microsoft.VisualBasic
Imports System.Windows.Forms.PictureBox
Imports System.Math
End Sub
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 37 of 43
'init picturebox
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
g = GetGraphics()
Fin = "..\states.shp"
MyShape.ReadDataOnMove = True
ListBox1.Items.Clear()
If File.Exists(Fin) Then
MyShape.OpenShape(Fin, shpOpen)
ListBox1.Items.Add("Number of Records: " &
MyShape.RecordCount)
ListBox1.Items.Add(MyShape.xMin & vbTab & MyShape.xMax)
ListBox1.Items.Add(MyShape.yMin & vbTab & MyShape.yMax)
pbMaxX = PictureBox1.Width
pbMaxY = PictureBox1.Height
XDegPerPixel = Math.Abs(MaxX - MinX) / pbMaxX
YDegPerPixel = Math.Abs(MaxY - MinY) / pbMaxY
With MyShape
For Record = 0 To MyShape.RecordCount - 1
For ThisPart = 1 To .Parts.Count
For VertCount = .Parts
(ThisPart).Begins To .Parts(ThisPart).Ends
arLon.Add(.Vertices
(VertCount).X_Cord)
arLat.Add(.Vertices
(VertCount).Y_Cord)
Next VertCount
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 38 of 43
'For clarity
' Lon = CType(arLon
(pt), Double)
' Lat = CType(arLat
(pt), Double)
' X = Math.Abs(Lon -
MinX) / XDegPerPixel
' Y = Math.Abs(MaxY
- Lat) / YDegPerPixel
'For optimization
X = Math.Abs(CType(arLon
(pt), Double) - MinX) / XDegPerPixel
Y = Math.Abs(MaxY - CType
(arLat(pt), Double)) / YDegPerPixel
StartPoint = New Point
(CType(X, Integer), CType(Y, Integer))
'For clarity
' Lon = CType(arLon
(pt + 1), Double)
' Lat = CType(arLat
(pt + 1), Double)
' X = Math.Abs(Lon -
MinX) / XDegPerPixel
' Y = Math.Abs(MaxY
- Lat) / YDegPerPixel
'For optimization
X = Math.Abs(CType(arLon
(pt + 1), Double) - MinX) / XDegPerPixel
Y = Math.Abs(MaxY - CType
(arLat(pt + 1), Double)) / YDegPerPixel
EndPoint = New Point(CType
(X, Integer), CType(Y, Integer))
g.DrawLine(LinePen,
StartPoint, EndPoint)
pt += 1
Next Coord
'clear ArrayList for next record
arLat.Clear()
arLon.Clear()
Next ThisPart
MyShape.MoveNext()
Next Record
End With
'draw shape file
PictureBox1.Image = bmap
g.Dispose()
End If
End Sub
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 39 of 43
.OpenShape("..\states.shp", shpOpen)
ListBox1.Items.Add(.ShapeFields
(ThisData).FieldName & vbTab & .ShapeFields(ThisData).Value.ToString)
Next ThisData
Next VertCount
Next ThisPart
Next ThisRecord
ListBox1.Items.Add("")
ListBox1.Items.Add(ThisRecord.ToString)
End If
End With
End Sub
pbMaxX = PictureBox1.Width
pbMaxY = PictureBox1.Height
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 40 of 43
Me.Text = "Lat: " & Format(Lat, "###0.00000") & Space(10) & "Lon:
" & Format(Lon, "###0.00000")
End Sub
This example uses the ModifyShape method to alter Vertice and ShapeField data for
an existing ShapeFile. In this case three polygons are created, the second polygon
represents an outer square with a donut. The DeleteVertice and DeleteShape
methods do exactly what they say. Note how the PartNo of the Vertice is used to
denote the donut. The ModifyShape command is used to write the data permanently
to the ShapeFile.
Sub ModifyShape()
With ShapeFiles1
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 41 of 43
.CreateShape
Set ModVert = .Vertices.AddVertice(10, 1, 1)
Set ModVert = .Vertices.AddVertice(10, 10, 1)
Set ModVert = .Vertices.AddVertice(20, 10, 1)
Set ModVert = .Vertices.AddVertice(20, 1, 1)
Set ModVert = .Vertices.AddVertice(10, 1, 1)
Set ModVert = .Vertices.AddVertice(14, 4, 2)
Set ModVert = .Vertices.AddVertice(14, 6, 2)
Set ModVert = .Vertices.AddVertice(16, 6, 2)
Set ModVert = .Vertices.AddVertice(16, 4, 2)
Set ModVert = .Vertices.AddVertice(14, 4, 2)
.ShapeFields(1).Value = "ROSS" + Chr(190)
.ShapeFields(2).Value = 2.34
.ShapeFields(3).Value = CDate(Now)
.CreateShape
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 42 of 43
End With
End Sub
The following example shows how you can copy the vertice and database information
to another ShapeFile
Drag an instance of the ShapeFile Control from the IDE over to your Visual Basic
project to get the ShapeFiles1 instance or dimension a new instance of the DLL with the
same name. The example uses the polygon ShapeFile created in the Creating Shapes
example.
Sub CopyTo()
Dim i As Long
With ShapeFiles1
.ReadDataOnMove = HeaderOnly
' First open an existing ShapeFile that we can play With it
.OpenShape "C:\Shapes\Polygons_base.shp", shpOpen
.MoveFirst
' When the First record is created the data structure is copied
For i = 1 To .RecordCount
.CopyTo "C:\Shapes\CopyTo.shp", i
.MoveNext
Next
.LoadShapeData
For i = 1 To .Vertices.Count
Debug.Print i, .Vertices(i).X_Cord, .Vertices(i).Y_Cord
Next
End With
End Sub
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019
Examples - Reading a ShapeFile Page 43 of 43
The following example shows how you can copy the vertice and database
information from one ShapeFile into another. It also shows you how to create the
database definition of a new ShapeFile for that of an existing file.
Drag an instance of the ShapeFile Control from the IDE over to your Visual Basic
project to get the ShapeFiles1 instance or dimension a new instance of the DLL
with the same name. The example uses the polygon ShapeFile created in the
Creating Shapes example.
Sub CopyFrom()
Dim i As Long
End Sub
file:///C:/Users/MLIGO/AppData/Local/Temp/~hh9CF.htm 4/26/2019