-
Notifications
You must be signed in to change notification settings - Fork 1
/
queryMapping.js
300 lines (256 loc) · 10.1 KB
/
queryMapping.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// this file maps intent from DialogFlow to respective query in queries.js
const queries = require('./queries');
let dbUrl = process.env.MONGO_URL;
let dbName = process.env.MONGO_DB;
queries.setupDatabase(dbUrl, dbName);
const mapping = {
'Default Fallback Intent': async function (parameters) {
return {
message: 'Sorry, I could not recognize this command',
geoInfo: [],
showInCards: false,
intent: {
action: 'failure'
}
}
},
find_in_radius: async function (parameters) {
let radius = parameters['unit-length']['amount'];
radius = radius === undefined ? 100000 : radius
// console.log(radius)
let factor = 1;
if (parameters['unit-length'].unit === 'm')
factor = 1;
else if (parameters['unit-length'].unit === 'km')
factor = 1000;
else if (parameters['unit-length'].unit === 'mi')
factor = 1600;
let distance = radius * factor;
switch (parameters.poi) {
case 'schools':
let schools = await queries.findSchoolsInRadius(distance);
for (let each of schools) {
each['showInCards'] = true
color: '#fff'
}
return {
message: `There are ${schools.length} schools in given radius.`,
geoInfo: schools,
intent: {
action: 'plot'
}
}
break;
case 'hospitals':
let hospitals = await queries.findHospitalsInRadius(distance);
for (let each of schools) {
each['showInCards'] = true
color: '#fff'
}
return {
message: `There are ${hospitals.length} hospitals in given radius.`,
geoInfo: hospitals,
intent: {
action: 'plot'
}
}
break;
}
},
find_future_X: async function (parameters) {
let number = parameters['number'];
let budget = parameters['number'];
var cas1 = parameters['availability'];
var cas2 = parameters['current'];
switch (cas1) {
case 'findAvailabilityWasteLand':
number = number == '' ? 100 : number
let area = 0.000000009643762973 * number;
// area = 0
let futureX = await queries.findWasteLand();
let filteredAnswer = [];
for (var i = futureX.length - 1; i >= 0; i--) {
console.log(futureX[i].metadata.Shape_Area, area)
console.log(typeof (futureX[i].metadata.Shape_Area), typeof (area))
if (futureX[i].metadata.Shape_Area >= area) {
futureX[i]["showInCards"] = true;
filteredAnswer.push(futureX[i]);
}
}
console.log(filteredAnswer);
return {
message: `We found ${filteredAnswer.length} suitable location.`,
geoInfo: filteredAnswer,
intent: { action: 'plot' },
showInCards: true
}
break;
}
switch (cas2) {
case 'getKuchaRoads':
ans = await queries.genericFind('Road', { 'metadata.type': 'Kutchha Road' })
budget = parameters['number'];
if (!budget)
budget = 10000
filteredans = await queries.filterRoadByBudget(budget, ans)
for (let each of filteredans) {
each["metadata"]["Shape_Area"] = 0
each['showInCards'] = true
}
// console.log(filteredans);
message = `${filteredans.length} Kutchha roads can be developed with given budget of ${budget} crores`
return {
message: message,
geoInfo: filteredans,
intent: { action: 'plot' },
showInCards: true
}
}
},
find_current_X: async function (parameters) {
let current;
if (typeof (parameters['current']) == 'string') {
current = parameters['current']
} else {
current = parameters['current'][0]
}
let cursor;
let ans = [];
let message = 'Cannot determine the command';
switch (current) {
case 'getReservoir':
ans = await queries.genericFind('LULC', { 'metadata.lc_code': 'WBRT' })
message: `Found ${ans.length} reservoirs.`
break;
case 'getCanal':
ans = await queries.genericFind('LULC', { 'metadata.dscr2': 'Canal' })
message: `Found ${ans.length} canals.`
break;
case 'getAgriculture':
ans = await queries.genericFind('LULC', { 'metadata.lc_code': { $in: ['AGPL', 'AGCR'] } })
message = `Found ${ans.length} agriculture lands.`
for (let each of ans) {
each['showInCards'] = true
switch (each.metadata.lc_code) {
case 'AGPL':
each['color'] = '#7ed321'
break
case 'AGCR':
each['color'] = '#d0cb00'
break
default:
each['color'] = 'blue'
}
}
break;
case 'getAllWater':
ans = await queries.genericFind('LULC', { 'metadata.dscr1': 'Water bodies' })
console.log('ans', ans)
for (let each of ans) {
each['showInCards'] = true
switch (each.metadata.lc_code) {
case 'WBCN':
each['color'] = '#299ef4'
break
case 'WBLP':
case 'WBRS':
each['color'] = '#144ba2'
break
case 'WBRT':
each['color'] = '#00e1e1'
break
default:
each['color'] = 'blue'
}
}
message = `Found ${ans.length} water sources.`
break;
case 'getMines':
ans = await queries.genericFind('LULC', { 'metadata.dscr3': 'Mining / Industrial' })
message = `Found ${ans.length} mines.`
break;
case 'getDrainage':
ans = await queries.genericFind('LULC', { 'metadata.dscr3': '' })
message = `Found ${ans.length} drainages.`
break;
case 'getTransport':
ans = await queries.genericFind('LULC', { 'metadata.dscr3': 'Transportation' })
message = `Found ${ans.length} transport locations.`
break;
case 'population':
ans = queries.describeDemography()
message = `There are ${ans.population} people with sex ratio ${ans.sex_ratio} and ${ans['0_6_age']} children.`
ans = null
break
default:
console.log('no match')
}
return {
message: message,
geoInfo: ans,
showInCards: true,
intent: { action: 'plot' }
}
},
find_nearest_NH: async function (parameters) {
let result = await queries.findNearestNationalHighway();
result.gp['color'] = '#c0ea56'
result.gp['showInCards'] = false
result.nh['color'] = '#fff'
result.nh['showInCards'] = false
return {
message: `Nearest national highway is at approximately ${result.distance} kilometers`,
geoInfo: [result.gp, result.nh],
intent: { action: 'plot' },
showInCards: false
};
},
find_nearest_schools: async function (parameters) {
let result = await queries.findNearestSchoolsToVillage();
// result['showInCards'] = false
return {
message: `Nearest school to the village at approximately ${result.schools[0].distance} kilometers`,
geoInfo: [result.village, ...result.schools],
intent: { action: 'plot' },
showInCards: false
}
},
navigate: async function (parameters) {
let village = parameters.navigate_to
if (village === 'chandlodiya') {
return {
message: `Navigating to ${village}.`,
intent: {
action: 'moveTo',
loc: {
lng: 72.5410875, lat: 23.0632311
},
zoom: 21
},
geoInfo: []
}
} else {
return {
message: `Navigating to ${village}.`,
intent: {
action: 'moveTo',
loc: { lng: 84.1538802, lat: 18.5337648 },
zoom: 14
},
geoInfo: []
}
}
},
switch_language: async function (parameters) {
let language_initials = parameters.poi.slice(0, 2)
return {
message: `Changing language to ${parameters.poi}`,
intent: {
action: 'changeLanguage',
lang: language_initials
},
geoInfo: []
}
}
}
module.exports = { mapping }