-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnomatim.js
280 lines (260 loc) · 9.52 KB
/
nomatim.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright 2020 - 2024, Robin de Gruijter ([email protected])
This file is part of com.gruijter.hyundai_kia.
com.gruijter.hyundai_kia is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
com.gruijter.hyundai_kia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with com.gruijter.hyundai_kia. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const https = require('https');
const qs = require('querystring');
// const util = require('util');
const _makeHttpsRequest = (options = {}) => new Promise((resolve, reject) => {
const opts = options;
opts.timeout = options.timeout || 5000;
const req = https.request(opts, (res) => {
let resBody = '';
res.on('data', (chunk) => {
resBody += chunk;
});
res.once('end', () => {
if (!res.complete) {
return reject(Error('The connection was terminated while the message was still being sent'));
}
res.body = resBody;
return resolve(res); // resolve the request
});
});
req.on('error', (e) => {
req.destroy();
return reject(e);
});
req.on('timeout', () => {
req.destroy();
});
req.end();
});
const search = async (params) => {
try {
const errTxt = 'Parameter needs to be a string or an object with street, city, county, state, country, postalcode';
if (typeof params === 'object') {
if (!Object.keys(params).some((key) => ['street', 'city', 'county', 'state', 'country', 'postalcode'].includes(key))) throw Error(errTxt);
} else if (typeof params !== 'string') throw Error(errTxt);
const query = {
format: 'jsonv2', // [xml|json|jsonv2|geojson|geocodejson]
addressdetails: 1,
extratags: 1,
namedetails: 1,
limit: 1,
email: '[email protected]', // <valid email address> only used to contact you in the event of a problem, see Usage Policy
};
if (typeof params === 'string') query.q = params;
if (typeof params === 'object') Object.assign(query, params);
const headers = {
'Content-Length': 0,
};
const options = {
hostname: 'nominatim.openstreetmap.org',
path: `/search?${qs.stringify(query)}`,
headers,
'User-Agent': 'Homey Hyundai_Kia',
method: 'GET',
};
const result = await _makeHttpsRequest(options, '');
if (result.statusCode !== 200 || result.headers['content-type'] !== 'application/json; charset=UTF-8') {
throw Error(`geo search service error: ${result.statusCode}`);
}
const jsonData = JSON.parse(result.body);
if (jsonData.length < 1) throw Error('location not found');
// console.log(util.inspect(jsonData, { depth: null, colors: true }));
return Promise.resolve(jsonData[0]);
} catch (error) {
return Promise.reject(error);
}
};
const reverseGeo = async (lat, lon) => {
try {
const query = {
format: 'jsonv2', // [xml|json|jsonv2|geojson|geocodejson]
// osm_type: 'N', // [N|W|R] node / way / relation, preferred over lat,lon
lat, // The location to generate an address for
lon, // The location to generate an address for
zoom: 18, // [0-18] Level of detail required where 0 is country and 18 is house/building
addressdetails: 1, // [0|1] Include a breakdown of the address into elements
email: '[email protected]', // <valid email address> only used to contact you in the event of a problem, see Usage Policy
// extratags: 1, // [0|1] Include additional information in the result if available, e.g. wikipedia link, opening hours.
// namedetails: 1, // [0|1] Include a list of alternative names in the results. language variants, references, operator and brand
};
const headers = {
'Content-Length': 0,
};
const options = {
hostname: 'nominatim.openstreetmap.org',
path: `/reverse?${qs.stringify(query)}`,
headers,
'User-Agent': 'Homey Hyundai_Kia',
method: 'GET',
};
const result = await _makeHttpsRequest(options, '');
if (result.statusCode !== 200 || !result.headers['content-type'].includes('json')) {
throw Error(`reverse geo service error: ${result.statusCode}`);
}
const jsonData = JSON.parse(result.body);
// console.log(util.inspect(jsonData, { depth: null, colors: true }));
return Promise.resolve(jsonData);
} catch (error) {
return Promise.reject(error);
}
};
const test = () => {
const testLocs = [[51.50667, -0.08713], [52.46760, 13.52803], [41.88980, 12.49124], [38.89734, -77.03655]];
const resArray = testLocs.map((loc) => reverseGeo(loc[0], loc[1]));
const testAddress = 'Amsterdam nemo';
resArray.push(search(testAddress));
return Promise.all(resArray);
};
const getCarLocString = async (location) => {
try {
let local = '-?-';
let address = '-?-';
const loc = await reverseGeo(location.latitude, location.longitude);
if (!loc.address) { // no reverse geolocation available
return Promise.resolve({ location, address });
}
// const countryCode = loc.address.country_code.toUpperCase();
local = loc.address.city_district || loc.address.village || loc.address.town || loc.address.city
|| loc.address.municipality || loc.address.county || loc.address.state_district || loc.address.state || loc.address.region;
// locString = `${countryCode}${loc.address.postcode} ${local}`;
// local = `${local}`;
address = loc.display_name;
return Promise.resolve({ local, address });
} catch (error) {
return Promise.reject(error);
}
};
module.exports.test = test;
module.exports.search = search;
module.exports.reverseGeo = reverseGeo;
module.exports.getCarLocString = getCarLocString;
/*
addressdetails
Address details in the xml and json formats return a list of names together with a designation label. Per default the following labels may appear:
continent
country, country_code
region, state, state_district, county
municipality, city, town, village
city_district, district, borough, suburb, subdivision
hamlet, croft, isolated_dwelling
neighbourhood, allotments, quarter
city_block, residental, farm, farmyard, industrial, commercial, retail
road
house_number, house_name
emergency, historic, military, natural, landuse, place, railway, man_made, aerialway, boundary, amenity, aeroway, club, craft, leisure, office, mountain_pass, shop, tourism, bridge, tunnel, waterway
{ place_id: 81479432,
licence: 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
osm_type: 'way',
osm_id: 27687816,
lat: '52.374028',
lon: '4.91789314639283',
display_name: 'Marinekazerne Amsterdam, Dijksgrachtkade, Oostelijke Eilanden, Amsterdam, Noord-Holland, Nederland, 1019BT, Nederland',
address:
{ address29: 'Marinekazerne Amsterdam',
road: 'Dijksgrachtkade',
neighbourhood: 'Oostelijke Eilanden',
suburb: 'Amsterdam',
city: 'Amsterdam',
state: 'Noord-Holland',
postcode: '1019BT',
country: 'Nederland',
country_code: 'nl' },
boundingbox: [ '52.3725587', '52.3759276', '4.9143916', '4.9210072' ] }
{
place_id: 235247644,
licence: 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
osm_type: 'relation',
osm_id: 124410,
lat: '41.890224',
lon: '12.49116833244149',
display_name: 'Piazza del Colosseo, Rione XIX Celio, Municipio Roma I, Roma, Roma Capitale, Lazio, Italia',
address: {
road: 'Piazza del Colosseo',
quarter: 'Rione XIX Celio',
suburb: 'Municipio Roma I',
city: 'Roma',
county: 'Roma Capitale',
state: 'Lazio',
country: 'Italia',
country_code: 'it'
},
boundingbox: [ '41.8893606', '41.8910905', '12.4908796', '12.4938184' ]
}
{
place_id: 98155333,
licence: 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
osm_type: 'way',
osm_id: 43063205,
lat: '52.46765655',
lon: '13.527714596391501',
display_name: 'Modellpark Berlin-Brandenburg, 81, An der Wuhlheide, Oberschöneweide, Treptow-Köpenick, Berlin, 12459, Deutschland',
address: {
tourism: 'Modellpark Berlin-Brandenburg',
house_number: '81',
road: 'An der Wuhlheide',
suburb: 'Oberschöneweide',
borough: 'Treptow-Köpenick',
city: 'Berlin',
postcode: '12459',
country: 'Deutschland',
country_code: 'de'
},
boundingbox: [ '52.4665429', '52.4685188', '13.5263966', '13.5292893' ]
}
{
place_id: 224676065,
licence: 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
osm_type: 'way',
osm_id: 702285118,
lat: '51.5039477',
lon: '-0.09196511873755582',
display_name: 'Roman Southwark, London Bridge, Borough, Southwark, London Borough of Southwark, City of London, Greater London, England, SE1 2PF, United Kingdom',
address: {
historic: 'Roman Southwark',
road: 'London Bridge',
quarter: 'Borough',
suburb: 'Southwark',
city: 'London Borough of Southwark',
county: 'City of London',
state_district: 'Greater London',
state: 'England',
postcode: 'SE1 2PF',
country: 'United Kingdom',
country_code: 'gb'
},
boundingbox: [ '51.5007496', '51.5072237', '-0.0973303', '-0.0862909' ]
}
{
place_id: 279192455,
licence: 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
osm_type: 'way',
osm_id: 779232532,
lat: '38.8974662',
lon: '-77.03660175919836',
display_name: 'Penn Quarter, Washington, District of Columbia, United States of America',
address: {
neighbourhood: 'Penn Quarter',
city: 'Washington',
county: 'Washington',
state: 'District of Columbia',
country: 'United States of America',
country_code: 'us'
},
boundingbox: [ '38.8974521', '38.8974818', '-77.0366164', '-77.0365858' ]
}
*/