2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var crypto = require('crypto');
|
|
|
|
var Step = require('step');
|
|
|
|
var fs = require('fs');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var ogr = require('./ogr');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
|
|
|
function shp() {
|
|
|
|
}
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
shp.prototype = new ogr('shp');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var p = shp.prototype;
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
p._contentType = "application/zip; charset=utf-8";
|
|
|
|
p._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
|
2013-10-01 23:19:44 +08:00
|
|
|
p._needSRS = true;
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
p.generate = function(options, callback) {
|
2013-11-15 20:24:48 +08:00
|
|
|
this.toSHP(options, callback);
|
2013-05-16 17:24:52 +08:00
|
|
|
};
|
|
|
|
|
2013-11-15 20:24:48 +08:00
|
|
|
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;
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var fmtObj = this;
|
2013-05-16 17:24:52 +08:00
|
|
|
var zip = 'zip'; // FIXME: make configurable
|
|
|
|
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(':');
|
2013-05-29 22:48:20 +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
|
|
|
|
|
|
|
|
Step (
|
|
|
|
function createOutDir() {
|
|
|
|
fs.mkdir(outdirpath, 0777, this);
|
|
|
|
},
|
|
|
|
function spawnDumper(err) {
|
|
|
|
if ( err ) throw err;
|
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) {
|
|
|
|
if ( err ) throw err;
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
var child = spawn(zip, ['-qrj', zipfile, outdirpath ]);
|
|
|
|
|
|
|
|
child.on('exit', function(code) {
|
|
|
|
//console.log("Zip complete, zip return code was " + code);
|
|
|
|
if (code) {
|
|
|
|
next(new Error("Zip command return code " + code));
|
|
|
|
//res.statusCode = 500;
|
|
|
|
}
|
|
|
|
next(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
function cleanupDir(topError) {
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
//console.log("Cleaning up " + outdirpath);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
else unlinkall(dir, files, finish)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fs.readdir(outdirpath, function(err, files) {
|
|
|
|
if ( err ) {
|
|
|
|
if ( err.code != 'ENOENT' ) {
|
|
|
|
next(new Error([topError, err].join('\n')));
|
|
|
|
} else {
|
|
|
|
next(topError);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unlinkall(outdirpath, files, function(err) {
|
|
|
|
fs.rmdir(outdirpath, function(err) {
|
|
|
|
if ( err ) console.log("Removing dir " + path + ": " + 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
|
|
|
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
module.exports = shp;
|
2013-05-16 17:24:52 +08:00
|
|
|
|