Browse Source

添加shp解析空间信息和属性信息,并录入字段表的关联关系

LAPTOP-BJJ3IV5R\SIWEI 7 months ago
parent
commit
ef0458588c

+ 25 - 0
onemap-api/onemap-api-system/src/main/java/com/onemap/system/api/TableFiledRelationService.java

@@ -0,0 +1,25 @@
+package com.onemap.system.api;
+
+import com.onemap.common.core.constant.ServiceNameConstants;
+import com.onemap.common.core.web.domain.AjaxResult;
+import com.onemap.system.api.factory.TableFiledRelationFactory;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+
+import java.util.Map;
+
+@FeignClient(contextId = "tableFiledRelationService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = TableFiledRelationFactory.class)
+public interface TableFiledRelationService {
+
+    /**
+     * 修改【请填写功能名称】
+     * Map 对象
+     * <p>
+     * tableName string
+     * tableDescribe string
+     * tableColumns  List<Map<String, String>> (key 字段名 ,value 字段类型)
+     */
+    @PostMapping("/relation//add")
+    AjaxResult AddTableFiledRelation(@RequestBody Map<String, Object> tTableFiledRelation);
+}

+ 24 - 0
onemap-api/onemap-api-system/src/main/java/com/onemap/system/api/factory/TableFiledRelationFactory.java

@@ -0,0 +1,24 @@
+package com.onemap.system.api.factory;
+
+import com.onemap.common.core.web.domain.AjaxResult;
+import com.onemap.system.api.TableFiledRelationService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cloud.openfeign.FallbackFactory;
+
+import java.util.Map;
+
+public class TableFiledRelationFactory implements FallbackFactory<TableFiledRelationService> {
+    private static final Logger log = LoggerFactory.getLogger(SpatialFallbackFactory.class);
+
+    @Override
+    public TableFiledRelationService create(Throwable throwable) {
+        log.error("TableFiledRelationFactory服务调用失败:{}", throwable.getMessage());
+        return new TableFiledRelationService() {
+            @Override
+            public AjaxResult AddTableFiledRelation(Map<String, Object> tTableFiledRelation) {
+                return AjaxResult.error("内部错误");
+            }
+        };
+    }
+}

+ 36 - 7
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/service/impl/ShpFileSaveServiceImpl.java

@@ -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();
+            }
         }
 
     }

+ 15 - 0
onemap-modules/onemap-system/src/main/java/com/onemap/system/controller/TTableFiledRelationController.java

@@ -1,6 +1,7 @@
 package com.onemap.system.controller;
 
 import java.util.List;
+import java.util.Map;
 import javax.servlet.http.HttpServletResponse;
 
 import com.onemap.common.core.utils.poi.ExcelUtil;
@@ -95,4 +96,18 @@ public class TTableFiledRelationController extends BaseController {
     public AjaxResult remove(@PathVariable String[] ids) {
         return toAjax(tTableFiledRelationService.deleteTTableFiledRelationByIds(ids));
     }
+
+    /**
+     * 修改【请填写功能名称】
+     * Map 对象
+     * <p>
+     * tableName string
+     * tableDescribe string
+     * tableColumns  List<Map<String, String>> (key 字段名 ,value 字段类型)
+     */
+    @Log(title = "添加表和字段", businessType = BusinessType.UPDATE)
+    @PostMapping("/add")
+    public AjaxResult AddTableFiledRelation(@RequestBody Map<String, Object> tTableFiledRelation) {
+        return toAjax(tTableFiledRelationService.addTableFiledRelation(tTableFiledRelation));
+    }
 }

+ 3 - 0
onemap-modules/onemap-system/src/main/java/com/onemap/system/service/ITTableFiledRelationService.java

@@ -3,6 +3,7 @@ package com.onemap.system.service;
 import com.onemap.system.domain.TTableFiledRelation;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 【请填写功能名称】Service接口
@@ -59,4 +60,6 @@ public interface ITTableFiledRelationService
      * @return 结果
      */
     public int deleteTTableFiledRelationById(String id);
