MapWinGIS:SampleCode-VB Net:CreatePolyline

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

Create Random Polylines

This is almost identical to the VB6 example code that Dan posted on creating a new point shapefile. This one creates random, randomly colored polylines. It also shows that if you want to create a new shapefile with the same name, you should remove the old one, close the shapefile object, and then delete the files from the disk.

 Public Class Form1
     Dim Layer As Integer
     Dim sf As MapWinGIS.Shapefile
 
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         'Declare variables
 
         Dim f As New MapWinGIS.Field
         Dim pt As New MapWinGIS.Point
         Dim sh As New MapWinGIS.Shape
         Dim shp, pnt As Long
         Dim Result As Boolean
 
         If Layer > -1 Then
             AxMap1.RemoveLayer(Layer)
             Layer = -1
             sf.Close()
         Else
             sf = New MapWinGIS.Shapefile
         End If
         
         'Create a new shapefile of type SHP_POLYLINE
         If System.IO.File.Exists("c:\temp\newpoints.dbf") Then
             System.IO.File.Delete("C:\temp\newpoints.shp")
             System.IO.File.Delete("C:\temp\newpoints.dbf")
             System.IO.File.Delete("C:\temp\newpoints.shx")
         End If
         Result = sf.CreateNew("c:\temp\newpoints.shp", MapWinGIS.ShpfileType.SHP_POLYLINE)
         If Result = False Then GoTo ERRORHANDLER
 
         'Start Editing it...
         Result = sf.StartEditingShapes(True)
         If Result = False Then GoTo ERRORHANDLER
 
         'Set up three new fields of type string, double and integer
         f = New MapWinGIS.Field
         f.Type = MapWinGIS.FieldType.STRING_FIELD
         f.Name = "mytext"
         f.Width = 20
         Result = sf.EditInsertField(f, 0)
         If Result = False Then GoTo ERRORHANDLER
 
         f = New MapWinGIS.Field
         f.Type = MapWinGIS.FieldType.INTEGER_FIELD
         f.Name = "myinteger"
         Result = sf.EditInsertField(f, 1)
         If Result = False Then GoTo ERRORHANDLER
 
         f = New MapWinGIS.Field
         f.Type = MapWinGIS.FieldType.DOUBLE_FIELD
         f.Name = "mydouble"
         f.Precision = 8
         Result = sf.EditInsertField(f, 2)
         If Result = False Then GoTo ERRORHANDLER
 
         'Create 10 random polylines with 5 points and random colors
         For shp = 0 To 10
             'initialize the shape
             sh = New MapWinGIS.Shape
             sh.ShapeType = MapWinGIS.ShpfileType.SHP_POLYLINE
             'Set up the points
             For pnt = 0 To 4
                 pt = New MapWinGIS.Point
                 pt.x = Math.Cos(Rnd() * 1000)
                 pt.y = Math.Sin(Rnd() * 10)
 
                 'Add the points to a shape
                 Result = sh.InsertPoint(pt, pnt)
                 If Result = False Then GoTo ERRORHANDLER
             Next pnt
 
             'Add the shape to the shapefile
             Result = sf.EditInsertShape(sh, shp)
             If Result = False Then GoTo ERRORHANDLER
 
             'Edit the attributes for this shape record
             Result = sf.EditCellValue(0, shp, "Some Text " & shp) 'insert some string value
             If Result = False Then GoTo ERRORHANDLER
             Result = sf.EditCellValue(1, shp, shp) 'insert some integer value
             If Result = False Then GoTo ERRORHANDLER
             Result = sf.EditCellValue(2, shp, CDbl(pt.x * pt.y)) 'insert some double value
             If Result = False Then GoTo ERRORHANDLER
         Next shp
 
         Result = sf.StopEditingShapes(True, True)
         Layer = AxMap1.AddLayer(sf, True)
         For shp = 0 To sf.NumShapes - 1
             AxMap1.set_ShapeLineColor(Layer, shp, CUInt(RGB(CInt(Rnd() * 255), CInt(Rnd() * 255), CInt(Rnd() * 255))))
         Next shp
         Exit Sub
 
 ERRORHANDLER:
         'If any of our function calls returned false then jump here and tell us what went wrong...
         MsgBox("Shapefile Error: " & sf.ErrorMsg(sf.LastErrorCode) & vbCrLf & "Shape Error: " & _
         sh.ErrorMsg(sh.LastErrorCode) & vbCrLf & "Point Error: " & pt.ErrorMsg(pt.LastErrorCode) & _
         vbCrLf & "Field Error: " & f.ErrorMsg(f.LastErrorCode))
 
     End Sub
 
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         Layer = -1
     End Sub
 End Class

Code Posted by Shade1974 on 6/16/2006

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:SampleCode-VB_Net:CreatePolyline"

This page has been accessed 6,119 times. This page was last modified on 16 June 2006, at 20:14.