How to modify the shapefile attribute in geotools

Source: Internet
Author: User

In geoserver ChineseCommunityThe discussion address is: http://opengeo.cn/bbs/read.php? Tid = 1701 & page = E & #

After you use geotools to modify the shapefile, you can find that all attribute names in the DBF file are garbled, but the attribute values are not garbled. No garbled characters have been found before the modification.
In additionCodeThe encoding method has been set as follows:

 
Shapefiledatastore shape =NewShapefiledatastore (URL );
Shape. setstringcharset (charset. forname ("GBK "));

The modified code is as follows:

     /**  
* Modify the shapefile.
* @ Param Datastore
* @ Param Featureid of the data to be modified by fidstr
* @ Return
*/
Public Static Boolean Updateshapefile (shapefiledatastore datastore, string fidstr)
{
Defaulttransaction transaction = Null ;
Simplefeaturestore store = Null ;
Try {
Datastore. setstringcharset (charset. forname ("GBK "));
String [] featurenames = datastore. gettypenames ();
String featurename = featurenames [0];
// Create default transaction object
Transaction = New Defaulttransaction ();

// It also indicates the element name used by the data source. Generally, the shapefile name and the shapefile type name are the same.
Store = (simplefeaturestore) datastore. getfeaturesource (featurename );
// Associate default transactions with data sources
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 ("data 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 ){
// Close
Transaction. Close ();
Transaction = Null ;
}
}
Return True ;
}

Transaction. Commit ();

In addition, the content of the SHP file also contains Chinese characters, but there is no garbled problem, so start with the DBF file header.

ViewSource codeThe operations on DBF Files are found in the org \ geotools \ data \ shapefile \ DBF package. dbasefilewriter class implements write operations on DBF Files, including the property name (head) and write the attribute value (body), while the head is written in

 
PublicDbasefilewriter (dbasefileheader header, writablebytechannel out, charset)
ThrowsIoexception {
// Write the header
Header. writeheader (out );
......
}

The writeheader method is defined in the dbasefileheader class.

The original code is as follows:

//Write the field name
For(IntJ = 0; j <11; j ++ ){
If(Fields. fieldname. Length ()> J ){
Buffer. Put ((Byte) Fields. fieldname. charat (j ));
}Else{
Buffer. Put ((Byte) 0 );
}
}

This means that the attribute name is put into buffer in the form of byte. The number of bytes occupied by an attribute name cannot exceed 11. If the number is less than 11, the attribute name is supplemented with (byte) 0.
The key lies in (byte) fields. fieldname. charat (j). For Chinese characters, it occupies two bytes. Here, the conversion from Char to byte excludes the problem.
Take the following string as an example:
String STR = "asset ";
Byte [] bytes = Str. getbytes ();
For (INT I = 0; I <bytes. length; I ++)
System. Out. println (bytes );
The output result should be:
-41
-54
It occupies two bytes, which is correct.
If you follow the method in it:
Char chars = 'created ';
Byte [] bytes = {(byte) chars };
For (INT I = 0; I <bytes. length; I ++)
System. Out. println (bytes );
The output result is:
68
This is obviously incorrect.

According to the above logic, modify the original code as follows (I am only 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 (); // changeByte [] bytes = Str. getbytes ("GBK"); otherwise, Chinese characters cannot be recognized.
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 );
}
}

 

 

Reprinted from:

Http://opengeo.cn/bbs/read.php? Tid = 1701 & page = E & #

Http://www.cnblogs.com/lingxue3769/archive/2011/11/17/2252659.html

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.