2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
var step = require('step');
|
|
|
|
var fs = require('fs');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
2019-01-16 22:58:11 +08:00
|
|
|
var Ogr = require('./../ogr');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
function ShpFormat() {
|
2013-05-16 17:24:52 +08:00
|
|
|
}
|
|
|
|
|
2019-01-16 22:58:11 +08:00
|
|
|
ShpFormat.prototype = new Ogr('shp');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
ShpFormat.prototype._contentType = "application/zip; charset=utf-8";
|
|
|
|
ShpFormat.prototype._fileExtension = "zip";
|
2013-11-05 23:56:07 +08:00
|
|
|
// As of GDAL 1.10 SRID detection is bogus, so we use
|
|
|
|
// our own method. See:
|
|
|
|
// http://trac.osgeo.org/gdal/ticket/5131
|
|
|
|
// http://trac.osgeo.org/gdal/ticket/5287
|
|
|
|
// http://github.com/CartoDB/CartoDB-SQL-API/issues/110
|
|
|
|
// http://github.com/CartoDB/CartoDB-SQL-API/issues/116
|
2014-08-03 02:27:05 +08:00
|
|
|
ShpFormat.prototype._needSRS = true;
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
ShpFormat.prototype.generate = function(options, callback) {
|
2013-11-15 20:24:48 +08:00
|
|
|
this.toSHP(options, callback);
|
2013-05-16 17:24:52 +08:00
|
|
|
};
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
ShpFormat.prototype.toSHP = function (options, callback) {
|
2013-11-15 20:24:48 +08:00
|
|
|
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;
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var fmtObj = this;
|
2017-04-06 00:47:44 +08:00
|
|
|
var zip = global.settings.zipCommand || 'zip';
|
2017-04-17 19:31:18 +08:00
|
|
|
var zipOptions = '-qrj';
|
2013-05-16 17:24:52 +08:00
|
|
|
var tmpdir = global.settings.tmpDir || '/tmp';
|
2013-05-27 17:21:56 +08:00
|
|
|
var reqKey = [ 'shp', dbname, user_id, gcol, this.generateMD5(sql) ].concat(skipfields).join(':');
|
2017-04-06 00:47:44 +08:00
|
|
|
var outdirpath = tmpdir + '/sqlapi-' + process.pid + '-' + reqKey;
|
2013-05-16 17:24:52 +08:00
|
|
|
var zipfile = outdirpath + '.zip';
|
|
|
|
var shapefile = outdirpath + '/' + filename + '.shp';
|
|
|
|
|
|
|
|
// TODO: following tests:
|
|
|
|
// - fetch query with no "the_geom" column
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
step (
|
2013-05-16 17:24:52 +08:00
|
|
|
function createOutDir() {
|
2018-10-24 21:42:54 +08:00
|
|
|
fs.mkdir(outdirpath, 0o777, this);
|
2013-05-16 17:24:52 +08:00
|
|
|
},
|
|
|
|
function spawnDumper(err) {
|
2018-11-08 01:05:39 +08:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-05-13 19:00:01 +08:00
|
|
|
|
2013-11-15 20:24:48 +08:00
|
|
|
fmtObj.toOGR(options, 'ESRI Shapefile', shapefile, this);
|
2013-05-16 17:24:52 +08:00
|
|
|
},
|
|
|
|
function doZip(err) {
|
2018-11-08 01:05:39 +08:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2013-05-16 17:24:52 +08:00
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
2017-04-17 19:31:18 +08:00
|
|
|
var child = spawn(zip, [zipOptions, zipfile, outdirpath ]);
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2017-04-11 01:18:35 +08:00
|
|
|
child.on('error', function (err) {
|
2017-04-17 19:59:47 +08:00
|
|
|
next(new Error('Error executing zip command, ' + err));
|
2017-04-11 01:18:35 +08:00
|
|
|
});
|
|
|
|
|
2017-04-11 00:26:23 +08:00
|
|
|
var stderrData = [];
|
|
|
|
child.stderr.setEncoding('utf8');
|
|
|
|
child.stderr.on('data', function (data) {
|
|
|
|
stderrData.push(data);
|
|
|
|
});
|
|
|
|
|
2013-05-16 17:24:52 +08:00
|
|
|
child.on('exit', function(code) {
|
2017-04-11 00:26:23 +08:00
|
|
|
if (code !== 0) {
|
|
|
|
var errMessage = 'Zip command return code ' + code;
|
|
|
|
if (stderrData.length) {
|
|
|
|
errMessage += ', Error: ' + stderrData.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(new Error(errMessage));
|
2013-05-16 17:24:52 +08:00
|
|
|
}
|
|
|
|
|
2017-04-11 00:26:23 +08:00
|
|
|
return next();
|
|
|
|
});
|
2013-05-16 17:24:52 +08:00
|
|
|
},
|
|
|
|
function cleanupDir(topError) {
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
// Unlink the dir content
|
|
|
|
var unlinkall = function(dir, files, finish) {
|
|
|
|
var f = files.shift();
|
|
|
|
if ( ! f ) { finish(null); return; }
|
|
|
|
var fn = dir + '/' + f;
|
|
|
|
fs.unlink(fn, function(err) {
|
|
|
|
if ( err ) {
|
|
|
|
console.log("Unlinking " + fn + ": " + err);
|
|
|
|
finish(err);
|
2015-05-13 19:00:01 +08:00
|
|
|
} else {
|
|
|
|
unlinkall(dir, files, finish);
|
2013-05-16 17:24:52 +08:00
|
|
|
}
|
|
|
|
});
|
2014-08-03 02:23:47 +08:00
|
|
|
};
|
2013-05-16 17:24:52 +08:00
|
|
|
fs.readdir(outdirpath, function(err, files) {
|
|
|
|
if ( err ) {
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( err.code !== 'ENOENT' ) {
|
2013-05-16 17:24:52 +08:00
|
|
|
next(new Error([topError, err].join('\n')));
|
|
|
|
} else {
|
|
|
|
next(topError);
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-13 19:00:01 +08:00
|
|
|
unlinkall(outdirpath, files, function(/*err*/) {
|
2013-05-16 17:24:52 +08:00
|
|
|
fs.rmdir(outdirpath, function(err) {
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( err ) {
|
|
|
|
console.log("Removing dir " + outdirpath + ": " + err);
|
|
|
|
}
|
2013-05-29 23:33:10 +08:00
|
|
|
next(topError, zipfile);
|
2013-05-16 17:24:52 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-05-29 23:33:10 +08:00
|
|
|
},
|
|
|
|
function finalStep(err, zipfile) {
|
|
|
|
callback(err, zipfile);
|
2013-05-16 17:24:52 +08:00
|
|
|
}
|
|
|
|
);
|
2013-05-27 17:21:56 +08:00
|
|
|
};
|
2013-05-16 17:24:52 +08:00
|
|
|
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
module.exports = ShpFormat;
|