Skip to content

Commit 22cf75c

Browse files
committed
Add pngOptions and preRenderFn options
- see consbio#26 for preRenderFn. - pngOptions are passed to the sharp.png() method as is.
1 parent 32963c1 commit 22cf75c

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

dist/render.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ var getRemoteAsset = function getRemoteAsset(url, callback) {
377377
* @param {number} width - width of output map (default: 1024)
378378
* @param {number} height - height of output map (default: 1024)
379379
* @param {Object} - configuration object containing style, zoom, center: [lng, lat],
380-
* width, height, bounds: [west, south, east, north], ratio, padding, skipEncoding
380+
* width, height, bounds: [west, south, east, north], ratio, padding, skipEncoding,
381+
* preRenderFn, pngOptions
381382
* @param {String} tilePath - path to directory containing local mbtiles files that are
382383
* referenced from the style.json as "mbtiles://<tileset>"
383384
*/
@@ -401,7 +402,10 @@ var render = function render(style) {
401402
_options$padding = options.padding,
402403
padding = _options$padding === void 0 ? 0 : _options$padding,
403404
_options$skipEncoding = options.skipEncoding,
404-
skipEncoding = _options$skipEncoding === void 0 ? false : _options$skipEncoding;
405+
skipEncoding = _options$skipEncoding === void 0 ? false : _options$skipEncoding,
406+
_options$preRenderFn = options.preRenderFn,
407+
preRenderFn = _options$preRenderFn === void 0 ? null : _options$preRenderFn,
408+
pngOptions = options.pngOptions;
405409
var _options$center = options.center,
406410
center = _options$center === void 0 ? null : _options$center,
407411
_options$zoom = options.zoom,
@@ -589,6 +593,11 @@ var render = function render(style) {
589593
};
590594
var map = new _mapboxGlNative["default"].Map(mapOptions);
591595
map.load(style);
596+
597+
if (typeof preRenderFn === 'function') {
598+
preRenderFn(map);
599+
}
600+
592601
map.render({
593602
zoom: zoom,
594603
center: center,
@@ -637,7 +646,7 @@ var render = function render(style) {
637646
height: height * ratio,
638647
channels: 4
639648
}
640-
}).png().toBuffer().then(resolve)["catch"](reject);
649+
}).png(pngOptions).toBuffer().then(resolve)["catch"](reject);
641650
} catch (pngErr) {
642651
console.error('Error encoding PNG');
643652
console.error(pngErr);

dist/server.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ var PARAMS = {
7777
token: {
7878
isRequired: false,
7979
isString: true
80+
},
81+
pngOptions: {
82+
isRequired: false,
83+
isObject: true
8084
}
8185
};
8286

@@ -90,7 +94,8 @@ var renderImage = function renderImage(params, response, next, tilePath) {
9094
_params$bearing = params.bearing,
9195
bearing = _params$bearing === void 0 ? null : _params$bearing,
9296
_params$pitch = params.pitch,
93-
pitch = _params$pitch === void 0 ? null : _params$pitch;
97+
pitch = _params$pitch === void 0 ? null : _params$pitch,
98+
pngOptions = params.pngOptions;
9499
var style = params.style,
95100
_params$zoom = params.zoom,
96101
zoom = _params$zoom === void 0 ? null : _params$zoom,
@@ -235,7 +240,8 @@ var renderImage = function renderImage(params, response, next, tilePath) {
235240
ratio: ratio,
236241
bearing: bearing,
237242
pitch: pitch,
238-
token: token
243+
token: token,
244+
pngOptions: pngOptions
239245
}).then(function (data, rejected) {
240246
if (rejected) {
241247
console.error('render request rejected', rejected);

src/render.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ const getRemoteAsset = (url, callback) => {
320320
* @param {number} width - width of output map (default: 1024)
321321
* @param {number} height - height of output map (default: 1024)
322322
* @param {Object} - configuration object containing style, zoom, center: [lng, lat],
323-
* width, height, bounds: [west, south, east, north], ratio, padding, skipEncoding
323+
* width, height, bounds: [west, south, east, north], ratio, padding, skipEncoding,
324+
* preRenderFn, pngOptions
324325
* @param {String} tilePath - path to directory containing local mbtiles files that are
325326
* referenced from the style.json as "mbtiles://<tileset>"
326327
*/
@@ -334,6 +335,8 @@ export const render = (style, width = 1024, height = 1024, options) =>
334335
ratio = 1,
335336
padding = 0,
336337
skipEncoding = false,
338+
preRenderFn = null,
339+
pngOptions,
337340
} = options
338341
let { center = null, zoom = null, tilePath = null } = options
339342

@@ -553,6 +556,10 @@ export const render = (style, width = 1024, height = 1024, options) =>
553556
const map = new mbgl.Map(mapOptions)
554557
map.load(style)
555558

559+
if (typeof preRenderFn === 'function') {
560+
preRenderFn(map)
561+
}
562+
556563
map.render(
557564
{
558565
zoom,
@@ -603,7 +610,7 @@ export const render = (style, width = 1024, height = 1024, options) =>
603610
channels: 4,
604611
},
605612
})
606-
.png()
613+
.png(pngOptions)
607614
.toBuffer()
608615
.then(resolve)
609616
.catch(reject)

src/server.js

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const PARAMS = {
2828
bearing: { isRequired: false, isDecimal: true },
2929
pitch: { isRequired: false, isDecimal: true },
3030
token: { isRequired: false, isString: true },
31+
pngOptions: { isRequired: false, isObject: true },
3132
}
3233

3334
const renderImage = (params, response, next, tilePath) => {
@@ -38,6 +39,7 @@ const renderImage = (params, response, next, tilePath) => {
3839
padding = 0,
3940
bearing = null,
4041
pitch = null,
42+
pngOptions,
4143
} = params
4244
let { style, zoom = null, center = null, bounds = null, ratio = 1 } = params
4345

@@ -206,6 +208,7 @@ const renderImage = (params, response, next, tilePath) => {
206208
bearing,
207209
pitch,
208210
token,
211+
pngOptions,
209212
})
210213
.then((data, rejected) => {
211214
if (rejected) {

0 commit comments

Comments
 (0)