Skip to content

Commit

Permalink
Update to take advantage of latest node-mapnik _render_grid().
Browse files Browse the repository at this point in the history
  • Loading branch information
Young Hahn committed Feb 23, 2011
1 parent 68315c0 commit 4596511
Showing 1 changed file with 3 additions and 78 deletions.
81 changes: 3 additions & 78 deletions server/tiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var _ = require('underscore'),
path = require('path'),
Step = require('step'),
Tile = require('tilelive').Tile,
Pool = require('tilelive').Pool,
cache = require('models-cache');

module.exports = function(app, settings) {
Expand All @@ -21,51 +20,6 @@ module.exports = function(app, settings) {
});
};

// Load and cache features for the particular project. These will be used
// by grid.json to provide feature data.
// @TODO: remove this functionality once node-mapnik can return the `data`
// key from render_grid.
var featureCache = {};
var loadFeatures = function(req, res, next) {
if (!res.project.get('_interactivity')) return next();

var that = this;
var resource;
var options = { mapfile_dir: settings.mapfile_dir };
var interactivity = res.project.get('_interactivity');
if (featureCache[res.project.id]) {
res.features = featureCache[res.project.id];
next();
} else {
Step(
function() {
Pool.acquire('map', res.project.toJSON(), options, this);
},
function(err, r) {
if (err) return this(err);
resource = r;
featureCache[res.project.id] = _.reduce(
resource.mapnik.features(interactivity.layer),
function(features, feature) {
if (feature[interactivity.key_name]) {
var key = feature[interactivity.key_name].toString();
features[key] = feature;
}
return features;
},
{}
);
res.features = featureCache[res.project.id];
this();
},
function(err, data) {
Pool.release('map', res.project.toJSON(), resource);
next();
}
);
}
};

// GET endpoint for TMS tile image requests. Uses `tilelive.js` Tile API.
//
// - `:id` String, project model id.
Expand Down Expand Up @@ -101,7 +55,7 @@ module.exports = function(app, settings) {
});

// Interaction grid.json endpoint.
app.get('/1.0.0/:id/:z/:x/:y.grid.json', loadProject, loadFeatures, function(req, res, next) {
app.get('/1.0.0/:id/:z/:x/:y.grid.json', loadProject, function(req, res, next) {
req.query.callback = req.query.callback || 'grid';
var interactivity = res.project.get('_interactivity');
try {
Expand All @@ -112,7 +66,8 @@ module.exports = function(app, settings) {
mapfile_dir: settings.mapfile_dir,
format_options: {
layer: parseInt(interactivity.layer, 10),
key_name: interactivity.key_name
key_name: interactivity.key_name,
data: true
}
};
var tile = new Tile(options);
Expand All @@ -125,37 +80,7 @@ module.exports = function(app, settings) {
} else if (!grid[0]) {
res.send('Grid not found', 404);
} else {
// @TODO: remove
var keys = grid[2].keys;
var grid = grid[0].toString();

// @TODO: remove
var data = _.reduce(
res.features,
function(features, feature) {
if (feature[interactivity.key_name]) {
var key = feature[interactivity.key_name].toString();
(keys.indexOf(key) !== -1) && (features[key] = feature);
}
return features;
},
{}
);

// Manually append grid data as a string to the grid buffer.
// Ideally we would
//
// JSON.stringify(_.extend(JSON.parse(grid), { data: gd }))
//
// But calling JSON stringify will escape UTF8 characters of a
// high enough ordinal making the grid data unusable. Instead,
// manipulate the JSON string directly, popping the trailing }
// off and splicing the grid data in at the "data" key.
grid = grid.substr(0, grid.length - 1)
+ ', "data":'
+ JSON.stringify(data)
+ '}';

res.send(
req.query.callback + '(' + grid + ');',
{'Content-Type': 'text/javascript; charset=utf-8'}
Expand Down

0 comments on commit 4596511

Please sign in to comment.