I read a shapefile (zipped) with GDAL library in C#. Shapefile contains .cpg file with "UTF-8" encoding specified.

When I call Feature.GetFieldAsString(attributeName) some of the attribute values get trimmed at the end, and some have broken symbols.

Converting shapefile to GeoJSON with ogr2ogr makes a GeoJSON file with all attributes as they are supposed to be.

For example, value returned by GetFieldAsString is "\"Тер�", when the real value, also seen in geojson is "\"Терем+\"".

This doesn't seem to be an issue with encoding, for the beginning of the string is read correctly. But I have no clue where to look for the solution for this issue.

  • 1
    I believe the encoding does have a role. Beginning of your string is pure ASCII characters and they do not expose the problem. – user30184 Sep 11 '19 at 18:44
  • Only " and + characters in the string are ASCII, the rest are cyrillic characters. – Maxim Gritsenko Sep 12 '19 at 5:14
  • Test data and C# code that shows the problem would be appreciated. Python code equivalent might be even better because I guess there are not so many C# programmers here. – user30184 Sep 12 '19 at 5:34

I've debugged the issue thoroughly and found the route cause. GDAL library itself returns correct value as a pointer to the attribute string, but the C# bindings, generated by SWIG, have some strange code that truncates valid UTF-8 string into a broken one.

It may be because we use an outdated GDAL.Core Nuget package, or the issue is in the binding itself. Still looking for a nice solution, but at least now I have somewhere to start from.

  • Please if you find a bug in GDAL create a ticket in GitHub with test data that can be used for re-producing the issue. – user30184 Sep 20 '19 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.