-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
63 lines (53 loc) · 1.82 KB
/
handler.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
'use strict';
const fetch = require('node-fetch');
const togeojson = require('@mapbox/togeojson');
const DOMParser = require('xmldom').DOMParser;
module.exports.inreach = async event => {
const username = process.env.GARMIN_MAPSHARE_USERNAME
const password = process.env.GARMIN_MAPSHARE_PASSWORD
const mapshare_url = process.env.GARMIN_MAPSHARE_URL
// const date_from = process.env.GARMIN_MAPSHARE_D1 || "2021-02-01T00:00z";
const auth = new Buffer(`${username}:${password}`).toString('base64');
// const response = await fetch(mapshare_url + `?d1=${date_from}`, {
const response = await fetch(mapshare_url + ``, {
headers: {
Authorization: 'Basic ' + auth
}
});
const kml = await response.text();
const geojson = togeojson.kml(new DOMParser('xmldom').parseFromString(kml));
for (var i=0; i < geojson.features.length; i++) {
if (geojson.features[i].geometry.type == 'LineString') {
geojson.features.splice(i, 1);
}
}
console.log(geojson);
function filterOutSentisiveStuff(key, value) {
const excludeKeys = [
"IMEI",
"In Emergency",
"Device Identifier",
"Incident Id",
"Text",
"Event"
];
if (key != "properties") {
return value
}
excludeKeys.forEach(excludedKey => {
if (excludedKey in value) {
delete value[excludedKey];
}
})
return value;
}
return {
statusCode: 200,
body: JSON.stringify(geojson, filterOutSentisiveStuff, 2),
headers: {
"Access-Control-Allow-Headers": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,GET"
}
};
};