From d14e450694f843c95bebab88448d237e58b7471d Mon Sep 17 00:00:00 2001 From: Florian Bischof Date: Sun, 9 Jul 2023 14:21:39 +0200 Subject: [PATCH] Add resolution based on zoom levels --- src/georaster-layer-for-leaflet.ts | 34 ++++++++++++++++++++++++------ src/types/index.ts | 2 +- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/georaster-layer-for-leaflet.ts b/src/georaster-layer-for-leaflet.ts index b4c6f30..2e96116 100644 --- a/src/georaster-layer-for-leaflet.ts +++ b/src/georaster-layer-for-leaflet.ts @@ -413,11 +413,11 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C // pad xmax and ymin of container to tolerate ceil() and floor() in snap() container: inSimpleCRS ? [ - extentOfLayer.xmin, - extentOfLayer.ymin - 0.25 * pixelHeight, - extentOfLayer.xmax + 0.25 * pixelWidth, - extentOfLayer.ymax - ] + extentOfLayer.xmin, + extentOfLayer.ymin - 0.25 * pixelHeight, + extentOfLayer.xmax + 0.25 * pixelWidth, + extentOfLayer.ymax + ] : [xmin, ymin - 0.25 * pixelHeight, xmax + 0.25 * pixelWidth, ymax], debug: debugLevel >= 2, origin: inSimpleCRS ? [extentOfLayer.xmin, extentOfLayer.ymax] : [xmin, ymax], @@ -441,8 +441,28 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C const recropTileProj = inSimpleCRS ? recropTileOrig : recropTileOrig.reproj(code); const recropTile = recropTileProj.crop(extentOfTileInMapCRS); if (recropTile !== null) { - maxSamplesAcross = Math.ceil(resolution * (recropTile.width / extentOfTileInMapCRS.width)); - maxSamplesDown = Math.ceil(resolution * (recropTile.height / extentOfTileInMapCRS.height)); + let resolutionValue; + + if (typeof resolution === "object") { + const zoomLevels = Object.keys(resolution); + const mapZoom = this.getMap().getZoom(); + + for (const key in zoomLevels) { + if (Object.prototype.hasOwnProperty.call(zoomLevels, key)) { + const zoomLvl = zoomLevels[key]; + if (zoomLvl <= mapZoom) { + resolutionValue = resolution[zoomLvl]; + } else { + break; + } + } + } + } else { + resolutionValue = resolution; + } + + maxSamplesAcross = Math.ceil(resolutionValue * (recropTile.width / extentOfTileInMapCRS.width)); + maxSamplesDown = Math.ceil(resolutionValue * (recropTile.height / extentOfTileInMapCRS.height)); } } diff --git a/src/types/index.ts b/src/types/index.ts index 990194f..32d8925 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -18,7 +18,7 @@ export type SimplePoint = { export type Mask = Feature | FeatureCollection | Polygon | MultiPolygon; interface GeoRasterLayerOptions_CommonOptions extends GridLayerOptions { - resolution?: number; + resolution?: number | { [key: number]: number }; debugLevel?: DebugLevel; pixelValuesToColorFn?: PixelValuesToColorFn; bounds?: LatLngBounds;