Compute statistics on the pixel values of this band. The return value is a tuple with the following structure: (minimum, maximum, mean, standard deviation). If approximate=True, the statistics may be computed based on overviews or a subset of image tiles.
(self, refresh=False, approximate=False)
| 62 | _stats_refresh = False |
| 63 | |
| 64 | def statistics(self, refresh=False, approximate=False): |
| 65 | """ |
| 66 | Compute statistics on the pixel values of this band. |
| 67 | |
| 68 | The return value is a tuple with the following structure: |
| 69 | (minimum, maximum, mean, standard deviation). |
| 70 | |
| 71 | If approximate=True, the statistics may be computed based on overviews |
| 72 | or a subset of image tiles. |
| 73 | |
| 74 | If refresh=True, the statistics will be computed from the data |
| 75 | directly, and the cache will be updated where applicable. |
| 76 | |
| 77 | For empty bands (where all pixel values are nodata), all statistics |
| 78 | values are returned as None. |
| 79 | |
| 80 | For raster formats using Persistent Auxiliary Metadata (PAM) services, |
| 81 | the statistics might be cached in an auxiliary file. |
| 82 | """ |
| 83 | # Prepare array with arguments for capi function |
| 84 | smin, smax, smean, sstd = c_double(), c_double(), c_double(), c_double() |
| 85 | stats_args = [ |
| 86 | self._ptr, |
| 87 | c_int(approximate), |
| 88 | byref(smin), |
| 89 | byref(smax), |
| 90 | byref(smean), |
| 91 | byref(sstd), |
| 92 | c_void_p(), |
| 93 | c_void_p(), |
| 94 | ] |
| 95 | |
| 96 | if refresh or self._stats_refresh: |
| 97 | func = capi.compute_band_statistics |
| 98 | else: |
| 99 | # Add additional argument to force computation if there is no |
| 100 | # existing PAM file to take the values from. |
| 101 | force = True |
| 102 | stats_args.insert(2, c_int(force)) |
| 103 | func = capi.get_band_statistics |
| 104 | |
| 105 | # Computation of statistics fails for empty bands. |
| 106 | try: |
| 107 | func(*stats_args) |
| 108 | result = smin.value, smax.value, smean.value, sstd.value |
| 109 | except GDALException: |
| 110 | result = (None, None, None, None) |
| 111 | |
| 112 | self._stats_refresh = False |
| 113 | |
| 114 | return result |
| 115 | |
| 116 | @property |
| 117 | def min(self): |