Skip to content

Commit 1545ad7

Browse files
committed
fixed redrawing
1 parent 5d3f927 commit 1545ad7

9 files changed

+72
-344
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"@types/geojson": "^7946.0.13",
7272
"bbox-fns": "^0.19.0",
7373
"geo-extent": "^1.3.1",
74-
"georaster-stack": "^0.4.3",
74+
"georaster-stack": "^0.4.6",
7575
"geotiff-epsg-code": "^0.3.1",
7676
"geotiff-read-bbox": "^2.2.0",
7777
"geowarp": "^1.23.1",
@@ -101,8 +101,8 @@
101101
"babel-loader": "^9.1.3",
102102
"concurrently": "^8.2.2",
103103
"envisage": "^0.1.0",
104-
"eslint": "^8.54.0",
105-
"eslint-config-prettier": "^9.0.0",
104+
"eslint": "^8.55.0",
105+
"eslint-config-prettier": "^9.1.0",
106106
"eslint-plugin-prettier": "^5.0.1",
107107
"fast-max": "^0.4.0",
108108
"fast-min": "^0.3.0",

src/georaster-layer-for-leaflet.ts

+63-20
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
312312
this.mask_srs = "EPSG:4326";
313313
}
314314
}
315+
this.mask_strategy = options.mask_strategy;
315316
},
316317

317318
getProjDef: function (proj: number | string) {
@@ -354,6 +355,11 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
354355
const coordsKey = this._tileCoordsToKey(coords);
355356

356357
const resolution = this._getResolution(coords.z);
358+
if (resolution === undefined) throw new Error("[georaster-layer-for-leaflet] resolution is undefined");
359+
360+
// saving resolution, which we will need later if/when redrawing
361+
(tile as any).resolution = resolution;
362+
357363
const key = `${coordsKey}:${resolution}`;
358364
const doneCb = (error?: Error, tile?: HTMLElement): void => {
359365
done(error, tile);
@@ -381,7 +387,7 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
381387

382388
const timed = debugLevel >= 1;
383389

384-
if (debugLevel >= 2) console.log("starting drawTile with", { tile, coords, context, done });
390+
if (debugLevel >= 2) console.log("starting drawTile with", { tile, coords, context, done, resolution });
385391

386392
let error: Error;
387393

@@ -390,6 +396,10 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
390396

391397
const cacheKey = [z, x, y].join("/");
392398

399+
if (isNaN(resolution)) {
400+
throw new Error(`[georaster-layer-for-leafler] [${cacheKey}] resolution isNaN`);
401+
}
402+
393403
if (this.options._valid_tiles && !this.options._valid_tiles.includes(cacheKey)) return;
394404

395405
// over-ride default log with tile coordinate info
@@ -595,6 +605,12 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
595605
console.log(`[georaster-layer-for-leaflet] [${cacheKey}] numberOfSamplesDown: ${numberOfSamplesDown}`);
596606
}
597607

608+
if (isNaN(numberOfSamplesAcross)) {
609+
throw new Error(
610+
`[georaster-layer-for-leaflet [${cacheKey}] numberOfSamplesAcross is NaN when resolution=${resolution} and extentOfInnerTileInMapCRS.width=${extentOfInnerTileInMapCRS.width} and extentOfTileInMapCRS.width=${extentOfTileInMapCRS.width}`
611+
);
612+
}
613+
598614
if (debugLevel >= 3) {
599615
console.log(
600616
"[georaster-layer-for-leaflet] extent of inner tile before snapping " +
@@ -701,16 +717,26 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
701717
try {
702718
const startReadRasters = timed ? performance.now() : 0;
703719
const stack = await this.stack;
720+
const stack_size = [numberOfSamplesAcross, numberOfSamplesDown];
704721
const { data: tileRasters }: { data: number[][][] } = await stack.read({
705722
extent: extentOfInnerTileInMapCRS,
706-
size: [numberOfSamplesAcross, numberOfSamplesDown]
723+
size: stack_size
707724
});
708-
if (timed)
709-
console.log(
710-
`[georaster-layer-for-leaflet] [${cacheKey}] reading rasters took: ${
711-
performance.now() - startReadRasters
712-
}ms`
725+
726+
if (tileRasters === undefined) {
727+
throw new Error(
728+
`tileRasters is undefined when extent is ${extentOfInnerTileInMapCRS.js} and size is ${JSON.stringify([
729+
numberOfSamplesAcross,
730+
numberOfSamplesDown
731+
])}`
713732
);
733+
}
734+
735+
if (timed) {
736+
const durationReadRasters = performance.now() - startReadRasters;
737+
console.log(`[georaster-layer-for-leaflet] [${cacheKey}] reading rasters took: ${durationReadRasters}ms`);
738+
}
739+
714740
if (this.options.onReadRasters) {
715741
this.options.onReadRasters({
716742
data: tileRasters,
@@ -724,6 +750,7 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
724750
if (this.calcStats) {
725751
const start_calc_stats = debugLevel >= 1 ? performance.now() : 0;
726752
const { noDataValue } = this;
753+
const original_ranges: number[] = Array.from(this.currentStats.ranges);
727754
for (let bandIndex = 0; bandIndex < tileRasters.length; bandIndex++) {
728755
let min = this.currentStats.mins[bandIndex];
729756
let max = this.currentStats.maxs[bandIndex];
@@ -735,21 +762,9 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
735762
if (value !== noDataValue) {
736763
if (min === undefined || value < min) {
737764
min = value;
738-
// invalidate cache because previous tiles used less accurate stats
739-
this._cache = { innerTile: {}, tile: {} };
740765
}
741766
if (max === undefined || value > max) {
742767
max = value;
743-
// invalidate cache because previous tiles used less accurate stats
744-
this._cache = { innerTile: {}, tile: {} };
745-
const tiles = this.getActiveTiles();
746-
747-
// redraw old tiles
748-
tiles.forEach((tile: Tile) => {
749-
const { coords, el } = tile;
750-
// this.drawTile({ tile: el, coords, context: el.getContext("2d") });
751-
});
752-
if (debugLevel >= 1) console.log("redrew tiles");
753768
}
754769
}
755770
}
@@ -758,6 +773,34 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
758773
this.currentStats.maxs[bandIndex] = max;
759774
this.currentStats.ranges[bandIndex] = max - min;
760775
}
776+
777+
let redraw = false;
778+
for (let bandIndex = 0; bandIndex < tileRasters.length; bandIndex++) {
779+
const old_range = original_ranges[bandIndex];
780+
const new_range = this.currentStats.ranges[bandIndex];
781+
const diff_range = new_range - old_range;
782+
const percentage_change = diff_range / old_range;
783+
const threshold = 1 / 256;
784+
if (percentage_change > threshold) {
785+
redraw = true;
786+
break;
787+
}
788+
}
789+
790+
if (redraw) {
791+
if (debugLevel >= 1) console.log("[georaster-layer-for-leaflet] redrawing tiles");
792+
// invalidate cache because previous tiles used less accurate stats
793+
this._cache = { innerTile: {}, tile: {} };
794+
const tiles = this.getActiveTiles();
795+
796+
// redraw old tiles
797+
tiles.forEach((tile: Tile) => {
798+
const { coords, el } = tile;
799+
this.drawTile({ tile: el, coords, context: el.getContext("2d"), resolution: (el as any).resolution });
800+
});
801+
if (debugLevel >= 1) console.log("[georaster-layer-for-leaflet] finished redrawing tiles");
802+
}
803+
761804
if (this._dynamic) {
762805
const rawToRgbFn = (rawToRgb as any).default || rawToRgb;
763806
try {
@@ -1031,7 +1074,7 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
10311074

10321075
tiles.forEach((tile: Tile) => {
10331076
const { coords, el } = tile;
1034-
this.drawTile({ tile: el, coords, context: el.getContext("2d") });
1077+
this.drawTile({ tile: el, coords, context: el.getContext("2d"), resolution: (el as any).resolution });
10351078
});
10361079
if (debugLevel >= 1) console.log("Finished updating active tile colours");
10371080
return this;

tests/color-scale.2.html

-49
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323

2424
<script>
2525
// initalize leaflet map
26-
<<<<<<< Updated upstream
2726
var map = L.map("map").setView([0, 0], 5);
28-
=======
29-
var map = L.map('map').setView([0, 0], 1);
30-
>>>>>>> Stashed changes
3127

3228
// add OpenStreetMap basemap
3329
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
@@ -49,7 +45,6 @@
4945
console.log(chroma.brewer);
5046
var scale = chroma.scale("Viridis");
5147

52-
<<<<<<< Updated upstream
5348
var layer = new GeoRasterLayer({
5449
debugLevel: 4,
5550
georaster: georaster,
@@ -68,50 +63,6 @@
6863
return color;
6964
},
7065
resolution: 256
71-
=======
72-
const pixelValuesToColorFn = function (pixelValues) {
73-
// debugger;
74-
var pixelValue = pixelValues[0]; // there's just one band in this raster
75-
76-
// if there's zero wind, don't return a color
77-
// if (pixelValue === 0) return null;
78-
// if (pixelValue === "0") return `rgba(0,0,0,0)`;
79-
// if (pixelValue === 0) return `rgba(0,0,0,0)`;
80-
if (pixelValue === 0) return null;
81-
// XX GEOWARP CANVAS SHOULD SKIP PAINTING NULL VALUES
82-
83-
84-
// scale to 0 - 1 used by chroma
85-
var scaledPixelValue = (pixelValue - min) / range;
86-
87-
88-
// return scale(scaledPixelValue).hex();
89-
90-
91-
return scale(scaledPixelValue).rgb();
92-
93-
// console.log({pixelValue, min, range, scaledPixelValue, color});
94-
// debugger;
95-
};
96-
97-
var layer = new GeoRasterLayer({
98-
debugLevel: 0,
99-
georaster: georaster,
100-
opacity: 0.7,
101-
// mask: "auto",
102-
pixelValuesToColorFn,
103-
resolution: 128,
104-
// _valid_tiles: ["1/0/0"],
105-
// onReadRasters: function ({ data, height, width }) {
106-
// data = data[0].flat().map(n => pixelValuesToColorFn([n]));
107-
// console.log("stretched data:", data);
108-
// toCanvas({ data, height, width })
109-
// .then(async (canvas) => {
110-
// const url = await canvas.toDataURL();
111-
// console.log("data url:", url);
112-
// })
113-
// }
114-
>>>>>>> Stashed changes
11566
});
11667
console.log("layer:", layer);
11768
layer.addTo(map);

tests/color-scale.html

+2-11
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,12 @@
4545
var scale = chroma.scale("Viridis");
4646

4747
var layer = new GeoRasterLayer({
48-
<<<<<<< Updated upstream
4948
debugLevel: 4,
5049
georaster,
50+
mask: "auto",
5151
opacity: 0.7,
52-
pixelValuesToColorFn: function (pixelValues) {
52+
pixelValuesToColorFn: function(pixelValues) {
5353
var pixelValue = pixelValues[0]; // there's just one band in this raster
54-
=======
55-
debugLevel: 4,
56-
georaster,
57-
mask: "auto",
58-
opacity: 0.7,
59-
pixelValuesToColorFn: function(pixelValues) {
60-
var pixelValue = pixelValues[0]; // there's just one band in this raster
61-
>>>>>>> Stashed changes
62-
6354
// if there's zero wind, don't return a color
6455
if (pixelValue === 0) return null;
6556

tests/dynamic_threshold_ndvi.html

-108
This file was deleted.

0 commit comments

Comments
 (0)