Skip to content

Commit

Permalink
Merge pull request #48 from dbaston/fix_10010_decorator
Browse files Browse the repository at this point in the history
Python scripts: Use decorator to enable exceptions
  • Loading branch information
rouault authored May 28, 2024
2 parents 39d5a57 + de9fd31 commit 0187ae9
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 272 deletions.
12 changes: 11 additions & 1 deletion swig/python/gdal-utils/osgeo_utils/auxiliary/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple, Union

from osgeo import __version__ as gdal_version_str
from osgeo import gdal
from osgeo import gdal, ogr, osr
from osgeo_utils.auxiliary.base import (
MaybeSequence,
OptionalBoolStr,
Expand All @@ -54,6 +54,16 @@
]


def enable_gdal_exceptions(fun):
def enable_exceptions_wrapper(*args, **kwargs):
with gdal.ExceptionMgr(useExceptions=True), osr.ExceptionMgr(
useExceptions=True
), ogr.ExceptionMgr(useExceptions=True):
return fun(*args, **kwargs)

return enable_exceptions_wrapper


def DoesDriverHandleExtension(drv: gdal.Driver, ext: str) -> bool:
exts = drv.GetMetadataItem(gdal.DMD_EXTENSIONS)
return exts is not None and ext.lower() in exts.lower().split(" ")
Expand Down
22 changes: 3 additions & 19 deletions swig/python/gdal-utils/osgeo_utils/gdal2tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from xml.etree import ElementTree

from osgeo import gdal, osr
from osgeo_utils.auxiliary.util import enable_gdal_exceptions

Options = Any

Expand Down Expand Up @@ -4585,20 +4586,9 @@ def single_threaded_tiling(
shutil.rmtree(os.path.dirname(conf.src_file))


@enable_gdal_exceptions
def multi_threaded_tiling(
input_file: str, output_folder: str, options: Options, pool
) -> None:
with gdal.ExceptionMgr(), osr.ExceptionMgr():
return _multi_threaded_tiling(
input_file=input_file,
output_folder=output_folder,
options=options,
pool=pool,
)


def _multi_threaded_tiling(
input_file: str, output_folder: str, options: Options, pool
) -> None:
nb_processes = options.nb_processes or 1

Expand Down Expand Up @@ -4696,14 +4686,8 @@ def main(argv: List[str] = sys.argv, called_from_main=False) -> int:
return submain(argv, called_from_main=called_from_main)


@enable_gdal_exceptions
def submain(argv: List[str], pool=None, pool_size=0, called_from_main=False) -> int:
with gdal.ExceptionMgr(), osr.ExceptionMgr():
return _submain(
argv=argv, pool=pool, pool_size=pool_size, called_from_main=called_from_main
)


def _submain(argv: List[str], pool=None, pool_size=0, called_from_main=False) -> int:

argv = gdal.GeneralCmdLineProcessor(argv)
if argv is None:
Expand Down
39 changes: 7 additions & 32 deletions swig/python/gdal-utils/osgeo_utils/gdal2xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@
OptionalProgressCallback,
get_progress_callback,
)
from osgeo_utils.auxiliary.util import PathOrDS, get_bands, open_ds
from osgeo_utils.auxiliary.util import (
PathOrDS,
enable_gdal_exceptions,
get_bands,
open_ds,
)


