Atlaslog / Field Notes / Loading & display
Field note · Loading & display
Shapefile attribute text garbled while geometry is perfect
Quick answer
The DBF stores no encoding, so GDAL checks .cpg, then ENCODING in .prj, then falls back to the system code page. UTF-8 bytes read as GBK produce strings of rare CJK glyphs, while GBK read as UTF-8 produces question marks.
Symptom
- The NAME field shows a run of three rare CJK glyphs where the city name should be, and another file shows ?????.
- ogr2ogr prints Warning 1: One or several characters couldn't be converted correctly from UTF-8 to ISO-8859-1.
- The border lines are pixel-perfect while every place name is unreadable.
Checklist
- Check for a .cpg file beside the .shp; GDAL reads it first, then ENCODING in .prj, then the system code page.
- Hex-dump one known CJK character: three bytes starting E4~E9 means UTF-8, two bytes starting B0~F7 means GBK.
- Run ogrinfo -al and look at a few field values for the mojibake pattern.
- Run an encoding detector over the .dbf instead of eyeballing the glyphs.
- For the browser, pass encoding explicitly to shapefile.js rather than relying on auto-detection.
Fix
Write a .cpg file containing UTF-8 (or GBK) next to the shapefile, or rewrite with ogr2ogr -lco ENCODING=UTF-8; in shapefile.js pass {encoding: 'gbk'} or {encoding: 'utf-8'} explicitly.
Code
# 查看 dbf 编码声明
ogrinfo -al -so districts.shp | Select-String -Pattern "Encoding"
# 用 chardet 检测
python -c "import chardet;print(chardet.detect(open('districts.dbf','rb').read(20000)))"
# {'encoding': 'GB2312', 'confidence': 0.99}# 方案一:写 .cpg 声明实际编码(GBK)
Set-Content -Path districts.cpg -Value "GBK" -Encoding ASCII
# 方案二:整体转成 UTF-8,同时生成 .cpg
ogr2ogr -f "ESRI Shapefile" out districts.shp -lco ENCODING=UTF-8
# 方案三:GeoJSON 输出时指定源编码
ogr2ogr -f GeoJSON districts.geojson districts.shp -lco ENCODING=GBK
Watch out
- Setting the QGIS layer encoding without re-exporting, so other consumers still see mojibake.
- Assuming UTF-8 always and re-saving GBK bytes as UTF-8, which turns every name into question marks permanently.
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). Shapefile attribute text garbled while geometry is perfect. https://www.jingweizhi.com/en/cookbook/shp-gbk-garbled.html (accessed 2026-11-03).