Skip to content

Commit 01d9885

Browse files
"Add flag to explicitly calculate raster statistics because its computationaly expensive" (#287)
1 parent d33a3b2 commit 01d9885

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

geokit/_algorithms/combineSimilarRasters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def checkSimilarRasters(
6363
datasets = _datasets
6464

6565
# get all raster infos
66-
infoDataset = [rasterInfo(d) for d in datasets]
66+
infoDataset = [rasterInfo(sourceDS=d, compute_statistics=True) for d in datasets]
6767
# check all relevant variables
6868
for rInfo in infoDataset[1:]:
6969
# same srs is required
@@ -182,7 +182,7 @@ def combineSimilarRasters(
182182
# GET REFERENCE CONTEXT FOR THE OUTPUT RASTER
183183

184184
# determine info for all datasets
185-
raster_info_list = [rasterInfo(d) for d in datasets]
185+
raster_info_list = [rasterInfo(sourceDS=d, compute_statistics=True) for d in datasets]
186186

187187
# get reference srs - are all the same thanks to checkSimilarRasters
188188
srs_ref = raster_info_list[0].srs

geokit/core/raster.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)