# Shape CreateFromString

**URL:** <https://mapwindow.discourse.group/t/shape-createfromstring/278>\
**Category:** MapWinGIS\
**Created:** [October 6, 2019, 9:25am UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278 "2019-10-06T09:25:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [October 6, 2019, 9:25am UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/1 "2019-10-06T09:25:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [October 6, 2019, 7:41pm UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/2 "2019-10-06T19:41:05Z")

</div>

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](http://VB.Net), and I apologize for there being no comments).

```
    Dim shp As New Shape
    shp.Create(ShpfileType.SHP_POLYGON)
    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
    newShape.CreateFromString(serialized)

    Dim sf As New Shapefile
    sf.CreateNew("", ShpfileType.SHP_POLYGON)
    sf.EditAddShape(newShape)
    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:

 ![image](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/b6f9fa88b3a0e9f81e67f5b4262bb538986358a4.png)

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?

Regards,  
Jerry

---

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [October 8, 2019, 4:20am UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/3 "2019-10-08T04:20:27Z")

</div>

> [@jerryfaust](#):
>
> EditAddShape(newShape)

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:

> ‘15;0;539251.035187|5892982.170575|0.000000|539269.687287|5893144.637505|0.000000|539288.413600|5893142.457949|0.000000|539269.769746|5892979.991018|0.000000|539251.035187|5892982.170575|0.000000|’

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?

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [October 8, 2019, 5:59am UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/4 "2019-10-08T05:59:17Z")

</div>

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
        MsgBox(sf.ErrorMsg(sf.LastErrorCode))
    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.

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [October 8, 2019, 6:33am UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/5 "2019-10-08T06:33:05Z")

</div>

> [@jerryfaust](#):
>
> you creating the Shapefile with `ShpfileType.SHP_POLYGONZ`

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."

---

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [October 8, 2019, 12:52pm UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/6 "2019-10-08T12:52:14Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [October 8, 2019, 3:09pm UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/7 "2019-10-08T15:09:51Z")

</div>

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

```
    If sf.StartEditingShapes() Then
        If sf.EditAddShape(newShape) < 0 Then
            MsgBox(sf.ErrorMsg(sf.LastErrorCode))
        End If
        sf.StopEditingShapes()
    End If
```

---

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [October 9, 2019, 2:54am UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/8 "2019-10-09T02:54:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [January 5, 2022, 3:08pm UTC](https://mapwindow.discourse.group/t/shape-createfromstring/278/9 "2022-01-05T15:08:52Z")

</div>


