Skip to content

Commit

Permalink
python/plugins: turn on GDAL exceptions to avoid deprecation warning
Browse files Browse the repository at this point in the history
Fixes #57344
  • Loading branch information
rouault committed May 17, 2024
1 parent dd01d11 commit 090bb25
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 25 deletions.
31 changes: 17 additions & 14 deletions python/plugins/db_manager/db_plugins/gpkg/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import sqlite3

from osgeo import gdal, ogr, osr
gdal.UseExceptions()
ogr.UseExceptions()


def classFactory():
Expand Down Expand Up @@ -75,11 +77,13 @@ def _opendb(self):
# Keep this explicit assignment to None to make sure the file is
# properly closed before being re-opened
self.gdal_ds = None
self.gdal_ds = gdal.OpenEx(self.dbname, gdal.OF_UPDATE)
if self.gdal_ds is None:
self.gdal_ds = gdal.OpenEx(self.dbname)
if self.gdal_ds is None:
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{0}" not found').format(self.dbname))
try:
self.gdal_ds = gdal.OpenEx(self.dbname, gdal.OF_UPDATE)
except Exception:
try:
self.gdal_ds = gdal.OpenEx(self.dbname)
except Exception:
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{0}" not found').format(self.dbname))
if self.gdal_ds.GetDriver().ShortName != 'GPKG':
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{dbname}" not recognized as GPKG ({shortname} reported instead.)').format(dbname=self.dbname, shortname=self.gdal_ds.GetDriver().ShortName))
self.has_raster = self.gdal_ds.RasterCount != 0 or self.gdal_ds.GetMetadata('SUBDATASETS') is not None
Expand Down Expand Up @@ -186,15 +190,11 @@ def cancel(self):

@classmethod
def isValidDatabase(cls, path):
if hasattr(gdal, 'OpenEx'):
try:
ds = gdal.OpenEx(path)
if ds is None or ds.GetDriver().ShortName != 'GPKG':
return False
else:
ds = ogr.Open(path)
if ds is None or ds.GetDriver().GetName() != 'GPKG':
return False
return True
except Exception:
return False
return ds.GetDriver().ShortName == 'GPKG'

def getInfo(self):
return None
Expand Down Expand Up @@ -442,7 +442,10 @@ def getTableExtent(self, table, geom, force=False):
ds = self.gdal_ds
else:
subdataset_name = 'GPKG:%s:%s' % (self.gdal_ds.GetDescription(), tablename)
ds = gdal.Open(subdataset_name)
try:
ds = gdal.Open(subdataset_name)
except Exception:
ds = None
if ds is None:
return None
gt = ds.GetGeoTransform()
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/grassprovider/tests/AlgorithmsTestBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

import processing

gdal.UseExceptions()


def processingTestDataPath():
return os.path.join(os.path.dirname(__file__), 'testdata')
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))

gdal.UseExceptions()


class GdalAlgorithmProvider(QgsProcessingProvider):

Expand Down
15 changes: 6 additions & 9 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

import psycopg2

with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
from osgeo import ogr

from qgis.core import (Qgis,
QgsBlockingProcess,
QgsRunProcess,
Expand All @@ -55,9 +51,9 @@
from processing.tools.system import isWindows, isMac

try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
from osgeo import gdal # NOQA
from osgeo import gdal, ogr
gdal.UseExceptions()
ogr.UseExceptions()

gdalAvailable = True
except:
Expand Down Expand Up @@ -436,8 +432,9 @@ def ogrLayerName(uri):
if f.startswith('layerid='):
layerid = int(f.split('=')[1])

ds = ogr.Open(basePath)
if not ds:
try:
ds = ogr.Open(basePath)
except Exception:
return None

ly = ds.GetLayer(layerid)
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/processing/algs/gdal/extractprojection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

gdal.UseExceptions()
osr.UseExceptions()


class ExtractProjection(GdalAlgorithm):
INPUT = 'INPUT'
Expand Down
4 changes: 4 additions & 0 deletions python/plugins/processing/algs/qgis/HypsometricCurves.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

from osgeo import gdal, ogr, osr

gdal.UseExceptions()
ogr.UseExceptions()
osr.UseExceptions()

from qgis.core import (QgsRectangle,
QgsGeometry,
QgsFeatureRequest,
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/qgis/PointsFromLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
from processing.tools import raster
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm

gdal.UseExceptions()


class PointsFromLines(QgisAlgorithm):
INPUT_RASTER = 'INPUT_RASTER'
Expand Down
7 changes: 5 additions & 2 deletions python/plugins/processing/gui/TestTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
from qgis.PyQt.QtCore import QCoreApplication, QMetaObject
from qgis.PyQt.QtWidgets import QDialog, QVBoxLayout, QTextEdit, QMessageBox

gdal.UseExceptions()


def extractSchemaPath(filepath):
"""
Expand Down Expand Up @@ -264,8 +266,9 @@ def createTest(text):
'files'))
return

dataset = gdal.Open(token, GA_ReadOnly)
if dataset is None:
try:
dataset = gdal.Open(token, GA_ReadOnly)
except Exception:
QMessageBox.warning(None,
tr('Error'),
tr('Seems some outputs are temporary '
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/tests/AlgorithmsTestBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

import processing

gdal.UseExceptions()


def GDAL_COMPUTE_VERSION(maj, min, rev):
return ((maj) * 1000000 + (min) * 10000 + (rev) * 100)
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/tools/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

from qgis.core import QgsProcessingException

gdal.UseExceptions()


def scanraster(layer, feedback, band_number=1):
filename = str(layer.source())
Expand Down

0 comments on commit 090bb25

Please sign in to comment.