rain
2024-03-23 d2108ee735894d73bf655bda1651ecaefbc4c09d
图斑
1 files modified
13 files added
474 ■■■■■ changed files
src/main/java/com/dji/sample/patches/controller/DemoController.java 21 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/controller/PatchesController.java 25 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/dao/DemoMapper.java 10 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/dao/PatchesMapper.java 14 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/model/DemoEntity.java 14 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/model/PageBean.java 15 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/model/PatchesEntity.java 74 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/service/DemoService.java 8 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/service/PatchesService.java 8 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/service/impl/DemoServiceImpl.java 19 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/service/impl/PatchesServiceImpl.java 33 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java 101 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/utils/ZipUtil.java 129 ●●●●● patch | view | raw | blame | history
src/main/resources/application.yml 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/controller/DemoController.java
New file
@@ -0,0 +1,21 @@
package com.dji.sample.patches.controller;
import com.dji.sample.patches.service.impl.DemoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.IOException;
@RestController
public class DemoController {
    @Autowired
    private  DemoServiceImpl demoServiceImpl;
    @GetMapping("/getGeo")
    public void getGeo (File file) throws IOException {
        demoServiceImpl.insertGeo(file);
    }
}
src/main/java/com/dji/sample/patches/controller/PatchesController.java
New file
@@ -0,0 +1,25 @@
package com.dji.sample.patches.controller;
import com.dji.sample.patches.model.PageBean;
import com.dji.sample.patches.service.impl.PatchesServiceImpl;
import org.opengis.geometry.coordinate.Polygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("${url.patches.prefix}${url.patches.version}")
@RestController
public class PatchesController {
    @Autowired
    private PatchesServiceImpl patchesServiceimpl;
    @GetMapping("/getPatches")
    public PageBean page(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "5") Integer pageSize,
                         Integer id, String bsm, Polygon dkfw, String dklx){
        //调用service分页查询
        PageBean pageBean =patchesServiceimpl.limitGet( id,  bsm, dkfw, dklx,  page,  pageSize);
        return pageBean;
    }
}
src/main/java/com/dji/sample/patches/dao/DemoMapper.java
New file
@@ -0,0 +1,10 @@
package com.dji.sample.patches.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dji.sample.patches.model.DemoEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DemoMapper extends BaseMapper<DemoEntity> {
    void insertJson(String json);
}
src/main/java/com/dji/sample/patches/dao/PatchesMapper.java
New file
@@ -0,0 +1,14 @@
package com.dji.sample.patches.dao;
import com.dji.sample.patches.model.PatchesEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.opengis.geometry.coordinate.Polygon;
import java.util.List;
public interface PatchesMapper {
    @Select("select id ,bsm,dkfw ,dklx from tb_lot_info")
List<PatchesEntity> limitGet(@Param("id") Integer id, @Param("bsm") String bsm,
                             @Param("dkfw") Polygon dkfw,@Param("dklx") String dklx);
}
src/main/java/com/dji/sample/patches/model/DemoEntity.java
New file
@@ -0,0 +1,14 @@
package com.dji.sample.patches.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DemoEntity {
    String geo;
}
src/main/java/com/dji/sample/patches/model/PageBean.java
New file
@@ -0,0 +1,15 @@
package com.dji.sample.patches.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageBean {
    private long total;
    private List rows;
}
src/main/java/com/dji/sample/patches/model/PatchesEntity.java
New file
@@ -0,0 +1,74 @@
package com.dji.sample.patches.model;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.opengis.geometry.coordinate.Polygon;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "tb_lot_info")
public class PatchesEntity implements Serializable {
    @TableId(type = IdType.AUTO)
    private Integer id;
    @TableField("bsm")
    private String bsm;
    @TableField("dklx")
    private String dklx;
    @TableField("xzqdm")
    private String xzqdm;
    @TableField("xmc")
    private String xmc;
    @TableField("dkbh")
    private String dkbh;
    @TableField("dkmc")
    private String dkmc;
    @TableField("dkmj")
    private Double dkmj;
    @TableField("sfbhzdk")
    private String sfbhzdk;
    @TableField("xzb")
    private Double xzb;
    @TableField("yzb")
    private Double yzb;
    @TableField("bz")
    private String bz;
    @TableField("kzxx")
    private String kzxx;
    @TableField("dkfw")
    private Polygon dkfw;
    @TableField("sjlx")
    private String sjlx;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("create_time")
    private LocalDateTime createTime;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("update_time")
    private LocalDateTime updateTime;
}
src/main/java/com/dji/sample/patches/service/DemoService.java
New file
@@ -0,0 +1,8 @@
package com.dji.sample.patches.service;
import java.io.File;
import java.io.IOException;
public interface DemoService {
    void insertGeo(File file) throws IOException;
}
src/main/java/com/dji/sample/patches/service/PatchesService.java
New file
@@ -0,0 +1,8 @@
package com.dji.sample.patches.service;
import com.dji.sample.patches.model.PageBean;
import org.opengis.geometry.coordinate.Polygon;
public interface PatchesService {
    PageBean limitGet (Integer id,String bsm, Polygon dkfw ,String dklx,int page, int pageSize);
}
src/main/java/com/dji/sample/patches/service/impl/DemoServiceImpl.java
New file
@@ -0,0 +1,19 @@
package com.dji.sample.patches.service.impl;
import com.dji.sample.patches.dao.DemoMapper;
import com.dji.sample.patches.service.DemoService;
import com.dji.sample.patches.utils.ShapeFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    private DemoMapper mapper;
    private ShapeFileUtil shapeFileUtil;
      @Override
      public void insertGeo(File file) throws IOException {
          System.out.println(shapeFileUtil.shpToGeoJson(file));
      }
  }
