Skip to content

Commit

Permalink
[DBManager] Remove geopackage support from spatialite plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Oct 18, 2016
1 parent d10e564 commit 2c1356c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 94 deletions.
74 changes: 2 additions & 72 deletions python/plugins/db_manager/db_plugins/spatialite/connector.py
Expand Up @@ -55,7 +55,6 @@ def __init__(self, uri):

self._checkSpatial()
self._checkRaster()
self._checkGeopackage()

def _connectionInfo(self):
return str(self.dbname)
Expand Down Expand Up @@ -92,11 +91,6 @@ def _checkRaster(self):
self.has_raster = self._checkRasterTables()
return self.has_raster

def _checkGeopackage(self):
""" check if it's a geopackage db """
self.is_gpkg = self._checkGeopackageTables()
return self.is_gpkg

def _checkGeometryColumnsTable(self):
try:
c = self._get_cursor()
Expand All @@ -118,36 +112,6 @@ def _checkRasterTables(self):
ret = c.fetchone()
return ret and ret[0]

def _checkGeopackageTables(self):
try:
sql = u"SELECT HasGeoPackage()"
result = self._execute(None, sql).fetchone()[0] == 1
except ConnectionError:
result = False
except Exception:
# SpatiaLite < 4.2 does not have HasGeoPackage() function
result = False

if result:
try:
sql = u"SELECT CheckGeoPackageMetaData()"
result = self._execute(None, sql).fetchone()[0] == 1
except ConnectionError:
result = False
else:
# Spatialite < 4.2 has no GeoPackage support, check for filename and GPKG layout
ver = list(map(int, self.getInfo()[0].split('.')[0:2]))
if ver[0] < 4 or (ver[0] == 4 and ver[1] < 2):
hasGpkgFileExt = self.dbname[-5:] == ".gpkg" or self.dbname[-11:] == ".geopackage"

sql = u"SELECT count(*) = 3 FROM sqlite_master WHERE name IN ('gpkg_geometry_columns', 'gpkg_spatial_ref_sys', 'gpkg_contents')"
ret = self._execute(None, sql).fetchone()
hasGpkgLayout = ret and ret[0]

result = hasGpkgFileExt and hasGpkgLayout

return result

def getInfo(self):
c = self._get_cursor()
self._execute(c, u"SELECT sqlite_version()")
Expand All @@ -159,7 +123,7 @@ def getSpatialInfo(self):
- geos version
- proj version
"""
if not self.has_spatial and not self.is_gpkg:
if not self.has_spatial:
return

c = self._get_cursor()
Expand Down Expand Up @@ -187,9 +151,6 @@ def hasTableColumnEditingSupport(self):
def hasCreateSpatialViewSupport(self):
return True

def isGpkg(self):
return self.is_gpkg

def fieldTypes(self):
return [
"integer", "bigint", "smallint", # integers
Expand Down Expand Up @@ -307,14 +268,6 @@ def getVectorTables(self, schema=None):
WHERE m.type in ('table', 'view')
ORDER BY m.name, g.f_geometry_column""" % cols

elif self.is_gpkg:
# get info from gpkg_geometry_columns table
dim = " 'XY' || CASE z WHEN 1 THEN 'Z' END || CASE m WHEN 1 THEN 'M' END AS coord_dimension "
sql = u"""SELECT m.name, m.type = 'view', g.table_name, g.column_name, g.geometry_type_name AS gtype, %s, g.srs_id
FROM sqlite_master AS m JOIN gpkg_geometry_columns AS g ON upper(m.name) = upper(g.table_name)
WHERE m.type in ('table', 'view')
ORDER BY m.name, g.column_name""" % dim

else:
return []

