Geotools modifying Shapefile property name garbled problem

Source: Internet
Author: User

The discussion address for the GeoServer Chinese community is:http://opengeo.cn/bbs/read.php?tid=1701&page=e& #a

After modifying shapefiles with Geotools, the property names in the DBF file contents are garbled, but the property values are not garbled. The changes have not been garbled before.
And the code has been coded in the following way:

Shapefiledatastore shape = new Shapefiledatastore (URL), Shape.setstringcharset (Charset.forname ("GBK"));
Copy Code

My modified code is as follows:

    /** * Modify Shapefiles. * @param dataStore * @param fidstr the data to be modified corresponds to Featureid * @return */public static Boolean Updateshapefile (Sh          Apefiledatastore datastore,string fidstr) {defaulttransaction transaction = null;          Simplefeaturestore store = null;            try {datastore.setstringcharset (charset.forname ("GBK"));            string[] Featurenames = Datastore.gettypenames ();              String featurename = featurenames[0];                        Creates the default transaction object transaction = new Defaulttransaction ();              Also indicate the feature name used by the data source, usually the Shapefile file name and the Shapefile type name are usually the same.            store = (simplefeaturestore) datastore.getfeaturesource (featurename);            Associate default transaction and data source Store.settransaction (transaction);            Create filter FilterFactory2 FF = (FilterFactory2) commonfactoryfinder.getfilterfactory2 (null);            Set<featureid> fids = new hashset<featureid> (); Featureid FID =Ff.featureid (FIDSTR);            Fids.add (FID);            Filter filter = (filter) ff.id (FIDS);                        Modify the filtered feature according to the filter store.modifyfeatures ("Table number", "Test", filter);          Submit Transaction.commit ();             } catch (IOException e) {e.printstacktrace ();              try {//rollback transaction.rollback ();              } catch (IOException E1) {e1.printstacktrace ();        } return false;                  }finally{if (transaction!=null) {//Off transaction.close ();            transaction = NULL;    }} return true; }
Copy Code

After locating the problem is found in transaction.commit ();

and the file contents of SHP files also have Chinese characters, but there is no garbled problem, then from the dbf file header.

View the source code of the Geotools, found that the operation of the DBF file is under the ORG\GEOTOOLS\DATA\SHAPEFILE\DBF package, where the class Dbasefilewriter implementation of the DBF file write operations, Includes the write of the property name (head) and the write of the property value (body), while the head is written in the

    Public Dbasefilewriter (Dbasefileheader header, Writablebytechannel out, Charset Charset)            throws IOException {        Write the header        Header.writeheader (out);    }
Copy Code

The Writeheader method is defined in the Dbasefileheader class.

The original code is as follows:

              Write the field name for              (int j = 0; J < one; J + +) {                  if (Fields.fieldName.length () > J) {                      buffer.put ( BYTE) fields.fieldName.charAt (j));                  else {                      buffer.put ((byte) 0);                  }              }
Copy Code

This means that the attribute name is placed in buffer as a byte, a property name cannot be more than 11 bytes, and less than 11 is supplemented with (byte) 0.
The key is (byte) fields.fieldName.charAt (j), which takes two bytes for Chinese characters, except for the problem when it is converted from char to byte.
Take the following string:
String str = "capital";
byte[] bytes = Str.getbytes ();
for (int i=0;i<bytes.length;i++)
System.out.println (bytes);
The output should be:
-41
-54
This takes up two bytes, which is the right one.
If you follow the method in it:
char chars = ' capital ';
byte[] bytes = {(byte) chars};
for (int i=0;i<bytes.length;i++)
System.out.println (bytes);
The output is:
68
It's obviously not right here.

Just follow the above logic, modify the original code as follows (I'm just a method here, this code should be optimized again):
int j = 0;
for (int counter=0; j<11&&counter<fields.fieldname.length (); counter++) {
Char cha = fields.fieldName.charAt (counter);
String str = new String (cha+ "");
byte[] bytes = Str.getbytes ();
for (int k=0;k<bytes.length;k++) {
Buffer.put (Bytes[k]);
j + +;
}
}
if (j!=11) {
for (int k=0;k< (11-J); k++) {
Buffer.put ((byte) 0);
}
}



Geotools modifying Shapefile property name garbled problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.