||
java对于shp文件的读取是地理信息软件开发的一个基本的步骤
geotools是一个开源的软件包,对于shp文件的读取有十分的帮助
下面是基于geotools对于shp文件的读取实例
//shp文件的读取
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
File shpfile = new File(shppath);
ShapefileDataStore shpDataStore = (ShapefileDataStore)dataStoreFactory.createDataStore(shpfile.toURI().toURL());
shpDataStore.setCharset(Charset.forName("GBK"));//设置字符编码
String typeName = shpDataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
featureSource = shpDataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
int indexfield = result.getSchema().indexOf(indexname);
FeatureIterator<SimpleFeature> itertor = result.features();
int i = 0;
while (itertor.hasNext()) {
i++;
Document document = new Document();
SimpleFeature feature = itertor.next();
String fieldstr = feature.getAttribute(indexfield).toString();
//文本内容存储到索引记录库
if (fieldstr != null){
for (String[] strs:hotclick){
if (strs[2].equals(fieldstr)){
Field textfield = new TextField("content", fieldstr, Field.Store.YES);
float fscore = 10.0f;//对已经搜索的地名地址进行增加权重
textfield.setBoost(fscore);
document.add(textfield);
}
}
Field textfield = new TextField("content", fieldstr, Field.Store.YES);
document.add(textfield);
//把文档添加到索引库
indexWriter.addDocument(document);
}
if ((i % 10000) == 0){
System.out.println("完成 "+ i + " 个索引。");
}
}
itertor.close();
shpDataStore.dispose();//使用之后必须关掉
shpfile.exists();
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 19:18
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社