String Encoding in Attribute table is change when stop editing

Hi every one, sorry for my bad English.
I am newbie and I am doing a small tool to show and update Attribute table from a shapefile.
I can use Identified event to get Fields of the Selected shape, then show Attribure of this shape to a Datagridview in VB.Net.
I can either Generate Label for this data. The data in Datagridview and Label is good. You can see in the picture.

The problem is, when I turn on Editing mode, I change something in Attribute table, the label is updated successfully. Data still be OK. But when I save the shapefile by

sf.StopEditingTable(True)

The string in label and datagridview become unreadable. I think there is something wrong with unicode Encoding.

Please help me if you can. Thanks so much.

Good morning,

is only the text of your modified label after saving unreadable, or all other labels as well?
When you edit and save something you’re operating in the realms of MapWinGIS, depending on the type of layer you’re working with, the GlobalSetting.OgrStringEncoding might make a difference,
if you haven’t already playing with it.

Cheers
JT

1 Like

Thanks for your reply, jTati

When I click Save button, all other labels of this shapefile unreadble, not only edited record, as you can see in the picture below.

I havn’t used GlobalSetting.OgrStringEncoding before. I searced for this class example but found nothing. Can you explain more detail or give me a way to solve this problem.
Many thanks.

Good Morning,

i haven’t worked with that setting either, but i would suggest, that you try, when loading the form containing the ocx you generate a new MapWinGis.GlobalSettings-Object and set its StringEncoding.
In VBA it would mean placing these lines in the Form_Load-Event

DIM myGlobalSettings as New MapWinGis.GlobalSettings
myGlobalSettings.OgrStringEncoding = oseUtf8

If the value “oseUtf8” doesn’t work, you can try to set it to the other available value “oseAnsi”.

But as i wrote, this is just a suggestion and depends on the kind of layer you’re using.

Cheers
JT

1 Like

Hello @Lytrieu002

It might help to post some of your code. I notice that you use StopEditingTable (rather than StopEditingShapes, which also saves the table data).

Also, if you are using a Shapefile, does it have a corresponding code page file (.CPG).

MapWinGIS will use the code page file, if present, and otherwise will assume UTF-8, and so it should handle your strings properly on the way in and on the way out.

Regards,
Jerry.

1 Like

Dear jerryfaust

Thanks so much for your help. I checked that every shapefile has “.cpg” file and it’s content is “UTF-8”.
When the first time I load the Attribute table to a Datagridview and generate Label for it, everything works well. But when I StartEditingTable and then immediately StopEditingTable with no changes (see code below), the string still become unreadable.

Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
        Dim sf As Shapefile = New Shapefile()
        sf = AxM.get_Shapefile(txtLayerHandle.Text)
        If sf IsNot Nothing Then
            If sf.Table.StartEditingTable Then
                WL("Turned on Editing.")
                If sf.Table.StopEditingTable(True) Then
                    WL("Turned off Editing and Saved!")
                End If
            End If
        End If
    End Sub

Additional information:
Content of .cpg file:
image
Content become unreadble after click Button4 (Button4_Click_1)

Code of Turn on/Turn off editing mode button

Private Sub btnStartEditing_Click(sender As Object, e As EventArgs) Handles btnStartEditing.Click
        If isEditingMode Then
            saveEditing()
        Else
            isEditingMode = True
            btnStartEditing.Text = "Stop editing"
            Dim sf As Shapefile = New Shapefile()
            sf = AxM.get_Shapefile(txtLayerHandle.Text)
            If (Not sf.Table.StartEditingTable(Nothing)) Then
                WL("Không thể bật chế độ Editing. Chi tiết: " & sf.Table.ErrorMsg(sf.LastErrorCode))
            Else
                WL("Đã bật chế độ Editting trên LayerHanle = " & txtLayerHandle.Text)
                AxM.Identifier.IdentifierMode = tkIdentifierMode.imAllLayersStopOnFirst
                AxM.CursorMode = tkCursorMode.cmIdentify
                AxM.ZoomToLayer(layerHandle)
                AxM.CurrentZoom -= 1
                isEditingMode = True
            End If
        End If
        btnAddShape.Enabled = isEditingMode
        btnDeleteShape.Enabled = isEditingMode
    End Sub

Save data sub

