nature-gis使用示例.md 12 KB

nature-gis使用示例

pom文件依赖

<!-- gis相关依赖 -->
        <dependency>
            <groupId>com.zjugis</groupId>
            <artifactId>gis</artifactId>
            <scope>system</scope>
            <version>1.4.2-SNAPSHOT</version>
            <systemPath>${project.basedir}/src/lib/gis-1.4.2-SNAPSHOT.jar</systemPath>
        </dependency>

         <!-- 内部读取空间文件工具 -->
        <dependency>
            <groupId>com.zjugis</groupId>
            <artifactId>nature-gis</artifactId>
            <scope>system</scope>
            <version>1.0-SNAPSHOT</version>
            <systemPath>${project.basedir}/src/lib/nature-gis-1.0-SNAPSHOT.jar</systemPath>
        </dependency>

        <!-- dxf文件读取 -->
        <dependency>
            <groupId>org.kabeja</groupId>
            <artifactId>kabeja</artifactId>
            <version>0.4</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/lib/kabeja-0.4.jar</systemPath>
        </dependency>

        <!-- 文件文本读取 -->
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-core</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-parsers-standard-package</artifactId>
            <version>2.8.0</version>
        </dependency>
        <!-- dwg文件读取 -->
        <dependency>
            <groupId>com.1spatial</groupId>
            <artifactId>dwg-lib</artifactId>
            <version>0.8</version>
        </dependency>


        <dependency>
            <groupId>com.healthmarketscience.jackcess</groupId>
            <artifactId>jackcess</artifactId>
            <version>3.5.1</version>
        </dependency>
 <!-- 参数校验 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <optional>true</optional>
        </dependency>

mdb导入示例

实体

public class LsydDTO extends MdbBaseDTO {
    @NotNull(message = "pzmj(批准面积)不能为空")
    private BigDecimal landArea;
    @NotEmpty(message = "sqdw(申请单位)不能为空")
    private String landUseCompany;
}

注:mdb导入相关DTO必须继承MDBbaseDTO且配合javax.validation相关注解,不要使用lombok注解否则生成的getset会自动加入方法级的注解,会使验证信息重复

public class MdbBaseDTO {
    @JSONField(
        name = "SHAPE_GEOMETRY"
    )
    @NotEmpty(
        message = "图形属性不能为空"
    )
    private JSONObject shapeGeometry;
    @JSONField(
        serialize = false,
        deserialize = false
    )
    private String shape;

    public MdbBaseDTO() {
    }

    public JSONObject getShapeGeometry() {
        return this.shapeGeometry;
    }

    public void setShapeGeometry(JSONObject shapeGeometry) {
        this.shapeGeometry = shapeGeometry;
    }

    public String getShape() {
        return this.shape;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }
}

实现类使用方法

