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.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;
if (url && (url.startsWith('pmtiles:') || url.startsWith('mbtiles:'))) { let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
// found pmtiles or mbtiles source, replace with info from local file if (dataId.startsWith('{') && dataId.endsWith('}')) {
delete source.url; dataId = dataId.slice(1, -1);
}
let inputFile;
let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
const fromData = dataId.startsWith('{') && dataId.endsWith('}');
if (fromData) {
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,109 +1497,114 @@ 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);
if (!inputFileStats.isFile() || inputFileStats.size === 0) { if (!inputFileStats.isFile() || inputFileStats.size === 0) {
throw Error(`Not valid PMTiles file: "${inputFile}"`); throw Error(`Not valid PMTiles file: "${inputFile}"`);
}
}
if (source_type === 'pmtiles') {
map.sources[name] = PMtilesOpen(inputFile);
map.source_types[name] = 'pmtiles';
const metadata = await GetPMtilesInfo(map.sources[name]);
if (!repoobj.dataProjWGStoInternalWGS && metadata.proj4) {
// how to do this for multiple sources with different proj4 defs?
const to3857 = proj4('EPSG:3857');
const toDataProj = proj4(metadata.proj4);
repoobj.dataProjWGStoInternalWGS = (xy) =>
to3857.inverse(toDataProj.forward(xy));
}
const type = source.type;
metadata['extension'] = 'pmtiles';
Object.assign(source, metadata);
source.type = type;
source.tiles = [
// meta url which will be detected when requested
`pmtiles://${name}/{z}/{x}/{y}.${metadata.format || 'pbf'}`,
];
delete source.scheme;
if (
!attributionOverride &&
source.attribution &&
source.attribution.length > 0
) {
if (!tileJSON.attribution.includes(source.attribution)) {
if (tileJSON.attribution.length > 0) {
tileJSON.attribution += ' | ';
}
tileJSON.attribution += source.attribution;
} }
} }
} else {
queue.push( if (source_type === 'pmtiles') {
new Promise((resolve, reject) => { map.sources[name] = PMtilesOpen(inputFile);
inputFile = path.resolve(options.paths.mbtiles, inputFile); map.source_types[name] = 'pmtiles';
const inputFileStats = fs.statSync(inputFile); const metadata = await GetPMtilesInfo(map.sources[name]);
if (!inputFileStats.isFile() || inputFileStats.size === 0) {
throw Error(`Not valid MBTiles file: "${inputFile}"`); if (!repoobj.dataProjWGStoInternalWGS && metadata.proj4) {
// how to do this for multiple sources with different proj4 defs?
const to3857 = proj4('EPSG:3857');
const toDataProj = proj4(metadata.proj4);
repoobj.dataProjWGStoInternalWGS = (xy) =>
to3857.inverse(toDataProj.forward(xy));
}
const type = source.type;
metadata['extension'] = 'pmtiles';
Object.assign(source, metadata);
source.type = type;
source.tiles = [
// meta url which will be detected when requested
`pmtiles://${name}/{z}/{x}/{y}.${metadata.format || 'pbf'}`,
];
delete source.scheme;
if (
!attributionOverride &&
source.attribution &&
source.attribution.length > 0
) {
if (!tileJSON.attribution.includes(source.attribution)) {
if (tileJSON.attribution.length > 0) {
tileJSON.attribution += ' | ';
}
tileJSON.attribution += source.attribution;
} }
map.sources[name] = new MBTiles(inputFile + '?mode=ro', (err) => { }
map.sources[name].getInfo((err, info) => { } else {
if (err) { queue.push(
console.error(err); new Promise((resolve, reject) => {
return; inputFile = path.resolve(options.paths.mbtiles, inputFile);
} const inputFileStats = fs.statSync(inputFile);
map.source_types[name] = 'mbtiles'; if (!inputFileStats.isFile() || inputFileStats.size === 0) {
throw Error(`Not valid MBTiles file: "${inputFile}"`);
if (!repoobj.dataProjWGStoInternalWGS && info.proj4) { }
// how to do this for multiple sources with different proj4 defs? map.sources[name] = new MBTiles(
const to3857 = proj4('EPSG:3857'); inputFile + '?mode=ro',
const toDataProj = proj4(info.proj4); (err) => {
repoobj.dataProjWGStoInternalWGS = (xy) => map.sources[name].getInfo((err, info) => {
to3857.inverse(toDataProj.forward(xy)); if (err) {
} console.error(err);
return;
const type = source.type;
info['extension'] = 'mbtiles';
Object.assign(source, info);
source.type = type;
source.tiles = [
// meta url which will be detected when requested
`mbtiles://${name}/{z}/{x}/{y}.${info.format || 'pbf'}`,
];
delete source.scheme;
if (options.dataDecoratorFunc) {
source = options.dataDecoratorFunc(
name,
'tilejson',
source,
);
}
if (
!attributionOverride &&
source.attribution &&
source.attribution.length > 0
) {
if (!tileJSON.attribution.includes(source.attribution)) {
if (tileJSON.attribution.length > 0) {
tileJSON.attribution += ' | ';
} }
tileJSON.attribution += source.attribution; map.source_types[name] = 'mbtiles';
}
} if (!repoobj.dataProjWGStoInternalWGS && info.proj4) {
resolve(); // how to do this for multiple sources with different proj4 defs?
}); const to3857 = proj4('EPSG:3857');
}); const toDataProj = proj4(info.proj4);
}), repoobj.dataProjWGStoInternalWGS = (xy) =>
); to3857.inverse(toDataProj.forward(xy));
}
const type = source.type;
info['extension'] = 'mbtiles';
Object.assign(source, info);
source.type = type;
source.tiles = [
// meta url which will be detected when requested
`mbtiles://${name}/{z}/{x}/{y}.${info.format || 'pbf'}`,
];
delete source.scheme;
if (options.dataDecoratorFunc) {
source = options.dataDecoratorFunc(
name,
'tilejson',
source,
);
}
if (
!attributionOverride &&
source.attribution &&
source.attribution.length > 0
) {
if (
!tileJSON.attribution.includes(source.attribution)
) {
if (tileJSON.attribution.length > 0) {
tileJSON.attribution += ' | ';
}
tileJSON.attribution += source.attribution;
}
}
resolve();
});
},
);
}),
);
}
} }
} }
} }

