mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-08 00:38:10 -05:00
[DB Manager / GPKG] Remove GDAL 1.x support
This commit is contained in:
parent
4eed39b63c
commit
3d9ca40f4e
@ -51,31 +51,13 @@ class GPKGDBConnector(DBConnector):
|
|||||||
|
|
||||||
def _opendb(self):
|
def _opendb(self):
|
||||||
|
|
||||||
self.gdal_ds = None
|
self.gdal_ds = gdal.OpenEx(self.dbname, gdal.OF_UPDATE)
|
||||||
if hasattr(gdal, 'OpenEx'):
|
if self.gdal_ds is None:
|
||||||
# GDAL >= 2
|
self.gdal_ds = gdal.OpenEx(self.dbname)
|
||||||
self.gdal_ds = gdal.OpenEx(self.dbname, gdal.OF_UPDATE)
|
if self.gdal_ds is None or self.gdal_ds.GetDriver().ShortName != 'GPKG':
|
||||||
if self.gdal_ds is None:
|
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{0}" not found').format(self.dbname))
|
||||||
self.gdal_ds = gdal.OpenEx(self.dbname)
|
self.has_raster = self.gdal_ds.RasterCount != 0 or self.gdal_ds.GetMetadata('SUBDATASETS') is not None
|
||||||
if self.gdal_ds is None or self.gdal_ds.GetDriver().ShortName != 'GPKG':
|
self.connection = None
|
||||||
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{0}" not found').format(self.dbname))
|
|
||||||
self.has_raster = self.gdal_ds.RasterCount != 0 or self.gdal_ds.GetMetadata('SUBDATASETS') is not None
|
|
||||||
self.connection = None
|
|
||||||
self.gdal2 = True
|
|
||||||
else:
|
|
||||||
# GDAL 1.X compat. To be removed at some point
|
|
||||||
self.gdal_ds = ogr.Open(self.dbname, update=1)
|
|
||||||
if self.gdal_ds is None:
|
|
||||||
self.gdal_ds = ogr.Open(self.dbname)
|
|
||||||
if self.gdal_ds is None or self.gdal_ds.GetDriver().GetName() != 'GPKG':
|
|
||||||
raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{0}" not found').format(self.dbname))
|
|
||||||
# For GDAL 1.X, we cannot issue direct SQL SELECT to the OGR datasource
|
|
||||||
# so we need a direct sqlite connection
|
|
||||||
try:
|
|
||||||
self.connection = spatialite_connect(str(self.dbname))
|
|
||||||
except self.connection_error_types() as e:
|
|
||||||
raise ConnectionError(e)
|
|
||||||
self.gdal2 = False
|
|
||||||
|
|
||||||
def unquoteId(self, quotedId):
|
def unquoteId(self, quotedId):
|
||||||
if len(quotedId) <= 2 or quotedId[0] != '"' or quotedId[len(quotedId) - 1] != '"':
|
if len(quotedId) <= 2 or quotedId[0] != '"' or quotedId[len(quotedId) - 1] != '"':
|
||||||
@ -92,56 +74,40 @@ class GPKGDBConnector(DBConnector):
|
|||||||
return unquoted
|
return unquoted
|
||||||
|
|
||||||
def _fetchOne(self, sql):
|
def _fetchOne(self, sql):
|
||||||
if not self.gdal2:
|
sql_lyr = self.gdal_ds.ExecuteSQL(sql)
|
||||||
# GDAL 1.X compat. To be removed at some point
|
if sql_lyr is None:
|
||||||
c = self._get_cursor()
|
return None
|
||||||
self._execute(c, sql)
|
f = sql_lyr.GetNextFeature()
|
||||||
res = c.fetchone()
|
if f is None:
|
||||||
if res is not None:
|
ret = None
|
||||||
return res
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
else:
|
else:
|
||||||
sql_lyr = self.gdal_ds.ExecuteSQL(sql)
|
ret = [f.GetField(i) for i in range(f.GetFieldCount())]
|
||||||
if sql_lyr is None:
|
self.gdal_ds.ReleaseResultSet(sql_lyr)
|
||||||
return None
|
return ret
|
||||||
f = sql_lyr.GetNextFeature()
|
|
||||||
if f is None:
|
|
||||||
ret = None
|
|
||||||
else:
|
|
||||||
ret = [f.GetField(i) for i in range(f.GetFieldCount())]
|
|
||||||
self.gdal_ds.ReleaseResultSet(sql_lyr)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def _fetchAll(self, sql, include_fid_and_geometry=False):
|
def _fetchAll(self, sql, include_fid_and_geometry=False):
|
||||||
if not self.gdal2:
|
sql_lyr = self.gdal_ds.ExecuteSQL(sql)
|
||||||
# GDAL 1.X compat. To be removed at some point
|
if sql_lyr is None:
|
||||||
c = self._get_cursor()
|
return None
|
||||||
self._execute(c, sql)
|
ret = []
|
||||||
return c.fetchall()
|
while True:
|
||||||
else:
|
f = sql_lyr.GetNextFeature()
|
||||||
sql_lyr = self.gdal_ds.ExecuteSQL(sql)
|
if f is None:
|
||||||
if sql_lyr is None:
|
break
|
||||||
return None
|
else:
|
||||||
ret = []
|
if include_fid_and_geometry:
|
||||||
while True:
|
field_vals = [f.GetFID()]
|
||||||
f = sql_lyr.GetNextFeature()
|
if sql_lyr.GetLayerDefn().GetGeomType() != ogr.wkbNone:
|
||||||
if f is None:
|
geom = f.GetGeometryRef()
|
||||||
break
|
if geom is not None:
|
||||||
|
geom = geom.ExportToWkt()
|
||||||
|
field_vals += [geom]
|
||||||
|
field_vals += [f.GetField(i) for i in range(f.GetFieldCount())]
|
||||||
|
ret.append(field_vals)
|
||||||
else:
|
else:
|
||||||
if include_fid_and_geometry:
|
ret.append([f.GetField(i) for i in range(f.GetFieldCount())])
|
||||||
field_vals = [f.GetFID()]
|
self.gdal_ds.ReleaseResultSet(sql_lyr)
|
||||||
if sql_lyr.GetLayerDefn().GetGeomType() != ogr.wkbNone:
|
return ret
|
||||||
geom = f.GetGeometryRef()
|
|
||||||
if geom is not None:
|
|
||||||
geom = geom.ExportToWkt()
|
|
||||||
field_vals += [geom]
|
|
||||||
field_vals += [f.GetField(i) for i in range(f.GetFieldCount())]
|
|
||||||
ret.append(field_vals)
|
|
||||||
else:
|
|
||||||
ret.append([f.GetField(i) for i in range(f.GetFieldCount())])
|
|
||||||
self.gdal_ds.ReleaseResultSet(sql_lyr)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def _fetchAllFromLayer(self, table):
|
def _fetchAllFromLayer(self, table):
|
||||||
|
|
||||||
@ -167,15 +133,12 @@ class GPKGDBConnector(DBConnector):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _execute_and_commit(self, sql):
|
def _execute_and_commit(self, sql):
|
||||||
if not self.gdal2:
|
sql_lyr = self.gdal_ds.ExecuteSQL(sql)
|
||||||
DBConnector._execute_and_commit(self, sql)
|
self.gdal_ds.ReleaseResultSet(sql_lyr)
|
||||||
else:
|
|
||||||
sql_lyr = self.gdal_ds.ExecuteSQL(sql)
|
|
||||||
self.gdal_ds.ReleaseResultSet(sql_lyr)
|
|
||||||
|
|
||||||
def _execute(self, cursor, sql):
|
def _execute(self, cursor, sql):
|
||||||
|
|
||||||
if self.gdal2 and self.connection is None:
|
if self.connection is None:
|
||||||
# Needed when evaluating a SQL query
|
# Needed when evaluating a SQL query
|
||||||
try:
|
try:
|
||||||
self.connection = spatialite_connect(str(self.dbname))
|
self.connection = spatialite_connect(str(self.dbname))
|
||||||
@ -315,17 +278,16 @@ class GPKGDBConnector(DBConnector):
|
|||||||
geomname = 'MULTIPOLYGON'
|
geomname = 'MULTIPOLYGON'
|
||||||
elif geomtype_flatten == ogr.wkbGeometryCollection:
|
elif geomtype_flatten == ogr.wkbGeometryCollection:
|
||||||
geomname = 'GEOMETRYCOLLECTION'
|
geomname = 'GEOMETRYCOLLECTION'
|
||||||
if self.gdal2:
|
elif geomtype_flatten == ogr.wkbCircularString:
|
||||||
if geomtype_flatten == ogr.wkbCircularString:
|
geomname = 'CIRCULARSTRING'
|
||||||
geomname = 'CIRCULARSTRING'
|
elif geomtype_flatten == ogr.wkbCompoundCurve:
|
||||||
elif geomtype_flatten == ogr.wkbCompoundCurve:
|
geomname = 'COMPOUNDCURVE'
|
||||||
geomname = 'COMPOUNDCURVE'
|
elif geomtype_flatten == ogr.wkbCurvePolygon:
|
||||||
elif geomtype_flatten == ogr.wkbCurvePolygon:
|
geomname = 'CURVEPOLYGON'
|
||||||
geomname = 'CURVEPOLYGON'
|
elif geomtype_flatten == ogr.wkbMultiCurve:
|
||||||
elif geomtype_flatten == ogr.wkbMultiCurve:
|
geomname = 'MULTICURVE'
|
||||||
geomname = 'MULTICURVE'
|
elif geomtype_flatten == ogr.wkbMultiSurface:
|
||||||
elif geomtype_flatten == ogr.wkbMultiSurface:
|
geomname = 'MULTISURFACE'
|
||||||
geomname = 'MULTISURFACE'
|
|
||||||
geomdim = 'XY'
|
geomdim = 'XY'
|
||||||
if hasattr(ogr, 'GT_HasZ') and ogr.GT_HasZ(lyr.GetGeomType()):
|
if hasattr(ogr, 'GT_HasZ') and ogr.GT_HasZ(lyr.GetGeomType()):
|
||||||
geomdim += 'Z'
|
geomdim += 'Z'
|
||||||
@ -826,14 +788,13 @@ class GPKGDBConnector(DBConnector):
|
|||||||
if self.isRasterTable(table) or geom_column is None:
|
if self.isRasterTable(table) or geom_column is None:
|
||||||
return False
|
return False
|
||||||
_, tablename = self.getSchemaTableName(table)
|
_, tablename = self.getSchemaTableName(table)
|
||||||
if self.gdal2:
|
|
||||||
# Only try this for GDAL >= 2 (but only available in >= 2.1.2)
|
# (only available in >= 2.1.2)
|
||||||
sql = u"SELECT HasSpatialIndex(%s, %s)" % (self.quoteString(tablename), self.quoteString(geom_column))
|
sql = u"SELECT HasSpatialIndex(%s, %s)" % (self.quoteString(tablename), self.quoteString(geom_column))
|
||||||
gdal.PushErrorHandler()
|
gdal.PushErrorHandler()
|
||||||
ret = self._fetchOne(sql)
|
ret = self._fetchOne(sql)
|
||||||
gdal.PopErrorHandler()
|
gdal.PopErrorHandler()
|
||||||
else:
|
|
||||||
ret = None
|
|
||||||
if ret is None:
|
if ret is None:
|
||||||
# might be the case for GDAL < 2.1.2
|
# might be the case for GDAL < 2.1.2
|
||||||
sql = u"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name LIKE %s" % self.quoteString("%%rtree_" + tablename + "_%%")
|
sql = u"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name LIKE %s" % self.quoteString("%%rtree_" + tablename + "_%%")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user