<commit_before>var abaculus = require('abaculus'),
    request = require('request'),
    SphericalMercator = require('sphericalmercator'),
    Step = require('step');

function StaticMapsController(app, renderCache, mapStore, httpRendererFactory, options) {
    this._app = app;
    this._renderCache = renderCache;
    this._mapStore = mapStore;
    this._httpRendererFactory = httpRendererFactory;
    this._options = options || {};
}

module.exports = StaticMapsController;


StaticMapsController.prototype.register = function(app) {
    app.get(app.base_url_mapconfig + '/static/center/:token/:z/:lat/:lng/:width/:height.:format', this.center.bind(this));
    app.get(app.base_url_mapconfig + '/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format', this.bbox.bind(this));
};

StaticMapsController.prototype.bbox = function(req, res) {
    var west = +req.params.west,
        south = +req.params.south,
        east = +req.params.east,
        north = +req.params.north,

        width = +req.params.width,
        height = +req.params.height;

    var zoom = boundsZoom(
        {
            nw: [west, north],
            se: [east, south]
        },
        {
            width: width,
            height: height
        }
    );
    this.staticMap(req, res, zoom, {
        x: west + Math.abs(west - east) / 2,
        y: south + Math.abs(south - north) / 2,
        w: width,
        h: height
    });
};

StaticMapsController.prototype.center = function(req, res) {
    this.staticMap(req, res, +req.params.z, {
        x: +req.params.lng,
        y: +req.params.lat,
        w: +req.params.width,
        h: +req.params.height
    });
};

StaticMapsController.prototype.staticMap = function(req, res, zoom, center) {
    this._app.doCORS(res);

    var renderer;

    var format = req.params.format == 'jpg' ? 'jpeg' : 'png';

    req.params.format = 'png';

    var self = this;

    Step(
        function() {
            self._app.req2params(req, this);
        },
        function(err) {
            if (err) throw err;
            self._renderCache.getRenderer(req, this);
        },
        function(err, r) {
            renderer = r;
            if (err) throw err;

            self._mapStore.load(req.params.token, this);
        },
        function(err, mapConfig) {
            if (err) throw err;

            var next = this;

            var httpLayers = mapConfig.getLayers()
                .map(function(layer, index) {
                    return {
                        layer: layer,
                        index: index,
                        isHttpLayer: layer.type === 'http'
                    };
                })
                .reduce(function(acc, current) {
                    if (current.isHttpLayer) {
                        acc.push(current);
                    }
                    return acc;
                }, []);

            var httpGetTiles = [];
            function handleHttpLayerGetTile(httpGetTile) {
                httpGetTiles.push(httpGetTile);
                if (httpLayers.length === httpGetTiles.length) {
                    var getTiles = httpGetTiles.concat(renderer.getTile.bind(renderer));
                    next(null, getTiles)
                }
            }

            httpLayers.forEach(function(layer) {
                self._httpRendererFactory.getRenderer(mapConfig, {}, format, layer.index, function(err, renderer) {
                    if (err) throw err;
                    handleHttpLayerGetTile(renderer.getTile.bind(renderer));
                })
            });
        },
        function getImage(err, getTiles) {
            if (err) throw err;
            var abaculusParams = {
                zoom: zoom,
                scale: 1, // integer between 1-4 and sets resolution (scale: 1 is 72dpi, scale: 4, is 288dpi)
                center: center,
                format: format,
                getTile: getTiles,
                limit: (self._options.imageSizeLimit || 8192) + 1
            };

            abaculus(abaculusParams, this);
        },
        function handleImage(err, image, headers) {
            if (err) {
                if (!err.error) {
                    err.error = err.message;
                }
                self._app.sendError(res, err, self._app.findStatusCode(err), 'STATIC_MAP', err);
            } else {
                self._app.sendWithHeaders(res, image, 200, headers);
            }
        }
    );
};


// Following functionality has been influenced by leaflet's getBoundsZoom
// See http://leafletjs.com/reference.html#map-getboundszoom

var sphericalMercator = new SphericalMercator({
    size: 256
});

function subtract(pointA, pointB) {
    return [
        pointA[0] - pointB[0],
        pointA[1] - pointB[1]<commit_msg>Use 'all' layer to render static maps<commit_after>var abaculus = require('abaculus'),
    request = require('request'),
    SphericalMercator = require('sphericalmercator'),
    Step = require('step');

function StaticMapsController(app, renderCache, mapStore, httpRendererFactory, options) {
    this._app = app;
    this._renderCache = renderCache;
    this._mapStore = mapStore;
    this._httpRendererFactory = httpRendererFactory;
    this._options = options || {};
}

module.exports = StaticMapsController;


StaticMapsController.prototype.register = function(app) {
    app.get(app.base_url_mapconfig + '/static/center/:token/:z/:lat/:lng/:width/:height.:format', this.center.bind(this));
    app.get(app.base_url_mapconfig + '/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format', this.bbox.bind(this));
};

StaticMapsController.prototype.bbox = function(req, res) {
    var west = +req.params.west,
        south = +req.params.south,
        east = +req.params.east,
        north = +req.params.north,

        width = +req.params.width,
        height = +req.params.height;

    var zoom = boundsZoom(
        {
            nw: [west, north],
            se: [east, south]
        },
        {
            width: width,
            height: height
        }
    );
    this.staticMap(req, res, zoom, {
        x: west + Math.abs(west - east) / 2,
        y: south + Math.abs(south - north) / 2,
        w: width,
        h: height
    });
};

StaticMapsController.prototype.center = function(req, res) {
    this.staticMap(req, res, +req.params.z, {
        x: +req.params.lng,
        y: +req.params.lat,
        w: +req.params.width,
        h: +req.params.height
    });
};

StaticMapsController.prototype.staticMap = function(req, res, zoom, center) {
    this._app.doCORS(res);

    var renderer;

    var format = req.params.format == 'jpg' ? 'jpeg' : 'png';

    req.params.format = 'png';
    req.params.layer = 'all';

    var self = this;

    Step(
        function() {
            self._app.req2params(req, this);
        },
        function(err) {
            if (err) throw err;
            self._renderCache.getRenderer(req, this);
        },
        function getImage(err, r) {
            if (err) throw err;
            renderer = r;

            var abaculusParams = {
                zoom: zoom,
                scale: 1, // integer between 1-4 and sets resolution (scale: 1 is 72dpi, scale: 4, is 288dpi)
                center: center,
                format: format,
                getTile: renderer.getTile.bind(renderer),
                limit: (self._options.imageSizeLimit || 8192) + 1
            };

            abaculus(abaculusParams, this);
        },
        function handleImage(err, image, headers) {
            if (renderer) {
                renderer.release();
            }

            if (err) {
                if (!err.error) {
                    err.error = err.message;
                }
                self._app.sendError(res, err, self._app.findStatusCode(err), 'STATIC_MAP', err);
            } else {
                self._app.sendWithHeaders(res, image, 200, headers);
            }
        }
    );
};


// Following functionality has been influenced by leaflet's getBoundsZoom
// See http://leafletjs.com/reference.html#map-getboundszoom

var sphericalMercator = new SphericalMercator({
    size: 256
});

function subtract(pointA, pointB) {
    return [
        pointA[0] - pointB[0],
        pointA[1] - pointB[1]
{'fraction_non_alphanumeric': 0.12754917340412694, 'fraction_numerical': 0.006757572100880898, 'mean_word_length': 1.8066373179817135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5615842', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2432, 'n_words': 491}