Explorar el Código

宗地房屋属性及矢量数据入库

chenendian hace 1 mes
padre
commit
c0e67ae9ad

+ 1 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/cadastre/impl/ZymlServiceImpl.java

@@ -120,7 +120,7 @@ public class ZymlServiceImpl implements IZymlService {
                 c.setLegend(cur.getLegend());
                 c.setUrl(cur.getUrl());
                 c.setSource(cur.getSde());
-                c.setSjly(cur.getSjlx());
+                c.setSjly(cur.getSjly());
                 c.setFavorite(cur.getMy());
                 c.setMinimumlevel(cur.getMinimumlevel());
                 c.setMaximumlevel(cur.getMaximumlevel());

+ 26 - 5
siwei-modules/siwei-spatial/src/main/java/com/siwei/spatial/controller/file/SpatialFilesDbController.java

@@ -9,9 +9,11 @@ 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.ShpPostgisTableBuilder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
@@ -149,15 +151,34 @@ public class SpatialFilesDbController extends BaseController {
         if(StringUtils.isBlank(filePath)){
             filePath = "C:\\Users\\Administrator\\Desktop\\07\\shp_deom\\给一码管地\\地籍区.shp";
         }
+//        File shp = new File("C:\\Users\\Administrator\\Desktop\\07\\shp_deom\\给一码管地\\地表宗地.shp");
+//        String sql = null;
+//        try {
+//            sql = ShpPostgisTableBuilder.buildCreateTableSql(
+//                    shp,
+//                    "vector",
+//                    "test_table"
+//            );
+//        } catch (Exception e) {
+//            throw new RuntimeException(e);
+//        }
+//        System.out.println(sql);
         List<Map<String,Object>> res = iSpatialFilesDbService.testReadShape(filePath);
-        Map<String,Object> obj = res.get(0);
-        List<String> lll = obj.keySet().stream().sorted().collect(Collectors.toList());
-        System.out.println(lll);
-        System.exit(0);
-        //iSpatialFilesDbService.testInsertShp(res);
+//        Map<String,Object> obj = res.get(0);
+//        List<String> lll = obj.keySet().stream().sorted().collect(Collectors.toList());
+//        System.out.println(lll);
+//        System.exit(0);
+//        iSpatialFilesDbService.testInsertShp(res);
+        iSpatialFilesDbService.testInsertShpZD(res);
+
         return R.ok(res);
     }
 
+
+
+
+
+
     @GetMapping("/test/getShpData")
     public R<List<Map<String,Object>>> getShpData(@RequestParam("tabeName") String tabeName ) {
         List<Map<String,Object>> res = iSpatialFilesDbService.testGetShpData(tabeName);

+ 5 - 1
siwei-modules/siwei-spatial/src/main/java/com/siwei/spatial/mapper/file/TGeomDbDetailsMapper.java

@@ -91,8 +91,12 @@ public interface TGeomDbDetailsMapper
      */
     public int batchInsertDjq(@Param("list") List<Map<String, Object>> list, @Param("shpDbSRID") Integer shpDbSRID);
 
-
     List<Map<String,Object>> selectTableDataAndGeom(@Param("tableName") String tableName);
 
+    int batchInsertJzwShp(@Param("list") List<Map<String,Object>> list, @Param("shpDbSRID") Integer shpDbSRID);
+
+    int batchInsertZdShp(@Param("list") List<Map<String,Object>> list, @Param("shpDbSRID") Integer shpDbSRID);
+
+    int insertTableData(@Param("tableName") String tableName, @Param("data") Map<String, Object> data, @Param("shpDbSRID") Integer shpDbSRID);
 
 }

+ 1 - 0
siwei-modules/siwei-spatial/src/main/java/com/siwei/spatial/service/file/ISpatialFilesDbService.java

@@ -26,5 +26,6 @@ public interface ISpatialFilesDbService {
 
     TGeomDb uploadAddShpFileV1(String filePath, String fromRoute, Integer fromtype);
 
+    String testInsertShpZD(List<Map<String,Object>> shpDataMapList);
 
 }

+ 139 - 8
siwei-modules/siwei-spatial/src/main/java/com/siwei/spatial/service/file/impl/SpatialFilesDbServiceImpl.java

@@ -33,8 +33,8 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
-import java.util.stream.Collectors;
 
 @Service
 public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
@@ -387,7 +387,6 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
     }
 
 
-
     @Override
     @Transactional(rollbackFor = Exception.class)
     public TGeomDb  uploadAddShpDrawV1(String geom, String fromRoute, Integer fromtype) {
@@ -424,7 +423,6 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
     }
 
 
