-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vectorgator.js
executable file
·360 lines (320 loc) · 10.2 KB
/
Vectorgator.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* Created by Nicholas Hallahan <[email protected]>
* on 7/29/14.
*/
var pg = require('pg');
var settings = require('./settings.js');
var fs = require('fs');
// PostGIS Connection String
var conString = "postgres://" +
settings.postgres.user + ":" +
settings.postgres.password + "@" +
settings.postgres.server + ":" +
settings.postgres.port + "/" +
settings.postgres.database;
var sqlFiles = {};
var pipeSeparatedValues = {};
module.exports = {};
var run = module.exports.run = function() {
// ========= FINDING DISTINCT CATEGORIES =========
// creating a json file with a list of all of the categories for each field we group by
if (settings.job.categories && settings.job.categories.length > 0) {
// object that will be filled with all of the values for each category we group by
var categories = {};
var arr = settings.job.categories;
for (var i = 0, len = arr.length; i < len; ++i) {
var category = arr[i];
categories[category] = [];
var sql = sqlTemplate('distinct_values_from_field.sql', {
table_name: settings.job.points,
field: category
});
query(sql, function(err, res) {
if (err) {
console.error('findDistinctCategories error');
console.error(JSON.stringify(err,null,2));
}
if (res && res.length > 0) {
for (var i = 0, len = res.length; i < len; i++) {
var r = res[i];
var category = Object.keys(r)[0];
if (r[category] === null) {
continue;
}
categories[category].push(r[category]);
}
}
var json = JSON.stringify(categories);
var jsonPretty = JSON.stringify(categories, null, 2);
console.log('Writing categories to ./categories.json');
fs.writeFileSync('./categories.json', json);
fs.writeFileSync('./categories-pretty.json', jsonPretty);
});
}
}
/**
* If we have a field in our points table that is pipe separated and
* we want to aggregate based on the unique values, we create a master
* list of these values.
*/
var pipeSeparatedValuesFields = settings.job.pipeSeparatedValuesFields;
if (pipeSeparatedValuesFields && pipeSeparatedValuesFields.length > 0) {
for (var v = 0, lenV = pipeSeparatedValuesFields.length; v < lenV; v++) {
var field = pipeSeparatedValuesFields[v];
findUniquePipeSeparatedValues(settings.job.points, field, function() {
// First we need to know what tables are in the db
fetchTableNames(function(tables) {
// if tables specified in settings.js. Otherwise, all tables in db...
if (settings.job.tables && settings.job.tables.length > 0) {
tables = settings.job.tables;
}
for (var i = 0, len = tables.length; i < len; i++) {
var tableName = tables[i];
fetchFields(tableName);
}
});
});
}
} else {
// First we need to know what tables are in the db
fetchTableNames(function(tables) {
// if tables specified in settings.js. Otherwise, all tables in db...
if (settings.job.tables && settings.job.tables.length > 0) {
tables = settings.job.tables;
}
for (var i = 0, len = tables.length; i < len; i++) {
var tableName = tables[i];
fetchFields(tableName);
}
});
}
};
var query = module.exports.query = function(queryStr, cb) {
pg.connect(conString, function(err, client, done) {
if(err) {
console.error('error fetching client from pool', err);
}
client.query(queryStr, function(queryerr, result) {
//call `done()` to release the client back to the pool
done();
if(queryerr) {
console.error('ERROR RUNNING QUERY:', queryStr, queryerr);
}
cb((err || queryerr), (result && result.rows ? result.rows : result));
});
});
};
function findUniquePipeSeparatedValues(table, field, cb) {
pipeSeparatedValues[field] = {};
var sql = sqlTemplate('distinct_values_from_field.sql', {
table_name: table,
field: field
});
query(sql, function(err, res) {
if (err) {
console.error('findUniquePipeSeparatedValues error');
console.error(JSON.stringify(err,null,2));
cb();
return;
}
if (res && res.length > 0) {
for (var i = 0, len = res.length; i < len; i++) {
var r = res[i];
var providers = r.providers;
if (providers === null) continue;
var provList = providers.split('|');
for (var j = 0, len2 = provList.length; j < len2; j++) {
var prov = provList[j];
pipeSeparatedValues[field][prov] = true;
}
}
}
var json = JSON.stringify(pipeSeparatedValues, null, 2);
fs.writeFileSync('./pipeSeparatedValues.json', json);
cb();
});
}
function fetchTableNames(cb) {
var queryStr = sqlTemplate('table_names.sql');
query(queryStr, function(err, rows) {
var tables = {};
rows.forEach(function(row){
tables[row.tablename] = true;
});
cb(tables);
});
}
function fetchFields(polyTableName, cb) {
var sql = sqlTemplate('field_names.sql', { table_name: polyTableName });
query(sql, function(err, res) {
if (res && res.length > 0) {
var hasBBox = false;
var fields = [];
for (var i = 0, len = res.length; i < len; i++) {
var field = res[i];
if (field.column_name === 'bbox') {
hasBBox = true;
}
// fields of this type are geometry fields...
if (field.data_type === 'USER-DEFINED') {
continue;
}
fields.push(field.column_name);
}
var sql2 = fieldsSQL(fields, polyTableName, hasBBox);
query(sql2, function(err, res) {
if (res && res.length > 0) {
console.log('Total of ' + res.length + ' features from ' + polyTableName + '.');
pointsInPolySync(res, polyTableName);
}
});
}
});
}
function pointsInPolySync(features, polyTableName) {
var feature = features.pop();
if (!feature) return;
if (feature.bbox) {
feature.bbox = JSON.parse(feature.bbox);
}
// 1 is because we are always doing a total points aggregation via pointInPolyTotal
var opCount = 1;
if (settings.job.categories) {
opCount += settings.job.categories.length;
}
function multi() {
--opCount;
if (opCount === 0) {
return true;
}
return false;
}
function log() {
if (feature.total_count > 0) {
console.log('Writing: ' + feature.id + '.json from ' + polyTableName + ' with ' + feature.total_count + ' ' + settings.job.points + '.');
}
}
var country = settings.job.country;
if (country !== null && typeof country === 'string') {
var sqlPointInPolyTotal = sqlTemplate('point_in_poly_total_country.sql', {
poly_table: polyTableName,
id: feature.id,
points_table: settings.job.points,
country: country
});
} else {
var sqlPointInPolyTotal = sqlTemplate('point_in_poly_total.sql', {
poly_table: polyTableName,
id: feature.id,
points_table: settings.job.points
});
}
pointInPolyTotal(sqlPointInPolyTotal, feature, multi, function() {
log();
pointsInPolySync(features, polyTableName);
});
if (settings.job.categories) {
for (var i = 0, len = settings.job.categories.length; i < len; i++) {
var category = settings.job.categories[i];
if (country !== null && typeof country === 'string') {
var sql = sqlTemplate('point_in_poly_country.sql', {
poly_table: polyTableName,
id: feature.id,
points_table: settings.job.points,
category: category,
country: country
});
} else {
var sql = sqlTemplate('point_in_poly.sql', {
poly_table: polyTableName,
id: feature.id,
points_table: settings.job.points,
category: category
});
}
pointInPoly(category, sql, feature, multi, function() {
log();
pointsInPolySync(features, polyTableName);
});
}
}
}
function fieldsSQL(fields, tableName, hasBBox) {
if (hasBBox) {
var country = settings.job.country;
if (country !== null && typeof country === 'string') {
return sqlTemplate('poly_fields_bbox_country.sql', {
fields: fields.join(', '),
table_name: tableName,
country: country
});
} else {
return sqlTemplate('poly_fields_bbox.sql', {
fields: fields.join(', '),
table_name: tableName
});
}
return sqlTemplate('poly_fields.sql', {
fields: fields.join(', '),
table_name: tableName
});
}
}
function sqlTemplate(sqlFile, tplHash) {
var sql = sqlFiles[sqlFile];
if (!sql) {
sqlFiles[sqlFile] = sql = fs.readFileSync('sql/' + sqlFile, 'utf8');
}
if (tplHash) {
for (var key in tplHash) {
var exp = '{{' + key + '}}';
var regex = new RegExp(exp, 'g');
var val = tplHash[key];
sql = sql.replace(regex, val);
}
}
return sql;
}
function pointInPolyTotal(sql, feature, multi, cb) {
query(sql, function(err, res) {
var ready = multi();
var count = 0;
if (err) {
console.error('pointInPolyTotal error');
console.error(JSON.stringify(err,null,2));
}
if (res && res.length > 0) {
count = parseInt(res[0].count);
}
feature.total_count = count;
write(ready, feature, cb);
});
}
function pointInPoly(category, sql, feature, multi, cb) {
query(sql, function(err, res) {
var ready = multi();
if (err) {
console.error('sqlPointInPoly error');
console.error(JSON.stringify(err,null,2));
}
feature[category] = {};
if (res && res.length > 0) {
for (var j = 0, len2 = res.length; j < len2; j++) {
var r = res[j];
feature[category][r[category]] = parseInt(r.count);
}
}
write(ready, feature, cb);
});
}
function write(ready, feature, cb) {
// only write and recurse if the other queries have also returned
if (ready) {
var json = JSON.stringify(feature);
fs.writeFileSync('./output/' + feature.id + '.json', json);
var jsonPretty = JSON.stringify(feature, null, 2);
fs.writeFileSync('./output/' + feature.id + 'pretty.json', jsonPretty);
cb();
}
}
run();