Solutions for geotools supporting Chinese Characters

Source: Internet
Author: User

[Site]: http://www.blogjava.net/sinoly/archive/2007/09/30/geotools_shape.html

It has been plagued for several days and has been trying various methods to solve the problem of geotools reading the SHP format for Chinese encoding. However, an unintentional act yesterday made me realize that I had done too much useless work. After carefully reading the source code of javadoc and shapefile, we can see that the shapefiledatastore constructor has changed a lot:

Public static final charset default_string_charset = charset. forname ("ISO-8859-1 ");

This is the character encoding defined in the shapefiledatastore object. It is precisely because of its use of ISO-8859-1 encoding as the default encoding, so for a long time, to solve the Chinese problems of geotools or geoserver is always endless.
Let's take a look at the new shapefiledatastore constructor I saw in 2.4 (of course, beta and I chat that it seems that 2.3 has made the same change, but unfortunately I didn't get the source code of 2.3, huh, huh, however, versions earlier than 2.2 may seem different. Right when it is "new" in 2.4)

1 Public shapefiledatastore (URL, Boolean usememorymappedbuffer)
2 throws malformedurlexception
3 {
4 This (URL, usememorymappedbuffer, default_string_charset );
5}
6
7 public shapefiledatastore (URL, Boolean usememorymappedbuffer, charset dbfcharset)
8 throws malformedurlexception
9 {
10 readwritelock = new lock ();
11 namespace = NULL;
12 This. usememorymappedbuffer = true;
13 string filename = NULL;
14 shpurl = shapefiledatastorefactory. toshpurl (URL );
15 dbfurl = shapefiledatastorefactory. todbfurl (URL );
16 shxurl = shapefiledatastorefactory. toshxurl (URL );
17 prjurl = shapefiledatastorefactory. toprjurl (URL );
18 xmlurl = shapefiledatastorefactory. toxmlurl (URL );
19 this. dbfcharset = dbfcharset;
20}


The code for using geotools 2.4 to operate SHP format in column is as follows:
Code 1:

1 public class readshape {
2 public static void main (string [] ARGs)
3 throws filenotfoundexception, malformedurlexception, ioexception {
4
5 file shpfile = new file ("SHP/city map _ point. DBF ");
6. shapefiledatastore shpdatastore = new shapefiledatastore (shpfile. tourl ());
7 shpdatastore. setstringcharset (charset. forname ("GBK "));
8 featuresource FS = shpdatastore. getfeaturesource ();
9 featurecollection collection = FS. getfeatures ();
10 featureiterator iterator = collection. features ();
11 int numofattr = 0;
12 try {
13 while (iterator. hasnext ()){
14 feature = iterator. Next ();
15 numofattr = feature. getnumberofattributes ();
16 For (INT I = 0; I <numofattr; I ++ ){
17 string temp = feature. getattribute (I). tostring ();
18 system. Out. Print (temp + "/t ");
19}
20
21 system. Out. println ();
22}
23}
24 finally {
25 iterator. Close ();
26}
27}
28}

Code 2:

1 public class readshp {
2 public static void main (string [] ARGs)
3 throws filenotfoundexception, malformedurlexception, ioexception {
4
5 // initialize the filechannel object
6 filechannel in = new fileinputstream ("SHP/city map _ point. DBF"). getchannel ();
7 dbasefilereader DR = new dbasefilereader (in, true, charset. forname ("UTF-8 "));
8 dbasefileheader DH = dr. getheader ();
9 int fields = DH. getnumfields ();
10 For (INT I = 0; I <fields; I ++ ){
11 system. Out. Print (DH. getfieldname (I) + ""); // print the current attribute name
12}
13 system. Out. Print ("/N ");
14 While (dr. hasnext ()){
15 dbasefilereader. Row row = dr. readrow ();
16 For (INT I = 0; I <fields; I ++ ){
17 Object Data = row. Read (I );
18 if (DH. getfieldname (I). Equals ("name ")){
19 system. Out. Print (data );
20} else {
21 system. Out. Print (data );
22}
23 system. Out. Print ("/t ");
24}
25 system. Out. println ();
26}
27 dr. Close ();
28}
29}

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.