-
     @Override
     public List<Map<String,Object>> testReadShape(String shpFile) {
         List<Map<String, Object>> shpFeaturesList = readShapeFile(new File(shpFile));
@@ -437,19 +435,147 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
         //List<Map<String, Object>> shpFeaturesList = readShapeFile(new File(shpFile));
         //shpDataMapList = shpDataMapList.stream().filter(map -> map.get("djqdm").equals("360122015")).collect(Collectors.toList());
         //tGeomDbDetailsMapper.batchInsertDjq(shpDataMapList,4326);
-        tGeomDbDetailsMapper.batchInsertDjzq(shpDataMapList,4326);
+
+            int batchSize = 100;
+
+            for (int i = 0; i < shpDataMapList.size(); i += batchSize) {
+
+                int end = Math.min(i + batchSize, shpDataMapList.size());
+
+                List<Map<String,Object>> batch = shpDataMapList.subList(i, end);
+
+                //tGeomDbDetailsMapper.batchInsertJzwShp(batch,4326);
+                //tGeomDbDetailsMapper.batchInsertZdShp(batch,4326);
+
+            }
+        //tGeomDbDetailsMapper.batchInsertJzwShp(shpDataMapList,4326);
         return "";
     }
 
 
+    /**
+     * 针对地籍库中的宗地基本信息表,找出shp中个别对应的字段进行入库。
+     * @param shpDataMapList
+     * @return
+     */
     @Override
-    public List<Map<String,Object>> testGetShpData(String tabeName) {
-        List<Map<String,Object>>  geoms = tGeomDbDetailsMapper.selectTableDataAndGeom(tabeName);
-        return geoms;
-    }
+    @Transactional
+    public String testInsertShpZD(List<Map<String,Object>> shpDataMapList) {
+        String[] arr = {"bdcdyh", "bsm", "bz", "dah", "dj", "djh", "djzt", "jg", "jzmd", "jzxg", "mjdw", "qllx", "qlsdfs", "qlxz", "rjl", "tfh", "ybzddm", "ydyhfl", "ysdm", "yt", "zddm", "zdmj", "zdszb", "zdszd", "zdszn", "zdszx", "zdt", "zdtzm", "zl", "zt",""};
+        int i=0;
+        for (Map<String,Object> map : shpDataMapList){
+            Map<String,Object> map1 = new HashMap<>();
+            for (String key : arr) {
+                if (map.containsKey(key)) {
+                    map1.put(key, map.get(key));
+                }
+                if(key.equals("zdt")){
+                    map1.put("zdt", String.valueOf(map.get(key)).getBytes(StandardCharsets.UTF_8));
+                }
+
+                if(key.equals("yt")){
+                    String yt = String.valueOf(map.get("yt"));
+                    if(yt.contains("/")){
+                        yt = yt.replace("/","");
+                    }
+                    if(yt.length()>6){
+                        yt = yt.substring(0,6);
+                    }
+                    map1.put("yt", yt);
+                }
+
+                if(key.equals("jzxg")){
+                    Double jzxg =  (Double)map1.get("jzxg");
+                    map1.put("jzxg", jzxg+1);
+                }
+            }
+
+
+            if(map1.containsKey("jzxg")){
+                Double jzxg =  (Double)map1.get("jzxg");
+                if(jzxg<= 0 ){
+                    log.info("------------aaa{}",jzxg);
+                    continue;
+                }
+            }
 
+            if(map1.containsKey("rjl")){
+                Double rjl =  (Double)map.get("rjl");
+                if(rjl<= 0 ){
+                    log.info("------------bbb{}",rjl);
+                    continue;
+                }
+            }
+
+            if(map1.containsKey("qllx")) {
+                String qllx = String.valueOf(map.get("qllx"));
+                if (StringUtils.isBlank(qllx)) {
+                    log.info("------------ccc{}",qllx);
+                    continue;
+                }
+                int qllxTmp = Integer.parseInt(qllx);
+                if (qllxTmp < 1 || qllxTmp > 43) {
+                    log.info("------------ccc111{}",qllx);
+                    continue;
+                }
+            }
+
+            if(map1.containsKey("zdmj")) {
+                Double zdmj =  (Double)map.get("zdmj");
+                if(zdmj<= 0 ){
+                    log.info("------------dddd{}",zdmj);
+                    continue;
+                }
+            }
+
+            if(map1.containsKey("dj")) {
+                String dj = String.valueOf(map.get("dj"));
+                if (StringUtils.isBlank(dj)) {
+                    log.info("------------eee{}",dj);
+                    continue;
+                }
+                int djTmp = Integer.parseInt(dj);
+                if (djTmp < 1 || djTmp > 15) {
+                    log.info("------------fff{}",dj);
+                    continue;
+                }
+            }
+
+            if(map1.containsKey("qlxz")) {
+                int[] intArray = {101, 102, 103, 104, 105, 106, 107, 108, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212};
+                String qlxz = String.valueOf(map.get("qlxz"));
+                int qlxzTmp = Integer.parseInt(qlxz);
+                //判断是否包含于当前数组中
+                boolean contains = Arrays.stream(intArray).anyMatch(x -> x == qlxzTmp);
+                if (!contains) {
+                    log.info("------------eee{}",qlxz);
+                    continue;
+                }
+            }
+
+            if(map1.containsKey("jzmd")) {
+                Double jzmd =  (Double)map.get("jzmd");
+                if(jzmd< 0 || jzmd> 1 ){
+                    log.info("------------ffff{}",jzmd);
+                    continue;
+                }
+            }
+
+            if (map.containsKey("the_geom")) {
+                map1.put("geom", map.get("the_geom"));
+            }
+            map1.put("bdcdyb", "0".getBytes(StandardCharsets.UTF_8));
 
+            //map1.put("zdmj", 2);
+            //map1.put("dj", 1);
+            //map1.put("qlxz", 207);
+            //map1.put("jzmd", 0.122);
 
+            map1.put("bsm", ++i);
+            tGeomDbDetailsMapper.insertTableData("zdjbxx",map1,4326);
+        }
+        return "success";
+    }
 
 
 
