||
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();
}
}
}
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-5-13 04:57
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社