Expand All @@ -340,8 +293,6 @@ def getRasterTables(self, schema=None):
srid
"""

if self.is_gpkg:
return [] # Not implemented
if not self.has_geometry_columns:
return []
if not self.has_raster:
Expand Down Expand Up @@ -447,10 +398,7 @@ def getViewDefinition(self, view):
return ret[0] if ret is not None else None

def getSpatialRefInfo(self, srid):
if self.is_gpkg:
sql = u"SELECT srs_name FROM gpkg_spatial_ref_sys WHERE srs_id = %s" % self.quoteString(srid)
else:
sql = u"SELECT ref_sys_name FROM spatial_ref_sys WHERE srid = %s" % self.quoteString(srid)
sql = u"SELECT ref_sys_name FROM spatial_ref_sys WHERE srid = %s" % self.quoteString(srid)
c = self._execute(None, sql)
ret = c.fetchone()
return ret[0] if ret is not None else None
Expand Down Expand Up @@ -503,8 +451,6 @@ def deleteTable(self, table):
""" delete table from the database """
if self.isRasterTable(table):
return False
if self.is_gpkg:
return False # Not implemented

c = self._get_cursor()
sql = u"DROP TABLE %s" % self.quoteId(table)
Expand All @@ -518,8 +464,6 @@ def emptyTable(self, table):
""" delete all rows from table """
if self.isRasterTable(table):
return False
if self.is_gpkg:
return False # Not implemented

sql = u"DELETE FROM %s" % self.quoteId(table)
self._execute_and_commit(sql)
Expand All @@ -532,8 +476,6 @@ def renameTable(self, table, new_table):

if self.isRasterTable(table):
return False
if self.is_gpkg:
return False # Not implemented

c = self._get_cursor()

Expand Down Expand Up @@ -573,8 +515,6 @@ def renameView(self, view, new_name):
return self.renameTable(view, new_name)

def createSpatialView(self, view, query):
if self.is_gpkg:
return False # Not implemented

self.createView(view, query)
# get type info about the view
Expand Down Expand Up @@ -654,8 +594,6 @@ def setColumnNull(self, table, column, is_null):
return False # column editing not supported

def isGeometryColumn(self, table, column):
if self.is_gpkg:
return False # Not implemented

c = self._get_cursor()
schema, tablename = self.getSchemaTableName(table)
Expand All @@ -665,8 +603,6 @@ def isGeometryColumn(self, table, column):
return c.fetchone()[0] == 't'

def addGeometryColumn(self, table, geom_column='geometry', geom_type='POINT', srid=-1, dim=2):
if self.is_gpkg:
return False # Not implemented

schema, tablename = self.getSchemaTableName(table)
sql = u"SELECT AddGeometryColumn(%s, %s, %d, %s, %s)" % (
Expand Down Expand Up @@ -704,8 +640,6 @@ def deleteTableIndex(self, table, name):
def createSpatialIndex(self, table, geom_column='geometry'):
if self.isRasterTable(table):
return False
if self.is_gpkg:
return False # Not implemented

schema, tablename = self.getSchemaTableName(table)
sql = u"SELECT CreateSpatialIndex(%s, %s)" % (self.quoteString(tablename), self.quoteString(geom_column))
Expand All @@ -714,8 +648,6 @@ def createSpatialIndex(self, table, geom_column='geometry'):
def deleteSpatialIndex(self, table, geom_column='geometry'):
if self.isRasterTable(table):
return False
if self.is_gpkg:
return False # Not implemented

schema, tablename = self.getSchemaTableName(table)
try:
Expand All @@ -729,8 +661,6 @@ def deleteSpatialIndex(self, table, geom_column='geometry'):
self.deleteTable(idx_table_name)

def hasSpatialIndex(self, table, geom_column='geometry'):
if self.is_gpkg:
return False # Not implemented
if not self.has_geometry_columns or self.isRasterTable(table):
return False
c = self._get_cursor()
Expand Down
Expand Up @@ -58,10 +58,7 @@ def spatialInfo(self):
]
ret.append(HtmlTable(tbl))

if self.db.connector.is_gpkg:
pass

elif not self.db.connector.has_geometry_columns:
if not self.db.connector.has_geometry_columns:
ret.append(HtmlParagraph(
QApplication.translate("DBManagerPlugin", "<warning> geometry_columns table doesn't exist!\n"
"This table is essential for many GIS applications for enumeration of tables.")))
Expand Down
24 changes: 6 additions & 18 deletions python/plugins/db_manager/db_plugins/spatialite/plugin.py
Expand Up @@ -52,7 +52,7 @@ def typeName(self):

@classmethod
def typeNameString(self):
return 'SpatiaLite/Geopackage'
return 'SpatiaLite'

@classmethod
def providerName(self):
Expand Down Expand Up @@ -90,7 +90,7 @@ def addConnection(self, conn_name, uri):
def addConnectionActionSlot(self, item, action, parent, index):
QApplication.restoreOverrideCursor()
try:
filename, selected_filter = QFileDialog.getOpenFileName(parent, "Choose Sqlite/Spatialite/Geopackage file")
filename, selected_filter = QFileDialog.getOpenFileName(parent, "Choose Sqlite/Spatialite file")
if not filename:
return
finally:
Expand Down Expand Up @@ -184,21 +184,13 @@ def ogrUri(self):
return ogrUri

def mimeUri(self):
if self.database().connector.isGpkg():
# QGIS has no provider to load Geopackage vectors, let's use OGR
return u"vector:ogr:%s:%s" % (self.name, self.ogrUri())
return Table.mimeUri(self)

def toMapLayer(self):
from qgis.core import QgsVectorLayer

if self.database().connector.isGpkg():
# QGIS has no provider to load Geopackage vectors, let's use OGR
provider = "ogr"
uri = self.ogrUri()
else:
provider = self.database().dbplugin().providerName()
uri = self.uri().uri()
provider = self.database().dbplugin().providerName()
uri = self.uri().uri()

return QgsVectorLayer(uri, self.name, provider)

Expand Down Expand Up @@ -283,12 +275,8 @@ def mimeUri(self):
def toMapLayer(self):
from qgis.core import QgsRasterLayer, QgsContrastEnhancement

if self.database().connector.isGpkg():
# QGIS has no provider to load Geopackage rasters, let's use GDAL
uri = self.ogrUri()
else:
# QGIS has no provider to load Rasterlite rasters, let's use GDAL
uri = self.rasterliteGdalUri()
# QGIS has no provider to load Rasterlite rasters, let's use GDAL
uri = self.rasterliteGdalUri()

rl = QgsRasterLayer(uri, self.name)
if rl.isValid():
Expand Down

0 comments on commit 2c1356c

Please sign in to comment.