src/main/java/com/dji/sample/patches/service/impl/PatchesServiceImpl.java
New file
@@ -0,0 +1,33 @@
package com.dji.sample.patches.service.impl;
import com.dji.sample.patches.dao.PatchesMapper;
import com.dji.sample.patches.model.PageBean;
import com.dji.sample.patches.model.PatchesEntity;
import com.dji.sample.patches.service.PatchesService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.opengis.geometry.coordinate.Polygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PatchesServiceImpl implements PatchesService {
    @Autowired
    private PatchesMapper mapper;
    @Override
    public PageBean limitGet(Integer id, String bsm, Polygon dkfw, String dklx, int page, int pageSize) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);
        //2. 执行查询
        List<PatchesEntity> PatchersList = mapper.limitGet(id,bsm,dkfw,dklx);
        com.github.pagehelper.Page<PatchesEntity> p = (Page<PatchesEntity>) PatchersList;
        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
}
src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java
New file
@@ -0,0 +1,101 @@
package com.dji.sample.patches.utils;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
public class ShapeFileUtil {
    /*
     * @param zipFile: 压缩包文件地址
     * @return FeatureCollection
     * @author pangshicheng
     * @description 解析shp压缩包,并返回解析出的 FeatureCollection
     * @date 2023/7/18 16:02
     */
    public static FeatureCollection getFeatureCollectionByShpFile(File zipFile) throws IOException {
        try {
            String tempDir = FileUtil.getTmpDirPath();
            File shapeDir = new File(tempDir + File.separator + new Date().getTime());
            shapeDir.mkdir();
            List<String> files = ZipUtil.unZipFiles(zipFile, shapeDir.getPath() + File.separator);
            String shapFileName = "";
            for (String fileName : files) {
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                if ("shp".equals(suffix)) {
                    shapFileName = fileName;
                }
            }
            File shapeFile = new File(shapFileName);
            List<SimpleFeature> list = new ArrayList<>();
            Map<String, Object> shapeFileParams = new HashMap();
            shapeFileParams.put("url", shapeFile.toURI().toURL());
            // 设置编码
            shapeFileParams.put("charset", "GBK");
            DataStore dataStore = DataStoreFinder.getDataStore(shapeFileParams);
            if (dataStore == null) {
                throw new RuntimeException("couldn't load the damn data store: " + shapeFileParams);
            }
            String typeName = dataStore.getTypeNames()[0];
            FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
            Filter filter = Filter.INCLUDE;
            FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
            return collection;
        }catch (Exception e){
            throw e;
        }
    }
    /**
     * @param zipFile:
     * @return JSONObject
     * @author pangshicheng
     * @description 通过shp压缩文件,将其转换为GeoJson格式
     * @date 2023/7/18 16:04
     */
    public static JSONObject shpToGeoJson(File zipFile) throws IOException {
        FeatureJSON fjson = new FeatureJSON();
        JSONObject geoJsonObject=new JSONObject();
        geoJsonObject.put("type","FeatureCollection");
        try {
            // 获取FeatureCollection
            FeatureCollection collection = getFeatureCollectionByShpFile(zipFile);
            FeatureIterator iterator = collection.features();
            List<JSONObject> array  = new ArrayList<JSONObject>();
            //遍历feature转为json对象
            while (iterator.hasNext()) {
                SimpleFeature feature = (SimpleFeature) iterator.next();
                StringWriter writer = new StringWriter();
                fjson.writeFeature(feature, writer);
                String temp = writer.toString();
                byte[] b = temp.getBytes("iso8859-1");
                temp = new String(b, "gbk");
                JSONObject json = JSONObject.parseObject(temp);
                array.add(json);
            }
            iterator.close();
            //添加到geojsonObject
            geoJsonObject.put("features",array);
            iterator.close();
        }catch (Exception e){
            throw e;
        }
        return geoJsonObject;
    }
}
src/main/java/com/dji/sample/patches/utils/ZipUtil.java
New file
@@ -0,0 +1,129 @@
package com.dji.sample.patches.utils;
import org.springframework.context.annotation.Configuration;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
 * @author soulmate丶
 * @date 2021-10-26
 */
