KML.cc 4.06 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
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
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "KML.h"

#include <QDomDocument>
#include <QStringList>

const QString Kml::_version("version=\"1.0\"");
const QString Kml::_encoding("encoding=\"UTF-8\"");
const QString Kml::_opengis("http://www.opengis.net/kml/2.2");
const QString Kml::_qgckml("QGC KML");

Kml::Kml()
{
    //create header
    createHeader();
    //name
    createTextElement(_docEle, "name", _qgckml);
    //open
    createTextElement(_docEle, "open", "1");
    //create style
    createStyles();
}

void Kml::points(const QStringList& points)
{
    //create placemark
    QDomElement placemark = _domDocument.createElement("Placemark");
    _docEle.appendChild(placemark);
    createTextElement(placemark, "styleUrl", "yellowLineGreenPoly");
    createTextElement(placemark, "name", "Absolute");
    createTextElement(placemark, "visibility", "0");
    createTextElement(placemark, "description", "Transparent purple line");

    QStringList latLonAlt = points[0].split(",");
    QStringList lookAtList({latLonAlt[0], latLonAlt[1], "0" \
        , "-100", "45", "2500"});
    createLookAt(placemark, lookAtList);

    //Add linestring
    QDomElement lineString = _domDocument.createElement("LineString");
    placemark.appendChild(lineString);

    //extruder
    createTextElement(lineString, "extruder", "1");
    createTextElement(lineString, "tessellate", "1");
    createTextElement(lineString, "altitudeMode", "absolute");
    QString coordinates;
    for(const auto& point : points) {
        coordinates += point + "\n";
    }
    createTextElement(lineString, "coordinates", coordinates);
}

void Kml::save(QDomDocument& document)
{
    document = _domDocument;
}

void Kml::createHeader()
{
    QDomProcessingInstruction header = _domDocument.createProcessingInstruction("xml", _version + " " + _encoding);
    _domDocument.appendChild(header);
    QDomElement kml = _domDocument.createElement("kml");
    kml.setAttribute("xmlns", _opengis);
    _docEle = _domDocument.createElement("Document");
    kml.appendChild(_docEle);
    _domDocument.appendChild(kml);
}

void Kml::createStyles()
{
    QDomElement style = _domDocument.createElement("Style");
    style.setAttribute("id", "yellowLineGreenPoly");
    createStyleLine(style, "7f00ffff", "4", "7f00ff00");
    _docEle.appendChild(style);
}

void Kml::createLookAt(QDomElement& placemark, const QStringList &lookAtList)
{
    QDomElement lookAt = _domDocument.createElement("LookAt");
    placemark.appendChild(lookAt);
    createTextElement(lookAt, "longitude", lookAtList[0]);
    createTextElement(lookAt, "latitude", lookAtList[1]);
    createTextElement(lookAt, "altitude", lookAtList[2]);
    createTextElement(lookAt, "heading", lookAtList[3]);
    createTextElement(lookAt, "tilt", lookAtList[4]);
    createTextElement(lookAt, "range", lookAtList[5]);
}

void Kml::createTextElement(QDomElement& domEle, const QString& elementName, const QString& textElement)
{
    // <elementName>textElement</elementName>
    auto element = _domDocument.createElement(elementName);
    element.appendChild(_domDocument.createTextNode(textElement));
    domEle.appendChild(element);
}

void Kml::createStyleLine(QDomElement& domEle, const QString& lineColor, const QString& lineWidth, const QString& polyColor)
{
    /*
    <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
    </LineStyle>
    <PolyStyle>
        <color>7f00ff00</color>
    </PolyStyle>
    */
    auto lineStyle = _domDocument.createElement("LineStyle");
    auto polyStyle = _domDocument.createElement("PolyStyle");
    domEle.appendChild(lineStyle);
    domEle.appendChild(polyStyle);
    createTextElement(lineStyle, "color", lineColor);
    createTextElement(lineStyle, "width", lineWidth);
    createTextElement(polyStyle, "color", polyColor);
}

Kml::~Kml()
{
}