Private Sub saveEditing()
        Dim sf As Shapefile = New Shapefile()
        sf = AxM.get_Shapefile(txtLayerHandle.Text)
        If sf IsNot Nothing And isDataChanged Then  'isDataChanged set to True when user make some changes on Attribute table
            Select Case MsgBox("Bạn có muốn lưu lại kết quả đã chỉnh sửa?", vbYesNoCancel, "Thông báo")
                Case MsgBoxResult.Yes
                    'WL is just the sub write log file
                    If sf.Table.StopEditingTable(True) Then
                        'Stop editing and Saved
                        WL("Đã tắt chế độ edit và Đã lưu bảng thuộc tính của shapefile.")
                    Else
                        'Cannot stop editing
                        WL("Error Stop Editing Shape: " & sf.ErrorMsg(sf.LastErrorCode))
                    End If
                Case MsgBoxResult.No
                    'Stop editing and do not save
                    sf.Table.StopEditingTable(False)
                    WL("Đã tắt chế độ edit và Không lưu bảng thuộc tính của shapefile.")
                Case MsgBoxResult.Cancel
                    Exit Sub
            End Select
        End If
        isDataChanged = False
        isEditingMode = False
        isAddingShape = False
        btnStartEditing.Text = "Start editing"
        AxM.IdentifiedShapes.Clear()
        For i As Integer = 0 To AxM.NumLayers - 1
            Dim sfx As Shapefile = New Shapefile()
            sfx = AxM.get_Shapefile(i)
            sfx.SelectNone()
        Next
        AxM.CursorMode = tkCursorMode.cmPan
    End Sub

Hope to see your reply soon.
Many thanks!

Hello @Lytrieu002

Just a couple quick things.

  1. Rather than going through the Table class (StartEditingTable and StopEditingTable), can you try instead using the higher level sf.StartEditShapes and sf.StopEditingShapes? These calls encapsulate the Table operations, and perform some additional steps, and I’m curious to see if the same problem occurs.
  2. If there is still a problem, do you mind submitting the Shapefile, or a small subset of the shapefile with the encoded strings. I can debug the OCX on this end to determine where the problem lies.

Thank you, and Regards,
Jerry

1 Like

Thanks @jerryfaust

I tried StartEditShapes with the following code but the same result, the string still become unreadable. :frowning:

Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
        Dim sf As Shapefile = New Shapefile()
        sf = AxM.get_Shapefile(txtLayerHandle.Text)
        If sf IsNot Nothing Then
            If sf.StartEditingShapes(True) Then
                WL("Turned on Editing.")
                If sf.StopEditingShapes(True, True) Then
                    WL("Turned off Editing and Saved!")
                End If
            End If
        End If
    End Sub

Please get this shapefile in attached file.
Cong_dexuat_gd1.zip (2.4 KB)

Hello @Lytrieu002

I found and fixed the problem and got it working. I’m perplexed as to how this has been broken for so long without being more evident - perhaps because we’ve been fixing Unicode issues as related to OGR datasources, but not looking at the issues as related to Unicode Shapefiles (perhaps they are less common).

So as it is, I’ve still got some things to work through. In this case, the code was assuming the ANSI code page, and I’ve now accounted for Shapefiles specified as UTF-8, but I’m still uncertain about how we are handling other code pages.

Then I need to document the changes and create a Jira issue to log the bug and the fix.

At the moment, I don’t have a work-around to suggest for you. If @pmeems is willing, perhaps we can get an update soon that incorporates the most recent fixes.

Regards,
Jerry.

1 Like

I agree that Unicode Shapefiles is less common, primarily in the Asia.
I hope that this problem will be fix soon. I am eager to hear good news from you @jerryfaust @pmeems

Many thanks.

Hi @jerryfaust

You said that you found and fixed the problem and got it working. And in this case, the code was assuming the ANSI code page. So can you tell me the way to force this to UTF8 code page, because all of my shapefiles is in UTF8.
Thank you so much.

Without a code change to the OCX, the Shapefile output will default to ANSI. It occurs to me, however, that you may be able to get around the problem by opening the Shapefile as an OGR datasource. I don’t have time right now to give you more detail, but I may be able to review it this weekend.

Regards,
Jerry.

1 Like

A Jira ticket has been created for this issue, MWGIS-240. Work is still in progress.

1 Like

In MapWinGIS v5.3 this change has been implemented.
@Lytrieu002 Can you download and install that version and let us know if it solves your issue?

2 Likes