Trubleshooting: Shapefile garbled and character Truncation
Shapefile is a data format in the 1990s s, but it is still popular since it is open and easy to use. Recently, problems related to shapefile encoding have been constantly changing, and several frequently asked questions have been shared.
Why does my shapefile open garbled characters in ArcGIS 10.3?
Cause
The root cause of this problem is that the encoding type used to read files is different from the encoding type actually stored in files.
The Header file of the Shapefile (dBase Header) usually contains information about the encoding type used by the shapefile.LDID(Language Driver ID), so that when you use an application to open the shapefile, the application will know which encoding type to use to read it correctly without garbled characters. This information is usually contained in the shapefile data produced by ArcGIS Desktop. In the sub-file of the Shapefile, we sometimes find the same name*. BMPFile, the file also stores the encoding information, open with notepad, see such as UTF-8.
The priority of the two products identified by ArcGIS is,LDID takes precedence over the BMP file. That is, if the header file does not specify the encoding type for reading the shapefile, if there is just a BMP file, ArcGIS will use the encoding type here for reading.
We know that shapefile is an open format. As long as you understand the data specification, it can be produced independently from ArcGIS. In Windows Chinese Language settings, if you write code yourself or use a third-party program to produce shapefile, such as MapGISCP936 (GBK)Encoding storage, But no matter whether it is careless or intentionally not in the Data header file agreed "I used 936 !". If it is ArcGIS 10.2 or a previous version, it is okay. ArcGIS is identified in this way by default without garbled characters. HoweverArcGIS 10.2.1, ArcGIS 10.2.2, ArcGIS 10.3.xChaos found in these versions! Code! Now!When LDID and BMP are missing, these versions use the UTF-8 by default to read shapefile, which will inevitably be garbled.
Solution
Create a notepad next to the shapefile sub-file and change it to a BMP file with the same name. The text contentOemOr936.
In this way, when reading ArcGIS 10.2.1 and 10.2.10.3.x, you will know that your data is stored in this encoding type and read it in the correct way.
Why is the field name of Shapefile exported by ArcGIS 10.3 truncated into three Chinese characters? Isn't it possible to store at least four Chinese Characters in earlier versions?
Cause
This issue is still related to the encoding type.
ArcGIS 10.2 and earlier versions, ArcGIS encountered Chinese characters when writing shapefileDefaultUse the current Windows LanguageCharacter Set Encoding(Also calledCode Page/CodePage/OEM CodePage).Code Page 936 (GBK).
ArcGIS 10.2.1 and later versionsWhen ArcGIS writes shapefile,DefaultYesUTF-8Encoding type.
These two encoding types store the numbers of bytes used by Chinese characters. I also provided the wiki link in the above keywords. If you have any interest, go to the details page. In short, the shapefile itself has a limit of 9 bytes for the field. The Chinese Characters in CP936 encoding are usually stored in two bytes, so 9/2 = 4 Chinese characters can be stored; in UTF-8 encoding, Chinese characters must be stored in at least 3 bytes, so up to 9/3 = 3 Chinese characters can be stored.
For example, in the Python command line, we can find out the length of the Unicode string.
Solution Perfect Solution:
Using a geographical database, give up shapefile, and avoid various truncation problems. This is also the road to storing geographic data.
However, the fans of shapefile said, "My requirement is to restore the shapefile that previously stored 4 Chinese characters. I don't want to use a geographical database. I want to get the results of the old version of shapefile, I don't care what the shapefile encoding type is ...... Bla ......" Well, there are some ways to squeeze.
Workaround:
Note:
If you use10.2.1And10.2.2These two versions take effect only after patching. I have written another article before, detailsHere.
If you are using10.3.x, Then proceed directly.
Open the registry and go'
My Computer \ HKEY_CURRENT_USER \ Software \ ESRI \ Desktop 10.x'
Create item'Common', And then create'CodePage'Item, add'String', Name:DbfDefault, Healthy value:Oem (or 936).
So ArcGIS has TZ? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> encoding + Cgo8cD48YnI + PC9wPgoKPGgxIGlkPQ =" How do I detect the encoding type used by the current shapefile? ">How can I check the current shapefile encoding type?
Shapfile itself is an open format. Of course, there are many ways to view the internal structure of shapefile. There are many readers of this type of code. As long as you understand data standards, there should be a lot of methods.
Here I will share a simple way to view the encoding type through python.Struct moduleIt can process binary data to detect the LDID information in the header file of the Child dbf of the shapefile.
Python code example:
#-*-Coding: UTF-8-*-_ author _ = 'kikita 'import structdbf = u 'd: \ ExportShapefiles \ Chinese fields. dbf 'dat = open (dbf, 'rb '). read (30) [29:] id = struct. unpack ('B', dat) [0] print (id, hex (id ))
For example, the following print result is displayed:(77, '0x4d ')CodelPage 936 is found in the encoding table through this ID.
Of course, if you have a better method, the blogger can directly post it in the comment area for further discussion.
The above are the three most frequently asked questions. When these three questions are passed, many shapefile read and write garbled questions will be solved.