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

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: GDAL 3.6 · QGIS 3.34 · shapefile.js 6.x
Symptom
Checklist
  1. Check for a .cpg file beside the .shp; GDAL reads it first, then ENCODING in .prj, then the system code page.
  2. Hex-dump one known CJK character: three bytes starting E4~E9 means UTF-8, two bytes starting B0~F7 means GBK.
  3. Run ogrinfo -al and look at a few field values for the mojibake pattern.
  4. Run an encoding detector over the .dbf instead of eyeballing the glyphs.
  5. 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
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

Atlaslog (2026). Shapefile attribute text garbled while geometry is perfect. https://www.jingweizhi.com/en/cookbook/shp-gbk-garbled.html (accessed 2026-11-03).