Skip to content

Commit 46cab53

Browse files
committed
Fix: OverflowError error when casting approx sum to integer
1 parent f9f5ff5 commit 46cab53

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

raster_loader/io/common.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ def get_color_table(raster_dataset: rasterio.io.DatasetReader, band: int):
185185
return None
186186

187187

188-
189188
def rasterio_metadata(
190189
file_path: str,
191190
bands_info: List[Tuple[int, str]],
@@ -424,7 +423,12 @@ def not_enough_samples():
424423
)
425424
if not raster_is_masked:
426425
for band in bands:
427-
not_masked_samples[band].append(sample[band - 1])
426+
band_sample = sample[band - 1]
427+
is_valid_sample = not (
428+
np.isinf(band_sample) or np.isnan(band_sample)
429+
)
430+
if is_valid_sample:
431+
not_masked_samples[band].append(band_sample)
428432

429433
iterations += 1
430434

@@ -507,8 +511,14 @@ def raster_band_approx_stats(
507511
_sum = 0
508512
sum_squares = 0
509513
if count > 0:
510-
_sum = int(np.sum(samples_band))
511-
sum_squares = int(np.sum(np.array(samples_band) ** 2))
514+
try:
515+
_sum = int(np.sum(samples_band))
516+
except (OverflowError, ValueError):
517+
_sum = 0
518+
try:
519+
sum_squares = int(np.sum(np.array(samples_band) ** 2))
520+
except (OverflowError, ValueError):
521+
sum_squares = 0
512522

513523
if basic_stats:
514524
quantiles = None

0 commit comments

Comments
 (0)