Shape CreateFromString

Hello again. Help deal with Shape.CreateFromString (). It seems a simple function and everything should work I create a shape from SerializeToString (), but the shape is not added to the layer. I don’t know what it is. Maybe someone has a small working example. Maybe I’m missing something.

Hello @mainxt

I created a simple test, which appears to successfully serialize and deserialize a simple shape, which can be displayed on the map. (coded in VB.Net, and I apologize for there being no comments).

    Dim shp As New Shape
    With shp
        .AddPoint(0, 0)
        .AddPoint(0, 1)
        .AddPoint(1, 1)
        .AddPoint(1, 0)
        .AddPoint(0, 0)
    End With
    Dim serialized As String = shp.SerializeToString()
    Dim newShape As New Shape

    Dim sf As New Shapefile
    sf.CreateNew("", ShpfileType.SHP_POLYGON)
    AxMap1.AddLayer(sf, True)

The new shape seems to share the same attributes as the original shape, and can be displayed in the map, as seen here:

Can you tell me more about your shape? Perhaps show the serialized string? In your code, does it appear that the deserialized shape is invalid, or that it just won’t display when added to the layer?


Very strange. I don’t understand why I can’t. I selected shape on the current existing layer, get his a string using SerializeToString. Here is the result:


then I create a NewShape using CreateFromString, the result returns true, that is, it is created, everything works correctly. But when I add NewShape to maps using EditAddShape(newShape) nothing happens… the result returns -1. What could be wrong? How to debug?

A couple things to try.

If the EditAddShape returns -1, try getting the last error, as follows:

    Dim sf As New Shapefile
    sf.CreateNew("", ShpfileType.SHP_POLYGON)
    If sf.EditAddShape(newShape) < 0 Then
    End If

Another thing I notice, although I have not verified this, but I notice that your points have z-coordinates. Are you creating the Shapefile with ShpfileType.SHP_POLYGONZ ?

Let me know what you find out.


No, These are the coordinates of Universal Transverse Mercator. (UTM-41N)…

MsgBox(sf.ErrorMsg(sf.LastErrorCode)) = ‘Shapefile function can not be executed in non-edit mode’

I did not know about this possibility of debugging errors. I’ll keep that in mind. So you need to enable edit mode. I’ll see where it is."

I checked the ShpfileType value, it returned value = 15. It’s SHP_POINTM, I don’t even know what type it is. I didn’t create this layer in MapWinGIS. I got it by exporting MapInfo to shp format. The layer I have are regular polygons (shape).

Sorry about that. On an existing Shapefile, you would do the following:

    If sf.StartEditingShapes() Then
        If sf.EditAddShape(newShape) < 0 Then
        End If
    End If

Thanks @jerryfaust. Everything is as simple as I had never guessed before. Of course

sf.ErrorMsg (sf.LastErrorCode)

helps me a lot now. Thank you very much.