fix: get data sources specified in style to work
Signed-off-by: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
parent
f2d7e44f3e
commit
a106fec3fc
3 changed files with 161 additions and 142 deletions
|
|
@ -1469,23 +1469,26 @@ export const serve_rendered = {
|
|||
for (const name of Object.keys(styleJSON.sources)) {
|
||||
let source_type;
|
||||
let source = styleJSON.sources[name];
|
||||
const url = source.url;
|
||||
|
||||
if (url && (url.startsWith('pmtiles:') || url.startsWith('mbtiles:'))) {
|
||||
let url = source.url;
|
||||
if (url) {
|
||||
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
|
||||
delete source.url;
|
||||
|
||||
let inputFile;
|
||||
let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
|
||||
const fromData = dataId.startsWith('{') && dataId.endsWith('}');
|
||||
|
||||
if (fromData) {
|
||||
if (dataId.startsWith('{') && dataId.endsWith('}')) {
|
||||
dataId = dataId.slice(1, -1);
|
||||
}
|
||||
|
||||
const mapsTo = (params.mapping || {})[dataId];
|
||||
if (mapsTo) {
|
||||
dataId = mapsTo;
|
||||
}
|
||||
|
||||
let inputFile;
|
||||
const DataInfo = dataResolver(dataId);
|
||||
if (DataInfo.inputfile) {
|
||||
inputFile = DataInfo.inputfile;
|
||||
|
|
@ -1494,7 +1497,6 @@ export const serve_rendered = {
|
|||
console.error(`ERROR: data "${inputFile}" not found!`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValidHttpUrl(inputFile)) {
|
||||
const inputFileStats = fs.statSync(inputFile);
|
||||
|
|
@ -1546,7 +1548,9 @@ export const serve_rendered = {
|
|||
if (!inputFileStats.isFile() || inputFileStats.size === 0) {
|
||||
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) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
|
|
@ -1585,7 +1589,9 @@ export const serve_rendered = {
|
|||
source.attribution &&
|
||||
source.attribution.length > 0
|
||||
) {
|
||||
if (!tileJSON.attribution.includes(source.attribution)) {
|
||||
if (
|
||||
!tileJSON.attribution.includes(source.attribution)
|
||||
) {
|
||||
if (tileJSON.attribution.length > 0) {
|
||||
tileJSON.attribution += ' | ';
|
||||
}
|
||||
|
|
@ -1594,12 +1600,14 @@ export const serve_rendered = {
|
|||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const renderersReadyPromise = Promise.all(queue).then(() => {
|
||||
// standard and @2x tiles are much more usual -> default to larger pools
|
||||
|
|
|
|||
|
|
@ -110,28 +110,32 @@ export const serve_style = {
|
|||
|
||||
for (const name of Object.keys(styleJSON.sources)) {
|
||||
const source = styleJSON.sources[name];
|
||||
const url = source.url;
|
||||
|
||||
if (url && (url.startsWith('pmtiles:') || url.startsWith('mbtiles:'))) {
|
||||
let url = source.url;
|
||||
if (url) {
|
||||
if (url.startsWith('{') && url.endsWith('}')) {
|
||||
url = url.slice(1, -1);
|
||||
}
|
||||
if (url.startsWith('pmtiles://') || url.startsWith('mbtiles://')) {
|
||||
const protocol = url.split(':')[0];
|
||||
let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
|
||||
|
||||
const fromData = dataId.startsWith('{') && dataId.endsWith('}');
|
||||
if (fromData) {
|
||||
let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
|
||||
if (dataId.startsWith('{') && dataId.endsWith('}')) {
|
||||
dataId = dataId.slice(1, -1);
|
||||
}
|
||||
|
||||
const mapsTo = (params.mapping || {})[dataId];
|
||||
if (mapsTo) {
|
||||
dataId = mapsTo;
|
||||
}
|
||||
}
|
||||
|
||||
const identifier = reportTiles(dataId, fromData, protocol);
|
||||
const identifier = reportTiles(dataId, protocol);
|
||||
if (!identifier) {
|
||||
return false;
|
||||
}
|
||||
source.url = `local://data/${identifier}.json`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const obj of styleJSON.layers) {
|
||||
if (obj['type'] === 'symbol') {
|
||||
|
|
|
|||
|
|
@ -183,32 +183,38 @@ function start(opts) {
|
|||
item,
|
||||
id,
|
||||
opts.publicUrl,
|
||||
(dataId, fromData, protocol) => {
|
||||
(StyleSourceId, protocol) => {
|
||||
let dataItemId;
|
||||
for (const id of Object.keys(data)) {
|
||||
if (fromData) {
|
||||
if (id === dataId) {
|
||||
if (id === StyleSourceId) {
|
||||
// Style id was found in data ids, return that id
|
||||
dataItemId = id;
|
||||
}
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dataItemId) {
|
||||
// input files exists in the data config
|
||||
// input files exists in the data config, return found id
|
||||
return dataItemId;
|
||||
} else {
|
||||
if (fromData || !allowMoreData) {
|
||||
if (!allowMoreData) {
|
||||
console.log(
|
||||
`ERROR: style "${item.style}" using unknown file "${dataId}"! Skipping...`,
|
||||
`ERROR: style "${item.style}" using unknown file "${StyleSourceId}"! Skipping...`,
|
||||
);
|
||||
return undefined;
|
||||
} else {
|
||||
let id = dataId.substr(0, dataId.lastIndexOf('.')) || dataId;
|
||||
data[id][protocol] = dataId;
|
||||
let id = StyleSourceId.replace(/^.*\/(.*)$/, '$1'); // Remove url path up to last backslash, if it exists
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -229,22 +235,23 @@ function start(opts) {
|
|||
item,
|
||||
id,
|
||||
opts.publicUrl,
|
||||
(dataId) => {
|
||||
(StyleSourceId) => {
|
||||
let fileType;
|
||||
let inputFile;
|
||||
for (const id of Object.keys(data)) {
|
||||
if (id === dataId) {
|
||||
fileType = Object.keys(data[id])[0];
|
||||
if (isValidHttpUrl(data[id][fileType])) {
|
||||
if (StyleSourceId == id) {
|
||||
inputFile = data[id][fileType];
|
||||
} else {
|
||||
inputFile = path.resolve(
|
||||
options.paths[fileType],
|
||||
data[id][fileType],
|
||||
);
|
||||
break;
|
||||
} else if (data[id][fileType] == StyleSourceId) {
|
||||
inputFile = data[id][fileType];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isValidHttpUrl(inputFile)) {
|
||||
inputFile = path.resolve(options.paths[fileType], inputFile);
|
||||
}
|
||||
|
||||
return { inputfile: inputFile, filetype: fileType };
|
||||
},
|
||||
),
|
||||
|
|
|
|||
Loading…
Reference in a new issue