Skip to content

Instantly share code, notes, and snippets.

@kueda
Created January 8, 2014 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kueda/8321921 to your computer and use it in GitHub Desktop.
Save kueda/8321921 to your computer and use it in GitHub Desktop.
Windshaft server script for a single-database mapserver. Made with iNaturalist in mind, based on the Windshaft example server at https://github.com/CartoDB/Windshaft/blob/master/examples/readme_server.js
var Windshaft = require('lib/windshaft');
var _ = require('underscore');
var config = {
base_url: '/:table',
base_url_notable: '/',
grainstore: {
datasource: {
user:'kueda',
host: '127.0.0.1',
port: 5432,
geometry_field: "geom",
srid: 4326
}
}, //see grainstore npm for other options
redis: {host: '127.0.0.1', port: 6379},
enable_cors: true,
req2params: function(req, callback){
// no default interactivity. to enable specify the database column you'd like to interact with
req.params.interactivity = null;
// this is in case you want to test sql parameters eg ...png?sql=select * from my_table limit 10
req.params = _.extend({}, req.params);
_.extend(req.params, req.query);
req.params.dbname = 'inaturalist_development_srid4326'
// console.log("[DEBUG] req: ", req)
if (req.params.sql) {
req.params.sql = req.params.sql.replace(/\{\{table\}\}/, req.params.table)
if (req.params.sql.match(/ where /i)) {}
else {req.params.sql += " LIMIT 1000"}
}
var x = parseInt(req.params.x),
y = parseInt(req.params.y),
z = parseInt(req.params.z),
numTiles = Math.pow(2,z),
maxCoord = numTiles - 1
x = x >= 0 ? x : maxCoord + x
y = y >= 0 ? y : maxCoord + y
if (x > maxCoord) {x = x % numTiles}
if (y > maxCoord) {y = y % numTiles}
if (x < -1*maxCoord) {x = Math.abs(x) % numTiles}
if (y < -1*maxCoord) {y = Math.abs(y) % numTiles}
req.params.x = ''+x
req.params.y = ''+y
req.params.z = ''+z
// send the finished req object on
callback(null,req);
}
};
// Initialize tile server on port 4000
var ws = new Windshaft.Server(config);
ws.listen(4000);
console.log("map tiles are now being served out of: http://localhost:4000" + config.base_url + '/:z/:x/:y');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment