MapWindow: Create a shapefile and add a point to it

Platform:MapWindow
Task:Create a shapefile and add a point to it
Discussion:
Create a shapefile and add a point to it
Example:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateShapefile()

        MyShapefile.Open("C:\Temp\MyShapefile.shp")

        AddPointToShapefile(MyShapefile, 66, -155)
        AddPointToShapefile(MyShapefile, 68, -145)

        Dim MyLayer As Integer = AxMap1.AddLayer(MyShapefile, True)
        AxMap1.set_ShapeLayerPointSize(MyLayer, 10)
        AxMap1.set_ShapeLayerPointColor(MyLayer, 255)
        AxMap1.Refresh()
        AxMap1.ZoomToLayer(MyLayer)
    End Sub

    ''' <summary>
    ''' Creates a shapefile C:\Temp\MyShapefile.shp with FixDate, Lat and Lon columns
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CreateShapefile()
        Dim NewShapefile As New MapWinGIS.Shapefile
        Dim fld As New MapWinGIS.Field
        Dim MyShapefileNamePrefix As String = "C:\Temp\MyShapefile"
        Dim MyShapefileName As String = MyShapefileNamePrefix & ".shp"

        'build the shapefile
        Try
            'if the shapefile exists already kill it and associated files
            If My.Computer.FileSystem.FileExists(MyShapefileName) Then
                My.Computer.FileSystem.DeleteFile(MyShapefileName)
                My.Computer.FileSystem.DeleteFile(MyShapefileNamePrefix & ".dbf")
                My.Computer.FileSystem.DeleteFile(MyShapefileNamePrefix & ".shx")
            End If

            'Create the shapefile.
            NewShapefile.CreateNew(MyShapefileName, MapWinGIS.ShpfileType.SHP_POINT)

            'Start editing the shapefile (the parameter "true" starts editing the table at the same time).
            NewShapefile.StartEditingShapes(True)

            'Set up a field to be added to the new shapefile.
            fld = New MapWinGIS.Field
            fld.Name = "FixDate"
            fld.Type = MapWinGIS.FieldType.STRING_FIELD
            fld.Width = 20
            'Insert the new field into the shapefile.
            NewShapefile.EditInsertField(fld, 0)

            'Set up a field to be added to the new shapefile.
            fld = New MapWinGIS.Field
            fld.Name = "Lat"
            fld.Type = MapWinGIS.FieldType.DOUBLE_FIELD
            fld.Width = 20
            'Insert the new field into the shapefile.
            NewShapefile.EditInsertField(fld, 0)

            'Set up a field to be added to the new shapefile.
            fld = New MapWinGIS.Field
            fld.Name = "Lon"
            fld.Type = MapWinGIS.FieldType.DOUBLE_FIELD
            fld.Width = 20
            'Insert the new field into the shapefile.
            NewShapefile.EditInsertField(fld, 0)

            'Stop editing and close the output shapefile
            NewShapefile.StopEditingShapes(True, True)
            NewShapefile.Save()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub AddPointToShapefile(ByVal Shapefile As MapWinGIS.Shapefile, ByVal Lat As Double, ByVal Lon As Double)
        Dim MyShape As New MapWinGIS.Shape
        Dim MyPoint As New MapWinGIS.Point
        Dim Result As Boolean

        'start editing
        Shapefile.StartEditingShapes(True)

        'Create a new Point and give it x and y values
        MyPoint = New MapWinGIS.Point
        MyPoint.x = -155
        MyPoint.y = 65

        'Create a new shape and add the point to it. If you had a line shape or polygon shape then you would add several points to one shape.
        MyShape = New MapWinGIS.Shape
        MyShape.Create(MapWinGIS.ShpfileType.SHP_POINT)
        MyShape.InsertPoint(MyPoint, 0)

        'Insert the shape into the shapefile
        Result = Shapefile.EditInsertShape(MyShape, 0)
        If Result = False Then MsgBox("EditInsertShapeError: " & Shapefile.ErrorMsg(Shapefile.LastErrorCode))

        'Set the "cityname" field value
        Result = Shapefile.EditCellValue(0, 0, MyPoint.y)
        Result = Shapefile.EditCellValue(1, 0, MyPoint.x)
        Result = Shapefile.EditCellValue(2, 0, "2011-01-25")
        If Result = False Then MsgBox("EditCellValue" & Shapefile.ErrorMsg(Shapefile.LastErrorCode))

        Shapefile.StopEditingShapes()
        Shapefile.Save()
    End Sub
Back to Index