-
Notifications
You must be signed in to change notification settings - Fork 7
/
expand.js
152 lines (145 loc) · 4.38 KB
/
expand.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
/*
* partial implementation of overpass turbo extended query syntax
* (http://wiki.openstreetmap.org/wiki/Overpass_turbo/Extended_Overpass_Queries)
*/
var Promise = require('promise'),
request = require('request-promise');
// converts relative time to ISO time string
function relativeTime(instr, callback) {
var now = Date.now();
// very basic differential date
instr = instr.toLowerCase().match(/(-?[0-9]+) ?(seconds?|minutes?|hours?|days?|weeks?|months?|years?)?/);
if (instr === null) {
return Promise.reject(new Error('unable to expand date shortcut.'));
}
var count = parseInt(instr[1]);
var interval;
switch (instr[2]) {
case "second":
case "seconds":
interval=1; break;
case "minute":
case "minutes":
interval=60; break;
case "hour":
case "hours":
interval=3600; break;
case "day":
case "days":
default:
interval=86400; break;
case "week":
case "weeks":
interval=604800; break;
case "month":
case "months":
interval=2628000; break;
case "year":
case "years":
interval=31536000; break;
}
var date = now - count*interval*1000;
return Promise.resolve((new Date(date)).toISOString());
}
// Promise wrapper for browser XMLHttpRequest
// from http://www.html5rocks.com/en/tutorials/es6/promises/
function get(url) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = function() {
if (req.status == 200) {
resolve(req.response);
}
else {
reject(Error("XMLHttpRequest Error: "+req.statusText));
}
};
req.onerror = function() {
reject(Error("Network Error"));
};
req.send();
});
}
// helper function to query nominatim for best fitting result
function nominatimRequest(search,filter) {
var requestUrl = "https://nominatim.openstreetmap.org/search?format=json&q="+encodeURIComponent(search);
var _request;
if (typeof XMLHttpRequest !== "undefined") {
// browser
_request = get(requestUrl).then(JSON.parse);
} else {
// node
_request = request({
url: requestUrl,
method: "GET",
headers: {"User-Agent": "overpass-wizard"},
json: true
});
}
return _request.then(function(data) {
if (filter)
data = data.filter(filter);
if (data.length === 0)
return Promise.reject(new Error("No result found for geocoding search: "+search));
else
return data[0];
});
}
// geocoding shortcuts
function geocodeArea(instr) {
function filter(n) {
return n.osm_type && n.osm_id && n.osm_type!=="node";
}
return nominatimRequest(instr,filter).then(function(res) {
var area_ref = 1*res.osm_id;
if (res.osm_type == "way")
area_ref += 2400000000;
if (res.osm_type == "relation")
area_ref += 3600000000;
res = "area("+area_ref+")";
return res;
});
}
function geocodeCoords(instr) {
return nominatimRequest(instr).then(function(res) {
res = res.lat+','+res.lon;
return res;
});
}
var expansions = {
date: relativeTime,
geocodeArea: geocodeArea,
geocodeCoords: geocodeCoords
};
module.exports = function(overpassQuery, bbox, callback) {
// 1. bbox
if (bbox) overpassQuery = overpassQuery.replace(/{{bbox}}/g, bbox);
// 2. constants
var constantRegexp = "{{([a-zA-Z0-9_]+)=(.+?)}}";
var constants = overpassQuery.match(new RegExp(constantRegexp, 'g')) || [];
constants.forEach(function(constant) {
var constantDefinition = constant.match(new RegExp(constantRegexp)),
constantShortcut = "{{"+constantDefinition[1]+"}}",
constantValue = constantDefinition[2];
while (overpassQuery.indexOf(constantShortcut) >= 0) {
overpassQuery = overpassQuery.replace(constantShortcut, constantValue);
}
});
// 3. shortcuts
var shortcutRegexp = "{{(date|geocodeArea|geocodeCoords):([\\s\\S]*?)}}";
var shortcuts = overpassQuery.match(new RegExp(shortcutRegexp, 'g')) || [];
return Promise.all(shortcuts.map(function(shortcut) {
shortcut = shortcut.match(new RegExp(shortcutRegexp));
return expansions[shortcut[1]](shortcut[2]);
})).then(function(expansions) {
expansions.forEach(function(expansion, index) {
overpassQuery = overpassQuery.replace(shortcuts[index], expansion);
});
return overpassQuery;
}).then(function(overpassQuery) {
callback(undefined, overpassQuery)
}).catch(function(err) {
callback(err);
});
};