Skip to content

Commit

Permalink
Merge 51302af into ce81b32
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmartinelli committed Apr 9, 2018
2 parents ce81b32 + 51302af commit 9b29fd4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
14 changes: 12 additions & 2 deletions bin/geojson-thumbnail.js
Expand Up @@ -10,9 +10,11 @@ const index = require('../index');
program
.usage('<input file> <output file>')
.description('Render a GeoJSON thumbnail')
.option('--min-zoom <n>')
.option('--max-zoom <n>')
.parse(process.argv);

const run = (input, output) => {
const run = (input, output, minZoom, maxZoom) => {
const geojson = JSON.parse(fs.readFileSync(input));
const options = {
backgroundTileJSON: sources.mapboxSatellite(process.env.MapboxAccessToken)
Expand All @@ -24,6 +26,14 @@ const run = (input, output) => {
options.blendFormat = 'jpeg';
}

if (maxZoom) {
options.thumbnailMaxZoom = maxZoom;
}
if (minZoom) {
options.thumbnailMinZoom = minZoom;
}


index.renderThumbnail(geojson, function onImageRendered(err, image, headers, stats) {
if (err) throw err;
fs.writeFile(output, image, (err) => {
Expand All @@ -41,6 +51,6 @@ const run = (input, output) => {
if (program.args.length < 2) {
program.outputHelp();
} else {
run(program.args[0], program.args[1]);
run(program.args[0], program.args[1], program.minZoom, program.maxZoom);
}

11 changes: 9 additions & 2 deletions index.js
Expand Up @@ -10,14 +10,15 @@ const bbox = require('@turf/bbox');
const abaculus = require('@mapbox/abaculus');
const sm = new (require('@mapbox/sphericalmercator'))();

function bestRenderParams(geojson, backgroundTileJSON) {
function bestRenderParams(geojson, backgroundTileJSON, minZoom, maxZoom) {
let optimalZoom = zoom.decideZoom(bbox(geojson));
if (optimalZoom > backgroundTileJSON.maxzoom) {
optimalZoom = backgroundTileJSON.maxzoom;
}
if (optimalZoom < backgroundTileJSON.minzoom) {
optimalZoom = backgroundTileJSON.minzoom;
}
optimalZoom = Math.max(minZoom, Math.min(maxZoom, optimalZoom));

function paddedExtent(geojson) {
const extent = bbox(geojson);
Expand All @@ -30,6 +31,7 @@ function bestRenderParams(geojson, backgroundTileJSON) {

// TODO: Padding is super hacky without any real background checking what we should do
let minPad = Math.abs(sm.ll([0, 0], optimalZoom)[0] - sm.ll([10, 10], optimalZoom)[0]);

if (width < minSize) {
minPad = Math.abs(sm.ll([0, 0], optimalZoom)[0] - sm.ll([minSize - width, 10], optimalZoom)[0]);
}
Expand All @@ -52,6 +54,7 @@ function bestRenderParams(geojson, backgroundTileJSON) {

const bounds = paddedExtent(geojson);
return {
// ensure zoom is within min and max bounds configured for thumbnail
zoom: optimalZoom,
scale: 1,
format: 'png',
Expand All @@ -67,6 +70,8 @@ function bestRenderParams(geojson, backgroundTileJSON) {
* @param {Function} callback - Callback called with rendered imageonce finished
* @param {Object} options
* @param {Object} [options.backgroundTileJSON] - Provide a custom TileJSON for the background layer
* @param {Number} [options.thumbnailMinZoom] - Specify a min zoom level to render thumbnail
* @param {Number} [options.thumbnailMaxZoom] - Specify a max zoom level to render thumbnail
* @param {string} [options.blendFormat] - Format to use when blended together with the background image. https://github.com/mapbox/node-blend#options
*/
function renderThumbnail(geojson, callback, options) {
Expand All @@ -76,6 +81,8 @@ function renderThumbnail(geojson, callback, options) {
if (typeof callback !== 'function') throw new Error('Callback needs to be a function not an object');

options = Object.assign({
thumbnailMinZoom: 0,
thumbnailMaxZoom: 22,
stylesheet: styles.default,
// backgroundTileJSON: sources.naturalEarth(),
backgroundTileJSON: sources.mapboxSatellite(process.env.MapboxAccessToken)
Expand All @@ -91,7 +98,7 @@ function renderThumbnail(geojson, callback, options) {
new TileJSON(backgroundUri, (err, backgroundSource) => {
const overlaySource = new thumbnail.ThumbnailSource(geojson, template, imageOptions, options.mapOptions);
const blendSource = new blend.BlendRasterSource(backgroundSource, overlaySource);
const renderParams = Object.assign(bestRenderParams(geojson, options.backgroundTileJSON), {
const renderParams = Object.assign(bestRenderParams(geojson, options.backgroundTileJSON, options.thumbnailMinZoom, options.thumbnailMaxZoom), {
format: options.blendFormat || 'png',
tileSize: options.tileSize,
getTile: blendSource.getTile
Expand Down
6 changes: 6 additions & 0 deletions test/index.test.js
Expand Up @@ -47,3 +47,9 @@ tape('renderThumbnail as jpg', (assert) => {
blendFormat: 'jpeg'
});
});

tape('renderThumbnail with max zoom', (assert) => {
assertThumbnailRenders('/fixtures/peak.geojson', assert, {
thumbnailMaxZoom: 4
});
});

0 comments on commit 9b29fd4

Please sign in to comment.