-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_locality.js
85 lines (81 loc) · 3.4 KB
/
cache_locality.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
const https = require('https'),
async = require('async'),
fs = require("fs-extra");
const file_name = process.argv[2];
let location_file = './source/locations.json';
let db = require(location_file);
function getLocality(response) {
var json_response = JSON.parse(response);
var locality = "";
search_for = ["locality", "administrative_area_level_3", "administrative_area_level_2"];
if (json_response.results && json_response.results[0]) {
for (var index_address in json_response.results[0].address_components) {
if(json_response.results[0].address_components[index_address].types.some(function (v) { return search_for.indexOf(v) >= 0;
})) {
locality = json_response.results[0].address_components[index_address].short_name;
break;
}
}
}
return locality;
}
fs.readFile('./' + file_name, 'utf8', function readFileCallback(err, data){
if (err) {
console.log(err);
}
else {
json_data = JSON.parse(data);
let time, old_time, input;
const points = [];
if (json_data && json_data.features && json_data.features.length) {
if(json_data.features[0].geometry.type == 'Point') {
for (let point_index in json_data.features) {
input = [json_data.features[point_index].geometry.coordinates[1], json_data.features[point_index].geometry.coordinates[0]].join(',');
time = new Date(json_data.features[point_index].properties.time).getTime();
time_difference = (time - old_time) / 1000;
if (time_difference > 3600 || time_difference < -3600 || point_index == 0 || point_index == json_data.features.length -1) {
points.push(input);
if (time_difference > 3600 || time_difference < -3600) {
let more_input = [json_data.features[point_index - 1].geometry.coordinates[1], json_data.features[point_index - 1].geometry.coordinates[0]].join(',');
points.push(more_input);
}
}
old_time = time;
}
}
else if(json_data.features[0].geometry.type == 'LineString') {
for (let point_index in json_data.features[0].geometry.coordinates) {
input = [json_data.features[0].geometry.coordinates[point_index][1], json_data.features[0].geometry.coordinates[point_index][0]].join(',');
time = new Date(json_data.features[0].properties.coordTimes[point_index]).getTime();
time_difference = (time - old_time) / 1000;
if (time_difference > 3600 || time_difference < -3600 || point_index == 0 || point_index == json_data.features[0].geometry.coordinates.length -1) {
points.push(input);
if (time_difference > 3600 || time_difference < -3600) {
let more_input = [json_data.features[0].geometry.coordinates[point_index - 1][1], json_data.features[0].geometry.coordinates[point_index - 1][0]].join(',');
points.push(more_input);
}
}
old_time = time;
}
}
}
//console.log(points);
async.each(points, function(point, callback) {
https.get("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + point + "&key=AIzaSyCMETxi9mRjWMt726OBZAAhRyXj1xyuPOs", function(response) {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
let locality = getLocality(data);
if(!db[point]) {
db[point] = locality;
}
callback();
});
});
}, function(err) {
fs.writeFile(location_file, JSON.stringify(db), 'utf8', function(err, saved_data) {});
});
}
});