Skip to content

Commit 1af8eb0

Browse files
committed
Update to new zoom based resolution
1 parent 7525a16 commit 1af8eb0

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

src/georaster-layer-for-leaflet.ts

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
227227
}
228228
},
229229

230+
onAdd: function (map) {
231+
if (!this.options.maxZoom) {
232+
// maxZoom is needed to display the tiles in the correct order over the zIndex between the zoom levels
233+
// https://github.com/Leaflet/Leaflet/blob/2592967aa6bd392db0db9e58dab840054e2aa291/src/layer/tile/GridLayer.js#L375C21-L375C21
234+
this.options.maxZoom = map.getMaxZoom();
235+
}
236+
237+
L.GridLayer.prototype.onAdd.call(this, map);
238+
},
239+
230240
getRasters: function (options: GetRasterOptions) {
231241
const {
232242
innerTileTopLeftPoint,
@@ -325,7 +335,9 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
325335
// drawTile dynamically sets the width and padding based on
326336
// how much the georaster takes up the tile area
327337
const coordsKey = this._tileCoordsToKey(coords);
328-
const key = `${coordsKey}:${this.options.resolution}`;
338+
339+
const resolution = this._getResolution(coords.z);
340+
const key = `${coordsKey}:${resolution}`;
329341
const doneCb = (error?: Error, tile?: HTMLElement): void => {
330342
done(error, tile);
331343

@@ -339,13 +351,13 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
339351
done(undefined, this.cache[key]);
340352
return this.cache[key];
341353
} else {
342-
this.drawTile({ tile, coords, context, done: doneCb });
354+
this.drawTile({ tile, coords, context, done: doneCb, resolution });
343355
}
344356

345357
return tile;
346358
},
347359

348-
drawTile: function ({ tile, coords, context, done }: DrawTileOptions) {
360+
drawTile: function ({ tile, coords, context, done, resolution }: DrawTileOptions) {
349361
try {
350362
const { debugLevel = 0 } = this;
351363

@@ -451,7 +463,6 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
451463
const snappedSamplesDown = Math.abs(gridbox[3] - gridbox[1]);
452464
const rasterPixelsAcross = Math.ceil(oldExtentOfInnerTileInRasterCRS.width / pixelWidth);
453465
const rasterPixelsDown = Math.ceil(oldExtentOfInnerTileInRasterCRS.height / pixelHeight);
454-
const { resolution } = this.options;
455466
const layerCropExtent = inSimpleCRS ? extentOfLayer : this.extent;
456467
const recropTileOrig = oldExtentOfInnerTileInRasterCRS.crop(layerCropExtent); // may be null
457468
let maxSamplesAcross = 1;
@@ -460,28 +471,8 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
460471
const recropTileProj = inSimpleCRS ? recropTileOrig : recropTileOrig.reproj(code);
461472
const recropTile = recropTileProj.crop(extentOfTileInMapCRS);
462473
if (recropTile !== null) {
463-
let resolutionValue;
464-
465-
if (typeof resolution === "object") {
466-
const zoomLevels = Object.keys(resolution);
467-
const mapZoom = this.getMap().getZoom();
468-
469-
for (const key in zoomLevels) {
470-
if (Object.prototype.hasOwnProperty.call(zoomLevels, key)) {
471-
const zoomLvl = zoomLevels[key];
472-
if (zoomLvl <= mapZoom) {
473-
resolutionValue = resolution[zoomLvl];
474-
} else {
475-
break;
476-
}
477-
}
478-
}
479-
} else {
480-
resolutionValue = resolution;
481-
}
482-
483-
maxSamplesAcross = Math.ceil(resolutionValue * (recropTile.width / extentOfTileInMapCRS.width));
484-
maxSamplesDown = Math.ceil(resolutionValue * (recropTile.height / extentOfTileInMapCRS.height));
474+
maxSamplesAcross = Math.ceil(resolution * (recropTile.width / extentOfTileInMapCRS.width));
475+
maxSamplesDown = Math.ceil(resolution * (recropTile.height / extentOfTileInMapCRS.height));
485476
}
486477
}
487478

@@ -1083,6 +1074,30 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
10831074

10841075
clearCache() {
10851076
this.cache = {};
1077+
},
1078+
1079+
_getResolution(zoom: number) {
1080+
const { resolution } = this.options;
1081+
1082+
let resolutionValue;
1083+
if (typeof resolution === "object") {
1084+
const zoomLevels = Object.keys(resolution);
1085+
1086+
for (const key in zoomLevels) {
1087+
if (Object.prototype.hasOwnProperty.call(zoomLevels, key)) {
1088+
const zoomLvl = parseInt(zoomLevels[key]);
1089+
if (zoomLvl <= zoom) {
1090+
resolutionValue = resolution[zoomLvl];
1091+
} else {
1092+
break;
1093+
}
1094+
}
1095+
}
1096+
} else {
1097+
resolutionValue = resolution;
1098+
}
1099+
1100+
return resolutionValue;
10861101
}
10871102
});
10881103

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface DrawTileOptions {
5959
coords: Coords;
6060
context: CanvasRenderingContext2D;
6161
done: DoneCallback;
62+
resolution: number;
6263
}
6364

6465
// note: Tile is taken from leaflets `InternalTiles` type and should not be modified. - SFR 2021-01-19

0 commit comments

Comments
 (0)