| | |
| | | package com.dji.sample.patches.service.impl; |
| | | |
| | | import com.dji.sample.common.error.CommonErrorEnum; |
| | | import com.dji.sample.component.mqtt.model.RequestsReply; |
| | | 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.entity.LotInfo; |
| | | import com.dji.sample.patches.service.ShpToDataSourceService; |
| | | import com.dji.sample.patches.utils.FormatConversionUtil; |
| | | import com.dji.sample.patches.utils.GeoToolsUtil; |
| | | import com.dji.sample.patches.utils.MultipartFileTOFileUtil; |
| | | import com.dji.sample.patches.utils.ShapeFileUtil; |
| | | 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.core.io.FileSystemResource; |
| | | import org.springframework.core.io.Resource; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | 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.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | |
| | | import static com.dji.sample.common.error.CommonErrorEnum.ILLEGAL_ARGUMENT; |
| | | import static com.dji.sample.patches.utils.ZipUtil.compressToKMZ; |
| | | 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 mapper; |
| | | String head = POLYGON; |
| | | double airportLat = 28.624514734; // 机场纬度 |
| | | double airportLon = 115.856725497; // 机场经度 |
| | | |
| | | String kmlFilePath = "src/main/resources/template/wpmz/template.kml"; // KML文件路径 |
| | | String wpmlFilePath = "src/main/resources/template/wpmz/waylines.wpml"; // WPML文件路径 |
| | | String end = ".kmz"; |
| | | @Autowired |
| | | private ShpToDataSourceMapper shpToDataSourceMapper; |
| | | |
| | | private String head = POLYGON; |
| | | private MultipartFile multipartFile; |
| | | |
| | | String json; |
| | | |
| | | public List<LotInfo> insertGeo(MultipartFile file, String workspaceId, String waylineName) throws IOException { |
| | | @Autowired |
| | | private PatchesConfigPojo patchesConfigPojo; |
| | | |
| | | @Transactional |
| | | public MultipartFile insertGeo(MultipartFile file, String workspaceId, String waylineName, double airportLat, double airportLon) throws Exception { |
| | | List<LotInfo> list = new ArrayList<>(); |
| | | MultipartFileTOFileUtil multipartFileTOFileUtil = new MultipartFileTOFileUtil(); |
| | | File file1 = multipartFileTOFileUtil.multipartFile2File(file); |
| | | File file1 = MultipartFileTOFileUtil.multipartFile2File(file, patchesConfigPojo.getUnzip()); |
| | | List<String> s = ShapeFileUtil.shpToGeoJson(file1); |
| | | String[] arr1 = FormatConversionUtil.formatConversion(s); |
| | | for (int i = 0; i < arr1.length; i++) { |
| | |
| | | lotInfo.setWorkspaceId(workspaceId); |
| | | lotInfo.setDkfw(json); |
| | | lotInfo.setDkbh("dkbh" + i); |
| | | mapper.insert(lotInfo); |
| | | list.add(lotInfo); |
| | | System.out.println(lotInfo); |
| | | String strs = getCentros(list); |
| | | String end = strs.replaceAll("\\(", "").replaceAll(", NaN\\)", ""); |
| | | String[] points = end.split(","); |
| | | lotInfo.setXzb(Double.valueOf(points[0])); |
| | | lotInfo.setYzb(Double.valueOf(points[1])); |
| | | shpToDataSourceMapper.insert(lotInfo); |
| | | } |
| | | Coordinate[] coordinates = GeoToolsUtil.getRoutePointOrder(list, airportLat, airportLon); |
| | | List<PointPO> coordinates = GeoToolsUtil.getRoutePointOrder(list, airportLat, airportLon); |
| | | XMLTemplateModel xmlModel = XMLTemplateModel.init(coordinates, list); |
| | | CreateWaylineFileUtils.createWaylineFile(xmlModel); |
| | | String destinationKMZFilePath = "src/main/resources/template/kmz/" + waylineName + ".kmz"; |
| | | try { |
| | | compressToKMZ(kmlFilePath, wpmlFilePath, destinationKMZFilePath); |
| | | System.out.println("Files compressed to KMZ successfully."); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | System.out.println("Error compressing files to KMZ: " + e.getMessage()); |
| | | } |
| | | return 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; |
| | | } |
| | | |
| | | public ResponseEntity<Resource> backWayline(String fileName) { |
| | | String name = fileName + end; |
| | | try { |
| | | File kmzFile = new File("src/main/resources/template/kmz/" + name); |
| | | boolean kzm=(kmzFile.exists()); |
| | | System.out.println(kzm); |
| | | System.out.println(kmzFile); |
| | | if (!kzm) { |
| | | System.out.println(kzm); |
| | | // 如果文件不存在,返回404 Not Found响应 |
| | | return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); |
| | | } |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + name + "\""); |
| | | headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE); |
| | | return ResponseEntity.ok().headers(headers).contentLength(kmzFile.length()). |
| | | contentType(MediaType.APPLICATION_OCTET_STREAM).body(new FileSystemResource(kmzFile)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); |
| | | @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) |
| | | .xmc(DistrictCodeUtils.nameToCode(file.getFXzqdm())) |
| | | .investigate(0) |
| | | .taskId(id) |
| | | .taskName(name) |
| | | .build(); |
| | | return builder.build(); |
| | | } |
| | | |
| | | public List<LotInfo> getNoPlan(){ |
| | | return shpToDataSourceMapper.selectList(new LambdaQueryWrapper<LotInfo>().eq(LotInfo::getIsPlan,0)); |
| | | } |
| | | } |
| | | |
| | | |