@@ -312,6 +312,7 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
312
312
this . mask_srs = "EPSG:4326" ;
313
313
}
314
314
}
315
+ this . mask_strategy = options . mask_strategy ;
315
316
} ,
316
317
317
318
getProjDef : function ( proj : number | string ) {
@@ -354,6 +355,11 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
354
355
const coordsKey = this . _tileCoordsToKey ( coords ) ;
355
356
356
357
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
+
357
363
const key = `${ coordsKey } :${ resolution } ` ;
358
364
const doneCb = ( error ?: Error , tile ?: HTMLElement ) : void => {
359
365
done ( error , tile ) ;
@@ -381,7 +387,7 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
381
387
382
388
const timed = debugLevel >= 1 ;
383
389
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 } ) ;
385
391
386
392
let error : Error ;
387
393
@@ -390,6 +396,10 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
390
396
391
397
const cacheKey = [ z , x , y ] . join ( "/" ) ;
392
398
399
+ if ( isNaN ( resolution ) ) {
400
+ throw new Error ( `[georaster-layer-for-leafler] [${ cacheKey } ] resolution isNaN` ) ;
401
+ }
402
+
393
403
if ( this . options . _valid_tiles && ! this . options . _valid_tiles . includes ( cacheKey ) ) return ;
394
404
395
405
// over-ride default log with tile coordinate info
@@ -595,6 +605,12 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
595
605
console . log ( `[georaster-layer-for-leaflet] [${ cacheKey } ] numberOfSamplesDown: ${ numberOfSamplesDown } ` ) ;
596
606
}
597
607
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
+
598
614
if ( debugLevel >= 3 ) {
599
615
console . log (
600
616
"[georaster-layer-for-leaflet] extent of inner tile before snapping " +
@@ -701,16 +717,26 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
701
717
try {
702
718
const startReadRasters = timed ? performance . now ( ) : 0 ;
703
719
const stack = await this . stack ;
720
+ const stack_size = [ numberOfSamplesAcross , numberOfSamplesDown ] ;
704
721
const { data : tileRasters } : { data : number [ ] [ ] [ ] } = await stack . read ( {
705
722
extent : extentOfInnerTileInMapCRS ,
706
- size : [ numberOfSamplesAcross , numberOfSamplesDown ]
723
+ size : stack_size
707
724
} ) ;
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
+ ] ) } `
713
732
) ;
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
+
714
740
if ( this . options . onReadRasters ) {
715
741
this . options . onReadRasters ( {
716
742
data : tileRasters ,
@@ -724,6 +750,7 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
724
750
if ( this . calcStats ) {
725
751
const start_calc_stats = debugLevel >= 1 ? performance . now ( ) : 0 ;
726
752
const { noDataValue } = this ;
753
+ const original_ranges : number [ ] = Array . from ( this . currentStats . ranges ) ;
727
754
for ( let bandIndex = 0 ; bandIndex < tileRasters . length ; bandIndex ++ ) {
728
755
let min = this . currentStats . mins [ bandIndex ] ;
729
756
let max = this . currentStats . maxs [ bandIndex ] ;
@@ -735,21 +762,9 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
735
762
if ( value !== noDataValue ) {
736
763
if ( min === undefined || value < min ) {
737
764
min = value ;
738
- // invalidate cache because previous tiles used less accurate stats
739
- this . _cache = { innerTile : { } , tile : { } } ;
740
765
}
741
766
if ( max === undefined || value > max ) {
742
767
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" ) ;
753
768
}
754
769
}
755
770
}
@@ -758,6 +773,34 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
758
773
this . currentStats . maxs [ bandIndex ] = max ;
759
774
this . currentStats . ranges [ bandIndex ] = max - min ;
760
775
}
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
+
761
804
if ( this . _dynamic ) {
762
805
const rawToRgbFn = ( rawToRgb as any ) . default || rawToRgb ;
763
806
try {
@@ -1031,7 +1074,7 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
1031
1074
1032
1075
tiles . forEach ( ( tile : Tile ) => {
1033
1076
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 } ) ;
1035
1078
} ) ;
1036
1079
if ( debugLevel >= 1 ) console . log ( "Finished updating active tile colours" ) ;
1037
1080
return this ;
0 commit comments