linqy的个人博客分享 http://blog.sciencenet.cn/u/linqy

博文

java基于geotools开源包对shp文件的读取

已有 4111 次阅读 2017-10-23 19:48 |个人分类:java|系统分类:科研笔记

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();




https://blog.sciencenet.cn/blog-3134052-1082154.html

上一篇:java文件的读写与与路径的判断
下一篇:获取Lucene分词的内容
收藏 IP: 111.195.160.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-13 10:13

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部