rain
2024-07-31 4e8892f75a0d9b76e147cccdb1f188c272aed4c5
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
145
146
147
148
149
package com.dji.sample.patches.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dji.sample.patches.config.pojo.PatchesConfigPojo;
import com.dji.sample.patches.dao.ShpToDataSourceMapper;
import com.dji.sample.patches.model.dto.ShpDTO;
import com.dji.sample.patches.model.entity.LotInfo;
import com.dji.sample.patches.service.ShpToDataSourceService;
import com.dji.sample.patches.utils.*;
import com.dji.sample.patches.xml.mode.XMLTemplateModel;
import com.dji.sample.patches.xml.utils.CreateWaylineFileUtils;
import com.dji.sample.territory.model.entity.TbDkjbxxEntity;
import com.dji.sample.wayline.service.IWaylineFileService;
import org.locationtech.jts.geom.Coordinate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
 
import static com.dji.sample.patches.utils.FormatConversionUtil.getCentros;
import static com.dji.sample.patches.utils.MultipartFileTOFileUtil.convert;
import static com.dji.sample.patches.utils.ZipUtil.*;
import static com.dji.sample.territory.utils.CoordinateSystemUtil.*;
import static org.locationtech.jts.io.WKTConstants.POLYGON;
 
@Service
public class ShpToDataSourceServiceImpl implements ShpToDataSourceService {
 
    @Autowired
    private ShpToDataSourceMapper shpToDataSourceMapper;
 
    private String head = POLYGON;
    private MultipartFile multipartFile;
 
    String json;
 
    @Autowired
    private PatchesConfigPojo patchesConfigPojo;
 
    @Transactional
    public MultipartFile insertGeo(MultipartFile file, String workspaceId, String waylineName, double airportLat, double airportLon,String creator) throws Exception {
        List<LotInfo> list = new ArrayList<>();
        File file1 = MultipartFileTOFileUtil.multipartFile2File(file, patchesConfigPojo.getUnzip());
        List<ShpDTO> shpData = ShapeFileUtil.shpToGeoJson(file1);
        for (ShpDTO shpDatum : shpData) {
            LotInfo lotInfo = new LotInfo();
            String uuid = UUID.randomUUID().toString();
            String bsm = uuid.replaceAll("-", "");
            lotInfo.setBsm(bsm);
            lotInfo.setUserName(creator);
            lotInfo.setWorkspaceId(workspaceId);
            lotInfo.setDkfw(poylonCGCStoWGS(convertToWKT(shpDatum.getDKFW())));
            lotInfo.setDkbh(shpDatum.getDKBH());
            lotInfo.setXzb(shpDatum.getXZB());
            lotInfo.setYzb(shpDatum.getYZB());
            lotInfo.setXzqdm(shpDatum.getXZQDM());
            lotInfo.setXmc(DistrictCodeUtils.codeToName(lotInfo.getXzqdm()));
            list.add(lotInfo);
            shpToDataSourceMapper.insert(lotInfo);
        }
        List<PointPO> coordinates = GeoToolsUtil.getRoutePointOrder(list, airportLat, airportLon);
        XMLTemplateModel xmlModel = XMLTemplateModel.init(coordinates, list);
        CreateWaylineFileUtils.createWaylineFile(xmlModel, patchesConfigPojo.getTemplate(), patchesConfigPojo.getTargetTemplate(), patchesConfigPojo.getWaylines(), patchesConfigPojo.getTargetWaylines());
        // 压缩文件夹中的内容
        String destKMZFile = patchesConfigPojo.getDestKMZFile() + waylineName + ".kmz"; // 输出的KMZ文件路径
        zipFolder(patchesConfigPojo.getSourceDir(), destKMZFile);
        multipartFile = convert(new File(destKMZFile));
        MultipartFileTOFileUtil.deleteFile(file1);
        return multipartFile;
    }
 
    @Transactional
    public void savaInMysql(List<TbDkjbxxEntity> list, String workspaceId,String id,String name) {
        for (int i = 0; i < list.size(); i++) {
            LotInfo lotInfo = new LotInfo();
            lotInfo.setWorkspaceId(workspaceId);
            lotInfo.setTaskId(id);
            lotInfo.setTaskName(name);
            lotInfo = dbConvertToEntity(list.get(i), workspaceId, id, name);
            shpToDataSourceMapper.insert(lotInfo);
        }
    }
 
 
    /**
     * 将SQlite数据库中DKJBXX对象转换为LotInfo对象。
     *
     * @param file 需要转换的DKJBXX对象。
     * @return 返回一个构建好的LotInfo对象,包含从数据库实体中转换来的信息。
     */
    private LotInfo dbConvertToEntity(TbDkjbxxEntity file, String workspaceId,String id,String name) {
        LotInfo.LotInfoBuilder builder = LotInfo.builder();
        builder.bsm(file.getFId())
                .xzqdm(file.getFXzqdm())
                .dkbh(file.getFTbbh())
                .dkfw(file.getFShape())
                .workspaceId(workspaceId)
                .isPlan(0)
                .isPush(0)
                .xmc(DistrictCodeUtils.nameToCode(file.getFXzqdm()))
                .investigate(0)
                .taskId(id)
                .taskName(name)
                .build();
        return builder.build();
    }
 
//    private LotInfo shpDtoToLotInfo(ShpDTO shpDTO){
//        LotInfo.LotInfoBuilder builder=LotInfo.builder();
//        builder.
//
//    }
 
    public List<LotInfo> getNoPlan(){
   return shpToDataSourceMapper.selectList(new LambdaQueryWrapper<LotInfo>().eq(LotInfo::getIsPlan,0));
    }
    public static String convertToWKT(String coordinates) {
        // Remove outermost square brackets and split by comma
        String cleanedCoordinates = coordinates.substring(4, coordinates.length() - 4);
        String[] polygons = cleanedCoordinates.split("\\]\\],\\[\\[");
        StringBuilder wkt = new StringBuilder("MULTIPOLYGON(");
 
        for (String polygon : polygons) {
            wkt.append("((");
            String[] points = polygon.split("\\],\\[");
            for (String point : points) {
                String[] coords = point.split(",");
                double x = Double.parseDouble(coords[0]);
                double y = Double.parseDouble(coords[1]);
                wkt.append(x).append(" ").append(y).append(", ");
            }
            // Remove the last comma and space
            wkt.setLength(wkt.length() - 2);
            wkt.append(")), ");
        }
        // Remove the last comma and space, and close the polygon
        wkt.setLength(wkt.length() - 2);
        wkt.append(")");
 
        return wkt.toString();
    }
}