@@ -459,6 +585,11 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
 
 
 
+    @Override
+    public List<Map<String,Object>> testGetShpData(String tabeName) {
+        List<Map<String,Object>>  geoms = tGeomDbDetailsMapper.selectTableDataAndGeom(tabeName);
+        return geoms;
+    }
 
 
 

+ 169 - 0
siwei-modules/siwei-spatial/src/main/java/com/siwei/spatial/utils/ShpPostgisTableBuilder.java

@@ -0,0 +1,169 @@
+package com.siwei.spatial.utils;
+
+import org.geotools.data.FeatureSource;
+import org.geotools.data.shapefile.ShapefileDataStore;
+import org.geotools.data.simple.SimpleFeatureCollection;
+import org.geotools.referencing.CRS;
+import org.locationtech.jts.geom.Geometry;
+import org.opengis.feature.type.AttributeDescriptor;
+import org.opengis.feature.type.GeometryDescriptor;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+
+import java.io.File;
+import java.nio.charset.Charset;
+import java.util.List;
+
+public class ShpPostgisTableBuilder {
+
+    /**
+     * 自动生成 PostGIS 建表 SQL
+     */
+    public static String buildCreateTableSql(File shpFile, String schema, String tableName) throws Exception {
+
+        ShapefileDataStore dataStore =
+                new ShapefileDataStore(shpFile.toURI().toURL());
+
+        dataStore.setCharset(Charset.forName("UTF-8"));
+
+        FeatureSource featureSource =
+                dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
+
+        SimpleFeatureCollection collection =
+                (SimpleFeatureCollection) featureSource.getFeatures();
+
+        List<AttributeDescriptor> attributes =
+                collection.getSchema().getAttributeDescriptors();
+
+        CoordinateReferenceSystem crs =
+                collection.getSchema().getCoordinateReferenceSystem();
+
+        Integer srid = CRS.lookupEpsgCode(crs, true);
+
+        if (srid == null) {
+            srid = 4326;
+        }
+
+        GeometryDescriptor geomDesc =
+                collection.getSchema().getGeometryDescriptor();
+
+        String geomType = getGeometryType(
+                geomDesc.getType().getBinding().getSimpleName()
+        );
+
+        StringBuilder sql = new StringBuilder();
+
+        sql.append("CREATE TABLE ")
+                .append(schema)
+                .append(".")
+                .append(tableName)
+                .append(" (\n");
+
+        sql.append("gid SERIAL PRIMARY KEY,\n");
+
+        for (AttributeDescriptor attr : attributes) {
+
+            String name = attr.getLocalName().toLowerCase();
+
+            if (name.equals("the_geom") || name.equals("geom")) {
+                continue;
+            }
+
+            Class<?> binding = attr.getType().getBinding();
+
+            String pgType = convertType(binding);
+
+            sql.append(name)
+                    .append(" ")
+                    .append(pgType)
+                    .append(",\n");
+        }
+
+        sql.append("geom geometry(")
+                .append(geomType)
+                .append(",")
+                .append(srid)
+                .append(")\n");
+
+        sql.append(");\n");
+
+        sql.append("CREATE INDEX idx_")
+                .append(tableName)
+                .append("_geom ON ")
+                .append(schema)
+                .append(".")
+                .append(tableName)
+                .append(" USING GIST (geom);");
+
+        dataStore.dispose();
+
+        return sql.toString();
+    }
+
+
+    /**
+     * SHP类型 → PostgreSQL类型
+     */
+    private static String convertType(Class<?> clazz) {
+
+        if (clazz == String.class) {
+            return "varchar(255)";
+        }
+
+        if (clazz == Integer.class) {
+            return "integer";
+        }
+
+        if (clazz == Long.class) {
+            return "bigint";
+        }
+
+        if (clazz == Double.class) {
+            return "numeric(18,8)";
+        }
+
+        if (clazz == Float.class) {
+            return "numeric(18,8)";
+        }
+
+        if (clazz == java.util.Date.class) {
+            return "date";
+        }
+
+        if (Geometry.class.isAssignableFrom(clazz)) {
+            return "geometry";
+        }
+
+        return "varchar(255)";
+    }
+
+
+    /**
+     * Geometry类型转换
+     */
+    private static String getGeometryType(String type) {
+
+        switch (type) {
+
+            case "Point":
+                return "Point";
+
+            case "MultiPoint":
+                return "MultiPoint";
+
+            case "LineString":
+                return "LineString";
+
+            case "MultiLineString":
+                return "MultiLineString";
+
+            case "Polygon":
+                return "MultiPolygon";
+
+            case "MultiPolygon":
+                return "MultiPolygon";
+
+            default:
+                return "Geometry";
+        }
+    }
+}

