MapWinGIS:SampleCode-VB Net:SerializeShapes

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

Serialize Shapes

This function demonstrates a method that will serialize all the shapes in a shapefile into a single string. If you were converting this for a database, you could store the results in a new column called shape, along with the attribute information for the shapefile.

     Private Sub CopyShapes()
         Dim text As String = vbNullString
         Dim ShapeStrings() As String
         Dim s As MapWinGIS.Shape
         Dim shp As Integer
         Dim sf As New MapWinGIS.Shapefile
         Dim typ As MapWinGIS.ShpfileType
         Dim indx As Integer
         Dim res As Boolean
         Dim start As Integer
         res = sf.Open("C:\FLORIDA\CountyBounds\SolidBnds.shp")
         If res = False Then
             Exit Sub
         End If
         '-----------Encode an entire shapefile to a string
         For shp = 0 To sf.NumShapes - 1
             s = sf.Shape(shp)
             If shp = 0 Then
                 text = s.SerializeToString()
                 text += "," & s.SerializeToString()
             End If
         '------------Read a string for an entire shapefile
         ShapeStrings = text.Split(",")
         'Use the first shape to figure out the shapefile type
         s = New MapWinGIS.Shape
         start = 0
         'The first shape might not be readable, so get the first one that is
         While s.CreateFromString(ShapeStrings(start)) = False
             start += 1
             If start > ShapeStrings.GetUpperBound(0) Then
                 MessageBox.Show("No readable shapes could be found in the strings.")
                 Exit Sub
             End If
         End While
         typ = s.ShapeType
         If System.IO.File.Exists("C:\Test.shp") Then System.IO.File.Delete("C:\Test.shp")
         If System.IO.File.Exists("C:\Test.shx") Then System.IO.File.Delete("C:\Test.shx")
         If System.IO.File.Exists("C:\Test.dbf") Then System.IO.File.Delete("C:\Test.dbf")
         res = sf.CreateNew("C:\Test.shp", typ)
         If res = False Then
             Exit Sub
         End If
         Dim fld As Integer
         Dim field As New MapWinGIS.Field()
         field.Type = MapWinGIS.FieldType.INTEGER_FIELD
         field.Name = "Index"
         res = sf.EditInsertField(field, fld)
         If res = False Then If (ShowError(sf.ErrorMsg(sf.LastErrorCode), 0)) = True Then Exit Sub
         res = sf.EditInsertShape(s, indx)
         If res = False Then If (ShowError(sf.ErrorMsg(sf.LastErrorCode), shp)) = True Then Exit Sub
         sf.EditCellValue(fld, indx, start)
         'Note, this code is for illustration purposes and does not include handling for fields which may be necessary
         start += 1
         For shp = start To ShapeStrings.GetUpperBound(0)
             s = New MapWinGIS.Shape
             res = s.CreateFromString(ShapeStrings(shp))
             If res = False Then
                 'We will warn on an individual failure, but not halt execution
                 'Of course our new shapefile will not have the correct number of shapes in it 
                 'if we continue here.
                 If (ShowError(s.ErrorMsg(s.LastErrorCode), shp)) = True Then Exit Sub
                 res = sf.EditInsertShape(s, indx)
                 If res = False Then If (ShowError(sf.ErrorMsg(sf.LastErrorCode), shp)) = True Then Exit Sub
                 sf.EditCellValue(fld, indx, shp)
             End If
         sf.StopEditingShapes() 'This should effectively save the shapefile
     End Sub
     Function ShowError(ByVal Message As String, ByVal Shape As Integer) As Boolean
         'This function shows the error, and returns true if they decide to abort
         Return (MessageBox.Show("The following error occured on shape " & Shape & "." & vbNewLine & _
            Message, "Serializing Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) = _
     End Function
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     End Sub

Code posted by Shade1974 on 11/6/2006

Retrieved from ""

This page has been accessed 2,841 times. This page was last modified on 6 November 2006, at 16:22.