From 0018ed952432d3e5d59d16af64ca416cd6c48f8c Mon Sep 17 00:00:00 2001 From: Petr Sloup Date: Tue, 13 Dec 2016 16:00:16 +0100 Subject: [PATCH] Prepare for the new tile schema and compatibility checking --- package.json | 2 +- src/main.js | 55 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index ff200b1..09a0cbf 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "request": "2.79.0", "sharp": "0.16.2", "sphericalmercator": "1.0.5", - "tileserver-gl-styles": "0.3.0", + "tileserver-gl-styles": "1.0.0", "vector-tile": "1.3.0" }, "optionalDependencies": { diff --git a/src/main.js b/src/main.js index a9fdfa7..bdfcd08 100644 --- a/src/main.js +++ b/src/main.js @@ -78,8 +78,7 @@ var startWithMBTiles = function(mbtilesFile) { "options": { "paths": { "root": styleDir, - "fonts": "glyphs", - "sprites": "sprites", + "fonts": "fonts", "styles": "styles", "mbtiles": path.dirname(mbtilesFile) } @@ -89,27 +88,53 @@ var startWithMBTiles = function(mbtilesFile) { }; if (info.format == 'pbf' && - info.name.toLowerCase().indexOf('osm2vectortiles') > -1) { - config['data']['osm2vectortiles'] = { + info.name.toLowerCase().indexOf('openmaptiles') > -1) { + config['data']['openmaptiles'] = { "mbtiles": path.basename(mbtilesFile) }; + var omtV = (info.version || '').split('.'); + var styles = fs.readdirSync(path.resolve(styleDir, 'styles')); for (var i = 0; i < styles.length; i++) { - var styleFilename = styles[i]; - if (styleFilename.endsWith('.json')) { - var styleObject = { - "style": path.basename(styleFilename), - "tilejson": { - "bounds": bounds - } - }; - config['styles'][path.basename(styleFilename, '.json')] = - styleObject; + var styleName = styles[i]; + var styleFileRel = styleName + '/style.json'; + var styleFile = path.resolve(styleDir, 'styles', styleFileRel); + if (fs.existsSync(styleFile)) { + var styleJSON = require(styleFile); + var omtVersionCompatibility = + ((styleJSON || {}).metadata || {})['openmaptiles:version'] || 'x'; + var m = omtVersionCompatibility.toLowerCase().split('.'); + + var isCompatible = !( + m[0] != 'x' && ( + m[0] != omtV[0] || ( + (m[1] || 'x') != 'x' && ( + m[1] != omtV[1] || ( + (m[2] || 'x') != 'x' && + m[2] != omtV[2] + ) + ) + ) + ) + ); + + if (isCompatible) { + var styleObject = { + "style": styleFileRel, + "tilejson": { + "bounds": bounds + } + }; + config['styles'][styleName] = styleObject; + } else { + console.log('Style', styleName, 'requires OpenMapTiles version', + omtVersionCompatibility, 'but mbtiles is version', info.version); + } } } } else { - console.log('WARN: MBTiles not in "osm2vectortiles" format. ' + + console.log('WARN: MBTiles not in "openmaptiles" format. ' + 'Serving raw data only...'); config['data'][(info.id || 'mbtiles') .replace(/\//g, '_')