Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to implement this with mapnik #34

Open
stevensnoeijen opened this issue Aug 16, 2018 · 2 comments
Open

how to implement this with mapnik #34

stevensnoeijen opened this issue Aug 16, 2018 · 2 comments

Comments

@stevensnoeijen
Copy link

I've read the readme and been looking though the code for a few hours now, but I don't understand how to implement this with tilelive-mapnik.
Can anyone explain this a little better and update the documentation.

The code I have atm:

// Tile server using the node web framework Express (http://expressjs.com).
var express = require('express');
var app = express();
app.use(express.static('public'))

var tilelive = require('@mapbox/tilelive');
require('@mapbox/tilelive-mapnik').registerProtocols(tilelive);
var tileliveRedis = require('@mapbox/tilelive-redis')({}, require('@mapbox/tilejson'));

var filename = __dirname + '/' + 'test/mapnik.xml';
tilelive.load('mapnik://' + filename, function(err, source) {
    if (err) throw err;
    app.get('/:z/:x/:y.*', function(req, res) {
        source.getTile(req.param('z'), req.param('x'), req.param('y'), function(err, tile, headers) {
            // `err` is an error object when generation failed, otherwise null.
            // `tile` contains the compressed image file as a Buffer
            // `headers` is a hash with HTTP headers for the image.
            if (!err) {
                res.send(tile);
            } else {
                res.send('Tile rendering error: ' + err + '\n');
            }
        });
    });
});


console.log('Listening on port: ' + 1234);
app.listen(1234);
@stevensnoeijen
Copy link
Author

found it, will put an explanation here soon and make an pull-request to the documentation

@stevensnoeijen
Copy link
Author

Got it working like this:

var express = require('express');
var tilelive = require('@mapbox/tilelive');
var tilejson = require('@mapbox/tilejson');
var tileliveMapnik = require('@mapbox/tilelive-mapnik');
var tileliveRedis = require('@mapbox/tilelive-redis');

//setup
tileliveMapnik.registerProtocols(tilelive);

const port = 1234;
var Caching = tileliveRedis({
  //client: (set to aws info)
  ttl: 86400,//one day, caches 1.5 day
  stale: 43200,//half a day, when called after 1 day the ttl will stale (extend) again
  timeout: 500
}, tilejson);

var app = express();
app.use(express.static('public'))

//create cache with empty tilejson
var caching = new Caching('./tilejson.json', function(err, data){
  if(err){
    console.log('error loading tilejson, closing service.');
    process.exit(1);
  }
});

//walk
tilelive.load('mapnik://://' + __dirname + '/' + '/mapstyles/Amsterdam/mapnik-walk.xml', function(err, source) {
    if (err) throw err;

    app.get('/mapstyles/AmsterdamWalk/:z/:x/:y.png', function(req, res, next) {
        if(req.query.force == 'true'){
          source.getTile(req.params['z'], req.params['x'], req.params['y'], function(err, tile, headers) {
              if (!err) {
                  res.set(headers);
                  res.send(tile);
              } else {
                  res.send('Tile rendering error: ' + err + '\n');
              }
          });
        }else{
          next();
        }
      }, function(req, res){
          //get cached
          caching.get('http://localhost:' + port + '/mapstyles/AmsterdamWalk/' + req.params['z'] + '/' + req.params['x'] + '/' + req.params['y'] + '.png?force=true', function(err, tile, headers){
            if (!err) {
                res.set(headers);
                res.set('Cache-Control', 'public, max-age=86400, s-maxage=86400');//1 day client cache
                res.send(tile);
            } else {
                res.send('Tile rendering error: ' + err + '\n');
            }
          });
        }
    );
});

app.listen(port, function(){
    console.log('Listening on port: ' + port);
});

But i'm not sure this is the correct way, because im creating a cache with a tilejson without tileurls, but this implementation works for me atm...
Can anyone tell me if there is a better way to implement the redis cache?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant