1- import { faSpinner } from '@fortawesome/free-solid-svg-icons' ;
2- import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
31import { IDict } from '@jupytergis/schema' ;
42import { Button } from '@jupyterlab/ui-components' ;
53import { ReadonlyJSONObject } from '@lumino/coreutils' ;
@@ -12,6 +10,8 @@ import { IStopRow, ISymbologyDialogProps } from '../../symbologyDialog';
1210import BandRow from './BandRow' ;
1311import ColorRamp from './ColorRamp' ;
1412import StopRow from './StopRow' ;
13+ import { getGdal } from '../../../gdal' ;
14+ import { Spinner } from '../../../mainview/spinner' ;
1515
1616export interface IBandRow {
1717 band : number ;
@@ -137,7 +137,7 @@ const SingleBandPseudoColor = ({
137137 : setSelectedFunction ( 'exact' ) ;
138138 } ;
139139
140- const getBandInfo = ( ) => {
140+ const getBandInfo = async ( ) => {
141141 const bandsArr : IBandRow [ ] = [ ] ;
142142 const source = context . model . getSource ( layer ?. parameters ?. source ) ;
143143 const sourceInfo = source ?. parameters ?. urls [ 0 ] ;
@@ -146,25 +146,41 @@ const SingleBandPseudoColor = ({
146146 return ;
147147 }
148148
149- if ( stateDb && layerState && layerState . tifData ) {
150- const tifData = JSON . parse ( layerState . tifData as string ) ;
151-
152- tifData [ 'bands' ] . forEach ( ( bandData : TifBandData ) => {
153- bandsArr . push ( {
154- band : bandData . band ,
155- colorInterpretation : bandData . colorInterpretation ,
156- stats : {
157- minimum : sourceInfo . min ?? bandData . minimum ,
158- maximum : sourceInfo . max ?? bandData . maximum ,
159- mean : bandData . mean ,
160- stdDev : bandData . stdDev
161- } ,
162- metadata : bandData . metadata ,
163- histogram : bandData . histogram
164- } ) ;
149+ let tifData ;
150+
151+ if ( layerState && layerState . tifData ) {
152+ tifData = JSON . parse ( layerState . tifData as string ) ;
153+ } else {
154+ const Gdal = await getGdal ( ) ;
155+
156+ const fileData = await fetch ( sourceInfo . url ) ;
157+ const file = new File ( [ await fileData . blob ( ) ] , 'loaded.tif' ) ;
158+
159+ const result = await Gdal . open ( file ) ;
160+ const tifDataset = result . datasets [ 0 ] ;
161+ tifData = await Gdal . gdalinfo ( tifDataset , [ '-stats' ] ) ;
162+ Gdal . close ( tifDataset ) ;
163+
164+ stateDb ?. save ( `jupytergis:${ layerId } ` , {
165+ tifData : JSON . stringify ( tifData )
165166 } ) ;
166- setBandRows ( bandsArr ) ;
167167 }
168+
169+ tifData [ 'bands' ] . forEach ( ( bandData : TifBandData ) => {
170+ bandsArr . push ( {
171+ band : bandData . band ,
172+ colorInterpretation : bandData . colorInterpretation ,
173+ stats : {
174+ minimum : sourceInfo . min ?? bandData . minimum ,
175+ maximum : sourceInfo . max ?? bandData . maximum ,
176+ mean : bandData . mean ,
177+ stdDev : bandData . stdDev
178+ } ,
179+ metadata : bandData . metadata ,
180+ histogram : bandData . histogram
181+ } ) ;
182+ } ) ;
183+ setBandRows ( bandsArr ) ;
168184 } ;
169185
170186 const buildColorInfo = ( ) => {
@@ -438,13 +454,7 @@ const SingleBandPseudoColor = ({
438454 < div className = "jp-gis-layer-symbology-container" >
439455 < div className = "jp-gis-band-container" >
440456 { bandRows . length === 0 ? (
441- < div className = "jp-gis-band-info-loading-container" >
442- < span > Fetching band info...</ span >
443- < FontAwesomeIcon
444- icon = { faSpinner }
445- className = "jp-gis-loading-spinner"
446- />
447- </ div >
457+ < Spinner loading = { bandRows . length === 0 } />
448458 ) : (
449459 < BandRow
450460 // Band numbers are 1 indexed
0 commit comments