Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GDAL error handler #2045

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions geemap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11636,16 +11636,26 @@ def image_to_numpy(image):
"""
import rasterio
from osgeo import gdal
from contextlib import contextmanager

@contextmanager
def gdal_error_handler():
"""Context manager for GDAL error handler."""
gdal.PushErrorHandler("CPLQuietErrorHandler")
try:
yield
finally:
gdal.PopErrorHandler()

gdal.UseExceptions()

gdal.PushErrorHandler("CPLQuietErrorHandler")
with gdal_error_handler():

if not os.path.exists(image):
raise FileNotFoundError("The provided input file could not be found.")
if not os.path.exists(image):
raise FileNotFoundError("The provided input file could not be found.")

with rasterio.open(image, "r") as ds:
arr = ds.read() # read all raster values
with rasterio.open(image, "r") as ds:
arr = ds.read() # read all raster values

return arr

Expand Down
Loading