rain
2024-04-08 c1915a7e947710572826a746e975e8d09516a0e7
src/main/java/com/dji/sample/patches/xml/controller/XMLController.java
@@ -3,15 +3,12 @@
import com.dji.sample.patches.model.entity.LotInfo;
import com.dji.sample.patches.utils.GeoToolsUtil;
import com.dji.sample.patches.xml.mode.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import com.dji.sample.patches.xml.utils.CreateWaylineFileUtils;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.Coordinate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -25,7 +22,6 @@
@RequestMapping(value = "/xml")
@Slf4j
public class XMLController {
    public static void main(String[] args) {
@@ -43,63 +39,20 @@
//        list.add(LotInfo.builder().dkbh("dkbh09").dkfw("POLYGON((115.842039042965 28.6314426646115, 115.840992080122 28.631252307731, 115.842324578286 28.6305860586493, 115.843181184248 28.6305860586493, 115.84403779021 28.6304908802091, 115.84394261177 28.6317281999322, 115.842039042965 28.6314426646115))").build());
//        list.add(LotInfo.builder().dkbh("dkbh10").dkfw("POLYGON((115.807011889796 28.623935465138, 115.805869748513 28.6224126100944, 115.810247956764 28.6220318963334, 115.809581707682 28.623935465138, 115.807011889796 28.623935465138))").build());
        // 机场经纬度
        double airportLat = 28.624514734; // 机场纬度
        double airportLon = 115.856725497; // 机场经度
        // 解析图斑生成航点,按顺序返回
        Coordinate[] coordinates = GeoToolsUtil.getRoutePointOrder(list, airportLat, airportLon);
        Coordinate[] coordinates = GeoToolsUtil.getRoutePointOrder(list);
        // 初始化模板对象
        XMLTemplateModel xmlModel = XMLTemplateModel.init(coordinates, list);
        XMLTemplateModel xmlModel = XMLTemplateModel.init(coordinates,list);
        //生成航线文件
        CreateWaylineFileUtils.createWaylineFile(xmlModel);
        xml2XmlDoc(xmlModel, "src\\main\\resources\\template\\template.xml", "src\\main\\resources\\template\\wpmz\\template.kml");
        xml2XmlDoc(xmlModel, "src\\main\\resources\\template\\waylines.xml", "src\\main\\resources\\template\\wpmz\\waylines.wpml");
    }
    /**
     * 将xml模板转换为newxml
     *
     * @param model           需要填充到模板的数据
     * @param templetFilePath 模板文件路径
     * @param targetFilePath  目标文件保存路径
     */
    public static void xml2XmlDoc(XMLTemplateModel model, String templetFilePath, String targetFilePath) {
        Writer out = null;
        try {
            // 将模板文件路径拆分为文件夹路径和文件名称
            String tempLetDir = templetFilePath.substring(0, templetFilePath.lastIndexOf("\\"));
            // 注意:templetFilePath.lastIndexOf("/")中,有的文件分隔符为:\ 要注意文件路径的分隔符
            String templetName = templetFilePath.substring(templetFilePath.lastIndexOf("\\") + 1);
            // 将目标文件保存路径拆分为文件夹路径和文件名称
            String targetDir = targetFilePath.substring(0, targetFilePath.lastIndexOf("\\"));
            String targetName = targetFilePath.substring(targetFilePath.lastIndexOf("\\") + 1);
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding(String.valueOf(StandardCharsets.UTF_8));
            // 如果目标文件目录不存在创建
            File file = new File(targetDir);
            if (!file.exists()) {
                file.mkdirs();
            }
            //加载模板数据(从文件路径中获取文件)
            configuration.setDirectoryForTemplateLoading(new File(tempLetDir));
            //获取模板实例
            Template template = configuration.getTemplate(templetName);
            File outFile = new File(targetDir + File.separator + targetName);
            //模板和数据模型合并生成文件
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), StandardCharsets.UTF_8));
            //生成文件
            template.process(model, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            log.error("write xml failed:", e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    log.error("close out failed:", e);
                }
            }
        }
    }
}