MapWinGIS::VB6SampleCode-CreatePointSFEditcellValue

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

Here is sample code VB6 for creating a point shapefile from scratch and adding attributes using EditCellValue. Notice that this checks for return values from every function call so that you can check what-if anything-is wrong with the code. This is good practice. We designed the control such that it wouldn't do a hard crash, but just returns false when the functions aren't right. Hopefully the code is self explanatory.

Dan

        Private Sub Command1_Click()

        'Declare variables
        Dim sf As New MapWinGIS.Shapefile
        Dim f As New MapWinGIS.Field
        Dim pt As New MapWinGIS.Point
        Dim sh As New MapWinGIS.Shape
        Dim i As Long
        Dim Result As Boolean

        'Create a new shapefile of type SHP_POINT
        Result = sf.CreateNew("c:\temp\newpoints.shp", SHP_POINT)
        If Result = False Then GoTo ERRORHANDLER

        'Start Editing it...
        Result = sf.StartEditingShapes(True)
        If Result = False Then GoTo ERRORHANDLER

        Dim FieldIndex1 as long, FieldIndex2 as long, FieldIndex3 as Long

        'Set up three new fields of type string, double and integer
        set f = New MapWinGIS.Field
        f.Type = STRING_FIELD
        f.Name = "mytext"
        f.Width = 20
        FieldIndex1 = sf.NumFields
        Result = sf.EditInsertField(f, FieldIndex1)
        If Result = False Then GoTo ERRORHANDLER

        set f = New MapWinGIS.Field
        f.Type = INTEGER_FIELD
        f.Name = "myinteger"
        FieldIndex2 = sf.NumFields
        Result = sf.EditInsertField(f, FieldIndex2)
        If Result = False Then GoTo ERRORHANDLER

        set f = New MapWinGIS.Field
        f.Type = DOUBLE_FIELD
        f.Name = "mydouble"
        f.Precision = 8
        FieldIndex3 = sf.NumFields
        Result = sf.EditInsertField(f, FieldIndex3)
        If Result = False Then GoTo ERRORHANDLER

        'Create 1000 random points in an interesting pattern
        For i = 0 To 1000
            'Set up the point
            set pt = New MapWinGIS.Point
            pt.x = Cos(Rnd() * 1000)
            pt.y = Sin(Rnd() * 10) * pt.x

            'Add the point to a shape
            set sh = New MapWinGIS.Shape
            sh.ShapeType = SHP_POINT
            Result = sh.InsertPoint(pt, 0)
            If Result = False Then GoTo ERRORHANDLER

            'Add the shape to the shapefile
            Result = sf.EditInsertShape(sh, i)
            If Result = False Then GoTo ERRORHANDLER

            'Edit the attributes for this shape record
            Result = sf.EditCellValue(FieldIndex1, i, "Some Text " & i) 'insert some string value
            If Result = False Then GoTo ERRORHANDLER
            Result = sf.EditCellValue(FieldIndex2, i, i) 'insert some integer value
            If Result = False Then GoTo ERRORHANDLER
            Result = sf.EditCellValue(FieldIndex3, i, CDbl(pt.x * pt.y)) 'insert some double value
            If Result = False Then GoTo ERRORHANDLER
        Next i

        Result = sf.StopEditingShapes(True, True)

        Map1.AddLayer(sf, True)

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

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS::VB6SampleCode-CreatePointSFEditcellValue"

This page has been accessed 6,289 times. This page was last modified on 5 March 2010, at 06:45.