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

code fix calling gdal.UseExceptions #5272

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 16 additions & 6 deletions gui/wxpython/core/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
from core.gthread import gThread

try:
haveGdal = True
from osgeo import gdal

gdal.UseExceptions()
haveGdal = True
except ImportError:
haveGdal = False


from grass.pydispatch.signal import Signal


Expand Down Expand Up @@ -300,20 +303,27 @@ def __init__(self, targetFile, region, bandsNum, gdalDriver, fillValue=None):
)

driver = gdal.GetDriverByName(self.gdalDrvType)
if not driver:
grass.fatal(f"GDAL driver {self.gdalDrvType} not found.")

self.tDataset = driver.Create(
targetFile, region["cols"], region["rows"], bandsNum, gdal.GDT_Byte
)
if self.tDataset is None:
grass.fatal("Failed to create target dataset.")

if fillValue is not None:
# fill raster bands with a constant value
for iBand in range(1, self.tDataset.RasterCount + 1):
self.tDataset.GetRasterBand(iBand).Fill(fillValue)

def AddRasterBands(self, sourceFile, sTBands):
"""Add raster bands from sourceFile into the merging raster."""
def AddRasterBands(self, sourceFile, sTBands):
"""Add raster bands from sourceFile into the merging raster."""

try:
sDataset = gdal.Open(sourceFile, gdal.GA_ReadOnly)
if sDataset is None:
return
except RuntimeError as e:
grass.warning(f"AddRasterBands Warning: {e}")
return

sGeotransform = sDataset.GetGeoTransform()

Expand Down
21 changes: 12 additions & 9 deletions gui/wxpython/gui_core/gselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2623,17 +2623,20 @@ def _getRasterliteDBRasters(self, dsn):
"""Get Rasterlite DB rasters

:param str dsn: Rasterlite DB data source name

:return list: list of Rasterlite DB rasters
"""
try:
from osgeo import gdal
except ImportError:
GError(
parent=self,
message=_("The Python GDAL package is missing. Please install it."),
)
return []

try:
rasterlite = gdal.Open(dsn)
if rasterlite is None:
raise RuntimeError(f"Failed to open Rasterlite database: {dsn}")

rasters = rasterlite.GetSubDatasets()
return rasters
except RuntimeError as e:
GError(parent=self, message=f"GDAL error while opening Rasterlite DB: {e}")
return []

rasterlite = gdal.Open(dsn)
rasters = rasterlite.GetSubDatasets()
if rasters:
Expand Down
123 changes: 74 additions & 49 deletions gui/wxpython/modules/import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,9 @@ def reload(self, data, listData):
self.list.SelectAll(select=True)
self.layersData = data

def OnRun(self, event):
"""Import/Link data (each layes as separate vector map)"""
def OnRun(self, event):
"""Import/Link data (each layer as separate vector map)"""

self.commandId = -1
data = self.list.GetLayers()

Expand All @@ -485,66 +486,90 @@ def OnRun(self, event):
if not dsn:
return

# Enable GDAL exceptions once at the start
try:
from osgeo import gdal

gdal.UseExceptions()
except ImportError:
GError(
parent=self,
message=_("The Python GDAL package is missing. Please install it."),
)
return

for layer, output, listId in data:
userData = {}
idsn = dsn # Default value

try:
# Check if dataset exists first
if not os.path.exists(dsn):
GError(parent=self, message=f"Dataset does not exist: {dsn}")
return

dataset = gdal.Open(dsn)
if dataset is None:
raise RuntimeError(f"Failed to open dataset: {dsn}")

if self.dsnInput.GetType() == "dir":
idsn = os.path.join(dsn, layer)
elif self.dsnInput.GetType() == "db":
idsn = dsn
if "PG:" in dsn:
idsn = f"{dsn} table={layer}"
elif os.path.exists(idsn):
try:
from osgeo import gdal
except ImportError:
GError(
parent=self,
message=_(
"The Python GDAL package is missing. Please install it."
),
)
return
dataset = gdal.Open(dsn)
if "Rasterlite" in dataset.GetDriver().ShortName:
else:
driver_name = dataset.GetDriver().ShortName
if "Rasterlite" in driver_name:
idsn = f"RASTERLITE:{dsn},table={layer}"
else:
idsn = dsn

# check number of bands
nBandsStr = RunCommand("r.in.gdal", flags="p", input=idsn, read=True)
nBands = -1
if nBandsStr:
try:
nBands = int(nBandsStr.rstrip("\n"))
except ValueError:
pass
if nBands < 0:
GWarning(_("Unable to determine number of raster bands"), parent=self)
nBands = 1

