-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.js
307 lines (279 loc) · 8.34 KB
/
meta.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
301
302
303
304
305
306
307
var pg = require('pg'),
_ = require('underscore'),
db = require('./db'),
dev = require('./dev');
_.mixin({
// ### _.objMap
// _.map for objects, keeps key/value associations
objMap: function (input, mapper, context) {
return _.reduce(input, function (obj, v, k) {
obj[k] = mapper.call(context, v, k, input);
return obj;
}, {}, context);
},
// ### _.objFilter
// _.filter for objects, keeps key/value associations
// but only includes the properties that pass test().
objFilter: function (input, test, context) {
return _.reduce(input, function (obj, v, k) {
if (test.call(context, v, k, input)) {
obj[k] = v;
}
return obj;
}, {}, context);
}
});
exports.timeline = function (req, res) {
var client = new pg.Client(db.conn);
client.connect();
var years = [],
q = dev.checkQuery(
`SELECT * FROM (
SELECT firstdispl AS year FROM basepoint
UNION SELECT lastdispla AS year FROM basepoint
UNION SELECT firstdispl AS year FROM baseline
UNION SELECT lastdispla AS year FROM baseline
UNION SELECT firstdispl AS year FROM basepoly
UNION SELECT lastdispla AS year FROM basepoly
UNION SELECT firstdispl AS year FROM mapsplans
UNION SELECT lastdispla AS year FROM mapsplans
UNION SELECT firstdispl AS year FROM viewsheds
UNION SELECT lastdispla AS year FROM viewsheds
) as q
ORDER BY year`, req);
client.query(q, function (err, result) {
years = _.map(result.rows, r => r.year);
years.pop();
res.send(years);
client.end();
});
}
exports.layers = function (req, res) {
var client = new pg.Client(db.conn);
client.connect();
var year = req.params.year,
arr = [],
layers = {},
q = dev.checkQuery(
`SELECT
folder,
geo.layer,
geo.featuretyp,
geo.stylename,
layername,
fill,
stroke,
shape
FROM (
SELECT layer, featuretyp, stylename
FROM baseline
WHERE firstdispl <= $1 AND lastdispla >= $1
GROUP BY layer, featuretyp, stylename
UNION SELECT layer, featuretyp, stylename
FROM basepoint
WHERE firstdispl <= $1 AND lastdispla >= $1
GROUP BY layer, featuretyp, stylename
UNION SELECT layer, featuretyp, stylename
FROM basepoly
WHERE firstdispl <= $1 AND lastdispla >= $1
GROUP BY layer, featuretyp, stylename
) AS geo
INNER JOIN legend AS le
ON geo.stylename = le.stylename
INNER JOIN layers AS la
ON geo.layer = la.layer AND geo.featuretyp = la.featuretyp
WHERE geo.featuretyp IS NOT NULL
ORDER BY sort`, req);
client.query(q, [year], function (err, arr) {
var styles = _.indexBy(arr.rows, 'stylename');
var layers = _.objMap(_.groupBy(arr.rows, 'folder'), function (f) {
return _.objMap(_.groupBy(f, 'layer'), function (l, name) {
let layer = {};
if (_.uniq(_.pluck(l, 'stylename')).length === 1) {
layer.id = l[0].stylename;
layer.features = _.pluck(l, 'featuretyp');
layer.style = _.pick(l[0], 'fill', 'stroke', 'shape');
} else {
layer.features = {};
_.each(l, function (f) {
layer.features[f.featuretyp] = {
id: f.stylename,
style: _.pick(f, 'fill', 'stroke', 'shape')
};
});
}
return layer;
});
});
res.send(layers);
client.end();
});
}
exports.raster = function (req, res) {
var client = new pg.Client(db.conn);
client.connect();
var year = req.params.year,
max = req.query.max || year,
arr = [],
q = dev.checkQuery(
`SELECT
globalid AS id,
'SSID' || imageid AS file,
repository,
firstdispl AS date,
creator,
title AS description,
notes AS credits,
layer,
ST_AsText(ST_Envelope(geom)) AS bbox
FROM mapsplans
WHERE firstdispl <= $1 AND lastdispla >= $2
UNION SELECT
globalid AS id,
'SSID' || imageid AS file,
repository,
firstdispl AS date,
creator,
title AS description,
notes AS credits,
layer, ST_AsText(ST_Envelope(geom)) AS bbox
FROM viewsheds
WHERE firstdispl <= $1 AND lastdispla >= $2
ORDER BY layer`, req);
client.query(q, [max, year], function (err, arr) {
res.send(arr.rows);
client.end();
});
}
exports.search = function (req, res) {
var client = new pg.Client(db.conn);
client.connect();
var year = req.params.year,
word = `%${req.params.word}%`,
names = {},
q = dev.checkQuery(
`SELECT
array_agg( id ) as gid,
namecomple,
array_agg( file ) AS file,
layer,
featuretyp
FROM (
SELECT
globalid AS id,
namecomple,
NULL AS file,
layer,
featuretyp
FROM basepoint
WHERE namecomple ILIKE $1 AND firstdispl <= $2 AND lastdispla >= $2
UNION SELECT
globalid AS id,
namecomple,
NULL AS file,
layer,
featuretyp
FROM baseline
WHERE namecomple ILIKE $1 AND firstdispl <= $2 AND lastdispla >= $2
UNION SELECT
globalid AS id,
namecomple,
NULL AS file,
layer,
featuretyp
FROM basepoly
WHERE namecomple ILIKE $1 AND firstdispl <= $2 AND lastdispla >= $2
UNION SELECT
globalid AS id,
'SSID' || imageid AS file,
title AS namecomple,
layer,
NULL AS featuretyp
FROM viewsheds
WHERE title ILIKE $1 AND firstdispl <= $2 AND lastdispla >= $2
UNION SELECT
globalid AS id,
'SSID' || imageid AS file,
title AS namecomple,
layer,
NULL AS featuretyp
FROM mapsplans
WHERE title ILIKE $1 AND firstdispl <= $2 AND lastdispla >= $2
) as q
GROUP BY namecomple, layer, featuretyp
ORDER BY layer, featuretyp`, req);
client.query(q, [word, year], function (err, result) {
_.each(result.rows, (r) => {
names[r.namecomple] = { id: r.gid, layer: r.layer, featuretyp: r.featuretyp };
if (r.file[0]) names[r.namecomple].file = r.file;
});
res.send(names);
client.end();
});
}
exports.plans = function (req, res) {
var client = new pg.Client(db.conn);
client.connect();
var plans = [],
q = dev.checkQuery("SELECT planyear, planname FROM plannedpoly UNION SELECT planyear, planname FROM plannedline ORDER BY planyear, planname", req);
client.query(q, function (err, plans) {
res.send(plans.rows);
client.end();
});
}
exports.details = function (req, res) {
var client = new pg.Client(db.conn);
client.connect();
var id = _.reduce(req.params.id.split(","), function (memo, i) { return memo += "'" + i + "',"; }, "ANY(ARRAY[").replace(/,$/, "])"),
details = [],
q = dev.checkQuery(
`SELECT creator, firstowner, owner, occupant, address, firstdispl, lastdispla, globalid
FROM basepoint
WHERE globalid = ${id}
UNION SELECT creator, firstowner, owner, occupant, address, firstdispl, lastdispla, globalid
FROM baseline
WHERE globalid = ${id}
UNION SELECT creator, firstowner, owner, occupant, address, firstdispl, lastdispla, globalid
FROM basepoly
WHERE globalid = ${id}`, req);
client.query(q, function (err, result) {
_.each(result.rows, function (r) {
if (r.lastdispla == 8888) r.lastdispla = 'Present';
r.year = r.firstdispl + " - " + r.lastdispla;
r = _.objFilter(_.omit(r, ["globalid", "firstdispl", "lastdispla",]), function (value) {
return value != null;
});
details.push(r);
});
res.send(details);
client.end();
});
}
exports.names = function (req, res) {
var client = new pg.Client(db.conn);
client.connect();
var names = {},
lang = req.params.lang,
q = dev.checkQuery("SELECT LOWER( text ) AS text, name_en, name_pr FROM names", req);
client.query(q, function (err, result) {
_.each(result.rows, function (r) {
names[r.text] = r["name_" + lang];
});
res.send(names);
client.end();
});
}
exports.collector = function (req, res) {
var client = new pg.Client(db.conn);
client.connect();
var data = req.body;
var geo = JSON.parse(data.polygon);
var q = "INSERT INTO viewsheds_dev ( layer, globalid, creator, repository, firstdispl, lastdispla, imageid, title, geom, uploaddate, latitude, longitude ) VALUES ( 'viewsheds', " + data.id + ", '" + data.creator + "', '" + data.repository + "', " + data.firstdisplay + ", " + data.lastdisplay + ", '" + data.ssid + "', '" + data.title + "', ST_GeomFromGeoJSON('" + JSON.stringify(geo.geometry) + "'), 9999, " + data.lat + ", " + data.lon + ")";
client.query(q);
query.on('end', function () {
res.status(200).send('Successfully added ' + data.id);
});
query.on('error', function (err) {
res.status(500).send(err);
});
}