Skip to content

Commit

Permalink
Use external gdalinfo -checksum call to verify raster checksum
Browse files Browse the repository at this point in the history
This avoids a segfault during GDAL checksum killing the entire python process.
  • Loading branch information
ehusby committed Aug 22, 2023
1 parent e9c6dbb commit 8b117d6
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions batch_check_setsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logging
import os
import re
import shlex
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -2059,19 +2060,28 @@ def check_rasters(raster_ffiles, checkfile, args):
"All bands will be checked for valid SETSM data range."
]))

if args.get(ARGSTR_CHECK_METHOD) == ARGCHO_CHECK_METHOD_CHECKSUM:
cmd = "gdalinfo -checksum {}".format(raster_ffile)
LOGGER.debug("Running checksum with the following command:\n{}".format(cmd))
proc = subprocess.run(shlex.split(cmd), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
if proc.returncode != 0:
errmsg_print_and_list(errmsg_list,
"'gdalinfo -checksum' failure with return code {}".format(proc.returncode))
raise RasterFileReadError()

for band_index in range(num_bands):
band_num = band_index + 1
band = ds.GetRasterBand(band_num)
LOGGER.debug("Processing Band {}".format(band_num))

if args.get(ARGSTR_CHECK_METHOD) == ARGCHO_CHECK_METHOD_CHECKSUM:
try:
LOGGER.debug("Doing checksum")
checksum = band.Checksum()
LOGGER.debug("Checksum succeeded: {}".format(checksum))
except RuntimeError as e:
errmsg_print_and_list(errmsg_list,
"Band {} checksum error: {}".format(band_num, e))
# if args.get(ARGSTR_CHECK_METHOD) == ARGCHO_CHECK_METHOD_CHECKSUM:
# try:
# LOGGER.debug("Doing checksum")
# checksum = band.Checksum()
# LOGGER.debug("Checksum succeeded: {}".format(checksum))
# except RuntimeError as e:
# errmsg_print_and_list(errmsg_list,
# "Band {} checksum error: {}".format(band_num, e))

if args.get(ARGSTR_CHECK_METHOD) == ARGCHO_CHECK_METHOD_READ or setsm_suffix is not None:
try:
Expand Down

0 comments on commit 8b117d6

Please sign in to comment.