Skip to content

Commit c4a7831

Browse files
committed
improved backwards compatibility
1 parent 1cade6b commit c4a7831

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

package.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
},
6969
"homepage": "https://github.com/GeoTIFF/georaster-layer-for-leaflet#readme",
7070
"dependencies": {
71-
"@types/geojson": "^7946.0.13",
71+
"@types/geojson": "^7946.0.14",
7272
"bbox-fns": "^0.20.2",
7373
"fast-max": "^0.5.1",
7474
"fast-min": "^0.4.0",
@@ -78,7 +78,7 @@
7878
"geotiff-epsg-code": "^0.3.1",
7979
"geotiff-read-bbox": "^2.2.0",
8080
"geowarp": "^1.26.1",
81-
"geowarp-canvas": "^0.1.0",
81+
"geowarp-canvas": "^0.2.0",
8282
"memoizee": "^0.4.15",
8383
"pixel-utils": "^0.9.0",
8484
"proj4": "^2.10.0",
@@ -92,17 +92,16 @@
9292
"xdim": "^1.10.1"
9393
},
9494
"devDependencies": {
95-
"@babel/cli": "^7.23.4",
96-
"@babel/core": "^7.23.7",
95+
"@babel/cli": "^7.23.9",
96+
"@babel/core": "^7.23.9",
9797
"@babel/plugin-transform-export-namespace-from": "^7.23.4",
9898
"@babel/plugin-transform-optional-chaining": "^7.23.4",
9999
"@babel/plugin-transform-template-literals": "^7.23.3",
100-
"@babel/preset-env": "^7.23.8",
100+
"@babel/preset-env": "^7.23.9",
101101
"@babel/preset-typescript": "^7.23.3",
102-
"@types/chroma-js": "^2.4.3",
103102
"@types/leaflet": "^1.9.8",
104-
"@typescript-eslint/eslint-plugin": "^6.19.1",
105-
"@typescript-eslint/parser": "^6.19.1",
103+
"@typescript-eslint/eslint-plugin": "^6.20.0",
104+
"@typescript-eslint/parser": "^6.20.0",
106105
"babel-loader": "^9.1.3",
107106
"concurrently": "^8.2.2",
108107
"envisage": "^0.1.0",
@@ -114,12 +113,12 @@
114113
"leaflet": "^1.9.4",
115114
"mkdirp": "^3.0.1",
116115
"null-loader": "^4.0.1",
117-
"prettier": "^3.2.4",
116+
"prettier": "^3.2.5",
118117
"reproject-geojson": "^0.5.0",
119118
"rimraf": "^5.0.5",
120119
"to-canvas": "^0.2.0",
121120
"typescript": "^5.3.3",
122-
"webpack": "^5.89.0",
121+
"webpack": "^5.90.1",
123122
"webpack-cli": "^5.1.4"
124123
},
125124
"peerDependencies": {

src/georaster-layer-for-leaflet.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,20 +335,28 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
335335
this.mask_strategy = options.mask_strategy;
336336
},
337337

338-
getProjDef: function (proj: number | string) {
338+
getProjectionString: function (proj: number | string) {
339339
if (isUTM(proj)) return getProjString(proj);
340340
if (typeof proj === "number") proj = "EPSG:" + proj;
341341
if (proj in this.proj4.defs) return proj;
342342
if ("EPSG:" + proj in this.proj4.defs) return "EPSG:" + proj;
343343
throw new Error("[georaster-layer-for-leaflet] unsupported projection:" + proj);
344344
},
345345

346-
getProjector: function (_from: number | string, _to: number | string) {
346+
getProjector: function (_from: number | string, _to: number | string = "EPSG:4326") {
347+
if (_from === null || _from === undefined) {
348+
if (new Set(this.georasters.map((georaster: any) => georaster.projection)).size !== 1) {
349+
throw new Error("[georaster-layer-for-leaflet] getProjector called without a _from and georasters don't all have the same projection");
350+
} else {
351+
_from = this.georasters[0].projection;
352+
}
353+
}
354+
347355
if (!this.isSupportedProjection(_from)) {
348356
throw Error("[georaster-layer-for-leaflet] unsupported projection: " + _from);
349357
}
350358
if (!this.isSupportedProjection(_to)) throw Error("[georaster-layer-for-leaflet] unsupported projection: " + _to);
351-
return this.proj4(this.getProjDef(_from), this.getProjDef(_to));
359+
return this.proj4(this.getProjectionString(_from), this.getProjectionString(_to));
352360
},
353361

354362
createTile: function (coords: Coords, done: DoneCallback) {
@@ -566,7 +574,14 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
566574
// const defaultCanvasWidth = Math.max(256, this.resolution || 256);
567575
// const widthOfCanvasPixelInMapCRS = extentOfTileInMapCRS.width / defaultCanvasHeight;
568576
// const heightOfCanvasPixelInMapCRS = extentOfTileInMapCRS.height / defaultCanvasWidth;
569-
if (debugLevel >= 3) log({ heightOfScreenPixelInMapCRS, widthOfScreenPixelInMapCRS });
577+
if (debugLevel >= 3)
578+
console.log(
579+
`[georaster-layer-for-leaflet] [${cacheKey}] heightOfScreenPixelInMapCRS: ${heightOfScreenPixelInMapCRS}`
580+
);
581+
if (debugLevel >= 3)
582+
console.log(
583+
`[georaster-layer-for-leaflet] [${cacheKey}] widthOfScreenPixelInMapCRS: ${widthOfScreenPixelInMapCRS}`
584+
);
570585

571586
// even if we aren't doing the more advanced sample alignment above
572587
// we should still factor in the resolution when determing the resolution of the sampled rasters
@@ -579,10 +594,13 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
579594
resolution * (extentOfInnerTileInMapCRS.height / extentOfTileInMapCRS.height)
580595
);
581596

582-
const skew = map_crs_code === ("EPSG:" + this.georasters[0].projection) ? [0, 0] : measureSkew(
583-
[this.georasters[0].xmin, this.georasters[0].ymin, this.georasters[0].xmax, this.georasters[0].ymax],
584-
this.getProjector(this.georasters[0].projection, map_crs_code).forward
585-
);
597+
const skew =
598+
map_crs_code === "EPSG:" + this.georasters[0].projection
599+
? [0, 0]
600+
: measureSkew(
601+
[this.georasters[0].xmin, this.georasters[0].ymin, this.georasters[0].xmax, this.georasters[0].ymax],
602+
this.getProjector(this.georasters[0].projection, map_crs_code).forward
603+
);
586604
if (debugLevel >= 2) console.log(`[georaster-layer-for-leaflet] [${cacheKey}] skew:`, skew);
587605

588606
if (
@@ -978,7 +996,11 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
978996
theoretical_max,
979997
expr,
980998
turbo: this.options.turbo ?? false,
981-
skip_no_data_strategy: "any" // don't bother trying to render pixels with no data values
999+
skip_no_data_strategy: "any", // don't bother trying to render pixels with no data values
1000+
before_warp: (options: any) => {
1001+
// provide backwards compatability
1002+
this.getColor = (pixel: number[]) => options.expr({ pixel });
1003+
}
9821004
});
9831005
tile.style.visibility = "visible";
9841006
} catch (e: any) {
@@ -1146,6 +1168,11 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
11461168
return false;
11471169
},
11481170

1171+
// provided for backwards compatability
1172+
getColor: function (values: number[]): string | undefined {
1173+
throw new Error("[georaster-layer-for-leaflet] please call getColor after creating at least one tile");
1174+
},
1175+
11491176
/**
11501177
* Redraws the active map tiles updating the pixel values using the supplie callback
11511178
*/

tests/arrayBuffer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
.then(function (arrayBuffer) {
3939
parseGeoraster(arrayBuffer).then(function (georaster) {
4040
console.log("georaster:", georaster);
41-
var layer = new GeoRasterLayer({
41+
layer = new GeoRasterLayer({
4242
debugLevel: 0,
4343
georaster,
4444
resolution: 512,

0 commit comments

Comments
 (0)