formatting

This commit is contained in:
acalcutt 2024-12-21 15:27:45 -05:00 committed by Miko
parent fb73f53861
commit 088a5e8834

View file

@ -8,12 +8,12 @@ import { getPMtilesTile } from './pmtiles_adapter.js';
export class LocalDemManager { export class LocalDemManager {
/** /**
* Creates a new LocalDemManager instance. * Creates a new LocalDemManager instance.
* @param encoding - The encoding type for the DEM data. * @param {string} encoding - The encoding type for the DEM data.
* @param maxzoom - The maximum zoom level for the DEM data. * @param {number} maxzoom - The maximum zoom level for the DEM data.
* @param source - The source object that contains either pmtiles or mbtiles. * @param {object} source - The source object that contains either pmtiles or mbtiles.
* @param {'pmtiles' | 'mbtiles'} sourceType - The type of data source * @param {'pmtiles' | 'mbtiles'} sourceType - The type of data source
* @param [extractZXYFromUrlTrimFunction] - The function to extract the zxy from the url. * @param {Function} [GetTileFunction] - the function that returns a tile from the pmtiles object.
* @param [GetTileFunction] - the function that returns a tile from the pmtiles object. * @param {Function} [extractZXYFromUrlTrimFunction] - The function to extract the zxy from the url.
*/ */
constructor( constructor(
encoding, encoding,
@ -53,24 +53,24 @@ export class LocalDemManager {
/** /**
* Processes image data from a blob. * Processes image data from a blob.
* @param blob - The image data as a Blob. * @param {Blob} blob - The image data as a Blob.
* @param abortController - An AbortController to cancel the image processing. * @param {AbortController} abortController - An AbortController to cancel the image processing.
* @returns {Promise<any>} - A Promise that resolves with the processed image data, or null if aborted. * @returns {Promise<any>} - A Promise that resolves with the processed image data, or null if aborted.
* @throws If an error occurs during image processing. * @throws If an error occurs during image processing.
*/ */
async getImageData(blob, abortController) { async getImageData(blob, abortController) {
try { try {
if (Boolean(abortController?.signal?.aborted)) return null; // Check for abort signal. if (Boolean(abortController?.signal?.aborted)) return null;
const buffer = await blob.arrayBuffer(); const buffer = await blob.arrayBuffer();
const image = sharp(Buffer.from(buffer)); const image = sharp(Buffer.from(buffer));
const metadata = await image.metadata(); const metadata = await image.metadata();
if (Boolean(abortController?.signal?.aborted)) return null; // Check for abort signal. if (Boolean(abortController?.signal?.aborted)) return null;
const { data, info } = await image const { data, info } = await image
.raw() .raw()
.toBuffer({ resolveWithObject: true }); .toBuffer({ resolveWithObject: true });
if (Boolean(abortController?.signal?.aborted)) return null; // Check for abort signal. if (Boolean(abortController?.signal?.aborted)) return null;
const parsed = mlcontour.decodeParsedImage( const parsed = mlcontour.decodeParsedImage(
info.width, info.width,
@ -78,22 +78,21 @@ export class LocalDemManager {
this.encoding, this.encoding,
data, data,
); );
if (Boolean(abortController?.signal?.aborted)) return null; // Check for abort signal. if (Boolean(abortController?.signal?.aborted)) return null;
return parsed; return parsed;
} catch (error) { } catch (error) {
console.error('Error processing image:', error); console.error('Error processing image:', error);
throw error; // Rethrow to handle upstream throw error;
// return null; // Or handle error gracefully
} }
} }
/** /**
* Fetches a tile using the provided url and abortController * Fetches a tile using the provided url and abortController
* @param url - The url that should be used to fetch the tile. * @param {string} url - The url that should be used to fetch the tile.
* @param abortController - An AbortController to cancel the request. * @param {AbortController} abortController - An AbortController to cancel the request.
* @returns {Promise<{data: Blob, expires: undefined, cacheControl: undefined}>} A promise that resolves with the response data. * @returns {Promise<{data: Blob, expires: undefined, cacheControl: undefined}>} A promise that resolves with the response data.
* @throws If an error occurs fetching or processing the tile. * @throws If an error occurs fetching or processing the tile.
*/ */
async GetTile(url, abortController) { async GetTile(url, abortController) {
console.log(url); console.log(url);
@ -102,7 +101,7 @@ export class LocalDemManager {
throw new Error(`Could not extract zxy from $`); throw new Error(`Could not extract zxy from $`);
} }
if (abortController.signal.aborted) { if (abortController.signal.aborted) {
return null; // Or throw an error return null;
} }
try { try {
@ -163,20 +162,19 @@ export class LocalDemManager {
console.log('fetch cancelled'); console.log('fetch cancelled');
return null; return null;
} }
throw error; // Rethrow for handling upstream throw error;
} }
} }
/** /**
* Default implementation for extracting z,x,y from a url * Default implementation for extracting z,x,y from a url
* @param url - The url to extract from * @param {string} url - The url to extract from
* @returns {{z: number, x: number, y:number} | null} Returns the z,x,y of the url, or null if can't extract * @returns {{z: number, x: number, y:number} | null} Returns the z,x,y of the url, or null if can't extract
*/ */
_extractZXYFromUrlTrimFunction(url) { _extractZXYFromUrlTrimFunction(url) {
// 1. Find the index of the last `/`
const lastSlashIndex = url.lastIndexOf('/'); const lastSlashIndex = url.lastIndexOf('/');
if (lastSlashIndex === -1) { if (lastSlashIndex === -1) {
return null; // URL does not have any slashes return null;
} }
const segments = url.split('/'); const segments = url.split('/');
@ -192,13 +190,12 @@ export class LocalDemManager {
const cleanedYSegment = const cleanedYSegment =
lastDotIndex === -1 ? ySegment : ySegment.substring(0, lastDotIndex); lastDotIndex === -1 ? ySegment : ySegment.substring(0, lastDotIndex);
// 3. Attempt to parse segments as numbers
const z = parseInt(zSegment, 10); const z = parseInt(zSegment, 10);
const x = parseInt(xSegment, 10); const x = parseInt(xSegment, 10);
const y = parseInt(cleanedYSegment, 10); const y = parseInt(cleanedYSegment, 10);
if (isNaN(z) || isNaN(x) || isNaN(y)) { if (isNaN(z) || isNaN(x) || isNaN(y)) {
return null; // Conversion failed, invalid URL format return null;
} }
return { z, x, y }; return { z, x, y };
@ -206,7 +203,7 @@ export class LocalDemManager {
/** /**
* Get the underlying maplibre-contour LocalDemManager * Get the underlying maplibre-contour LocalDemManager
* @returns the underlying maplibre-contour LocalDemManager * @returns {mlcontour.LocalDemManager} the underlying maplibre-contour LocalDemManager
*/ */
getManager() { getManager() {
return this.manager; return this.manager;