[gdal-dev] Chinese Character for field name in ESRI Shapefile Problem

Even Rouault even.rouault at spatialys.com
Mon Oct 12 02:01:43 PDT 2015


Jhon Chin,
> Hi, everybody:
> 
> add one more question :
> 
> the field name can be read 10 ascii characters, the last characters would
> be trimmed, 

Not necessarily 10 ascii characters, but 10 bytes. The number of characters 
that can fit on 10 bytes depends on the encoding. 1 ASCII character = 1 byte. 
But for non ASCII characters such as Chinese characters, one character fits on 
several bytes.

> is there anyone encounter this problem?

Yes, everybody ! This is a limitation of the > 30 year old DBF format.

I've tried the following Python script that creates a shapefile with the CP936 
encoding.

{{{
# -*- coding: utf-8 -*-

from osgeo import ogr

ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('test_cp936.dbf')
lyr = ds.CreateLayer('test_cp936', options = ['ENCODING=CP936'])
lyr.CreateField(ogr.FieldDefn("归一化植被指数",ogr.OFTInteger))
}}}

It emits a warning about truncation since the field name cannot fit on 10 bytes 
once recoded in CP936.

Here's the result:

$ ogrinfo cp936.dbf -al
INFO: Open of `cp936.dbf'
      using driver `ESRI Shapefile' successful.

Layer name: cp936
Metadata:
  DBF_DATE_LAST_UPDATE=2015-10-12
Geometry: Line String
Feature Count: 0
Extent: (0.000000, 0.000000) - (0.000000, 0.000000)
Layer SRS WKT:
(unknown)
归一化植被: Integer (9.0)

So the last 2 characters are lost.

> 
> any help would be appreciated!
> 
>                Jhon Chen
> 
> On Mon, Oct 12, 2015 at 3:43 PM, Jhon Chin <giantchen2010 at gmail.com> wrote:
> > Hi, everybody:
> > 
> > I'm now trying to write polygonized result into an esri shapefile, and I
> > tried
> > 
> > to add some field to the attribute table in the shapefile. But it
> > corrupted
> > 
> > when i was trying to create an *ogrfielddefn* instance.The code goes
> > 
> > like this:
> >            OGRFieldDefn oField("归一化植被指数",OFTInteger);
> >            if(poLayer->CreateField(&oField) != OGRERR_NONE)
> >            {
> >            
> >                 printf("create 归一化植被指数 failed!");
> >                 exit(1);
> >            
> >            }

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list