Skip to content

Instantly share code, notes, and snippets.

@jdesboeufs
Created May 28, 2019 14:13
Show Gist options
  • Save jdesboeufs/da56ae552fbe09d33424c2264993feca to your computer and use it in GitHub Desktop.
Save jdesboeufs/da56ae552fbe09d33424c2264993feca to your computer and use it in GitHub Desktop.
Orchestration de la génération des tuiles vectorielles
const execa = require('execa')
const bluebird = require('bluebird')
const {bboxPolygon, area} = require('@turf/turf')
const TERRITOIRES = [
{name: 'france', bbox: [-5.53, 42.03, 8.87, 51.72]},
{name: 'corse', bbox: [8.05, 41.26, 10.09, 43.17]},
{name: 'reunion', bbox: [55.06, -21.47, 56.04, -20.79]},
{name: 'martinique', bbox: [-61.47, 14.22, -60.53, 15.07]},
{name: 'guadeloupe', bbox: [-62.07, 15.70, -60.78, 16.64]},
{name: 'mayotte', bbox: [44.77, -13.21, 45.55, -12.47]},
{name: 'saint-martin-saint-barthelemy', bbox: [-63.18, 17.85, -62.75, 18.13]},
{name: 'guyane', bbox: [-54.95, 2.01, -51.26, 5.94]},
// {name: 'saint-pierre-et-miquelon', bbox: [-56.70, 46.56, -55.87, 47.32]},
// {name: 'nouvelle-caledonie', bbox: [161.67, -23.16, 168.59, -18.12]},
// {name: 'polynesie-francaise', bbox: [-156.04, -29.07, -132.53, -6.75]},
// {name: 'wallis-et-futuna', bbox: [-178.46, -14.62, -175.90, -12.88]}
]
const WORLD_MIN_ZOOM = 0
const WORLD_MAX_ZOOM = 6
const WORLD_PARTS = 8
const TERRITOIRES_MIN_ZOOM = 7
const TERRITOIRES_MAX_ZOOM = 16
const MAX_AREA_PER_PART = 50000 // 50 000 km2
// Calcule la surface de la bbox du territoire, en km2
function computeArea(territoire) {
return area(bboxPolygon(territoire.bbox)) / 1000000
}
function computePartsNumber(territoire) {
const area = computeArea(territoire)
if (area < MAX_AREA_PER_PART) {
return 1
}
return (area - (area % MAX_AREA_PER_PART)) / MAX_AREA_PER_PART
}
function generateTilesPart({name, bbox, minZoom, maxZoom, part, parts}) {
part = part || 0
parts = parts || 1
const projectName = `${name}-${part}`
const env = {
MIN_ZOOM: minZoom,
MAX_ZOOM: maxZoom,
MBTILES_NAME: projectName + '.mbtiles',
PART: part,
PARTS: parts
}
if (bbox) {
env.BBOX = bbox.join(',')
}
console.log(JSON.stringify({name, bbox, minZoom, maxZoom, part, parts}))
return execa('docker-compose', ['run', '--rm', '--name', projectName, 'generate-vectortiles'], {env})
}
function splitInParts(options) {
return (new Array(options.parts || 1)).fill(true).map((v, i) => ({...options, part: i}))
}
function listTilesParts() {
const tilesParts = splitInParts({
name: 'world',
minZoom: WORLD_MIN_ZOOM,
maxZoom: WORLD_MAX_ZOOM,
parts: WORLD_PARTS
})
TERRITOIRES.forEach(territoire => {
const options = {
...territoire,
minZoom: TERRITOIRES_MIN_ZOOM,
maxZoom: TERRITOIRES_MAX_ZOOM,
parts: computePartsNumber(territoire)
}
splitInParts(options).forEach(part => {
tilesParts.push(part)
})
})
return tilesParts
}
async function main() {
await bluebird.map(listTilesParts(), partOptions => generateTilesPart(partOptions), {concurrency: 8})
}
main().catch(error => {
console.error(error)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment