public static void importShpToPG(String shpfile, String charsetName ) throws IOException {
ShapefileDataStore shpDataStore = null;
//shpfile="E:\\jinzita\\caiqie\\caiqiemian.shp";
// charsetName="GBK";
// TimeUtils.startTime("read Shp feature");
try {
File file = new File(shpfile);
shpDataStore = new ShapefileDataStore(file.toURI().toURL());
//设置字符编码
Charset charset = Charset.forName(charsetName);
shpDataStore.setCharset(charset);
String typeName = shpDataStore.getTypeNames()[0];
System.out.println("name: " + typeName);
SimpleFeatureType schema = shpDataStore.getSchema(typeName);
CoordinateReferenceSystem srs = schema.getCoordinateReferenceSystem();
System.out.println(srs.toWKT());
Integer epsg = CRS.lookupEpsgCode(srs, true);
if (epsg == null) {
epsg = 4326;
}
String wkid = "" + epsg;
System.out.println("wkid:" + wkid);
SimpleFeatureSource featureSource = null;
featureSource = shpDataStore.getFeatureSource(typeName);
SimpleFeatureCollection result = featureSource.getFeatures();
System.out.println("feature size: " + result.size());
SimpleFeatureIterator itertor = result.features();
while (itertor.hasNext()) {
SimpleFeature feature = itertor.next();
Collection<Property> p = feature.getProperties();
Iterator<Property> it = p.iterator();
String geometry = "";
String id = feature.getID();
FeatureId fid = feature.getIdentifier();
while (it.hasNext()) {
Property pro = it.next();
String type = Classes.getShortName(pro.getType().getBinding());
System.out.println(type);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
说明:该实现需要Java的gdal库和jar包
连接:gdal https://www.aliyundrive.com/s/LR6boSBEFNF 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。
标签:shp,java,读取,shpDataStore,feature,System,println,out,String From: https://www.cnblogs.com/zcxxcvbn/p/16879844.html