Refactor OGR class interface to improve flexibility

The toOGR and toOGR_SingleFile function now take an "options"
object rather than a long list of arguments. This allows for
easier extension toward per-user database host and password
(see tickets #120 and #121)
This commit is contained in:
Sandro Santilli 2013-11-15 13:24:48 +01:00
parent 1e9d073cd4
commit 4a5c9b0eed
4 changed files with 32 additions and 12 deletions

View File

@ -10,8 +10,7 @@ p._contentType = "text/csv; charset=utf-8; header=present";
p._fileExtension = "csv"; p._fileExtension = "csv";
p.generate = function(options, callback) { p.generate = function(options, callback) {
var o = options; this.toOGR_SingleFile(options, 'CSV', callback);
this.toOGR_SingleFile(o.database, o.user_id, o.gn, o.sql, o.skipfields, 'CSV', 'csv', o.filename, callback);
}; };
module.exports = csv; module.exports = csv;

View File

@ -18,8 +18,7 @@ p._fileExtension = "kml";
p._needSRS = true; p._needSRS = true;
p.generate = function(options, callback) { p.generate = function(options, callback) {
var o = options; this.toOGR_SingleFile(options, 'KML', callback);
this.toOGR_SingleFile(o.database, o.user_id, o.gn, o.sql, o.skipfields, 'KML', 'kml', o.filename, callback);
}; };
module.exports = kml; module.exports = kml;

View File

@ -59,7 +59,15 @@ ogr.prototype = {
}; };
// Internal function usable by all OGR-driven outputs // Internal function usable by all OGR-driven outputs
ogr.prototype.toOGR = function(dbname, user_id, gcol, sql, skipfields, out_format, out_filename, out_layername, callback) { ogr.prototype.toOGR = function(options, out_format, out_filename, callback) {
var dbname = options.database;
var user_id = options.user_id;
var gcol = options.gn;
var sql = options.sql;
var skipfields = options.skipfields;
var out_layername = options.filename;
var ogr2ogr = 'ogr2ogr'; // FIXME: make configurable var ogr2ogr = 'ogr2ogr'; // FIXME: make configurable
var dbhost = global.settings.db_host; var dbhost = global.settings.db_host;
var dbport = global.settings.db_port; var dbport = global.settings.db_port;
@ -197,8 +205,16 @@ console.log('ogr2ogr ' + _.map(ogrargs, function(x) { return "'" + x + "'"; }).j
); );
}; };
// TODO: simplify to take an options object ogr.prototype.toOGR_SingleFile = function(options, fmt, callback) {
ogr.prototype.toOGR_SingleFile = function(dbname, user_id, gcol, sql, skipfields, fmt, ext, layername, callback) {
var dbname = options.database;
var user_id = options.user_id;
var gcol = options.gcol;
var sql = options.sql;
var skipfields = options.skipfields;
var ext = this._fileExtension;
var layername = options.filename;
var tmpdir = global.settings.tmpDir || '/tmp'; var tmpdir = global.settings.tmpDir || '/tmp';
var reqKey = [ fmt, dbname, user_id, gcol, this.generateMD5(layername), this.generateMD5(sql) ].concat(skipfields).join(':'); var reqKey = [ fmt, dbname, user_id, gcol, this.generateMD5(layername), this.generateMD5(sql) ].concat(skipfields).join(':');
var outdirpath = tmpdir + '/sqlapi-' + process.pid + '-' + reqKey; var outdirpath = tmpdir + '/sqlapi-' + process.pid + '-' + reqKey;
@ -206,7 +222,7 @@ ogr.prototype.toOGR_SingleFile = function(dbname, user_id, gcol, sql, skipfields
// TODO: following tests: // TODO: following tests:
// - fetch query with no "the_geom" column // - fetch query with no "the_geom" column
this.toOGR(dbname, user_id, gcol, sql, skipfields, fmt, dumpfile, layername, callback); this.toOGR(options, fmt, dumpfile, callback);
}; };
ogr.prototype.sendResponse = function(opts, callback) { ogr.prototype.sendResponse = function(opts, callback) {

View File

@ -23,11 +23,17 @@ p._fileExtension = "zip";
p._needSRS = true; p._needSRS = true;
p.generate = function(options, callback) { p.generate = function(options, callback) {
var o = options; this.toSHP(options, callback);
this.toSHP(o.database, o.user_id, o.gn, o.sql, o.skipfields, o.filename, callback);
}; };
p.toSHP = function (dbname, user_id, gcol, sql, skipfields, filename, callback) { p.toSHP = function (options, callback) {
var dbname = options.database;
var user_id = options.user_id;
var gcol = options.gn;
var sql = options.sql;
var skipfields = options.skipfields;
var filename = options.filename;
var fmtObj = this; var fmtObj = this;
var zip = 'zip'; // FIXME: make configurable var zip = 'zip'; // FIXME: make configurable
var tmpdir = global.settings.tmpDir || '/tmp'; var tmpdir = global.settings.tmpDir || '/tmp';
@ -45,7 +51,7 @@ p.toSHP = function (dbname, user_id, gcol, sql, skipfields, filename, callback)
}, },
function spawnDumper(err) { function spawnDumper(err) {
if ( err ) throw err; if ( err ) throw err;
fmtObj.toOGR(dbname, user_id, gcol, sql, skipfields, 'ESRI Shapefile', shapefile, filename, this); fmtObj.toOGR(options, 'ESRI Shapefile', shapefile, this);
}, },
function doZip(err) { function doZip(err) {
if ( err ) throw err; if ( err ) throw err;