大疆无人机航点飞行KMZ文件提取航点坐标

一、需要插件


        
            jaxen
            jaxen
            1.1.4
        


        
            dom4j
            dom4j
            1.6.1
        

二、KMZ解压成KML

package com.dji.sample.common.util;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

//将KMZ航线转成KML航线
public class KmzKml {
    public Document unzipKmzToKml() throws Exception, Exception {
        String strkmz="/home/ych/KmzKml/航点飞行测试.kmz";

        System.out.println("**********************     【KMZ转kml开始】kmz路径:       **********************\n"+ strkmz);

        File file = new File(strkmz);

        ZipFile zipFile = new ZipFile(file);

        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
        InputStream inputStream = null;
        ZipEntry entry = null;
        Document doc = null;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            String zipEntryName = entry.getName();
            //获取所需文件的节点
            if (zipEntryName.equals("wpmz/template.kml")) {

                inputStream = zipFile.getInputStream(entry);
                SAXReader reader = new SAXReader();
                doc = (Document) reader.read(inputStream);

                inputStream.close();
            }
        }
        zipFile.close();
        zipInputStream.close();
        return doc;
    }

}

三、KML提取航点坐标

package com.dji.sample.common.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.*;

//提取KML航线里的坐标值
public class Kml {
    public static Collection parseXmlWithDom4j(InputStream input) throws Exception {
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(input);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element root = document.getRootElement();//获取kml文件的根结点
        return getCoordinates(root);
    }

    //遍历当前节点下的全部节点
    public static Collection getCoordinates(Element node) {
        List coordinatesList = new ArrayList<>();
        if ("coordinates".equals(node.getName())) {
            String coordinatesStr = node.getTextTrim();
            String[] coordinatesArr = coordinatesStr.split(",");
            List coordinatesList1 = Arrays.asList(coordinatesArr);
            coordinatesList.add(String.valueOf(coordinatesList1));
        }
        Iterator iterator = node.elementIterator();
        while (iterator.hasNext()) {
            Element e = iterator.next();
            coordinatesList.addAll(getCoordinates(e));
        }
        return coordinatesList;
    }
}

四、调用

@PostMapping("${url.wayline.prefix}${url.wayline.version}/workspaces/{workspace_id}/waylines/{wayline_id}/kmzKml")
    public HttpResultResponse Kmz() throws Exception {
        KmzKml kmz = new KmzKml();
        Document unzipKmzToKml = kmz.unzipKmzToKml();

        // 将dom4j 的document对象转换成String
        // String asXML = unzipKmzToKml.asXML();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        Date date = new Date();
        String fileName = sdf.format(date);
        String strkml = "/home/ych/KmzKml/航点飞行.kml";

        // 创建kml到本地
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(strkml),format);
        xmlWriter.write(unzipKmzToKml);
        xmlWriter.close();

        System.out.println("\n**********************     【KMZ转kml成功】kml路径:       **********************\n"+ strkml);

        return HttpResultResponse.success();
    }
    @GetMapping("${url.wayline.prefix}${url.wayline.version}/workspaces/{workspace_id}/waylines/{wayline_id}/singlePoint")
    public  HttpResultResponse Kml() throws Exception {
        File file = new File("/home/ych/KmzKml/航点飞行.kml");
        InputStream in  = new FileInputStream(file);
        Kml kml = new Kml();
        Collection coordinatesList = kml.parseXmlWithDom4j(in);
        List  list = (List)coordinatesList;
        System.out.println(list.get(3));

        System.out.println("获取到的坐标值:");
        for (String coordinates : coordinatesList) {
            System.out.println(coordinates);
        }


        return HttpResultResponse.success(list);
    }

五、效果

获取到的坐标值:
[121.369754843606, 37.5227177120414]
[121.370733797755, 37.5233089872322]
[121.370402874547, 37.5243355293892]
[121.372488283707, 37.5240973025597]
[121.372337081214, 37.5227352183251]
[121.370980455301, 37.5213158608334]
[121.372248708559, 37.5224800846951]

六、航线文件解读





  
  Name
  1637600807044
  1637600875837
 
  
  
    safely
    goHome
    goContinue
    hover
    20
    23.98057,115.987663,100
    35
    8
    
      
      67
      0
    
    
      
      52
      0
    
  
 
  
  
    waypoint
    0
    0
    
      WGS84
      EGM96
      50
      GPS
      1
      100
    
    7
    usePointSetting
    
      followWayline
      45
      24.323345,116.324532,31.000000
      clockwise
    
    toPointAndStopWithDiscontinuityCurvature
    0
    
      
        
        
          longitude,latitude
        
      
      0
      90.2
      100
      1
      1
      1
      1
      0
    
    
      
        
        
          longitude,latitude
        
      
      1
      90.2
      100
      1
      1
      1
      1
      0
      
      
        0
        1
        1
        sequence
        
          reachPoint
        
        
        
          0
          gimbalRotate
          
            absoluteAngle
            0
            0
            0
            0
            1
            30
            0
            0
            0
          
        
        
        
          1
          takePhoto
          
            point1
            0
          
        
      
    
  


将航线里的标签检测到并读取值即可获得坐标,可以按照该思路获取其它类型的航线,再将坐标系转换即可将航线展示到前端。

你可能感兴趣的:(无人机,macos,java,spring)