|
@@ -8,6 +8,7 @@ import com.siwei.spatial.api.domain.file.TGeomDbDetails;
|
|
|
import com.siwei.spatial.service.file.ISpatialFilesDbService;
|
|
|
import com.siwei.spatial.service.file.ITGeomDbDetailsService;
|
|
|
import com.siwei.spatial.service.file.ITGeomDbService;
|
|
|
+import com.siwei.spatial.utils.FileUtils;
|
|
|
import org.geotools.data.FeatureSource;
|
|
|
import org.geotools.data.shapefile.ShapefileDataStore;
|
|
|
import org.geotools.data.simple.SimpleFeatureCollection;
|
|
@@ -67,26 +68,37 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public TGeomDb readShpFile(String shpFileName, String shpFilePath, String fromRoute) {
|
|
|
+ public TGeomDb readShpFile(String shpFileName, String shpFilePath, String fromRoute) throws IOException {
|
|
|
if (StringUtils.isEmpty(shpFilePath)) {
|
|
|
log.error("读取SHP文件路径失败");
|
|
|
return null;
|
|
|
}
|
|
|
- String path = shpFilePath.substring(0, shpFilePath.length() - 4);
|
|
|
- File shpFile = new File(localFilePath + path + ".shp");
|
|
|
- File dbfFile = new File(localFilePath + path + ".dbf");
|
|
|
- File shxFile = new File(localFilePath + path + ".shx");
|
|
|
+ String shpDir = shpFilePath.substring(0, shpFilePath.lastIndexOf(".zip"));
|
|
|
+ FileUtils.unzipAutoCharset(shpFilePath, shpDir);
|
|
|
+ List<Map<String, String>> shpFiles = FileUtils.readShpFiles(shpDir);
|
|
|
+
|
|
|
+ if (shpFiles.isEmpty()) {
|
|
|
+ log.error("读取SHP文件失败");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // TODO 目前只用了一个.shp文件,后续如果有多个shp文件,可能需要处理
|
|
|
+ Map<String, String> shpFileMap = shpFiles.get(0);
|
|
|
+ File shpFile = new File(shpFileMap.get("shp"));
|
|
|
+ File dbfFile = new File(shpFileMap.get("dbf"));
|
|
|
+ File shxFile = new File(shpFileMap.get("shx"));
|
|
|
+ File prjFile = new File(shpFileMap.get("prj"));
|
|
|
if (!shpFile.exists() || !dbfFile.exists() || !shxFile.exists()) {
|
|
|
log.error("读取SHP文件缺失");
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
if (StringUtils.isEmpty(shpFileName)) {
|
|
|
shpFileName = shpFile.getName().substring(0, shpFileName.length() - 4);
|
|
|
}
|
|
|
TGeomDb dto = new TGeomDb();
|
|
|
dto.setId(IdUtils.fastSimpleUUID());
|
|
|
dto.setName(shpFileName);
|
|
|
- dto.setShppath(path + ".shp");
|
|
|
+ dto.setShppath(shpFileMap.get("shp"));
|
|
|
dto.setFiletype("2");
|
|
|
dto.setFromroute(fromRoute);
|
|
|
|
|
@@ -165,25 +177,25 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
|
|
|
|
|
|
|
|
|
private String convertShpFieldType2H2GISOrPG(Class value) throws Exception {
|
|
|
- if (value == String.class) {//文本
|
|
|
+ if (value == String.class) {// 文本
|
|
|
return "varchar";
|
|
|
}
|
|
|
- if (value == Integer.class) {//短整型
|
|
|
+ if (value == Integer.class) {// 短整型
|
|
|
return "int";
|
|
|
}
|
|
|
- if (value == Long.class) {//长整型
|
|
|
+ if (value == Long.class) {// 长整型
|
|
|
return "bigint";
|
|
|
}
|
|
|
- if (value == Double.class || value == Float.class) {//浮点型 双精度 保留精度,比如在金币上运算更安全
|
|
|
+ if (value == Double.class || value == Float.class) {// 浮点型 双精度 保留精度,比如在金币上运算更安全
|
|
|
return "numeric";
|
|
|
}
|
|
|
if (value == Date.class) {
|
|
|
- return "timestamp without time zone";//日期, 使用包含时区的来处理
|
|
|
+ return "timestamp without time zone";// 日期, 使用包含时区的来处理
|
|
|
}
|
|
|
if (Geometry.class.isAssignableFrom(value)) {
|
|
|
return "public.geometry";
|
|
|
}
|
|
|
- //除了上述,真不知道还会有啥了。除非arcgis的shp又增加了新类型?!那无能为力了,抛出异常吧
|
|
|
+ // 除了上述,真不知道还会有啥了。除非arcgis的shp又增加了新类型?!那无能为力了,抛出异常吧
|
|
|
throw new Exception("不支持的类型!" + value.getName());
|
|
|
}
|
|
|
|
|
@@ -206,7 +218,7 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
|
|
|
try {
|
|
|
shapefileDataStore = new ShapefileDataStore(shpFile.toURI().toURL());
|
|
|
shapefileDataStore.setCharset(Charset.forName(readShpBM(shpFile.getAbsolutePath())));
|
|
|
- //这个typeNamae不传递,默认是文件名称
|
|
|
+ // 这个typeNamae不传递,默认是文件名称
|
|
|
FeatureSource featuresource = shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
|
|
|
|
|
|
// 获取 srid
|
|
@@ -216,7 +228,7 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
|
|
|
throw new Exception("SHP文件坐标系未查到");
|
|
|
}
|
|
|
|
|
|
- //获取当前数据的geometry类型(点、线、面)
|
|
|
+ // 获取当前数据的geometry类型(点、线、面)
|
|
|
// GeometryType geometryType = featuresource.getSchema().getGeometryDescriptor().getType();
|
|
|
// System.out.println(geometryType.getName());
|
|
|
// if ("Polygon".equals(geometryType.get) {
|
|
@@ -224,11 +236,11 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
|
|
|
// } else if ("MultiPolygon".equals(geomTypeData.getGeometryType())) {
|
|
|
// tb.add("the_geom", MultiPolygon.class);
|
|
|
// }
|
|
|
- //读取要素
|
|
|
+ // 读取要素
|
|
|
SimpleFeatureCollection simpleFeatureCollection = (SimpleFeatureCollection) featuresource.getFeatures();
|
|
|
- //获取当前矢量数据有哪些属性字段值
|
|
|
+ // 获取当前矢量数据有哪些属性字段值
|
|
|
|
|
|
- //获取字段列表
|
|
|
+ // 获取字段列表
|
|
|
List<String> attributesList = new ArrayList<>();
|
|
|
List<Map<String, String>> tableCommsList = new ArrayList<>();
|
|
|
List<AttributeDescriptor> attributes = simpleFeatureCollection.getSchema().getAttributeDescriptors();
|
|
@@ -243,7 +255,7 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
- //获取属性
|
|
|
+ // 获取属性
|
|
|
simpleFeatureIterator = simpleFeatureCollection.features();
|
|
|
while (simpleFeatureIterator.hasNext()) {
|
|
|
Map<String, Object> pg_rk = new LinkedHashMap<>();
|