-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.js
75 lines (69 loc) · 2.43 KB
/
features.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
var db = require('./db');
var _ = require('lodash');
var crypto = require('crypto');
var path = require('path');
var async = require('async');
exports.extractFeatures = function(state, collection, cb){
var featureType = lookupFeatureType(collection.file);
var features = _.map(collection.features, function(feature){
// Add extra meta data to the properties of the feature
feature.properties.state = state;
feature.properties.importedAt = new Date();
feature.properties.featureType = featureType;
return feature;
});
cb(null, features);
};
exports.storeCollectionInDB = function(collection, cb){
var done = cb;
console.error('storying ' + collection.length);
async.forEachSeries(collection, storeFeatureInDB, function(err){
// Need a little delay or else connection pool freaks out.
// Listening for drain in db.js would be nicer
setTimeout(function(err){
console.error("completed: " + collection.length);
done(err);
}, process.env.NODE_ENV === 'test' ? 200 : 30000);
});
};
function storeFeatureInDB(feature, cb){
// only store features with geometry??
if (!feature.geometry || !feature.geometry.coordinates || !feature.geometry.coordinates.length){
return cb();
}
var id = feature.properties.state + '-' + feature.properties.featureType + '-' +
crypto.createHash('md5').update(feature.geometry.coordinates.toString()).digest('hex');
var values = {
state: feature.properties.state,
type: feature.properties.featureType,
geometry: JSON.stringify(feature.geometry),
properties: JSON.stringify(feature.properties)
};
try{
db.upsert('features', id, values, cb);
}catch(e){
// better way to handle drain event?
conosle.error("caught: " + e + ' while upserting. Going to back off and let things drain.');
setTimeout(function(){
db.upsert('features', id, values, cb);
}, 30000);
}
}
exports.find = function(cb){
var sql = "select properties, geometry from features where state = 'nsw' and type in ('coal_titles', 'coal_applications')";
db.query(sql, [], function(err, result){
if (err) return cb(err);
var collection = { "type": "FeatureCollection"};
collection.features = _.map(result.rows, function(row){
var feature = {};
feature.type = 'Feature';
feature.properties = JSON.parse(row.properties);
feature.geometry = JSON.parse(row.geometry);
return feature;
});
cb(null, collection);
});
};
function lookupFeatureType(file){
return path.basename(file).replace(/\.shp$/, '').toLowerCase();
}