View file

@ -110,26 +110,30 @@ 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.startsWith('{') && url.endsWith('}')) {
url = url.slice(1, -1);
}
if (url.startsWith('pmtiles://') || url.startsWith('mbtiles://')) {
const protocol = url.split(':')[0];
if (url && (url.startsWith('pmtiles:') || url.startsWith('mbtiles:'))) { let dataId = url.replace('pmtiles://', '').replace('mbtiles://', '');
const protocol = url.split(':')[0]; if (dataId.startsWith('{') && dataId.endsWith('}')) {
let dataId = url.replace('pmtiles://', '').replace('mbtiles://', ''); dataId = dataId.slice(1, -1);
}
const fromData = dataId.startsWith('{') && dataId.endsWith('}');
if (fromData) {
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`;
} }
} }

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 (StyleSourceId == id) {
if (isValidHttpUrl(data[id][fileType])) { inputFile = data[id][fileType];
inputFile = data[id][fileType]; break;
} else { } else if (data[id][fileType] == StyleSourceId) {
inputFile = path.resolve( inputFile = data[id][fileType];
options.paths[fileType], break;
data[id][fileType],
);
}
} }
} }
if (!isValidHttpUrl(inputFile)) {
inputFile = path.resolve(options.paths[fileType], inputFile);
}
return { inputfile: inputFile, filetype: fileType }; return { inputfile: inputFile, filetype: fileType };
}, },
), ),