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

TypeError: Cannot read property 'getTile' of undefined #26

Closed
jvaldezch opened this issue Mar 8, 2018 · 5 comments
Closed

TypeError: Cannot read property 'getTile' of undefined #26

jvaldezch opened this issue Mar 8, 2018 · 5 comments

Comments

@jvaldezch
Copy link

Hi, I'm trying to pass the layer name dynamically but when trying to run I'm getting an error "TypeError: Cannot read property 'getTile' of undefined".

const express = require('express');
const fs = require('fs');

const app = express();

app.get('/', function (req, res, next) {
    res.send('Hello World!');
});

app.get('/tileserver/:layer/:z/:x/:y/tile.png', function (req, res, next) {
    const layer = req.params.layer;
    const x = req.params.x;
    const y = req.params.y;
    const z = req.params.z;
    
    const mapfile = __dirname + '/maps/' + layer + '.xml';
    const dircache = '../cache/' + layer;
    
    fs.exists(mapfile, function(exists) {
        if (exists) {

            var tilestrata = require('tilestrata');
            var disk = require('tilestrata-disk');
            var sharp = require('tilestrata-sharp');
            var mapnik = require('tilestrata-mapnik');
            var dependency = require('tilestrata-dependency');

            var strata = tilestrata();

            strata.layer('basemap')
                .route('tile@2x.png')
                    .use(disk.cache({dir: dircache}))
                    .use(mapnik({
                        pathname: mapfile,
                        tileSize: 512,
                        layer: 'all',
                        scale: 2
                    }))
                .route('tile.png')
                    .use(disk.cache({dir: dircache}))
                    .use(dependency('basemap', 'tile@2x.png'))
                    .use(sharp(function(image, sharp) {
                        return image.resize(256);
                    }));
            strata.listen();

            strata.getTile('basemap', 'tile.png', x, y, z, function(err, img, hdr) {
                if (err) {
                    console.log(err);
                    res.statusCode = 401;
                    res.send('None shall pass');
                } else {
                    res.writeHead(200, {
                        'Content-Type': 'image/png',
                        'Content-Length': img.length
                    });
                    res.end(img); 
                }
            });

        } else {
            res.statusCode = 401;
            res.send("Mapfile dont exists");
        }
    });
    
});

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.listen(3000);
@brianreavis
Copy link
Member

Do you have a stacktrace you can post?

@jvaldezch
Copy link
Author

@brandonreavis

/home/jvaldez/Repositories/tile_server/node_modules/tilestrata-mapnik/index.js:50
		source.getTile(req.z, req.x, req.y, function(err, buffer, headers) {
		       ^

TypeError: Cannot read property 'getTile' of undefined
    at Object.serveImage [as serve] (/home/jvaldez/Repositories/tile_server/node_modules/tilestrata-mapnik/index.js:50:10)
    at step_runProvider (/home/jvaldez/Repositories/tile_server/node_modules/tilestrata/lib/TileRequestHandler.js:222:25)
    at /home/jvaldez/Repositories/tile_server/node_modules/tilestrata/node_modules/async/lib/async.js:689:13
    at iterate (/home/jvaldez/Repositories/tile_server/node_modules/tilestrata/node_modules/async/lib/async.js:265:13)
    at /home/jvaldez/Repositories/tile_server/node_modules/tilestrata/node_modules/async/lib/async.js:277:29
    at /home/jvaldez/Repositories/tile_server/node_modules/tilestrata/node_modules/async/lib/async.js:44:16
    at /home/jvaldez/Repositories/tile_server/node_modules/tilestrata/node_modules/async/lib/async.js:694:17
    at /home/jvaldez/Repositories/tile_server/node_modules/tilestrata/node_modules/async/lib/async.js:173:37
    at complete (/home/jvaldez/Repositories/tile_server/node_modules/tilestrata/lib/TileRequestHandler.js:207:6)
    at /home/jvaldez/Repositories/tile_server/node_modules/tilestrata/node_modules/async/lib/async.js:52:16
[nodemon] app crashed - waiting for file changes before starting...

@brianreavis
Copy link
Member

Thanks! This is because the source hasn't been initialized yet. I should make getTile() wait for full initialization before attempting to produce the tile... I can work on that.

This said, there a few problems with your approach your here:

  • You don't want to listen() for every request coming in. This will cause an error whenever a second request comes in. Furthermore, listen is missing the required port argument.
  • You shouldn't create a new tilestrata instance for every request. Especially with mapnik in the picture, this will be extremely inefficient to setup and teardown every request.

If you're wanting to integrate with Express, check out the middleware. It should simplify what it seems you're intending: https://github.com/naturalatlas/tilestrata#integrate-with-expressjs--connect

If you have a lot of layers you don't want to hard-code, I'd recommend setting them up before starting the server. Tilestrata doesn't support adding / removing of layers once listening. You can use tools like node-glob to find the XMLs, setting up a layer for each.

@jvaldezch
Copy link
Author

@brandonreavis thanks for the advice I will try both approaches then.

@yh371174229
Copy link

@brandonreavis thanks for the advice I will try both approaches then.
Do you solve it ?? I meet this error too.
naturalatlas/tilestrata-mapnik#9

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

3 participants