Skip to content

Commit

Permalink
add logging level argument suppress_rio_logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dugalh committed Jun 10, 2023
1 parent 58b6b39 commit 0aec16a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion geedim/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def download(self, session=None, response=None, bar: tqdm = None):

# read the geotiff with a rasterio memory file
env = rio.Env(GDAL_NUM_THREADS='ALL_CPUs', GTIFF_FORCE_RGBA=False)
with utils.suppress_rio_warn_logs(), env, MemoryFile(ext_buffer) as mem_file:
with utils.suppress_rio_logs(), env, MemoryFile(ext_buffer) as mem_file:
with mem_file.open() as ds:
array = ds.read()
if (array.dtype == np.dtype('float32')) or (array.dtype == np.dtype('float64')):
Expand Down
8 changes: 4 additions & 4 deletions geedim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ def split_id(image_id: str) -> Tuple[str, str]:


@contextmanager
def suppress_rio_warn_logs():
""" A context manager that sets the `rasterio` logging level to ERROR, then returns it to its original value. """
def suppress_rio_logs(level: int = logging.ERROR):
""" A context manager that sets the `rasterio` logging level, then returns it to its original value. """
try:
# GEE sets GeoTIFF `colorinterp` tags incorrectly. This suppresses `rasterio` warning relating to this:
# 'Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel'
rio_level = logging.getLogger('rasterio').getEffectiveLevel()
logging.getLogger('rasterio').setLevel(logging.ERROR)
logging.getLogger('rasterio').setLevel(level)
yield
finally:
logging.getLogger('rasterio').setLevel(rio_level)
Expand All @@ -137,7 +137,7 @@ def get_bounds(filename: pathlib.Path, expand: float = 5):
dict
Geojson polygon.
"""
with suppress_rio_warn_logs(), rio.Env(GTIFF_FORCE_RGBA=False), rio.open(filename) as im:
with suppress_rio_logs(), rio.Env(GTIFF_FORCE_RGBA=False), rio.open(filename) as im:
bbox = im.bounds
if (im.crs.linear_units == "metre") and (expand > 0): # expand the bounding box
expand_x = (bbox.right - bbox.left) * expand / 100.0
Expand Down

0 comments on commit 0aec16a

Please sign in to comment.