@@ -805,9 +805,18 @@ def isFlipped(source):
805805 return False
806806
807807
808- def rasterInfo (sourceDS : load_raster_input ) -> RasterInfo :
808+ def rasterInfo (sourceDS : load_raster_input , compute_statistics : bool = False ) -> RasterInfo :
809809 """Returns a named tuple containing information relating to the input raster.
810810
811+ Parameters
812+ ----------
813+ sourceDS : Anything acceptable by loadRaster()
814+ The raster datasource
815+ compute_statistics : bool; optional
816+ If True, the maximum and minimum value of the raster data are computed.
817+ This is computationally expensive for large rasters and repeated calls of
818+ this function on the same raster should be avoided.
819+
811820 Returns
812821 -------
813822 namedtuple -> ( srs: The spatial reference system (as an OGR object)
@@ -850,17 +859,21 @@ def rasterInfo(sourceDS: load_raster_input) -> RasterInfo:
850859 output ["scale" ] = sourceBand .GetScale ()
851860 output ["offset" ] = sourceBand .GetOffset ()
852861
853- try :
854- sourceBand .ComputeStatistics (0 )
862+ if compute_statistics is True :
863+ try :
864+ sourceBand .ComputeStatistics (0 )
855865
856- maximum_value = sourceBand .GetMaximum ()
857- minimum_value = sourceBand .GetMinimum ()
866+ maximum_value = sourceBand .GetMaximum ()
867+ minimum_value = sourceBand .GetMinimum ()
858868
859- output ["maximum_value" ] = maximum_value
860- output ["minimum_value" ] = minimum_value
861- except :
862- output ["maximum_value" ] = output ["noData" ]
863- output ["minimum_value" ] = output ["noData" ]
869+ output ["maximum_value" ] = maximum_value
870+ output ["minimum_value" ] = minimum_value
871+ except :
872+ output ["maximum_value" ] = output ["noData" ]
873+ output ["minimum_value" ] = output ["noData" ]
874+ else :
875+ output ["maximum_value" ] = None
876+ output ["minimum_value" ] = None
864877
865878 xSize = sourceBand .XSize
866879 ySize = sourceBand .YSize
@@ -2509,7 +2522,7 @@ def warp(
25092522 """
25102523 # open source and get info
25112524 source = loadRaster (source )
2512- dsInfo = rasterInfo (source )
2525+ dsInfo = rasterInfo (sourceDS = source , compute_statistics = True )
25132526 if dsInfo .scale != 1.0 or dsInfo .offset != 0.0 :
25142527 isAdjusted = True
25152528 else :
0 commit comments