Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parallelize hillshade generation using PostGIS. Much worse than generating hillshades from GeoTIFF inputs.
#!/usr/bin/env node
"use strict";
var os = require("os");
var async = require("async"),
env = require("require-env"),
pg = require("pg"),
ProgressBar = require("progress");
var DATABASE_URL = env.require("DATABASE_URL"),
// TODO make limit configurable; this assumes that the client has the same
// number of cores as the server, which won't always be the case
CONCURRENCY = os.cpus().length;
pg.connect(DATABASE_URL, function(err, client, done) {
if (err) {
throw err;
}
return client.query("SELECT rid FROM dem", function(err, result) {
done();
if (err) {
throw err;
}
var bar = new ProgressBar("hillshading [:bar] :percent :etas", {
complete: "=",
incomplete: " ",
total: result.rows.length
});
return async.eachLimit(result.rows, CONCURRENCY, function(row, next) {
// use a connection for each task to use multiple processes on pg's end
return pg.connect(DATABASE_URL, function(err, client, done) {
if (err) {
return next(err);
}
// TODO populate overview tables
return client.query([
"INSERT INTO hillshade (rid, rast) (",
" SELECT",
" focus.rid,",
" ST_Clip(ST_HillShade(ST_Union(dem.rast), 1, '8BUI'), ST_Envelope(focus.rast)) rast",
" FROM dem focus",
" JOIN dem ON dem.rast && ST_Buffer(ST_Envelope(focus.rast), 1)",
" WHERE focus.rid = $1",
" GROUP BY focus.rid, focus.rast",
")"
].join("\n"), [row.rid], function(err) {
done();
bar.tick();
return next.apply(null, arguments);
});
});
}, function(err) {
if (err) {
console.error(err);
throw err;
}
clearInterval(status);
done();
pg.end();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.