forked from redhog/agpsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kml.js
198 lines (184 loc) · 5.27 KB
/
kml.js
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
var express = require('express');
var async = require('async');
var underscore = require('underscore');
var logger = require('./logger');
var listify = function (x) {
if (!x) return [];
if (typeof(x) == "string") return [x];
return x;
}
exports.getKml = function(req, res) {
var wrap = function (header, footer, content, done) {
res.write(header);
content(function () {
res.write(footer);
done();
});
}
var sendKml = function (content, done) {
// res.header('Content-type', 'application/vnd.google-earth.kml+xml');
res.header('Content-type', 'text/plain');
wrap(
'<?xml version="1.0" encoding="UTF-8"?>\n' +
'<kml xmlns="http://www.opengis.net/kml/2.2">\n' +
' <Document>\n' +
' <name>AGPS exported route</name>\n' +
' <description>AGPS exported route</description>\n',
' </Document>\n' +
'</kml>\n',
content,
function () {
res.end();
done();
}
);
}
var sendCoordinates = function (coordinates, done) {
wrap(
'<coordinates>\n',
'</coordinates>\n',
function (cb) {
coordinates(function (err, rows) {
if (err) { return console.log(err); }
rows.map(
function (row) {
res.write(row.lon + "," + row.lat + "," + (row.alt || 0) + "\n");
});
cb();
});
},
done
);
}
var sendRoute = function (name, coordinates, done) {
wrap(
' <Placemark>\n' +
' <name>' + name + ' - route</name>\n' +
' <description>Route</description>\n' +
' <LineString>\n' +
' <extrude>1</extrude>\n' +
' <tessellate>1</tessellate>\n' +
' <altitudeMode>absolute</altitudeMode>\n' +
' <coordinates>\n',
' </coordinates>\n' +
' </LineString>\n' +
' </Placemark>\n',
function (cb) { sendCoordinates(coordinates, cb); },
done
);
}
var writePoint = function (point) {
res.write('<Placemark>\n');
res.write(
' <name>' + point.name + '</name>\n' +
' <description>' + (point.description || '') + '</description>\n' +
' <Point>\n' +
' <coordinates>\n' +
point.lon + "," + point.lat + "," + (point.alt || 0) + "\n" +
' </coordinates>\n' +
' </Point>\n');
if (point.time) {
res.write("<TimeStamp><when>" + (new Date(point.time)).toISOString() + "</when></TimeStamp>");
}
res.write('</Placemark>\n');
}
var sendDatabaseRoute = function (query, cb) {
logger.onlyWithRoute(
query,
function (cb) {
sendRoute(
query.name,
function (cb) {
logger.getRoute(query, cb);
},
cb
);
},
cb
);
}
var sendDatabaseRoutePoints = function (query, cb) {
logger.onlyWithRoute(
query,
function (cb) {
exports.db.each(
"select data from (" + query.sql + ") where lon is not null and lat is not null order by timestamp asc",
query.params,
function(err, row) {
if (err) return console.log(err);
var data = JSON.parse(row.data);
data.name = query.name;
writePoint(data);
},
cb
);
},
cb
);
}
var sendDevice = function (device, cb) {
sendDatabaseRoute(
underscore.extend({}, req.query, {
name: device,
sql: "select * from events where class = 'TPV' and device = $device and lon is not null and lat is not null order by timestamp asc",
params: {$device: device}}),
cb);
}
var sendDevices = function (devices, cb) {
var i = 0;
var next = function () {
if (i >= devices.length) return cb();
sendDevice(devices[i], function () {
i++;
next();
});
};
next();
}
var sendVessel = function (vessel, cb) {
sendDatabaseRoute(
underscore.extend({}, req.query, {
name: vessel.mmsi,
sql: "select * from events join events_ais on events.id = events_ais.id where events.class = 'AIS' and events_ais.vessel_id = $vessel",
params: {$vessel: vessel.id}}),
function () {
sendDatabaseRoutePoints({
name: vessel.mmsi,
sql: "select * from events join events_ais on events.id = events_ais.id where events.class = 'AIS' and events_ais.vessel_id = $vessel",
params: {$vessel: vessel.id}},
cb);
});
}
var sendVessels = function (vessels, cb) {
var i = 0;
var next = function () {
if (i >= vessels.length) return cb();
sendVessel(vessels[i], function () {
i++;
next();
});
};
next();
}
sendKml(
function (cb) {
async.series([
function (cb) {
if (req.query.devices) {
sendDevices(listify(req.query.devices).map(unescape), cb);
} else {
logger.getDevices(function (err, devices) {
sendDevices(devices.map(function (device) { return device.path; }), cb);
});
}
},
function (cb) {
logger.getVessels(function (err, vessels) {
sendVessels(vessels, cb);
});
}
], cb);
},
function () {}
);
}