2

I'm successfully rendering and serving up US county level .pbf tiles from a custom dynamic node-mapnik tile server using PostGIS, but now I want to also render and serve up topojson and/or geojson tiles from this same server. Is there a way to extend the below code sample (full sample at the gist) to return the tiles in geojson or even better topojson format?

https://gist.github.com/nautilytics/75cbbb2d854de608e81c

    map.render(new mapnik.VectorTile(+params.z, +params.x, +params.y), opts, function (err, image) {

    if (err || !image) {
        res.removeHeader('Content-Encoding');
        res.writeHead(500, {
            'Content-Type': 'application/x-protobuf'
        });
        res.end();
        return;
    }

    // Fake empty RGBA
    image.isSolid(function (err, solid, key) {
        if (err) {
            res.writeHead(500, {
                'Content-Type': 'text/plain'
            });

            res.end(err.message);
            return;
        }
        // Solid handling.
        var done = function (err, buffer) {
            if (err) {
                res.writeHead(500, {
                    'Content-Type': 'text/plain'
                });

                res.end(err.message);
                return;
            }

            if (solid === false) {

                res.send(buffer); // return response
                return;
            }

            // Empty tiles are equivalent to no tile.
            if (!key) {
                res.removeHeader('Content-Encoding');
                res.writeHead(404, {
                    'Content-Type': 'application/octet-stream'
                });

                res.end(); //new Buffer('Tile is blank or does not exist', "utf-8")
                return;
            }

            // Fake a hex code by md5ing the key.
            var mockrgb = crypto.createHash('md5').update(buffer).digest('hex').substr(0, 6);
            buffer.solid = [parseInt(mockrgb.substr(0, 2), 16), parseInt(mockrgb.substr(2, 2), 16), parseInt(mockrgb.substr(4, 2), 16), 1].join(',');
            res.send(buffer);

        };

        //Compress
        res.setHeader('content-encoding', 'gzip');
        zlib.gzip(image.getData(), done);
    });
});
hotshotiguana
  • 1,520
  • 2
  • 26
  • 40

0 Answers0