liusuyi
2026-01-16 05f799b8a5ce57b1f98dc5f8ab08308c32a6ec63
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.ard.work.service.impl;
 
 
import com.ard.work.config.GeoserverConfig;
import com.ard.work.domain.dto.wayline.ArdPositionDTO;
import com.ard.work.service.IGeoToolsService;
import com.ard.work.util.GeoTools;
import com.ard.work.util.Vertex;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
@Service
@Slf4j
public class GeoToolsServiceImpl implements IGeoToolsService {
 
    @Autowired
    private GeoserverConfig geoserverConfig;
 
    private static Double distance = 1500 * 180 / Math.PI / 6371000;
    private static GeometryFactory geoFactory = new GeometryFactory();
 
 
    //坐标类型转换
    @Override
    public Vertex gcj02ToTargetCoordinate(Vertex gcjVertex){
        Vertex newVertex = new Vertex(0,0);
        if(geoserverConfig.getDeviceCoordinateType().equals("WGS84")){
            newVertex = GeoTools.GCJ02toWGS84(new Vertex(gcjVertex.x, gcjVertex.y));
        }else {
            newVertex = new Vertex(gcjVertex.x, gcjVertex.y);
        }
        return newVertex;
    }
 
    @Override
    public Vertex targetCoordinateToGcj02(Vertex targetVertex){
        Vertex newVertex = new Vertex(0,0);
        if(geoserverConfig.getDeviceCoordinateType().equals("WGS84")){
            newVertex = GeoTools.WGS84toGCJ02(new Vertex(targetVertex.x, targetVertex.y));
        }else {
            newVertex = new Vertex(targetVertex.x, targetVertex.y);
        }
        return newVertex;
    }
 
    @Override
    public List<ArdPositionDTO> calConvexHull(List<ArdPositionDTO> polygonPoints){
        //缓冲距离
        List<ArdPositionDTO> convexHullPoints = new ArrayList<>();
        try{
            //用户定义区域
            Integer pCount = polygonPoints.size();
            //首位最后一个点
            Coordinate[] uPoints = new Coordinate[pCount+1];
            int i = 0;
            for(i=0;i<pCount;i++){
                uPoints[i] = new Coordinate(polygonPoints.get(i).getLongitude(),polygonPoints.get(i).getLatitude());
            }
            //将第一个点重新加入到最后,形成闭环
            uPoints[i] = new Coordinate(polygonPoints.get(0).getLongitude(),polygonPoints.get(0).getLatitude());
            //多边形点的个数必须大于等于3
            if(uPoints.length >= 3){
                //生成集合要素
                Geometry oneGeometry = geoFactory.createPolygon(uPoints);
                //做凸包
                Geometry geoConvexHull = oneGeometry.convexHull();
                Coordinate[] points = geoConvexHull.getCoordinates();
                for (Coordinate coordinate : points) {
                    convexHullPoints.add(ArdPositionDTO.builder().longitude(coordinate.getX())
                            .latitude(coordinate.getY()).build());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convexHullPoints;
    }
 
 
    private double meterToDegree(double threshold){
        return threshold * 180 / Math.PI / 6371000;
    }
 
    public void deleteFile(File filePath) {
        //判断路径是否存在
        if(filePath.exists()) {
            //判断是否是目录
            if (filePath.isDirectory()) {
                //将目录逐层拆分成数组元素
                File[] files = filePath.listFiles();
                for (File f : files) {
                    //递归调用,由最底层往上逐层删除
                    deleteFile(f);
                }
            }
            //如果是文件,直接删除
            filePath.delete();
        }
    }
 
    public void zipShapeFile(String shpPath){
        try{
            File shpFile = new File(shpPath);
            String shpRoot = shpFile.getParentFile().getPath(),
                    _shpName = shpFile.getName(),
                    shpName = _shpName.substring(0, _shpName.lastIndexOf("."));
 
            String zipPath = shpRoot+File.separator+shpName+".zip";
            File zipFile = new File(zipPath);
            InputStream input = null;
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
            // zip的名称为
            zipOut.setComment(shpName);
            String[] shpFiles = new String[]{
                    shpRoot+File.separator+shpName+".dbf",
                    shpRoot+File.separator+shpName+".prj",
                    shpRoot+File.separator+shpName+".shp",
                    shpRoot+File.separator+shpName+".shx",
            };
            for(int i=0;i<shpFiles.length;i++){
                File _file = new File(shpFiles[i]);
                input = new FileInputStream(_file);
                zipOut.putNextEntry(new ZipEntry(_file.getName()));
                int temp = 0;
                while ((temp = input.read()) != -1) {
                    zipOut.write(temp);
                }
                input.close();
            }
            zipOut.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
}