fix: get data sources specified in style to work

Signed-off-by: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
Andrew Calcutt 2023-10-14 20:33:42 -04:00
parent f2d7e44f3e
commit a106fec3fc
3 changed files with 161 additions and 142 deletions

View file

@ -1469,23 +1469,26 @@ export const serve_rendered = {
for (const name of Object.keys(styleJSON.sources)) { for (const name of Object.keys(styleJSON.sources)) {
let source_type; let source_type;
let source = styleJSON.sources[name]; let source = styleJSON.sources[name];
const url = source.url; let url = source.url;
if (url) {
if (url && (url.startsWith('pmtiles:') || url.startsWith('mbtiles:'))) { if (url.startsWith('{') && url.endsWith('}')) {
url = url.slice(1, -1);
}
if (url.startsWith('pmtiles://') || url.startsWith('mbtiles://')) {
// found pmtiles or mbtiles source, replace with info from local file // found pmtiles or mbtiles source, replace with info from local file
delete source.url; delete source.url;
let inputFile;
let dataId = url.replace('pmtiles://', '').replace('mbtiles://', ''); let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
const fromData = dataId.startsWith('{') && dataId.endsWith('}'); if (dataId.startsWith('{') && dataId.endsWith('}')) {
if (fromData) {
dataId = dataId.slice(1, -1); dataId = dataId.slice(1, -1);
}
const mapsTo = (params.mapping || {})[dataId]; const mapsTo = (params.mapping || {})[dataId];
if (mapsTo) { if (mapsTo) {
dataId = mapsTo; dataId = mapsTo;
} }
let inputFile;
const DataInfo = dataResolver(dataId); const DataInfo = dataResolver(dataId);
if (DataInfo.inputfile) { if (DataInfo.inputfile) {
inputFile = DataInfo.inputfile; inputFile = DataInfo.inputfile;
@ -1494,7 +1497,6 @@ export const serve_rendered = {
console.error(`ERROR: data "${inputFile}" not found!`); console.error(`ERROR: data "${inputFile}" not found!`);
process.exit(1); process.exit(1);
} }
}
if (!isValidHttpUrl(inputFile)) { if (!isValidHttpUrl(inputFile)) {
const inputFileStats = fs.statSync(inputFile); const inputFileStats = fs.statSync(inputFile);
@ -1546,7 +1548,9 @@ export const serve_rendered = {
if (!inputFileStats.isFile() || inputFileStats.size === 0) { if (!inputFileStats.isFile() || inputFileStats.size === 0) {
throw Error(`Not valid MBTiles file: "${inputFile}"`); throw Error(`Not valid MBTiles file: "${inputFile}"`);
} }
map.sources[name] = new MBTiles(inputFile + '?mode=ro', (err) => { map.sources[name] = new MBTiles(
inputFile + '?mode=ro',
(err) => {
map.sources[name].getInfo((err, info) => { map.sources[name].getInfo((err, info) => {
if (err) { if (err) {
console.error(err); console.error(err);
@ -1585,7 +1589,9 @@ export const serve_rendered = {
source.attribution && source.attribution &&
source.attribution.length > 0 source.attribution.length > 0
) { ) {
if (!tileJSON.attribution.includes(source.attribution)) { if (
!tileJSON.attribution.includes(source.attribution)
) {
if (tileJSON.attribution.length > 0) { if (tileJSON.attribution.length > 0) {
tileJSON.attribution += ' | '; tileJSON.attribution += ' | ';
} }
@ -1594,12 +1600,14 @@ export const serve_rendered = {
} }
resolve(); resolve();
}); });
}); },
);
}), }),
); );
} }
} }
} }
}
const renderersReadyPromise = Promise.all(queue).then(() => { const renderersReadyPromise = Promise.all(queue).then(() => {
// standard and @2x tiles are much more usual -> default to larger pools // standard and @2x tiles are much more usual -> default to larger pools

View file

@ -110,28 +110,32 @@ export const serve_style = {
for (const name of Object.keys(styleJSON.sources)) { for (const name of Object.keys(styleJSON.sources)) {
const source = styleJSON.sources[name]; const source = styleJSON.sources[name];
const url = source.url; let url = source.url;
if (url) {
if (url && (url.startsWith('pmtiles:') || url.startsWith('mbtiles:'))) { if (url.startsWith('{') && url.endsWith('}')) {
url = url.slice(1, -1);
}
if (url.startsWith('pmtiles://') || url.startsWith('mbtiles://')) {
const protocol = url.split(':')[0]; const protocol = url.split(':')[0];
let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
const fromData = dataId.startsWith('{') && dataId.endsWith('}'); let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
if (fromData) { if (dataId.startsWith('{') && dataId.endsWith('}')) {
dataId = dataId.slice(1, -1); dataId = dataId.slice(1, -1);
}
const mapsTo = (params.mapping || {})[dataId]; const mapsTo = (params.mapping || {})[dataId];
if (mapsTo) { if (mapsTo) {
dataId = mapsTo; dataId = mapsTo;
} }
}
const identifier = reportTiles(dataId, fromData, protocol); const identifier = reportTiles(dataId, protocol);
if (!identifier) { if (!identifier) {
return false; return false;
} }
source.url = `local://data/${identifier}.json`; source.url = `local://data/${identifier}.json`;
} }
} }
}
for (const obj of styleJSON.layers) { for (const obj of styleJSON.layers) {
if (obj['type'] === 'symbol') { if (obj['type'] === 'symbol') {

View file

@ -183,32 +183,38 @@ function start(opts) {
item, item,
id, id,
opts.publicUrl, opts.publicUrl,
(dataId, fromData, protocol) => { (StyleSourceId, protocol) => {
let dataItemId; let dataItemId;
for (const id of Object.keys(data)) { for (const id of Object.keys(data)) {
if (fromData) { if (id === StyleSourceId) {
if (id === dataId) { // Style id was found in data ids, return that id
dataItemId = id; dataItemId = id;
}
} else { } else {
const fileType = Object.keys(data[id])[0]; const fileType = Object.keys(data[id])[0];
if (data[id][fileType] === dataId) { if (data[id][fileType] === StyleSourceId) {
// Style id was found in data filename, return the id that filename belong to
dataItemId = id; dataItemId = id;
} }
} }
} }
if (dataItemId) { if (dataItemId) {
// input files exists in the data config // input files exists in the data config, return found id
return dataItemId; return dataItemId;
} else { } else {
if (fromData || !allowMoreData) { if (!allowMoreData) {
console.log( console.log(
`ERROR: style "${item.style}" using unknown file "${dataId}"! Skipping...`, `ERROR: style "${item.style}" using unknown file "${StyleSourceId}"! Skipping...`,
); );
return undefined; return undefined;
} else { } else {
let id = dataId.substr(0, dataId.lastIndexOf('.')) || dataId; let id = StyleSourceId.replace(/^.*\/(.*)$/, '$1'); // Remove url path up to last backslash, if it exists
data[id][protocol] = dataId; id = id.substr(0, StyleSourceId.lastIndexOf('.')) || id; // Remove extension, if it exists
while (data[id]) id += '_'; //if the data source id already exists, add a "_" untill it doesn't
//Add the new data source to the data array.
data[id] = {
[protocol]: StyleSourceId,
};
return id; return id;
} }
} }
@ -229,22 +235,23 @@ function start(opts) {
item, item,
id, id,
opts.publicUrl, opts.publicUrl,
(dataId) => { (StyleSourceId) => {
let fileType; let fileType;
let inputFile; let inputFile;
for (const id of Object.keys(data)) { for (const id of Object.keys(data)) {
if (id === dataId) {
fileType = Object.keys(data[id])[0]; fileType = Object.keys(data[id])[0];
if (isValidHttpUrl(data[id][fileType])) { if (StyleSourceId == id) {
inputFile = data[id][fileType]; inputFile = data[id][fileType];
} else { break;
inputFile = path.resolve( } else if (data[id][fileType] == StyleSourceId) {
options.paths[fileType], inputFile = data[id][fileType];
data[id][fileType], break;
);
} }
} }
if (!isValidHttpUrl(inputFile)) {
inputFile = path.resolve(options.paths[fileType], inputFile);
} }
return { inputfile: inputFile, filetype: fileType }; return { inputfile: inputFile, filetype: fileType };
}, },
), ),