-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (55 loc) · 1.64 KB
/
index.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
// Converting SQL to JSON and change the encoded poly to an array of locations
var fs = require('fs');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'P@ssw0rd',
database: 'test'
});
// Function credits goes to :
// https://gist.github.com/ismaels/6636986
function decode(encoded) {
// array that holds the points
var points = []
var index = 0,
len = encoded.length;
var lat = 0,
lng = 0;
while (index < len) {
var b, shift = 0,
result = 0;
do {
b = encoded.charAt(index++).charCodeAt(0) - 63; //finds ascii //and substract it by 63
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++).charCodeAt(0) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
points.push({ latitude: (lat / 1E5), longitude: (lng / 1E5) })
}
return points
}
connection.connect();
connection.query('SELECT * from sa_regions;', function (err, rows, fields) {
if (err) throw err;
var tempencode = '';
for (var i = 0; i < rows.length; i++) {
tempencode = rows[i].location.trim();
rows[i].location = decode(tempencode);
}
fs.writeFile('./filename.json', JSON.stringify(rows), function (err) {
if (err) throw err;
console.log('Saved!');
});
});
connection.end();