-
Notifications
You must be signed in to change notification settings - Fork 5
/
validations.js
44 lines (43 loc) · 1.63 KB
/
validations.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
'use strict';
var Bluebird = require('bluebird');
var debug = require('debug')('into-cartodb:validations');
module.exports = Bluebird.coroutine(function * validate(tempTable, _fields, config, db, group) {
let fields = new Map();
_fields.forEach(function (field) {
fields.set(field, field);
});
var validations = getValidations(config);
for (let validation of validations) {
yield validation(tempTable, fields, db, group);
}
return fields;
});
var fixGeom = Bluebird.coroutine(function * fixGeom(tempTable, fields, db) {
var count = yield db(tempTable).count('*');
count = count.length === 1 && count[0].count;
if (!Number(count)) {
throw new Error('no rows inserted');
}
var hasGeom = yield db(tempTable).select(db.raw('bool_or(the_geom is not null) as hasgeom'));
hasGeom = hasGeom.length === 1 && hasGeom[0].hasgeom;
if (hasGeom) {
debug('has geometry');
let allValid = yield db(tempTable).select(db.raw('bool_and(st_isvalid(the_geom)) as allvalid'));
allValid = allValid.length === 1 && allValid[0].allvalid;
if (allValid) {
debug('geometry is all valid');
} else {
debug('has invalid geometry');
yield db.raw('update ?? set the_geom = ST_MakeValid(the_geom) where not st_isvalid(the_geom)', [tempTable]).batch();
yield db(tempTable).delete().whereRaw('GeometryType(the_geom) = \'GEOMETRYCOLLECTION\'');
}
fields.set('the_geom', 'the_geom');
}
});
function getValidations(config) {
if (!config.validations || !Array.isArray(config.validations) || !config.validations.length) {
return [fixGeom];
} else {
return [fixGeom].concat(config.validations);
}
}