Skip to content

Commit

Permalink
Make consistent use of gdal (#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Jan 5, 2024
1 parent 620c3f9 commit eb01060
Showing 1 changed file with 25 additions and 17 deletions.
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

0 comments on commit eb01060

Please sign in to comment.