|
@@ -3,14 +3,19 @@ package com.onemap.spatial.service.impl;
|
|
|
import com.onemap.common.core.utils.StringUtils;
|
|
|
import com.onemap.spatial.mapper.ShpFileMapper;
|
|
|
import com.onemap.spatial.service.IShpFileSaveService;
|
|
|
+import com.onemap.system.api.TableFiledRelationService;
|
|
|
import org.geotools.data.FeatureSource;
|
|
|
import org.geotools.data.shapefile.ShapefileDataStore;
|
|
|
import org.geotools.data.simple.SimpleFeatureCollection;
|
|
|
import org.geotools.data.simple.SimpleFeatureIterator;
|
|
|
+import org.geotools.data.simple.SimpleFeatureSource;
|
|
|
+import org.geotools.referencing.CRS;
|
|
|
import org.locationtech.jts.geom.Geometry;
|
|
|
import org.opengis.feature.simple.SimpleFeature;
|
|
|
+import org.opengis.feature.simple.SimpleFeatureType;
|
|
|
import org.opengis.feature.type.AttributeDescriptor;
|
|
|
import org.opengis.feature.type.GeometryType;
|
|
|
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -24,6 +29,8 @@ public class ShpFileSaveServiceImpl implements IShpFileSaveService {
|
|
|
|
|
|
@Resource
|
|
|
private ShpFileMapper shpFileMapper;
|
|
|
+ @Resource
|
|
|
+ private TableFiledRelationService tableFiledRelationService;
|
|
|
|
|
|
@Override
|
|
|
public void readFile(String filepath) {
|
|
@@ -63,12 +70,22 @@ public class ShpFileSaveServiceImpl implements IShpFileSaveService {
|
|
|
* 直接使用shapefileDatastore,如果不知道,也可以使用工厂模式(见下个方法)
|
|
|
* 建议,如果确定是shape文件,就直使用shapefileDatastore
|
|
|
*/
|
|
|
+ ShapefileDataStore shapefileDataStore = null;
|
|
|
+ SimpleFeatureIterator simpleFeatureIterator = null;
|
|
|
try {
|
|
|
- ShapefileDataStore shapefileDataStore = new ShapefileDataStore(shpFile.toURI().toURL());
|
|
|
+ shapefileDataStore = new ShapefileDataStore(shpFile.toURI().toURL());
|
|
|
shapefileDataStore.setCharset(Charset.forName("UTF-8"));
|
|
|
//这个typeNamae不传递,默认是文件名称
|
|
|
FeatureSource featuresource = shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
|
|
|
|
|
|
+
|
|
|
+ // 获取 srid
|
|
|
+ CoordinateReferenceSystem crs = featuresource.getSchema().getCoordinateReferenceSystem();
|
|
|
+ Integer srid = CRS.lookupEpsgCode(crs, true);
|
|
|
+ if (srid == null) {
|
|
|
+ throw new Exception("SHP文件坐标系未查到");
|
|
|
+ }
|
|
|
+
|
|
|
//获取当前数据的geometry类型(点、线、面)
|
|
|
GeometryType geometryType = featuresource.getSchema().getGeometryDescriptor().getType();
|
|
|
System.out.println(geometryType.getName());
|
|
@@ -95,6 +112,7 @@ public class ShpFileSaveServiceImpl implements IShpFileSaveService {
|
|
|
tableCommsList.add(attributesMap);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
//添加ID
|
|
|
Map<String, String> attributesMap = new LinkedHashMap<>();
|
|
|
attributesMap.put("key", "id");
|
|
@@ -109,11 +127,17 @@ public class ShpFileSaveServiceImpl implements IShpFileSaveService {
|
|
|
|
|
|
System.out.println(tableCommsList.toString());
|
|
|
|
|
|
- String tableName = "temporary" + "_" + System.currentTimeMillis() + "_" + StringUtils.getUUID();
|
|
|
+ String tableName = "z_shp" + "_" + System.currentTimeMillis() + "_" + StringUtils.getUUID();
|
|
|
shpFileMapper.createTable(tableCommsList, tableName);
|
|
|
+
|
|
|
+ Map<String, Object> table_data_0 = new HashMap<>();
|
|
|
+ table_data_0.put("tableName", tableName);
|
|
|
+ table_data_0.put("tableDescribe", "shp解析表" + tableName);
|
|
|
+ table_data_0.put("tableColumns", tableCommsList);
|
|
|
+ tableFiledRelationService.AddTableFiledRelation(table_data_0);
|
|
|
|
|
|
//获取属性
|
|
|
- SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
|
|
|
+ simpleFeatureIterator = simpleFeatureCollection.features();
|
|
|
while (simpleFeatureIterator.hasNext()) {
|
|
|
Map<String, Object> pg_rk = new LinkedHashMap<>();
|
|
|
List<Object> pg_rk_list = new ArrayList<>();
|
|
@@ -130,20 +154,25 @@ public class ShpFileSaveServiceImpl implements IShpFileSaveService {
|
|
|
|
|
|
Geometry geometry = (Geometry) simpleFeature.getAttribute("the_geom");
|
|
|
String geom = geometry.toText();
|
|
|
- String ewkt = "SRID=4326;" + geom;
|
|
|
+ String ewkt = "SRID=" + srid + ";" + geom;
|
|
|
ewkt = "public.st_transform(public.st_geomfromewkt('" + ewkt + "'), 4326)";
|
|
|
System.out.println(ewkt);
|
|
|
pg_rk.put("geom", simpleFeature.getAttribute("the_geom").toString());
|
|
|
shpFileMapper.insertTableData(pg_rk_list, tableName, ewkt);
|
|
|
}
|
|
|
- simpleFeatureIterator.close();
|
|
|
- shapefileDataStore.dispose();
|
|
|
+
|
|
|
+
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
} finally {
|
|
|
-
|
|
|
+ if (simpleFeatureIterator != null) {
|
|
|
+ simpleFeatureIterator.close();
|
|
|
+ }
|
|
|
+ if (shapefileDataStore != null) {
|
|
|
+ shapefileDataStore.dispose();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|