@Configuration
public class ZipUtil {
    /**
     * 保存zip文件到本地并调用解压方法并返回解压出的文件的路径集合
     *
     * @param file 文件
     * @return list //解压出的文件的路径合集
     */
    private static String zipPath = "D:/ceshi/tuban.zip";//zip根路径
    /**
     * zip解压
     *
     * @param srcFile     zip源文件
     * @param destDirPath 解压后的目标文件夹
     * @return list 解压文件的路径合集
     * @throws RuntimeException 解压失败会抛出运行时异常
     */
    public static List<String> unZipFiles(File srcFile, String destDirPath) throws RuntimeException {
        List<String> list = new ArrayList<>();
        long start = System.currentTimeMillis();
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
        }
        // 开始解压
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(srcFile, Charset.forName("GBK"));
            Enumeration<?> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                // 如果是文件夹,就创建个文件夹
                if (entry.isDirectory()) {
                    String dirPath = destDirPath + File.separator + entry.getName();
                    File dir = new File(dirPath);
                    dir.mkdirs();
                } else {
                    // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
                    File targetFile = new File(destDirPath + File.separator + entry.getName());
                    // 保证这个文件的父文件夹必须要存在
                    list.add(destDirPath + entry.getName());
                    if (!targetFile.getParentFile().exists()) {
                    }
                    targetFile.createNewFile();
                    // 将压缩文件内容写入到这个文件中
                    InputStream is = zipFile.getInputStream(entry);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    int len;
                    byte[] buf = new byte[1024];
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }
                    // 关流顺序,先打开的后关闭
                    fos.close();
                    is.close();
                }
            }
            long end = System.currentTimeMillis();
        } catch (Exception e) {
            throw new RuntimeException("unzip error from ZipUtils", e);
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
    /**
     * @param filePath 临时文件的删除
     *                 删除文件夹里面子目录
     *                 再删除文件夹
     */
    public static void deleteFiles(String filePath) {
        File file = new File(filePath);
        if ((!file.exists()) || (!file.isDirectory())) {
            return;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (filePath.endsWith(File.separator)) {
                temp = new File(filePath + tempList[i]);
            } else {
                temp = new File(filePath + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                deleteFiles(filePath + "\\" + tempList[i]);
            }
        }
        // 空文件的删除
        file.delete();
    }
}
src/main/resources/application.yml
@@ -39,6 +39,9 @@
  control:
    prefix: /control
    version: /api/v1
  patches:
    prefix: /patches
    version: /api/v1
# Tutorial: https://www.alibabacloud.com/help/en/object-storage-service/latest/use-a-temporary-credential-provided-by-sts-to-access-oss
#oss: