-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathygeocoder.js
More file actions
119 lines (105 loc) · 2.94 KB
/
ygeocoder.js
File metadata and controls
119 lines (105 loc) · 2.94 KB
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
const https = require('https');
class GeoCoder {
constructor(config = {}) {
config.debug = config.debug || false;
config.cacheTable = config.cacheTable || 'geocoder_cache';
config.apiKey = config.apiKey || '';
config.sleep = config.sleep || 30 ;
this.config = config;
this.db = this.config.db;
}
decode(text, params = {}) {
if (!text) {
return Promise.reject({ error: 'no address' });
}
const address = text
.replace(/&(quot|laquo|raquo);|[“”«»]/g, '"')
.replace(/\s/g, '+');
return new Promise((resolve, reject) => {
if (this.db && !(params.noCache || params.makeRequest)) {
this._getFromCache(address)
.then((result) => {
if (!result) {
this.decode(address, Object.assign({}, params, { makeRequest: true }))
.then((result) => {
resolve(result);
})
.catch(err => reject(err));
} else {
resolve(result);
}
})
.catch(err => reject(err));
} else {
this._loadData(address, params)
.then((result) => {
resolve(result);
})
.catch(err => reject(err));
}
});
}
_loadData(address, params) {
const apiKey = this.config.apiKey ? `&key=${this.config.apiKey}` : '';
const url = `/1.x/?format=json&geocode=${address}${apiKey}`.replace(/ /g, '+');
return new Promise((resolve, reject) => {
const req = https.get(
{
hostname: 'geocode-maps.yandex.ru',
path: encodeURI(url),
port: 443,
headers: {
'user-agent': 'alfabank.ru',
'accept-language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4'
}
},
(res) => {
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try{
const resp = JSON.parse(data.toString('utf8')).response;
if (resp.GeoObjectCollection.metaDataProperty.GeocoderResponseMetaData.found > 0) {
const geo = resp.GeoObjectCollection.featureMember[0].GeoObject.Point.pos.split(' ');
const result = {
lat: parseFloat(geo[1]) || 0,
lon: parseFloat(geo[0]) || 0
};
if (this.db && !params.noCache && result.lat && result.lon) {
this._saveToCache(address, result);
}
resolve(result);
}
} catch (e) {
return reject(e);
}
});
}
);
req.on('error', (e) => {
reject(e);
});
req.end();
});
}
_getFromCache(address) {
return this.db.query(`SELECT lon, lat FROM ${this.config.cacheTable} WHERE address = '${address}'`)
.then(({rows: result}) => {
if (!result || !result[0]) {
return Promise.resolve();
} else {
Promise.resolve({
lat: parseFloat(result[0].lat) || 0,
lon: parseFloat(result[0].lon) || 0
});
}
});
}
_saveToCache(address, geo) {
this.db.query(`REPLACE INTO ${this.config.cacheTable} (dt, address, lat, lon) VALUES (NOW(), '${address}', '${geo.lat}', '${geo.lon}')`);
}
}
module.exports = GeoCoder;