@enable_gdal_exceptions
def gdal2xyz(
srcfile: PathOrDS,
dstfile: PathLikeOrStr = None,
Expand All @@ -60,37 +66,6 @@ def gdal2xyz(
return_np_arrays: bool = False,
pre_allocate_np_arrays: bool = True,
progress_callback: OptionalProgressCallback = ...,
) -> Optional[Tuple]:
with gdal.ExceptionMgr():
return _gdal2xyz(
srcfile=srcfile,
dstfile=dstfile,
srcwin=srcwin,
skip=skip,
band_nums=band_nums,
delim=delim,
skip_nodata=skip_nodata,
src_nodata=src_nodata,
dst_nodata=dst_nodata,
return_np_arrays=return_np_arrays,
pre_allocate_np_arrays=pre_allocate_np_arrays,
progress_callback=progress_callback,
)


def _gdal2xyz(
srcfile: PathOrDS,
dstfile: PathLikeOrStr = None,
srcwin: Optional[Sequence[int]] = None,
skip: Union[int, Sequence[int]] = 1,
band_nums: Optional[Sequence[int]] = None,
delim: str = " ",
skip_nodata: bool = False,
src_nodata: Optional[Union[Sequence, Number]] = None,
dst_nodata: Optional[Union[Sequence, Number]] = None,
return_np_arrays: bool = False,
pre_allocate_np_arrays: bool = True,
progress_callback: OptionalProgressCallback = ...,
) -> Optional[Tuple]:
"""
translates a raster file (or dataset) into xyz format
Expand Down
50 changes: 6 additions & 44 deletions swig/python/gdal-utils/osgeo_utils/gdal_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@
from osgeo_utils.auxiliary.extent_util import GT, Extent
from osgeo_utils.auxiliary.gdal_argparse import GDALArgumentParser, GDALScript
from osgeo_utils.auxiliary.rectangle import GeoRectangle
from osgeo_utils.auxiliary.util import GetOutputDriverFor, open_ds
from osgeo_utils.auxiliary.util import (
GetOutputDriverFor,
enable_gdal_exceptions,
open_ds,
)

GDALDataType = int

Expand Down Expand Up @@ -104,6 +108,7 @@
"""


@enable_gdal_exceptions
def Calc(
calc: MaybeSequence[str],
outfile: Optional[PathLikeOrStr] = None,
Expand All @@ -124,49 +129,6 @@ def Calc(
progress_callback: Optional = gdal.TermProgress_nocb,
**input_files,
):
with gdal.ExceptionMgr():
return _Calc(
calc=calc,
outfile=outfile,
NoDataValue=NoDataValue,
type=type,
format=format,
creation_options=creation_options,
allBands=allBands,
overwrite=overwrite,
hideNoData=hideNoData,
projectionCheck=projectionCheck,
color_table=color_table,
extent=extent,
projwin=projwin,
user_namespace=user_namespace,
debug=debug,
quiet=quiet,
progress_callback=progress_callback,
**input_files,
)


def _Calc(
calc: MaybeSequence[str],
outfile: Optional[PathLikeOrStr] = None,
NoDataValue: Optional[Number] = None,
type: Optional[Union[GDALDataType, str]] = None,
format: Optional[str] = None,
creation_options: Optional[Sequence[str]] = None,
allBands: str = "",
overwrite: bool = False,
hideNoData: bool = False,
projectionCheck: bool = False,
color_table: Optional[ColorTableLike] = None,
extent: Optional[Extent] = None,
projwin: Optional[Union[Tuple, GeoRectangle]] = None,
user_namespace: Optional[Dict] = None,
debug: bool = False,
quiet: bool = False,
progress_callback: Optional = gdal.TermProgress_nocb,
**input_files,
):

if debug:
print(f"gdal_calc.py starting calculation {calc}")
Expand Down
7 changes: 2 additions & 5 deletions swig/python/gdal-utils/osgeo_utils/gdal_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import sys

from osgeo import gdal, osr
from osgeo_utils.auxiliary.util import enable_gdal_exceptions


def Usage(isError):
Expand Down Expand Up @@ -86,12 +87,8 @@ def ArgIsNumeric(s):
return True


@enable_gdal_exceptions
def gdal_edit(argv):
with gdal.ExceptionMgr(), osr.ExceptionMgr():
return _gdal_edit(argv)


def _gdal_edit(argv):

argv = gdal.GeneralCmdLineProcessor(argv)
if argv is None:
Expand Down
31 changes: 2 additions & 29 deletions swig/python/gdal-utils/osgeo_utils/gdal_fillnodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

from osgeo import gdal
from osgeo_utils.auxiliary.gdal_argparse import GDALArgumentParser, GDALScript
from osgeo_utils.auxiliary.util import enable_gdal_exceptions


def CopyBand(srcband, dstband):
Expand All @@ -47,6 +48,7 @@ def CopyBand(srcband, dstband):
)


@enable_gdal_exceptions
def gdal_fillnodata(
src_filename: Optional[str] = None,
band_number: int = 1,
Expand All @@ -59,35 +61,6 @@ def gdal_fillnodata(
smoothing_iterations: int = 0,
interpolation: Optional[str] = None,
options: Optional[list] = None,
):
with gdal.ExceptionMgr():
return _gdal_fillnodata(
src_filename=src_filename,
band_number=band_number,
dst_filename=dst_filename,
driver_name=driver_name,
creation_options=creation_options,
quiet=quiet,
mask=mask,
max_distance=max_distance,
smoothing_iterations=smoothing_iterations,
interpolation=interpolation,
options=options,
)


def _gdal_fillnodata(
src_filename: Optional[str] = None,
band_number: int = 1,
dst_filename: Optional[str] = None,
driver_name: str = "GTiff",
creation_options: Optional[list] = None,
quiet: bool = False,
mask: str = "default",
max_distance: Real = 100,
smoothing_iterations: int = 0,
interpolation: Optional[str] = None,
options: Optional[list] = None,
):
options = options or []
creation_options = creation_options or []
Expand Down
8 changes: 2 additions & 6 deletions swig/python/gdal-utils/osgeo_utils/gdal_pansharpen.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from typing import List, Optional, Sequence, Union

from osgeo import gdal
from osgeo_utils.auxiliary.util import GetOutputDriverFor
from osgeo_utils.auxiliary.util import GetOutputDriverFor, enable_gdal_exceptions


def Usage(isError):
Expand Down Expand Up @@ -68,12 +68,8 @@ def Usage(isError):
return 2 if isError else 0


@enable_gdal_exceptions
def main(argv=None):
with gdal.ExceptionMgr():
return _main(argv=argv)


def _main(argv=sys.argv):

argv = gdal.GeneralCmdLineProcessor(argv)
if argv is None:
Expand Down
34 changes: 2 additions & 32 deletions swig/python/gdal-utils/osgeo_utils/gdal_polygonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@

from osgeo import gdal, ogr
from osgeo_utils.auxiliary.gdal_argparse import GDALArgumentParser, GDALScript
from osgeo_utils.auxiliary.util import GetOutputDriverFor
from osgeo_utils.auxiliary.util import GetOutputDriverFor, enable_gdal_exceptions


@enable_gdal_exceptions
def gdal_polygonize(
src_filename: Optional[str] = None,
band_number: Union[int, str] = 1,
Expand All @@ -54,37 +55,6 @@ def gdal_polygonize(
layer_creation_options: Optional[list] = None,
connectedness8: bool = False,
):
with gdal.ExceptionMgr(), ogr.ExceptionMgr():
return _gdal_polygonize(
src_filename=src_filename,
band_number=band_number,
dst_filename=dst_filename,
overwrite=overwrite,
driver_name=driver_name,
dst_layername=dst_layername,
dst_fieldname=dst_fieldname,
quiet=quiet,
mask=mask,
options=options,
layer_creation_options=layer_creation_options,
connectedness8=connectedness8,
)


def _gdal_polygonize(
src_filename: Optional[str] = None,
band_number: Union[int, str] = 1,
dst_filename: Optional[str] = None,
overwrite: bool = False,
driver_name: Optional[str] = None,
dst_layername: Optional[str] = None,
dst_fieldname: Optional[str] = None,
quiet: bool = False,
mask: str = "default",
options: Optional[list] = None,
layer_creation_options: Optional[list] = None,
connectedness8: bool = False,
):

if isinstance(band_number, str) and not band_number.startswith("mask"):
band_number = int(band_number)
Expand Down
7 changes: 2 additions & 5 deletions swig/python/gdal-utils/osgeo_utils/gdal_proximity.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from typing import Optional, Sequence

from osgeo import gdal
from osgeo_utils.auxiliary.util import GetOutputDriverFor
from osgeo_utils.auxiliary.util import GetOutputDriverFor, enable_gdal_exceptions


def Usage(isError):
Expand All @@ -54,12 +54,9 @@ def Usage(isError):
return 2 if isError else 0


@enable_gdal_exceptions
def main(argv=sys.argv):
with gdal.ExceptionMgr():
return _main(argv=argv)


def _main(argv=sys.argv):
driver_name = None
creation_options = []
alg_options = []
Expand Down
7 changes: 2 additions & 5 deletions swig/python/gdal-utils/osgeo_utils/gdal_retile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import sys

from osgeo import gdal, ogr, osr
from osgeo_utils.auxiliary.util import enable_gdal_exceptions

progress = gdal.TermProgress_nocb

Expand Down Expand Up @@ -941,12 +942,8 @@ def Usage(isError):
return 2 if isError else 0


@enable_gdal_exceptions
def main(args=None, g=None):
with gdal.ExceptionMgr(), ogr.ExceptionMgr(), osr.ExceptionMgr():
return _main(args=args, g=g)


def _main(args=None, g=None):

if g is None:
g = RetileGlobals()
Expand Down
Loading

0 comments on commit 0187ae9

Please sign in to comment.