-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint2KML_refer.cpp
209 lines (165 loc) · 8.37 KB
/
Point2KML_refer.cpp
1
2
3
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <sstream>
#include "Point2KML_refer.h"
using namespace std;
using namespace tinyxml2;
#define PI 3.141592653589793
#define MATH_PI 3.14159265358979323846264338327950288 // pi
#define MATH_PI_2 1.57079632679489661923132169163975144 // pi/2
#define MATH_PI_4 0.785398163397448309615660845819875721 // pi/4
#define MATH_R2D 57.295779513082320876798154814105e0
#define MATH_D2R 0.017453292519943295769236907684886e0
// String split function
vector<string> Split(const string& str, char delimiter)
{
vector<string> tokens;
string token;
istringstream tokenStream(str);
while (getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
template<class TYPE>
tinyxml2::XMLElement* InsertChildElement(tinyxml2::XMLDocument& doc,
tinyxml2::XMLElement* pItem, const char* lpstrTag, TYPE val, bool bVal = true)
{
tinyxml2::XMLElement* pEle = doc.NewElement(lpstrTag);
if (bVal) pEle->SetText(val);
pItem->InsertEndChild(pEle);
return pEle;
}
bool generateKMLFromWaypoints(const std::vector<Waypoint>& M300KML, const std::string& outputFilename) {
int nWaypoint = M300KML.size();
tinyxml2::XMLDocument doc;
tinyxml2::XMLDeclaration* declaration = doc.NewDeclaration();
doc.InsertFirstChild(declaration);
tinyxml2::XMLElement* root = doc.NewElement("kml");
if (root == NULL) {
return false;
}
root->SetAttribute("xmlns", "http://www.opengis.net/kml/2.2");
doc.InsertEndChild(root);
tinyxml2::XMLElement* pDoc = doc.NewElement("Document");
pDoc->SetAttribute("xmlns", "");
root->InsertEndChild(pDoc);
vector<string> strMatchNameArray = Split(outputFilename, '.');
InsertChildElement(doc, pDoc, "name", strMatchNameArray[0].c_str());
InsertChildElement(doc, pDoc, "open", 1);
tinyxml2::XMLElement* pExt = doc.NewElement("ExtendedData");
pExt->SetAttribute("xmlns:mis", "www.dji.com");
pDoc->InsertEndChild(pExt);
InsertChildElement(doc, pExt, "mis:type", "Waypoint");
InsertChildElement(doc, pExt, "mis:stationType", "0");
// waylineGreenPoly
tinyxml2::XMLElement* pLineStyle = doc.NewElement("Style");
pLineStyle->SetAttribute("id", "waylineGreenPoly");
pDoc->InsertEndChild(pLineStyle);
tinyxml2::XMLElement* pLine = doc.NewElement("LineStyle");
pLineStyle->InsertEndChild(pLine);
InsertChildElement(doc, pLine, "color", "FF0AEE8B");
InsertChildElement(doc, pLine, "width", 6);
// waypointStyle
tinyxml2::XMLElement* pLinePoint = doc.NewElement("Style");
pLinePoint->SetAttribute("id", "waypointStyle");
pDoc->InsertEndChild(pLinePoint);
tinyxml2::XMLElement* pIconStyle = doc.NewElement("IconStyle");
pLinePoint->InsertEndChild(pIconStyle);
tinyxml2::XMLElement* pIcon = doc.NewElement("Icon");
pIconStyle->InsertEndChild(pIcon);
InsertChildElement(doc, pIcon, "href", "https://cdnen.dji-flighthub.com/static/app/images/point.png");
// Folder
tinyxml2::XMLElement* pFolder = doc.NewElement("Folder");
pDoc->InsertEndChild(pFolder);
InsertChildElement(doc, pFolder, "name", "Waypoints");
InsertChildElement(doc, pFolder, "description", "Waypoints in the Mission.");
for (int i = 0; i < nWaypoint; i++) {
tinyxml2::XMLElement* pPlacemark = doc.NewElement("Placemark");
pFolder->InsertEndChild(pPlacemark);
char strValue[128] = ""; sprintf(strValue, "Waypoint%d", i + 1);
InsertChildElement(doc, pPlacemark, "name", strValue);
InsertChildElement(doc, pPlacemark, "visibility", 1);
InsertChildElement(doc, pPlacemark, "description", "Waypoint");
InsertChildElement(doc, pPlacemark, "styleUrl", "#waypointStyle");
tinyxml2::XMLElement* pExtendedData = doc.NewElement("ExtendedData");
pExtendedData->SetAttribute("xmlns:mis", "www.dji.com");
pPlacemark->InsertEndChild(pExtendedData);
InsertChildElement(doc, pExtendedData, "mis:useWaylineAltitude", "false");
//InsertChildElement(doc, pExtendedData, "mis:heading", int(M300KML[i].heading * MATH_R2D + 0.5));
InsertChildElement(doc, pExtendedData, "mis:heading", int(M300KML[i].heading));
InsertChildElement(doc, pExtendedData, "mis:turnMode", "Auto");
//InsertChildElement(doc, pExtendedData, "mis:gimbalPitch", int(M300KML[i].pitch * MATH_R2D + 0.5));
InsertChildElement(doc, pExtendedData, "mis:gimbalPitch", int(M300KML[i].pitch));
InsertChildElement(doc, pExtendedData, "mis:useWaylineSpeed", "true");
InsertChildElement(doc, pExtendedData, "mis:speed", "3.0");
InsertChildElement(doc, pExtendedData, "mis:useWaylineHeadingMode", "false");
InsertChildElement(doc, pExtendedData, "mis:useWaylinePointType", "false");
InsertChildElement(doc, pExtendedData, "mis:pointType", "LineStop");
InsertChildElement(doc, pExtendedData, "mis:headingMode", "UsePointSetting");
InsertChildElement(doc, pExtendedData, "mis:cornerRadius", "0.2");
tinyxml2::XMLElement* pMisAction = doc.NewElement("mis:actions");
pMisAction->SetAttribute("param", "0");
pMisAction->SetAttribute("accuracy", "0");
pMisAction->SetAttribute("cameraIndex", "0");
pMisAction->SetAttribute("payloadType", "0");
pMisAction->SetAttribute("payloadIndex", "0");
pMisAction->SetText("ShootPhoto");
pExtendedData->InsertEndChild(pMisAction);
tinyxml2::XMLElement* pPoint = doc.NewElement("Point");
pPlacemark->InsertEndChild(pPoint);
InsertChildElement(doc, pPoint, "altitudeMode", "relativeToGround");
//sprintf(strValue, "%.8lf,%.8lf,%.3lf", pWaypoint[i].lon, pWaypoint[i].lat, pWaypoint[i].alt - heiOfHome);
sprintf(strValue, "%.8lf,%.8lf,%.3lf", M300KML[i].lon, M300KML[i].lat, M300KML[i].alt);
InsertChildElement(doc, pPoint, "coordinates", strValue);
}
// Placemark
tinyxml2::XMLElement* pPlacemark = doc.NewElement("Placemark");
pDoc->InsertEndChild(pPlacemark);
InsertChildElement(doc, pPlacemark, "name", "Wayline");
InsertChildElement(doc, pPlacemark, "description", "Wayline");
InsertChildElement(doc, pPlacemark, "visibility", 1);
tinyxml2::XMLElement* pExtendedData = doc.NewElement("ExtendedData");
pExtendedData->SetAttribute("xmlns:mis", "www.dji.com");
pPlacemark->InsertEndChild(pExtendedData);
InsertChildElement(doc, pExtendedData, "mis:altitude", 50);
InsertChildElement(doc, pExtendedData, "mis:autoFlightSpeed", "4.0");
InsertChildElement(doc, pExtendedData, "mis:actionOnFinish", "GoHome");
InsertChildElement(doc, pExtendedData, "mis:headingMode", "Auto");
InsertChildElement(doc, pExtendedData, "mis:gimbalPitchMode", "UsePointSetting");
InsertChildElement(doc, pExtendedData, "mis:powerSaveMode", "false");
InsertChildElement(doc, pExtendedData, "mis:waypointType", "LineStop");
tinyxml2::XMLElement* pdroneInfo = doc.NewElement("mis:droneInfo");
pExtendedData->InsertEndChild(pdroneInfo);
InsertChildElement(doc, pdroneInfo, "mis:droneType", "PM430");
InsertChildElement(doc, pdroneInfo, "mis:advanceSettings", "true");
tinyxml2::XMLElement* pdroneCameras = doc.NewElement("mis:droneCameras");
pdroneInfo->InsertEndChild(pdroneCameras);
tinyxml2::XMLElement* pdroneCamera = doc.NewElement("mis:camera");
pdroneCameras->InsertEndChild(pdroneCamera);
InsertChildElement(doc, pdroneCamera, "mis:cameraIndex", "0");
InsertChildElement(doc, pdroneCamera, "mis:cameraName", "Zenmuse P1");
InsertChildElement(doc, pdroneCamera, "mis:cameraType", "31");
InsertChildElement(doc, pdroneCamera, "mis:payloadCameraType", "2");
tinyxml2::XMLElement* pdroneHeight = doc.NewElement("mis:droneHeight");
pdroneInfo->InsertEndChild(pdroneHeight);
InsertChildElement(doc, pdroneHeight, "mis:useAbsolute", "true");
InsertChildElement(doc, pdroneHeight, "mis:hasTakeoffHeight", "false");
InsertChildElement(doc, pdroneHeight, "mis:takeoffHeight", "0.0");
InsertChildElement(doc, pPlacemark, "styleUrl", "#waylineGreenPoly");
tinyxml2::XMLElement* pLineString = doc.NewElement("LineString");
pPlacemark->InsertEndChild(pLineString);
InsertChildElement(doc, pLineString, "tessellate", "1");
InsertChildElement(doc, pLineString, "altitudeMode", "relativeToGround");
std::string coordinatesStr;
for (int i = 0; i < nWaypoint; i++) {
char strValue[128] = "";
//sprintf(strValue, "%.8lf,%.8lf,%.3lf ", pWaypoint[i].lon, pWaypoint[i].lat, pWaypoint[i].alt - heiOfHome);
sprintf(strValue, "%.8lf,%.8lf,%.3lf ", M300KML[i].lon, M300KML[i].lat, M300KML[i].alt);
coordinatesStr += strValue;
}
InsertChildElement(doc, pLineString, "coordinates", coordinatesStr.c_str());
return (tinyxml2::XML_SUCCESS == doc.SaveFile(outputFilename.c_str())) ? true : false;
}