-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
reverse.js
105 lines (86 loc) · 2.82 KB
/
reverse.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
const _ = require('lodash');
const peliasQuery = require('pelias-query');
const defaults = require('./reverse_defaults');
//------------------------------
// reverse geocode query
//------------------------------
var query = new peliasQuery.layout.FilteredBooleanQuery();
// mandatory matches
// (none)
// scoring boost
query.sort( peliasQuery.view.sort_distance );
// non-scoring hard filters
query.filter( peliasQuery.view.boundary_circle );
query.filter( peliasQuery.view.sources );
query.filter( peliasQuery.view.layers );
query.filter( peliasQuery.view.categories );
query.filter( peliasQuery.view.boundary_country );
query.filter( peliasQuery.view.boundary_gid );
// --------------------------------
function generateQuery( clean ){
const vs = new peliasQuery.Vars( defaults );
// set size
if( clean.querySize ){
vs.var( 'size', clean.querySize);
}
// sources
if( _.isArray(clean.sources) && !_.isEmpty(clean.sources) ) {
vs.var('sources', clean.sources);
}
// layers
if( _.isArray(clean.layers) && !_.isEmpty(clean.layers) ) {
// only include non-coarse layers
vs.var( 'layers', _.intersection(clean.layers, ['address', 'street', 'venue']));
}
// focus point to score by distance
if( _.isFinite(clean['point.lat']) &&
_.isFinite(clean['point.lon']) ){
vs.set({
'focus:point:lat': clean['point.lat'],
'focus:point:lon': clean['point.lon']
});
}
// bounding circle
// note: the sanitizers will take care of the case
// where point.lan/point.lon are provided in the
// absense of boundary.circle.lat/boundary.circle.lon
if( _.isFinite(clean['boundary.circle.lat']) &&
_.isFinite(clean['boundary.circle.lon']) ){
vs.set({
'boundary:circle:lat': clean['boundary.circle.lat'],
'boundary:circle:lon': clean['boundary.circle.lon']
});
if (_.isUndefined(clean['boundary.circle.radius'])){
// for coarse reverse when boundary circle radius is undefined
vs.set({
'boundary:circle:radius': defaults['boundary:circle:radius']
});
} else if (_.isFinite(clean['boundary.circle.radius'])){
// plain reverse where boundary circle is a valid number
vs.set({
'boundary:circle:radius': clean['boundary.circle.radius'] + 'km'
});
}
}
// boundary country
if( _.isArray(clean['boundary.country']) && !_.isEmpty(clean['boundary.country']) ){
vs.set({
'boundary:country': clean['boundary.country'].join(' ')
});
}
// boundary gid
if ( _.isString(clean['boundary.gid']) ){
vs.set({
'boundary:gid': clean['boundary.gid']
});
}
// categories
if (clean.categories) {
vs.var('input:categories', clean.categories);
}
return {
type: 'reverse',
body: query.render(vs)
};
}
module.exports = generateQuery;