CartoDB-SQL-API/app/models/formats/topojson.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-08-03 02:26:01 +08:00
var pg = require('./pg'),
_ = require('underscore'),
geojson = require('./geojson'),
TopoJSON = require('topojson');
2013-05-16 17:24:52 +08:00
2014-08-03 02:27:05 +08:00
function TopoJsonFormat() { }
2013-05-16 17:24:52 +08:00
2014-08-03 02:27:05 +08:00
TopoJsonFormat.prototype = new pg('topojson');
2013-05-16 17:24:52 +08:00
2014-08-03 02:27:05 +08:00
TopoJsonFormat.prototype.getQuery = function(sql, options) {
return geojson.prototype.getQuery(sql, options) + ' where ' + options.gn + ' is not null';
};
2013-05-16 17:24:52 +08:00
2014-08-03 02:27:05 +08:00
TopoJsonFormat.prototype.transform = function(result, options, callback) {
toTopoJSON(result, options.gn, options.skipfields, callback);
};
2013-05-16 17:24:52 +08:00
function toTopoJSON(data, gn, skipfields, callback){
geojson.toGeoJSON(data, gn, function(err, geojson) {
if ( err ) {
callback(err, null);
return;
}
var topology = TopoJSON.topology(geojson.features, {
/* TODO: expose option to API for requesting an identifier
"id": function(o) {
console.log("id called with obj: "); console.dir(o);
return o;
},
*/
"quantization": 1e4, // TODO: expose option to API (use existing "dp" for this ?)
"force-clockwise": true,
"property-filter": function(d) {
// TODO: delegate skipfields handling to toGeoJSON
return skipfields.indexOf(d) != -1 ? null : d;
}
});
callback(err, topology);
});
}
2014-08-03 02:27:05 +08:00
module.exports = TopoJsonFormat;