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

博文

Lucene利用geotools对shp文件的读取

已有 1893 次阅读 2017-10-24 15:20 |个人分类:java|系统分类:科研笔记

java读取shp文件

代码如下:

import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;

public class ShpRead {
public void readSHP(String shppath, String indexname) {
ShapefileDataStore shpDataStore = null;
try {
File shpfile = new File(shppath);
shpDataStore = new ShapefileDataStore(shpfile.toURI().toURL());
//设置字符编码
Charset charset = Charset.forName("GBK");
shpDataStore.setCharset(charset);
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();
while (itertor.hasNext()) {
SimpleFeature feature = itertor.next();
String fieldstr = feature.getAttribute(indexfield).toString();
}
itertor.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}




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

上一篇:Lucene中进行分词,并按照特定的词对document或者field加权boost
下一篇:java中vector的用法简单总结
收藏 IP: 111.195.160.*| 热度|

0

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

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

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

GMT+8, 2024-5-13 04:57

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部