# 自定义航线特殊说明:
在使用自定义航线时 特别要注意高度问题:海拔高度+距地面高度 为无人机飞行的真实高度!!!
如果你按照常规的思路去设置,比如设置了高度为50m,但你的海拔高度为100m,无人机会降落有触发避障自动返航功能的会航线执行失败自动返航

# maven 依赖
<dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.18</version>
</dependency>

# 根据官方文档创建对应的JavaBean对象  示例   非完整示例,需自定实现对应的Bean

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import lombok.Data;

@Data
@XStreamAlias("kml")
public class KmlInfo {

    @XStreamAsAttribute
    @XStreamAlias("xmlns")
    private String xmlns = "http://www.opengis.net/kml/2.2";

    @XStreamAsAttribute
    @XStreamAlias("xmlns:wpml")
    private String wpml = "http://www.dji.com/wpmz/1.0.6";

    @XStreamAlias("Document")
    private KmlDocument document;

    # 对象属性为数组时使用
    @XStreamImplicit(itemFieldName = KmlConstant.PLACEMARK)
    private List<KmlPlacemark> placemarkList;
}


@Data
@XStreamAlias("Placemark")
public class KmlPlacemark {
    # 属性自行填充
}


# 对象转为xml
KmlInfo kmlInfo = createKmlInfo(req);
WpmlInfo wpmlInfo = createWpmlInfo(kmlInfo);
XStream xStream = new XStream();
xStream.setMode(XStream.NO_REFERENCES);
xStream.processAnnotations(new Class[]{KmlInfo.class, WpmlInfo.class});
String templateKml = "<?xml version="1.0" encoding="UTF-8"?>\n"+xStream.toXML(kmlInfo);
String waylinesWpml = "<?xml version="1.0" encoding="UTF-8"?>\n"+xStream.toXML(wpmlInfo);

# xml转为对象
public static WpmlInfo parseWpmlInfo(File waylinesFile) {
        XStream xStream = new XStream();
        // 1. 安全配置
        xStream.addPermission(AnyTypePermission.ANY);
        xStream.processAnnotations(new Class[]{WpmlInfo.class});
        xStream.autodetectAnnotations(true);
        // 2. 处理命名空间(关键修复)
        xStream.alias("kml", WpmlInfo.class);
        xStream.alias("Document", WpmlDocument.class);
        xStream.alias("Folder", WpmlFolder.class);
        xStream.alias("Placemark", WpmlPlacemark.class);

        xStream.alias("wpml:actionGroup", KmlActionGroup.class);
        xStream.alias("wpml:startActionGroup", WpmlStartActionGroup.class);
        xStream.alias("wpml:action", KmlAction.class);
        // 3. 特殊处理wpml命名空间
        xStream.alias("wpml:missionConfig", WpmlMissionConfig.class);
        xStream.alias("wpml:droneInfo", KmlDroneInfo.class);
        xStream.alias("wpml:payloadInfo", KmlPayloadInfo.class);
        xStream.alias("wpml:waylineCoordinateSysParam", KmlWaylineCoordinateSysParam.class);
        xStream.alias("wpml:payloadParam", KmlPayloadParam.class);
        xStream.alias("wpml:waypointGimbalHeadingParam", WpmlWaypointGimbalHeadingParam.class);
        // 5. 忽略未知元素
        xStream.ignoreUnknownElements();
        String xml = readKmlInfo(waylinesFile);
        return (WpmlInfo) xStream.fromXML(xml);
    }

    public static KmlInfo parseKmlInfo(File templateFile) {
        XStream xStream = new XStream();
        // 1. 安全配置
        xStream.addPermission(AnyTypePermission.ANY);
        xStream.processAnnotations(new Class[]{KmlInfo.class});
        xStream.autodetectAnnotations(true);
        // 2. 处理命名空间(关键修复)
        xStream.alias("kml", KmlInfo.class);
        xStream.alias("Document", KmlDocument.class);
        xStream.alias("Folder", KmlFolder.class);
        xStream.alias("Placemark", KmlPlacemark.class);

        xStream.alias("wpml:actionGroup", KmlActionGroup.class);
        xStream.alias("wpml:action", KmlAction.class);
        // 3. 特殊处理wpml命名空间
        xStream.alias("wpml:missionConfig", KmlMissionConfig.class);
        xStream.alias("wpml:droneInfo", KmlDroneInfo.class);
        xStream.alias("wpml:payloadInfo", KmlPayloadInfo.class);
        xStream.alias("wpml:waylineCoordinateSysParam", KmlWaylineCoordinateSysParam.class);
        xStream.alias("wpml:payloadParam", KmlPayloadParam.class);
        // 5. 忽略未知元素
        xStream.ignoreUnknownElements();
        // 6. 预处理XML
        String xml = readKmlInfo(templateFile);
        return (KmlInfo) xStream.fromXML(xml);
    }

    public static String readKmlInfo(File file) {
        try (InputStream is = Files.newInputStream(file.toPath());
             Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(reader)) {
            return br.lines().collect(Collectors.joining("\n"));
        } catch (Exception e) {
            throw new BusinessException("读取文件 " + file.getAbsolutePath() + " 失败");
        }
    }

Logo

加入社区!打开量化的大门,首批课程上线啦!

更多推荐