+
+    public int addTableFiledRelation(Map<String, Object> tTableFiledRelation);
 }

+ 57 - 0
onemap-modules/onemap-system/src/main/java/com/onemap/system/service/impl/TTableFiledRelationServiceImpl.java

@@ -1,12 +1,18 @@
 package com.onemap.system.service.impl;
 
+import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 import com.onemap.common.core.utils.DateUtils;
 import com.onemap.common.core.utils.StringUtils;
 import com.onemap.common.security.utils.SecurityUtils;
+import com.onemap.system.domain.TTable;
+import com.onemap.system.domain.TTableFiled;
 import com.onemap.system.domain.TTableFiledRelation;
+import com.onemap.system.mapper.TTableFiledMapper;
 import com.onemap.system.mapper.TTableFiledRelationMapper;
+import com.onemap.system.mapper.TTableMapper;
 import com.onemap.system.service.ITTableFiledRelationService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -21,6 +27,10 @@ import org.springframework.stereotype.Service;
 public class TTableFiledRelationServiceImpl implements ITTableFiledRelationService {
     @Autowired
     private TTableFiledRelationMapper tTableFiledRelationMapper;
+    @Autowired
+    private TTableMapper tTableMapper;
+    @Autowired
+    private TTableFiledMapper tTableFiledMapper;
 
     /**
      * 查询【请填写功能名称】
@@ -90,4 +100,51 @@ public class TTableFiledRelationServiceImpl implements ITTableFiledRelationServi
     public int deleteTTableFiledRelationById(String id) {
         return tTableFiledRelationMapper.deleteTTableFiledRelationById(id);
     }
+
+    @Override
+    public int addTableFiledRelation(Map<String, Object> tTableFiledRelation) {
+        String tableName = (String) tTableFiledRelation.get("tableName");
+        String tableDescribe = (String) tTableFiledRelation.get("tableDescribe");
+        List<Map<String, String>> tableCommsList = (List<Map<String, String>>) tTableFiledRelation.get("tableColumns");
+        String tableId = StringUtils.getUUID();
+        TTable tTable = new TTable();
+        tTable.setId(tableId);
+        tTable.setTableName(tableName);
+        tTable.setTableDescribe(tableDescribe);
+        tTable.setIsExist(0);
+        tTable.setTableType(1);
+        tTable.setCreateTime(new Date());
+        tTable.setCreateUserId(SecurityUtils.getUserId().toString());
+        tTable.setVersion(DateUtils.dateTimeNow());
+        tTableMapper.insertTTable(tTable);
+
+        Integer i = 0;
+        for (Map<String, String> tTableFileds : tableCommsList) {
+            String filedId = StringUtils.getUUID();
+            TTableFiled tTableFiled = new TTableFiled();
+            tTableFiled.setId(filedId);
+            tTableFiled.setFiledName(tTableFileds.get("key"));
+            tTableFiled.setFiledDescribe(tTableFileds.get("key"));
+            tTableFiled.setFiledZh(tTableFileds.get("key"));
+            tTableFiled.setFiledType(tTableFileds.get("value"));
+            tTableFiled.setCreateTime(new Date());
+            tTableFiled.setCreateUserId(SecurityUtils.getUserId().toString());
+            tTableFiledMapper.insertTTableFiled(tTableFiled);
+
+            TTableFiledRelation tTableFiledRelationNew = new TTableFiledRelation();
+            tTableFiledRelationNew.setId(StringUtils.getUUID());
+            tTableFiledRelationNew.setFiledId(filedId);
+            tTableFiledRelationNew.setTableId(tableId);
+            tTableFiledRelationNew.setCreateTime(new Date());
+            tTableFiledRelationNew.setIsDisplay(0);
+            tTableFiledRelationNew.setIsRequited(0);
+            tTableFiledRelationNew.setCreateUserId(SecurityUtils.getUserId().toString());
+            i++;
+            tTableFiledRelationNew.setSort(Long.parseLong(i.toString()));
+            tTableFiledRelationMapper.insertTTableFiledRelation(tTableFiledRelationNew);
+        }
+
+
+        return 0;
+    }
 }