geemap icon indicating copy to clipboard operation
geemap copied to clipboard

Inconsistent use of gdal

Open schwehr opened this issue 1 year ago • 1 comments

Here https://github.com/gee-community/geemap/blob/9c990baa21d62337ead2c6a6fbd58e0a74038a6f/geemap/common.py#L10794

    from osgeo import gdal
    import rasterio

    # ... and suppress errors
    gdal.PushErrorHandler("CPLQuietErrorHandler")

Whereas here https://github.com/gee-community/geemap/blob/9c990baa21d62337ead2c6a6fbd58e0a74038a6f/geemap/common.py#L15128

    try:
        from osgeo import gdal, osr
    except ImportError:
        raise ImportError("GDAL is not installed. Install it with pip install GDAL")

# [SNIP]

    gdal.UseExceptions()
  1. One of the importss is wrapped in a try, but the others are not. I suggest dropping the try/except wrapping.
  2. The UseExceptions is great as that will become the default behavior of GDAL in the future. I suggest always doing that right after the imports each time there is a from osgeo import
  3. The gdal.PushErrorHandler calls are missing the matching gdal.PopErrorHandler(). You are welcome to use this context manager if it makes things easier. https://github.com/schwehr/gdal-autotest2/blob/577974592837fdfc18fd90b338cb657f1f2332bd/python/gcore/gcore_util.py#L50
@contextlib.contextmanager
def ErrorHandler(error_name):
  handler = gdal.PushErrorHandler(error_name)
  try:
    yield handler
  finally:
    gdal.PopErrorHandler()

schwehr avatar Dec 31 '23 17:12 schwehr

#1867 wasn't quite right. I will try to make a PR this week that does what I think was intended. Sorry that my explanation above wasn't clear.

schwehr avatar Jan 07 '24 17:01 schwehr