import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class ShpFileSpelitter {
public static void main(String[] args) throws IOException {
shptocf("G:\\XQ\\jinzita\\caiqiemian\\caiqiemian.shp","F:/shp/");
}
// 定义一个将Shapefile文件转换成多个独立的Shapefile文件的方法,每个文件只包含一个多边形
// 输入参数:shpurl - Shapefile文件的路径; drc - 输出文件夹的路径
public static void shptocf(String shpurl, String drc) throws IOException {
// 创建输入的 Shapefile 数据存储
ShapefileDataStore inputDataStore = new ShapefileDataStore(new File(shpurl).toURI().toURL());
inputDataStore.setCharset(StandardCharsets.UTF_8);
// 获取输入的 SimpleFeature 集合
SimpleFeatureCollection inputFeatureCollection = inputDataStore.getFeatureSource().getFeatures();
// 创建输入的 SimpleFeature 迭代器
SimpleFeatureIterator inputFeatureIterator = inputFeatureCollection.features();
// 获取输入的几何字段的名字
String inputGeometryFieldName = inputFeatureCollection.getSchema().getGeometryDescriptor().getLocalName();
// 获取输入Shapefile的坐标系
CoordinateReferenceSystem inputCRS = inputFeatureCollection.getSchema().getCoordinateReferenceSystem();
// 遍历输入的 SimpleFeature
try {
while (inputFeatureIterator.hasNext()) {
// 获取当前的 SimpleFeature
SimpleFeature inputFeature = inputFeatureIterator.next();
// 获取当前的几何对象
Geometry inputGeometry = (Geometry) inputFeature.getAttribute(inputGeometryFieldName);
// 获取当前 SimpleFeature 的类型
SimpleFeatureType featureType = inputFeature.getType();
// 判断几何对象是否为 MultiPolygon 类型
if (inputGeometry instanceof MultiPolygon) {
// 将几何对象转换为 MultiPolygon 类型
MultiPolygon multiPolygon = (MultiPolygon) inputGeometry;
// 获取多边形的数量
int numOfPolygons = multiPolygon.getNumGeometries();
// 遍历每个多边形
for (int i = 0; i < numOfPolygons; i++) {
// 获取当前的多边形
Polygon polygon = (Polygon) multiPolygon.getGeometryN(i);
// 创建一个 SimpleFeatureBuilder 对象用于创建新的 SimpleFeature
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
// 创建一个存储属性值的列表
List<Object> attributeValues = new ArrayList<>();
// 遍历当前 SimpleFeature 的属性
for (int j = 0; j < featureType.getAttributeCount(); j++) {
// 如果当前属性为几何字段,则添加多边形对象
if (featureType.getDescriptor(j).getLocalName().equals(inputGeometryFieldName)) {
attributeValues.add(polygon);
} else {
// 其他属性添加原始值
attributeValues.add(inputFeature.getAttribute(j));
}
}
// 将属性值添加到 featureBuilder 中
featureBuilder.addAll(attributeValues);
// 使用 featureBuilder 构建新的 SimpleFeature 对象
SimpleFeature outputFeature = featureBuilder.buildFeature(null);
// 将新的 SimpleFeature 写入单独的 Shapefile 文件
saveFeatureToShapefile(outputFeature, drc, inputFeature.getID(), i, inputCRS);
}
} else {
// 如果几何对象不是 MultiPolygon 类型,直接保存为 Shapefile 文件
saveFeatureToShapefile(inputFeature, drc, inputFeature.getID(), 0,inputCRS);
}
}
} finally {
// 关闭输入的 SimpleFeature 迭代器
inputFeatureIterator.close();
}
}
// 保存 SimpleFeature 对象到单独的 Shapefile 文件的辅助方法
// 输入参数:feature - 要保存的 SimpleFeature;drc - 输出文件夹的路径;id - 用于构造输出文件名的ID字符串;
private static void saveFeatureToShapefile(SimpleFeature feature, String drc, String id, int index,CoordinateReferenceSystem inputCRS) throws IOException {
// 创建一个 ShapefileDataStoreFactory 对象用于产生 Shapefile 数据存储
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
// 创建输出文件路径
File outputFile = new File(drc + id + "_"+feature.getAttribute("name")+"_"+ index + ".shp");
// 创建输出的 Shapefile 数据存储
ShapefileDataStore outputDataStore = (ShapefileDataStore) dataStoreFactory.createDataStore(outputFile.toURI().toURL());
// 创建一个 SimpleFeatureTypeBuilder 对象,用于构建新的 SimpleFeature 类型
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
outputDataStore.setCharset(StandardCharsets.UTF_8);
// 设置 SimpleFeature 类型的名称
typeBuilder.setName(feature.getType().getName());
// 设置坐标参考系
typeBuilder.setCRS(inputCRS != null ? inputCRS : DefaultGeographicCRS.WGS84);
// 添加所有的属性描述符
typeBuilder.addAll(feature.getType().getAttributeDescriptors());
// 构建新的 SimpleFeature 类型
SimpleFeatureType outputType = typeBuilder.buildFeatureType();
// 创建输出的 SimpleFeature 类型的架构
outputDataStore.createSchema(outputType);
// 强制输出的 Shapefile 使用 WGS84 坐标参考系
outputDataStore.forceSchemaCRS(inputCRS != null ? inputCRS : DefaultGeographicCRS.WGS84);
// 创建一个事务对象,用于写入 Shapefile 的操作
Transaction transaction = new DefaultTransaction("create");
try {
// 创建一个 FeatureWriter 对象,用于写入输出的 Shapefile
FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = outputDataStore.getFeatureWriterAppend(transaction);
// 获取要写入的新 SimpleFeature 对象
SimpleFeature newFeature = featureWriter.next();
// 设置新 SimpleFeature 的属性
newFeature.setAttributes(feature.getAttributes());
// 写入 Shapefile
featureWriter.write();
// 提交事务
transaction.commit();
// 关闭 FeatureWriter
featureWriter.close();
} catch (Exception e) {
// 如果写入过程中发生异常,则回滚事务
e.printStackTrace();
transaction.rollback();
} finally {
// 关闭事务
transaction.close();
}
}
}
使用geotools系列版本为17.0,jts-core版本为1.16.1