MapWinGIS:Shape CreateFromString
From MapWindow GIS
Initializes the Shape object and fills it with the geometry defined by the input string. The input string should be in the serialized string format as produced by the function SerializeToString.
VB.NET Usage
Function CreateFromString(Serialized As String) As Boolean
Parameters
|
Serialized | The serialized string to load. |
| ReturnValue | A boolean value representing the success or failure of loading the shape. |
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
















