-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmlclient.cpp
87 lines (65 loc) · 2.35 KB
/
kmlclient.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
#include "kmlclient.h"
#include "XPLMProcessing.h"
#include "XPLMPlugin.h"
#include "XPLMMenus.h"
#include "XPLMUtilities.h"
using namespace std;
PLUGIN_API float flightloopCallback(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter, void * inRefcon) {
if (inRefcon)
return ((KmlClient*)inRefcon)->flightloop(inElapsedSinceLastCall, inElapsedTimeSinceLastFlightLoop, inCounter, inRefcon);
return 0;
}
KmlClient::KmlClient() :
mCurrentLatitude(XPLMFindDataRef("sim/flightmodel/position/latitude")),
mCurrentLongitude(XPLMFindDataRef("sim/flightmodel/position/longitude")),
mCurrentAltitude(XPLMFindDataRef("sim/flightmodel/position/y_agl"))
{
enabled = false;
}
KmlClient::~KmlClient() {
disable();
}
void KmlClient::enable() {
XPLMRegisterFlightLoopCallback(flightloopCallback, 4, this);
}
void KmlClient::disable() {
if (enabled) {
stop_recording();
}
XPLMUnregisterFlightLoopCallback(flightloopCallback, this);
}
void KmlClient::start_recording() {
std::string str = std::string("Output/") + std::string(get_current_date_time()) + std::string(".kml");
handle.open(str);
handle << "<?xml version='1.0' encoding='utf-8'?>\n";
handle << "<kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2'>\n";
handle << "<Placemark>\n";
handle << "\t<name>XP flight path</name>\n";
handle << "\t<description>Generated by XP2KML</description>\n";
handle << "\t<gx:Track>\n";
handle << "\t\t<extrude>1</extrude>\n";
handle << "\t\t<tessellate>1</tessellate>\n";
handle << "\t\t<altitudeMode>relativeToGround</altitudeMode>\n";
enabled = true;
}
void KmlClient::stop_recording() {
enabled = false;
handle << "\t</gx:Track>\n";
handle << "</Placemark>\n";
handle << "</kml>\n";
handle.close();
}
float KmlClient::flightloop(float, float, int, void *) {
if (enabled) {
handle << "\t\t\t<gx:coord>" << XPLMGetDataf(mCurrentLongitude) << " " << XPLMGetDataf(mCurrentLatitude) << " " << XPLMGetDataf(mCurrentAltitude) << "</gx:coord>\n";
}
return 4;
}
const std::string KmlClient::get_current_date_time() {
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "%Y%m%d_%H%M%S");
auto str = oss.str();
return str;
}