引入geotools依赖
<!-- geotools依赖 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>15.4</version>
</dependency>
判断shape文件的合法性
总体思路:通过读取shp文件的几何类型和空间参考信息来判断合法性。如果都读取成功,则说明是合法的shp文件。(在某些场景下空间参考不是必须的)
public static boolean isLegalShp(String shpPath) throws IOException {
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
try {
FileDataStore dataStore = factory.createDataStore(new File(shpPath).toURI().toURL());
GeometryDescriptor descriptor = dataStore.getFeatureSource().getSchema().getGeometryDescriptor();
// 获取几何类型
GeometryType type = descriptor.getType();
// 获取空间参考信息
CoordinateReferenceSystem coord = descriptor.getCoordinateReferenceSystem();
if (type == null || coord == null) {
log.error("incorrect shapefile");
return false;
}
log.info("correct shapefile, geometry type:{}, coordinate system:{}", type.getName(), coord.getCoordinateSystem());
return true;
} catch (IOException e) {
log.error("get shp url error", e);
return false;
}
}