Create shapefile with UTF-8 encoding

I am trying to write a C# program in the environment of VS2017, using .NET 4.6.1, and the library is MAPWINGIS 5.3. After saving a shapefile using the syntax sf.SafeAS(), I noticed that the encoding of this shapefile seen in QGIS is ISO 8859-1.
Because the strings within the file contain Traditional Chinese characters, I need to save the Shapefile file in UTF-8 encoding.
I found on the forum that encoding can be specified using GlobalSettings, but I cannot find the association between Shapefile and GlobalSettings in the examples. Could someone please tell me how to achieve this? thank you!

 // point shapefile, has 2 fields and 5 instances
               
 var sf = new Shapefile();
 bool result = sf.CreateNewWithShapeID("", ShpfileType.SHP_POINT);
            
 // add field
 int nameFieldIndex = sf.EditAddField("名稱", FieldType.STRING_FIELD, 15, 18);
 int numFieldIndex = sf.EditAddField("點號", FieldType.STRING_FIELD, 15, 18);

 Shape shp = new Shape();
 shp.Create(ShpfileType.SHP_POINT);

 // create random coordinates for 5 instances
 for (int i = 0; i < 5; i++)
 {
       var pnt = new MapWinGIS.Point();                 
       pnt.x = 121 + i * 2;
       pnt.y = 23 + i * 2;

       shp.InsertPoint(pnt, ref i);
       sf.EditInsertShape(shp, ref i);
       sf.EditCellValue(nameFieldIndex, i, "武嶺點"+ i );
       sf.EditCellValue(numFieldIndex, i, "T0001");

 }
 
 sf.SaveAs(TextBox1.Text + @"\" + file.Name + ".shp",null);

Hi, @anthony206 ,
I just encountered the same issue as yours. Have you got some solutions?

Hello @anthony206

I did some work a few years back to make sure it would read the DBF in UTF-8 format if either there was no Codepage file, or if the Codepage was specifically set to UTF-8. And it looks like that should be the default when creating a Shapefile as well. So I’m not sure, off hand, what is going on.

As a longshot, you might try creating a Codepage file, naming it based on your shapefile name (<shapefilename>.cpg). The contents of the file would just be the text UTF-8, no quotes. See if it then reads your file properly.

If not, it might take some research and debugging to figure why it is not being created as UTF-8.

Regards,
Jerry.

Hello, @jerryfaust @anthony206
I have done some work to figure out the encoding problem in MapWinGIS and got some progress, here I’d like to share it to you.

  1. The encoding shown in QGIS is not the shapefile’s real encoding. If there is a cpg file with the shapefile, QGIS will display the shapefile’s encoding by reading it from cpg file. Otherwise, QGIS will display “iso-8859-1” as default.
  2. When creating a shapefile in MapWinGIS using Shapefile.CreateNew or Shapefile.CreateNewWithShapeID method, the encoding used is the operating system’s default code page(mine is CP936), and no cpg file is generated. Unluckly, we can not specify the encoding in the methods. So if your operating system’s default code page doesn’t support Chinese characters, the field value containing Chinese charactersin the shapefile attributes table will not be displayed normally when you check the shapefile in QQGIS or ArcMap.
  3. About the Shapefile.SaveAs method, if there isn’t a cpg file with the shapefile, it will save the shapefile as a copy with the operating system’s default code page; otherwise, it will save the shapefile as a copy with the encoding specified in the cpg file. But despite of having the cpg file or not, it will not generate a cpg file for the saved shapefile.
    The encoding issue is important for MapWinGIS’s multi-language compatibility and friendly data exchange. So I have a suggestion for MapWInGIS dev team: Create shapefile using UTF-8 encoding or specify it manually in code, and generate its cpg file when using Save or SaveAs or SaveAsEx method.
    Please correct if any mistakes.
    Wish to see improvements in the next release of MapWinGIS. It’s a really nice component for GIS developers!