【网址】:http://www.blogjava.net/sinoly/archive/2007/09/30/geotools_shape.html
困扰了好几天,一直在尝试各种方法解决Geotools读取shp格式对中文编码的问题。可是昨天一个无意的举动居然让我发觉自己做了太多的无用之功。仔细的看了JavaDoc以及shapefile源代码之后,可以可以明显的看到ShapefileDataStore的构造函数已经发生了很大的变化:
public static final Charset DEFAULT_STRING_CHARSET = Charset.forName("ISO-8859-1");
这个是ShapefileDataStore对象中定义的字符编码。也正是由于其使用ISO-8859-1编码作为默认编码,所以一直以来,解决geotools抑或geoserver的中文问题总是连绵不绝。
来看看我在2.4中看到的ShapefileDataStore的新的构造函数(当然,贝塔和我聊天说过好像2.3也是作出了同样修改的,可惜我没下2.3的源码,呵呵,但是2.2以前的版本这一块貌似是不同的。权当其是2.4中的“新增”之处吧)
public ShapefileDataStore(URL url, boolean useMemoryMappedBuffer)2
throws MalformedURLException3
{4
this(url, useMemoryMappedBuffer, DEFAULT_STRING_CHARSET);5
}6

7
public ShapefileDataStore(URL url, boolean useMemoryMappedBuffer, Charset dbfCharset)8
throws MalformedURLException9
{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
}
列下使用Geotools 2.4操作shp格式的代码如下:
代码1:
public class ReadShape {2
public static void main(String[] args) 3
throws FileNotFoundException,MalformedURLException,IOException{4
5
File shpFile=new File("shp/市区地物_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 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
}
代码2:
public class ReadSHP {2
public static void main(String[] args) 3
throws FileNotFoundException,MalformedURLException,IOException{4

5
//初始化FileChannel对象6
FileChannel in = new FileInputStream("shp/市区地物_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)+" ");//打印当前属性名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
}
本文介绍如何使用 GeoTools 2.4 版本解决读取 shp 文件时遇到的中文乱码问题,并提供两种示例代码。
2463

被折叠的 条评论
为什么被折叠?



