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

Make consistent use of gdal #1867

Merged
merged 1 commit into from
Jan 5, 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
42 changes: 25 additions & 17 deletions geemap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10790,6 +10790,8 @@ def get_local_tile_layer(
from osgeo import gdal
import rasterio

gdal.UseExceptions()

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

Expand Down Expand Up @@ -11583,7 +11585,14 @@ def image_to_numpy(image):
import rasterio
from osgeo import gdal

gdal.PushErrorHandler("CPLQuietErrorHandler")
gdal.UseExceptions()

handler = gdal.PushErrorHandler("CPLQuietErrorHandler")

try:
yield handler
finally:
gdal.PopErrorHandler()

if not os.path.exists(image):
raise FileNotFoundError("The provided input file could not be found.")
Expand Down Expand Up @@ -15125,10 +15134,9 @@ def tms_to_geotiff(
import numpy
from PIL import Image

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

gdal.UseExceptions()

try:
import httpx
Expand Down Expand Up @@ -15207,7 +15215,6 @@ def resolution_to_zoom_level(resolution):

Image.MAX_IMAGE_PIXELS = None

gdal.UseExceptions()
web_mercator = osr.SpatialReference()
web_mercator.ImportFromEPSG(3857)

Expand Down Expand Up @@ -15387,17 +15394,16 @@ def tif_to_jp2(filename, output, creationOptions=None):

"""

from osgeo import gdal

gdal.UseExceptions()

if not os.path.exists(filename):
raise Exception(f"File {filename} does not exist")

if not output.endswith(".jp2"):
output += ".jp2"

try:
from osgeo import gdal
except ImportError:
raise ImportError("GDAL is required to convert GeoTIFF to JPEG2000")

in_ds = gdal.Open(filename)
gdal.Translate(output, in_ds, format="JP2OpenJPEG", creationOptions=creationOptions)
in_ds = None
Expand Down Expand Up @@ -15869,9 +15875,10 @@ def array_to_memory_file(
if coords[0] == "time":
x_dim = coords[1]
y_dim = coords[2]
array = (
array.isel(time=0).rename({y_dim: "y", x_dim: "x"}).transpose("y", "x")
)
if array.dims[0] == "time":
array = array.isel(time=0)

array = array.rename({y_dim: "y", x_dim: "x"}).transpose("y", "x")
array = array.values

if array.ndim == 3 and transpose:
Expand Down Expand Up @@ -16005,9 +16012,10 @@ def array_to_image(
if coords[0] == "time":
x_dim = coords[1]
y_dim = coords[2]
array = (
array.isel(time=0).rename({y_dim: "y", x_dim: "x"}).transpose("y", "x")
)
if array.dims[0] == "time":
array = array.isel(time=0)

array = array.rename({y_dim: "y", x_dim: "x"}).transpose("y", "x")
array = array.values

if array.ndim == 3 and transpose:
Expand Down