2011-01-05 02:07:27 +08:00
|
|
|
var fs = require('fs'),
|
|
|
|
netlib = require('./netlib'),
|
|
|
|
url = require('url'),
|
|
|
|
path = require('path'),
|
|
|
|
_ = require('underscore')._,
|
|
|
|
spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: use node-minizip instead of shelling
|
|
|
|
* TODO: include or don't use underscore
|
|
|
|
*/
|
|
|
|
|
|
|
|
var External = function External(env) {
|
2011-01-07 05:33:49 +08:00
|
|
|
var env = env;
|
2011-01-05 02:07:27 +08:00
|
|
|
|
|
|
|
return {
|
2011-01-07 05:33:49 +08:00
|
|
|
env: env,
|
2011-01-13 06:18:03 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Download an external, process it, and return the usable filepath for
|
|
|
|
* Mapnik
|
|
|
|
* @param {String} resource_url the URI of the datasource from a mapfile.
|
|
|
|
* @param {Function} callback passed into processor function after
|
|
|
|
* localizing.
|
|
|
|
*/
|
|
|
|
process: function(resource_url, callback) {
|
|
|
|
var file_format = path.extname(resource_url).toLowerCase(),
|
|
|
|
that = this;
|
|
|
|
|
2011-01-16 05:36:30 +08:00
|
|
|
if (url.parse(resource_url).protocol == 'http:') {
|
|
|
|
fs.stat(this.tmppos(resource_url), function(err, stats) {
|
|
|
|
if (stats && stats.isFile()) {
|
|
|
|
callback(null, [
|
|
|
|
resource_url,
|
|
|
|
that.destinations(file_format)(resource_url, that)
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
netlib.download(resource_url, that.tmppos(resource_url),
|
|
|
|
function(err, url, filename) {
|
|
|
|
if (that.processors(file_format)) {
|
|
|
|
that.processors(file_format)(
|
|
|
|
filename,
|
|
|
|
resource_url,
|
|
|
|
callback,
|
|
|
|
that);
|
|
|
|
} else {
|
|
|
|
console.log('no processor found for %s', file_format);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2011-01-16 06:01:51 +08:00
|
|
|
} else {
|
|
|
|
// is this a fully-qualified URL?
|
|
|
|
fs.stat(resource_url, function(err, stat) {
|
|
|
|
if (!err && stat.isFile()) {
|
|
|
|
callback(null, [resource_url, resource_url]);
|
|
|
|
}
|
|
|
|
});
|
2011-01-16 05:36:30 +08:00
|
|
|
}
|
2011-01-13 06:18:03 +08:00
|
|
|
},
|
|
|
|
|
2011-01-05 02:07:27 +08:00
|
|
|
/**
|
|
|
|
* Get a processor, given a file's extension
|
|
|
|
* @param {String} extension the file's extension.
|
|
|
|
* @return {Function} processor function.
|
|
|
|
*/
|
|
|
|
processors: function(extension) {
|
|
|
|
return {
|
|
|
|
'.zip': this.unzip,
|
|
|
|
'.mss': this.plainfile,
|
2011-01-15 03:33:40 +08:00
|
|
|
'.shp': this.inplace,
|
2011-01-05 02:07:27 +08:00
|
|
|
'.geojson': this.plainfile,
|
|
|
|
'.kml': this.plainfile
|
|
|
|
}[extension];
|
|
|
|
},
|
|
|
|
|
2011-01-05 08:57:35 +08:00
|
|
|
destinations: function(extension) {
|
|
|
|
return {
|
|
|
|
'.zip': this.unzip_dest,
|
|
|
|
'.mss': this.plainfile_dest,
|
2011-01-15 03:33:40 +08:00
|
|
|
'.shp': this.inplace_dest,
|
2011-01-05 08:57:35 +08:00
|
|
|
'.geojson': this.plainfile_dest,
|
|
|
|
'.kml': this.plainfile_dest
|
|
|
|
}[extension];
|
|
|
|
},
|
|
|
|
|
2011-01-05 02:07:27 +08:00
|
|
|
/**
|
|
|
|
* Get the final resting position of an external's directory
|
|
|
|
* @param {String} ext name of the external.
|
|
|
|
* @return {String} file path.
|
|
|
|
*/
|
|
|
|
pos: function(ext) {
|
2011-01-07 05:33:49 +08:00
|
|
|
return path.join(this.env.data_dir, netlib.safe64(ext));
|
2011-01-05 02:07:27 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the temporary path of an external before processing
|
|
|
|
* @param {String} ext filename of the external.
|
|
|
|
* @return {String} file path.
|
|
|
|
*/
|
|
|
|
tmppos: function(ext) {
|
2011-01-07 05:33:49 +08:00
|
|
|
return path.join(this.env.data_dir, require('crypto')
|
2011-01-05 02:07:27 +08:00
|
|
|
.createHash('md5').update(ext).digest('hex'));
|
|
|
|
},
|
|
|
|
|
|
|
|
plainname: function(resource_url) {
|
|
|
|
return require('crypto')
|
|
|
|
.createHash('md5').update(resource_url).digest('hex') +
|
|
|
|
path.extname(resource_url);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2011-01-05 08:57:35 +08:00
|
|
|
unzip_dest: function(resource_url, that) {
|
|
|
|
return that.locateShp(that.pos(resource_url));
|
|
|
|
},
|
|
|
|
|
|
|
|
plainfile_dest: function(resource_url, that) {
|
|
|
|
return path.join(that.pos(resource_url),
|
|
|
|
that.plainname(resource_url));
|
|
|
|
},
|
|
|
|
|
2011-01-15 03:33:40 +08:00
|
|
|
inplace_dest: function(resource_url, that) {
|
|
|
|
console.log(url.parse(resource_url));
|
|
|
|
return resource_url;
|
|
|
|
},
|
|
|
|
|
2011-01-05 02:07:27 +08:00
|
|
|
/**
|
|
|
|
* Deal with a plain file, which is likely to be
|
|
|
|
* GeoJSON, KML, or one of the other OGR-supported formats,
|
|
|
|
* returning a Mapnik-usable filename
|
|
|
|
*
|
|
|
|
* @param {String} filename the place of the file on your system.
|
|
|
|
* @param {String} resource_url
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
plainfile: function(filename, resource_url, callback, that) {
|
|
|
|
// TODO: possibly decide upon default extension
|
|
|
|
var extension = path.extname(resource_url);
|
|
|
|
if (extension !== '') {
|
|
|
|
// TODO: make sure dir doesn't exist
|
|
|
|
var destination = path.join(that.pos(resource_url),
|
|
|
|
that.plainname(resource_url));
|
|
|
|
fs.mkdirSync(that.pos(resource_url), 0777);
|
|
|
|
fs.renameSync(
|
|
|
|
filename,
|
|
|
|
destination);
|
2011-01-05 03:47:00 +08:00
|
|
|
callback(null, [resource_url, destination]);
|
2011-01-05 02:07:27 +08:00
|
|
|
} else {
|
|
|
|
throw Exception('Non-extended files cannot be processed');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-01-15 03:33:40 +08:00
|
|
|
/**
|
|
|
|
* Deal with an inplace local file
|
|
|
|
*
|
|
|
|
* @param {String} filename the place of the file on your system.
|
|
|
|
* @param {String} resource_url
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
inplace: function(filename, resource_url, callback, that) {
|
2011-01-16 05:36:30 +08:00
|
|
|
|
2011-01-15 03:33:40 +08:00
|
|
|
callback(null, [resource_url, filename]);
|
|
|
|
},
|
|
|
|
|
2011-01-05 08:57:35 +08:00
|
|
|
locateShp: function(dir) {
|
|
|
|
try {
|
2011-01-05 02:07:27 +08:00
|
|
|
var unzipped = fs.readdirSync(dir);
|
|
|
|
var shp = _.detect(unzipped,
|
|
|
|
function(f) {
|
2011-01-13 07:32:47 +08:00
|
|
|
return path.extname(f).toLowerCase() == '.shp';
|
2011-01-05 02:07:27 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
if (!shp) {
|
|
|
|
var dirs = _.select(unzipped,
|
|
|
|
function(f) {
|
|
|
|
return fs.statSync(path.join(dir, f)).isDirectory();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (dirs) {
|
|
|
|
for (var i = 0, l = dirs.length; i < l; i++) {
|
|
|
|
var located = locateShp(path.join(dir, dirs[i]));
|
|
|
|
if (located) {
|
|
|
|
return located;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-05 08:57:35 +08:00
|
|
|
} else {
|
|
|
|
return path.join(dir, shp);
|
2011-01-05 02:07:27 +08:00
|
|
|
}
|
2011-01-05 08:57:35 +08:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2011-01-05 02:07:27 +08:00
|
|
|
|
2011-01-05 08:57:35 +08:00
|
|
|
/**
|
|
|
|
* Unzip a file and return a shapefile contained within it
|
|
|
|
*
|
|
|
|
* TODO: handle other files than shapefiles
|
|
|
|
* @param {String} filename the place of the shapefile on your system.
|
|
|
|
* @param {String} resource_url
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
2011-01-07 05:33:49 +08:00
|
|
|
unzip: function(filename, resource_url, callback, that) {
|
2011-01-05 08:57:35 +08:00
|
|
|
// regrettably complex because zip library isn't written for
|
|
|
|
// node yet.
|
2011-01-14 07:11:31 +08:00
|
|
|
console.log('unzipping download from ' + filename);
|
2011-01-05 02:07:27 +08:00
|
|
|
spawn('unzip', [filename, '-d', that.pos(resource_url)])
|
|
|
|
.on('exit', function(code) {
|
|
|
|
if (code > 0) {
|
2011-01-14 07:11:31 +08:00
|
|
|
console.log('unzip failed');
|
|
|
|
callback('Unzip returned a code of ' + code, [
|
|
|
|
resource_url, false]);
|
2011-01-05 02:07:27 +08:00
|
|
|
} else {
|
2011-01-14 07:11:31 +08:00
|
|
|
console.log('unzip succeeded');
|
2011-01-05 02:07:27 +08:00
|
|
|
// TODO; eliminate locality of reference
|
2011-01-05 08:57:35 +08:00
|
|
|
callback(null, [
|
|
|
|
resource_url,
|
|
|
|
that.locateShp(that.pos(resource_url))]);
|
2011-01-05 02:07:27 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-01-13 06:18:03 +08:00
|
|
|
};
|
2011-01-05 02:07:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = External;
|