userData["nbands"] = nBands
cmd = self.getSettingsPageCmd()
cmd.append("input=%s" % idsn)
cmd.append("output=%s" % output)

if self.override.IsChecked():
cmd.append("-o")
# Validate dataset again for `idsn`
if not os.path.exists(idsn):
GError(parent=self, message=f"Dataset does not exist: {idsn}")
return

if self.overwrite.IsChecked():
cmd.append("--overwrite")
dataset = gdal.Open(idsn)
if dataset is None:
raise RuntimeError(f"Failed to open dataset: {idsn}")

except RuntimeError as e:
GError(parent=self, message=f"GDAL error while opening dataset: {e}")
return

# check number of bands
nBandsStr = RunCommand("r.in.gdal", flags="p", input=idsn, read=True)
nBands = -1
# noqa: W293
if nBandsStr:
try:
nBands = int(nBandsStr.rstrip("\n"))
except ValueError:
pass
if nBands < 0:
GWarning(_("Unable to determine number of raster bands"), parent=self)
nBands = 1

userData["nbands"] = nBands
cmd = self.getSettingsPageCmd()
cmd.append("input=%s" % idsn)
cmd.append("output=%s" % output)

if self.override.IsChecked():
cmd.append("-o")

if (
UserSettings.Get(group="cmd", key="overwrite", subkey="enabled")
and "--overwrite" not in cmd
):
cmd.append("--overwrite")
if self.overwrite.IsChecked():
cmd.append("--overwrite")

self._addToCommandQueue()
# run in Layer Manager
self._giface.RunCmd(
cmd, onDone=self.OnCmdDone, userData=userData, addLayer=False
)
if (
UserSettings.Get(group="cmd", key="overwrite", subkey="enabled")
and "--overwrite" not in cmd
):
cmd.append("--overwrite")

self._addToCommandQueue()
# run in Layer Manager
self._giface.RunCmd(
cmd, onDone=self.OnCmdDone, userData=userData, addLayer=False
)

def OnCmdDone(self, event):
"""Load layers and close if required"""
Expand Down
2 changes: 2 additions & 0 deletions scripts/r.in.wms/wms_drv.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

try:
from osgeo import gdal

gdal.UseExceptions()
except ImportError:
gs.fatal(
_(
Expand Down
11 changes: 7 additions & 4 deletions scripts/r.in.wms/wms_gdal_drv.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,13 @@ def _download(self):
gdal.SetConfigOption("GDAL_HTTP_PROXY", str(self.proxy))
if self.proxy_user_pw:
gdal.SetConfigOption("GDAL_HTTP_PROXYUSERPWD", str(self.proxy_user_pw))
wms_dataset = gdal.Open(xml_file, gdal.GA_ReadOnly)
gs.try_remove(xml_file)
if wms_dataset is None:
gs.fatal(_("Unable to open GDAL WMS driver"))

try:
driver = gdal.GetDriverByName(self.gdal_drv_format)
if driver is None:
raise RuntimeError(f"Unable to find {self.gdal_drv_format} driver")
except RuntimeError as e:
gs.fatal(f"GDAL Driver Error: {e}")

self._debug("_download", "GDAL dataset created")

Expand Down
22 changes: 10 additions & 12 deletions scripts/v.import/v.import.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,16 @@ def main():

# create temp location from input without import
gs.verbose(_("Creating temporary project for <%s>...") % OGRdatasource)

try:
if OGRdatasource.lower().endswith("gml"):
try:
from osgeo import gdal
except ImportError:
gs.fatal(
_(
"Unable to load GDAL Python bindings (requires package "
"'python-gdal' being installed)"
)
)
if int(gdal.VersionInfo("VERSION_NUM")) < GDAL_COMPUTE_VERSION(2, 4, 1):
fix_gfsfile(OGRdatasource)
from osgeo import gdal

gdal.UseExceptions() # Proper indentation for GDAL config

if int(gdal.VersionInfo("VERSION_NUM")) < GDAL_COMPUTE_VERSION(2, 4, 1):
fix_gfsfile(OGRdatasource)

gs.run_command(
"v.in.ogr",
input=OGRdatasource,
Expand All @@ -281,9 +278,10 @@ def main():
overwrite=overwrite,
**vopts,
)
except RuntimeError as e:
gs.fatal(f"GDAL error while checking version: {e}")
except CalledModuleError:
gs.fatal(_("Unable to create project from OGR datasource <%s>") % OGRdatasource)

# switch to temp location
os.environ["GISRC"] = str(SRCGISRC)

Expand Down
Loading