MapWinGIS:Shape SerializeToString

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

Provides a string representing the shape's geometry. The string will be in the serialized string format and can be reloaded with CreateFromString.


VB.NET Usage

Function SerializeToString() As String

            Parameters

(none)

ReturnValue A string representing the shape geometry.

Visual Basic Net Example Code

     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
             MessageBox.Show(sf.ErrorMsg(sf.LastErrorCode))
             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()
             Else
                 text += "," & s.SerializeToString()
             End If
         Next
         sf.Close()
         '------------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
             MessageBox.Show(sf.ErrorMsg(sf.LastErrorCode))
             Exit Sub
         End If
         sf.StartEditingShapes()
         sf.StartEditingTable()
         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
             Else
                 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
         Next
         sf.StopEditingTable()
         sf.StopEditingShapes() 'This should effectively save the shapefile
         sf.Close()
     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) = _
            Windows.Forms.DialogResult.No)
     End Function
 
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         CopyShapes()
         MessageBox.Show("Done.")
     End Sub
 

Code posted by Shade1974 11/6/2006

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:Shape_SerializeToString"

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