Skip to content

Commit

Permalink
Add pngOptions and preRenderFn options
Browse files Browse the repository at this point in the history
- see consbio#26 for preRenderFn.
- pngOptions are passed to the sharp.png() method as is.
  • Loading branch information
meowcoder committed Jul 5, 2020
1 parent 32963c1 commit 22cf75c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
15 changes: 12 additions & 3 deletions dist/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ var getRemoteAsset = function getRemoteAsset(url, callback) {
* @param {number} width - width of output map (default: 1024)
* @param {number} height - height of output map (default: 1024)
* @param {Object} - configuration object containing style, zoom, center: [lng, lat],
* width, height, bounds: [west, south, east, north], ratio, padding, skipEncoding
* width, height, bounds: [west, south, east, north], ratio, padding, skipEncoding,
* preRenderFn, pngOptions
* @param {String} tilePath - path to directory containing local mbtiles files that are
* referenced from the style.json as "mbtiles://<tileset>"
*/
Expand All @@ -401,7 +402,10 @@ var render = function render(style) {
_options$padding = options.padding,
padding = _options$padding === void 0 ? 0 : _options$padding,
_options$skipEncoding = options.skipEncoding,
skipEncoding = _options$skipEncoding === void 0 ? false : _options$skipEncoding;
skipEncoding = _options$skipEncoding === void 0 ? false : _options$skipEncoding,
_options$preRenderFn = options.preRenderFn,
preRenderFn = _options$preRenderFn === void 0 ? null : _options$preRenderFn,
pngOptions = options.pngOptions;
var _options$center = options.center,
center = _options$center === void 0 ? null : _options$center,
_options$zoom = options.zoom,
Expand Down Expand Up @@ -589,6 +593,11 @@ var render = function render(style) {
};
var map = new _mapboxGlNative["default"].Map(mapOptions);
map.load(style);

if (typeof preRenderFn === 'function') {
preRenderFn(map);
}

map.render({
zoom: zoom,
center: center,
Expand Down Expand Up @@ -637,7 +646,7 @@ var render = function render(style) {
height: height * ratio,
channels: 4
}
}).png().toBuffer().then(resolve)["catch"](reject);
}).png(pngOptions).toBuffer().then(resolve)["catch"](reject);
} catch (pngErr) {
console.error('Error encoding PNG');
console.error(pngErr);
Expand Down
10 changes: 8 additions & 2 deletions dist/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ var PARAMS = {
token: {
isRequired: false,
isString: true
},
pngOptions: {
isRequired: false,
isObject: true
}
};

Expand All @@ -90,7 +94,8 @@ var renderImage = function renderImage(params, response, next, tilePath) {
_params$bearing = params.bearing,
bearing = _params$bearing === void 0 ? null : _params$bearing,
_params$pitch = params.pitch,
pitch = _params$pitch === void 0 ? null : _params$pitch;
pitch = _params$pitch === void 0 ? null : _params$pitch,
pngOptions = params.pngOptions;
var style = params.style,
_params$zoom = params.zoom,
zoom = _params$zoom === void 0 ? null : _params$zoom,
Expand Down Expand Up @@ -235,7 +240,8 @@ var renderImage = function renderImage(params, response, next, tilePath) {
ratio: ratio,
bearing: bearing,
pitch: pitch,
token: token
token: token,
pngOptions: pngOptions
}).then(function (data, rejected) {
if (rejected) {
console.error('render request rejected', rejected);
Expand Down
11 changes: 9 additions & 2 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ const getRemoteAsset = (url, callback) => {
* @param {number} width - width of output map (default: 1024)
* @param {number} height - height of output map (default: 1024)
* @param {Object} - configuration object containing style, zoom, center: [lng, lat],
* width, height, bounds: [west, south, east, north], ratio, padding, skipEncoding
* width, height, bounds: [west, south, east, north], ratio, padding, skipEncoding,
* preRenderFn, pngOptions
* @param {String} tilePath - path to directory containing local mbtiles files that are
* referenced from the style.json as "mbtiles://<tileset>"
*/
Expand All @@ -334,6 +335,8 @@ export const render = (style, width = 1024, height = 1024, options) =>
ratio = 1,
padding = 0,
skipEncoding = false,
preRenderFn = null,
pngOptions,
} = options
let { center = null, zoom = null, tilePath = null } = options

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

if (typeof preRenderFn === 'function') {
preRenderFn(map)
}

map.render(
{
zoom,
Expand Down Expand Up @@ -603,7 +610,7 @@ export const render = (style, width = 1024, height = 1024, options) =>
channels: 4,
},
})
.png()
.png(pngOptions)
.toBuffer()
.then(resolve)
.catch(reject)
Expand Down
3 changes: 3 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const PARAMS = {
bearing: { isRequired: false, isDecimal: true },
pitch: { isRequired: false, isDecimal: true },
token: { isRequired: false, isString: true },
pngOptions: { isRequired: false, isObject: true },
}

const renderImage = (params, response, next, tilePath) => {
Expand All @@ -38,6 +39,7 @@ const renderImage = (params, response, next, tilePath) => {
padding = 0,
bearing = null,
pitch = null,
pngOptions,
} = params
let { style, zoom = null, center = null, bounds = null, ratio = 1 } = params

Expand Down Expand Up @@ -206,6 +208,7 @@ const renderImage = (params, response, next, tilePath) => {
bearing,
pitch,
token,
pngOptions,
})
.then((data, rejected) => {
if (rejected) {
Expand Down

0 comments on commit 22cf75c

Please sign in to comment.