public List<String> mdbImport(MultipartFile mdbFile) {
        List<String> inspect = new ArrayList<>();
        File file = new File(mdbFile.getOriginalFilename());
        //必填字段
        List<String> existColumns = Arrays.asList("pzmj", "sqdw", "ytfl", "xmmc", "sxzqmc", "xzqmc", "zxzqmc", "zxzqdm", "xmzt", "qzgdmj", "qzyjjbntmj", "sqsj", "pzksrq", "pzwh", "pzdw", "pzzzrq", "fksj", "fkzt", "yssj", "tdzl", "sjzzsj", "syqx", "qzqtnyd", "qzjsyd", "qzwlyd", "sfcfw", "sfazghjs", "sfktqtc", "sfgbyt", "xxyt");
        try {
            FileUtils.copyInputStreamToFile(mdbFile.getInputStream(), file);
            MdbTable lsyd = MdbReader.getMdbTable(file, "lsyd", GeometryType.POLYGON, "4490");
            //mdb数据表字段校验
            MdbUtils.mdbInspect(lsyd,existColumns,LsydDTO.class);
            //检查列表数据是否符合规范
            List<String> vaildMdbMessage = MdbUtils.mdbInspect(lsyd,existColumns,LsydDTO.class);
            inspect.addAll(vaildMdbMessage);
            if(vaildMdbMessage==null){
                //构建lsydDto
                List<LsydDTO> lsydDTOS = JSONObject.parseArray(JSONObject.toJSONString(lsyd.getData()), LsydDTO.class);
                buildLsydDTO(lsydDTOS);
                landTemporaryMapper.insertLsydDTO(lsydDTOS);
                inspect.add("mdb文件导入成功");
            }else{
                inspect.add(0,"mdb文件导入失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            inspect.add("mdb文件解析失败");
        } catch (Exception e) {
            e.printStackTrace();
            inspect.add("请检查上传的mdb是否是提供的模板,以及数据图层名称是否是lsyd");
        } finally {
            file.delete();
        }
        return inspect;
    }
private void buildLsydDTO(List<LsydDTO> lsydDTOS) {
        lsydDTOS.forEach(lsydDTO -> {
            //获得空间属性
            JSONObject shapeGeometry = lsydDTO.getShapeGeometry();
            //获取空间类型
            String geometryType = (String) shapeGeometry.get("geometryType");
            String wkt = "";
            //获取wkt
            if ("MULTIPOLYGON".equals(geometryType)) {
                MultiPolygon multiPolygon = JSONObject.parseObject(JSONObject.toJSONString(shapeGeometry), MultiPolygon.class);
                wkt = multiPolygon.toClobJSON();
            }
            if ("POLYGON".equals(geometryType)) {
                Polygon polygon = JSONObject.parseObject(JSONObject.toJSONString(shapeGeometry), Polygon.class);
                wkt = polygon.toClobJSON();
            }
            lsydDTO.setShape(wkt);
            lsydDTO.setDksyh(UUID.randomUUID().toString().replace("-", ""));
            if (lsydDTO.getRealExpireTime() == null) {
                lsydDTO.setRealExpireTime(lsydDTO.getExpireTime());
            }
        });
    }

shp导入获取wkt

实现类使用方法

public String shapeImport(MultipartFile[] files) {
        List<MultipartFile> multipartFiles = Arrays.asList(files);
        File cpgFile = null, dbfFile = null, prjFile = null, sbnFile = null, sbxFile = null, shpFile = null, shxFile = null;
        String wkt = "";
        try {
            for (MultipartFile multipartFile : multipartFiles) {
                String originalFilename = multipartFile.getOriginalFilename();
                String extension = "";
                if (originalFilename != null && originalFilename.lastIndexOf(".") != -1) {
                    extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
                }
                switch (extension) {
                    case "cpg":
                        cpgFile = new File(originalFilename);
                        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), cpgFile);
                        break;
                    case "dbf":
                        dbfFile = new File(originalFilename);
                        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), dbfFile);
                        break;
                    case "prj":
                        prjFile = new File(originalFilename);
                        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), prjFile);
                        break;
                    case "sbn":
                        sbnFile = new File(originalFilename);
                        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), sbnFile);
                        break;
                    case "sbx":
                        sbxFile = new File(originalFilename);
                        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), sbxFile);
                        break;
                    case "shp":
                        shpFile = new File(originalFilename);
                        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), shpFile);
                        break;
                    case "shx":
                        shxFile = new File(originalFilename);
                        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), shxFile);
                        break;
                    default:
                }
            }
            if (cpgFile == null) {
                throw ServiceExceptionUtil.exception(CPG_FILE_NOT_FOUND);
            }
            if (dbfFile == null) {
                throw ServiceExceptionUtil.exception(DBF_FILE_NOT_FOUND);
            }
            if (prjFile == null) {
                throw ServiceExceptionUtil.exception(PRJ_FILE_NOT_FOUND);
            }
            if (sbnFile == null) {
                throw ServiceExceptionUtil.exception(SBN_FILE_NOT_FOUND);
            }
            if (sbxFile == null) {
                throw ServiceExceptionUtil.exception(SBX_FILE_NOT_FOUND);
            }
            if (shpFile == null) {
                throw ServiceExceptionUtil.exception(SHP_FILE_NOT_FOUND);
            }
            if (shxFile == null) {
                throw ServiceExceptionUtil.exception(SHX_FILE_NOT_FOUND);
            }
            wkt = ShpUtils.parse(shpFile);
        } catch (BusinessException e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(CRS_CODE_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(SHAPE_ANALYSIS_ERROR);
        } finally {
            if (cpgFile != null) {
                cpgFile.delete();
            }
            if (dbfFile != null) {
                dbfFile.delete();
            }
            if (prjFile != null) {
                prjFile.delete();
            }
            if (sbnFile != null) {
                sbnFile.delete();
            }
            if (sbxFile != null) {
                sbxFile.delete();
            }
            if (shpFile != null) {
                shpFile.delete();
            }
            if (shxFile != null) {
                shxFile.delete();
            }
        }
        return wkt;
    }

txt导入获取wkt

实现类使用方法

 public String txtImport(MultipartFile file) {
        File txtFile = new File(file.getOriginalFilename());
        String wkt = "";
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), txtFile);
            wkt = TxtUtils.getWkt(txtFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(TXT_FILE_ERROR);
        } catch (TikaException e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(TXT_PARSE_ERROR);
        } catch (MutiDataException e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(TXT_MUTI_DATA_ERROR);
        }finally {
            txtFile.delete();
        }
        return wkt;
    }

dxf导入获取wkt

实体类使用方法

public String dxfImport(MultipartFile file) {
        File dxfFile = new File(file.getOriginalFilename());
        String wkt = "";
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), dxfFile);
            wkt = DxfUtils.getWkt(dxfFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(DXF_FILE_ERROR);
        } catch (ParseException e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(DXF_PARSE_ERROR);
        } catch (LayerNotFoundException e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(DXF_LAYER_NOT_FOUND_ERROR);
        } finally {
            dxfFile.delete();
        }
        return wkt;
    }

dwg导入获取wkt

实体类使用方法

public String dwgImport(MultipartFile file) {
        File dwgFile = new File(file.getOriginalFilename());
        String wkt = "";
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), dwgFile);
            wkt = DwgUtils.getWkt(dwgFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw ServiceExceptionUtil.exception(DWG_FILE_ERROR);
        } finally {
            dwgFile.delete();
        }
        return wkt;
    }

pg入库wkt方法

update lsyd set shape = st_geometry(#{reqVO.wkt},4490) where dksyh = #{reqVO.dksyh}