吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package org.sxkj.odm.feign;
 
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Point;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.sxkj.odm.entity.MyPoint;
import org.sxkj.odm.service.IOdmTaskInfoService;
import org.sxkj.odm.utils.ParseTifUtils;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @Description 绘制像素框
 * @Author AIX
 * @Date 2025/12/31 11:32
 * @Version 1.0
 */
@NonDS
@RestController
@AllArgsConstructor
@Slf4j
public class DrawPixelBoxClient implements IDrawPixelBoxClient {
 
    @Autowired
    private IOdmTaskInfoService odmTaskInfoService;
 
    @Override
    @GetMapping("/client/draw-image-pixel-box")
    public Boolean drawImagePixelBox(@RequestParam("type") int type,
                                     @RequestParam("tifFilePath") String tifFilePath,
                                     @RequestParam("filePath") String filePath,
                                     @RequestParam("outPath") String outPath,
                                     @RequestParam("points") String points,
                                     @RequestParam("geom") String geom) {
 
        log.info("开始绘制像素框: type={}, tifFilePath={}, filePath={}, outPath={}, points={}, geom={}", type, tifFilePath, filePath, outPath, points, geom);
        if (StringUtil.isBlank(filePath) || StringUtil.isBlank(outPath)) {
            log.error("非法参数,文件路径不能为空");
            return false;
        }
        switch (type) {
            case 0:
                log.info("tif绘制像素框");
                if (null == geom || geom.isEmpty() || StringUtil.isBlank(tifFilePath) || !tifFilePath.contains("tif")) {
                    log.error("地理点集合为空或者tif文件路径不正确");
                    return false;
                }
                List<Coordinate> coordinates = polygonStringToCoordinates(geom);
                // 将所有点转换成地理坐标
                List<Point> pointList = ParseTifUtils.getTifPosByMoreLngLat(tifFilePath, new ArrayList<>(coordinates));
                log.debug("tif 地理坐标数据:{}", pointList);
                // 地理坐标转换成像素坐标
                List<MyPoint> pixelByPointList = odmTaskInfoService.getPixelByPointList(tifFilePath, pointList);
                return ParseTifUtils.drawPolygonByPoints(filePath, outPath, pixelByPointList, "jpg");
            case 1:
                log.info("图片绘制像素框");
                if (null == points || points.isEmpty()) {
                    log.error("像素点集合为空");
                    return false;
                }
                List<MyPoint> point1 = JSON.parseArray(points, MyPoint.class);
                return ParseTifUtils.drawPolygonByPoints(filePath, outPath, point1, "jpg");
            default:
                break;
        }
 
        return false;
    }
 
    /**
     * polygon 数据转点集合数据
     *
     * @param polygonString
     * @return
     */
    public List<Coordinate> polygonStringToCoordinates(String polygonString) {
        List<Coordinate> points = new ArrayList<>();
        String[] coordinates = polygonString.replace("POLYGON((", "").replace("))", "").split(",");
        // 遍历转换
        for (String coordinate : coordinates) {
            String[] point = coordinate.trim().split(" ");
            double x = Double.parseDouble(point[0]);
            double y = Double.parseDouble(point[1]);
            points.add(new Coordinate(x, y));
        }
        return points;
    }
}