+ 252 - 0
siwei-modules/siwei-spatial/src/main/resources/mapper/postgresql/spatial/file/TGeomDbDetailsMapper.xml

@@ -151,6 +151,253 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </foreach>
     </insert>
 
+
+
+    <insert id="batchInsertJzwShp">
+        INSERT INTO vector.fw (
+        objectid,
+        bsm,
+        ysdm,
+        bdcdyh,
+        zddm,
+        xzqdm,
+        zh,
+        xmmc,
+        jzwmc,
+        jgrq,
+        jzwgd,
+        zzdmj,
+        zydmj,
+        ycjzmj,
+        scjzmj,
+        jzwjbyt,
+        zcs,
+        dscs,
+        dxcs,
+        dxsd,
+        ghyt,
+        fwjg,
+        zts,
+        bz,
+        zt,
+        tstybm,
+        zrzh,
+        lplb,
+        zl,
+        bgjzxx,
+        bgtsxx,
+        rkxh,
+        rkr,
+        rksj,
+        ytmc,
+        dah,
+        srctable,
+        from_id,
+        to_id,
+        projectid,
+        shape_area,
+        shape_len,
+        geom
+        )
+
+        VALUES
+
+        <foreach collection="list" item="item" separator=",">
+            (
+
+            #{item.objectid},
+            #{item.bsm},
+            #{item.ysdm},
+            #{item.bdcdyh},
+            #{item.zddm},
+            #{item.xzqdm},
+            #{item.zh},
+            #{item.xmmc},
+            #{item.jzwmc},
+            #{item.jgrq},
+            #{item.jzwgd},
+            #{item.zzdmj},
+            #{item.zydmj},
+            #{item.ycjzmj},
+            #{item.scjzmj},
+            #{item.jzwjbyt},
+            #{item.zcs},
+            #{item.dscs},
+            #{item.dxcs},
+            #{item.dxsd},
+            #{item.ghyt},
+            #{item.fwjg},
+            #{item.zts},
+            #{item.bz},
+            #{item.zt},
+            #{item.tstybm},
+            #{item.zrzh},
+            #{item.lplb},
+            #{item.zl},
+            #{item.bgjzxx},
+            #{item.bgtsxx},
+            #{item.rkxh},
+            #{item.rkr},
+            #{item.rksj},
+            #{item.ytmc},
+            #{item.dah},
+            #{item.srctable},
+            #{item.from_id},
+            #{item.to_id},
+            #{item.projectid},
+            #{item.shape_area},
+            #{item.shape_len},
+            <choose>
+                <when test="item.the_geom != null and item.the_geom != ''">
+                    public.st_transform(
+                    public.st_geomfromewkt(#{item.the_geom}),
+                    #{shpDbSRID}
+                    )
+                </when>
+                <otherwise>
+                    null
+                </otherwise>
+            </choose>
+            )
+        </foreach>
+    </insert>
+
+
+
+
+
+    <insert id="batchInsertZdShp">
+
+        INSERT INTO vector.zd_shp (
+        objectid,bsm,ysdm,bdcdyh,zddm,zdtzm,zl,midw,
+        zdmj,yt,dj,jg,qllx,qlxz,qlsdfs,rjl,jzmd,jzxg,
+        zdszd,zdszn,zdszx,zdszb,tfh,djh,qlrmc,jzmj,jzwzdmj,
+        zt,bhyy,bhnr,djsj,dbr,fj,tstybm,xzqdm,sjzdtybm,
+        fzmj,ybzddm,jzlb,syqx,qsrq,zzrq,bgjzxx,bgtsxx,
+        dah,rkxh,rkr,rksj,ytmc,zdt,bz,srctable,
+        from_id,to_id,projectid,txzt,jdzt,ywh,bdcqzh,
+        ydyhfl,shape_area,shape_len,geom
+        )
+
+        VALUES
+
+        <foreach collection="list" item="item" separator=",">
+            (
+            #{item.objectid},
+            #{item.bsm},
+            #{item.ysdm},
+            #{item.bdcdyh},
+            #{item.zddm},
+            #{item.zdtzm},
+            #{item.zl},
+            #{item.midw},
+
+            #{item.zdmj},
+            #{item.yt},
+            #{item.dj},
+            #{item.jg},
+            #{item.qllx},
+            #{item.qlxz},
+            #{item.qlsdfs},
+            #{item.rjl},
+            #{item.jzmd},
+            #{item.jzxg},
+
+            #{item.zdszd},
+            #{item.zdszn},
+            #{item.zdszx},
+            #{item.zdszb},
+            #{item.tfh},
+            #{item.djh},
+            #{item.qlrmc},
+            #{item.jzmj},
+            #{item.jzwzdmj},
+
+            #{item.zt},
+            #{item.bhyy},
+            #{item.bhnr},
+            #{item.djsj},
+            #{item.dbr},
+            #{item.fj},
+            #{item.tstybm},
+            #{item.xzqdm},
+            #{item.sjzdtybm},
+
+            #{item.fzmj},
+            #{item.ybzddm},
+            #{item.jzlb},
+            #{item.syqx},
+            #{item.qsrq},
+            #{item.zzrq},
+            #{item.bgjzxx},
+            #{item.bgtsxx},
+
+            #{item.dah},
+            #{item.rkxh},
+            #{item.rkr},
+            #{item.rksj},
+            #{item.ytmc},
+            #{item.zdt},
+            #{item.bz},
+            #{item.srctable},
+
+            #{item.from_id},
+            #{item.to_id},
+            #{item.projectid},
+            #{item.txzt},
+            #{item.jdzt},
+            #{item.ywh},
+            #{item.bdcqzh},
+
+            #{item.ydyhfl},
+            #{item.shape_area},
+            #{item.shape_len},
+
+            <choose>
+
+                <when test="item.the_geom != null and item.the_geom != ''">
+                    public.st_transform(
+                    public.st_geomfromewkt(#{item.the_geom}),
+                    #{shpDbSRID}
+                    )
+                </when>
+
+                <otherwise>
+                    null
+                </otherwise>
+            </choose>
+
+            )
+        </foreach>
+
+    </insert>
+
+
+
+    <insert id="insertTableData">
+        INSERT INTO vector.${tableName} (
+        <foreach collection="data" index="key" item="value" separator=",">
+            ${key}
+        </foreach>
+        )
+        VALUES (
+        <foreach collection="data" index="key" item="value" separator=",">
+            <choose>
+                <!-- 判断 geom 是否为空,并进行转换 -->
+                <when test="key == 'geom' and value != null">
+                    public.st_transform(public.st_geomfromewkt(#{value}), #{shpDbSRID})
+                </when>
+                <otherwise>
+                    #{value}
+                </otherwise>
+            </choose>
+        </foreach>
+        )
+    </insert>
+
+
+
+
     <select id="selectTableDataAndGeom"   resultType="Map">
         SELECT * ,public.st_asewkt(geom) AS geom2
         FROM vector.${tableName}
@@ -159,4 +406,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
 
 
+
+
+
+
+
 </mapper>