Pass wkbType and SRID of loaded views to the provider via URI

Slighly speeds up loading of view layers in TopoViewer (#9510).
Unfortunately there's no way to also pass in the Extent, so loading
is still slow.
This commit is contained in:
Sandro Santilli 2014-02-08 17:03:32 +01:00
parent 3701561489
commit 56bebb193b
2 changed files with 18 additions and 22 deletions

View File

@ -201,16 +201,25 @@ class Database(DbItemObject):
from .data_model import SqlResultModel
return SqlResultModel(self, sql, parent)
def toSqlLayer(self, sql, geomCol, uniqueCol, layerName="QueryLayer", layerType=None, avoidSelectById=False):
def toSqlLayer(self, sql, geomCol, uniqueCol, layerName="QueryLayer", layerType=None, avoidSelectById=False, srid=None, wkbType=None):
from qgis.core import QgsMapLayer, QgsVectorLayer, QgsRasterLayer
uri = self.uri()
uri.setDataSource("", u"(%s\n)" % sql, geomCol, "", uniqueCol)
if avoidSelectById:
uri.disableSelectAtId( True )
if srid != None:
print "SRID is not none but %s" % srid
uri.setSrid( srid )
if wkbType != None:
print "WkbType is not none but %s" % wkbType
uri.setWkbType( wkbType )
provider = self.dbplugin().providerName()
if layerType == QgsMapLayer.RasterLayer:
return QgsRasterLayer(uri.uri(), layerName, provider)
return QgsVectorLayer(uri.uri(), layerName, provider)
print "Creating vector layer -- start, with uri: %s" % uri.uri()
layer = QgsVectorLayer(uri.uri(), layerName, provider)
print "Creating vector layer -- done"
return layer
def registerAllActions(self, mainWindow):
self.registerDatabaseActions(mainWindow)

View File

@ -82,14 +82,7 @@ def run(item, action, mainwindow):
QMessageBox.critical(mainwindow, "Invalid topology", u'Schema "%s" is not registered in topology.topology.' % item.schema().name)
return False
toposrid = int(res[0])
# Check if postgis supports typmod geometries
sql = u"SELECT typmodin FROM pg_type WHERE typname = 'geometry' AND typmodin::int > 0"
c = db.connector._get_cursor()
db.connector._execute( c, sql )
res = db.connector._fetchone( c )
supportsTypmod = res != None
toposrid = res[0]
# load layers into the current project
toponame = item.schema().name
@ -114,12 +107,9 @@ def run(item, action, mainwindow):
legend.setGroupVisible(group, False)
# face
geomcast = ''
if supportsTypmod:
geomcast = '::geometry(polygon,%s)' % toposrid
layer = db.toSqlLayer(u'SELECT face_id, topology.ST_GetFaceGeometry(%s, face_id)%s as geom ' \
'FROM %s.face WHERE face_id > 0' % (quoteStr(toponame), geomcast, quoteId(toponame)),
'geom', 'face_id', u'%s.face' % toponame)
layer = db.toSqlLayer(u'SELECT face_id, topology.ST_GetFaceGeometry(%s, face_id) as geom ' \
'FROM %s.face WHERE face_id > 0' % (quoteStr(toponame), quoteId(toponame)),
'geom', 'face_id', u'%s.face' % toponame, None, False, str(toposrid), QGis.WKBPolygon)
layer.loadNamedStyle(os.path.join(template_dir, 'face.qml'))
registry.addMapLayers([layer])
legend.setLayerVisible(layer, False)
@ -127,12 +117,9 @@ def run(item, action, mainwindow):
legend.moveLayer(layer, group)
# face_seed
geomcast = ''
if supportsTypmod:
geomcast = '::geometry(point,%s)' % toposrid
layer = db.toSqlLayer(u'SELECT face_id, ST_PointOnSurface(topology.ST_GetFaceGeometry(%s, face_id))%s as geom ' \
'FROM %s.face WHERE face_id > 0' % (quoteStr(toponame), geomcast, quoteId(toponame)),
'geom', 'face_id', u'%s.face_seed' % toponame)
layer = db.toSqlLayer(u'SELECT face_id, ST_PointOnSurface(topology.ST_GetFaceGeometry(%s, face_id)) as geom ' \
'FROM %s.face WHERE face_id > 0' % (quoteStr(toponame), quoteId(toponame)),
'geom', 'face_id', u'%s.face_seed' % toponame, None, False, str(toposrid), QGis.WKBPoint)
layer.loadNamedStyle(os.path.join(template_dir, 'face_seed.qml'))
registry.addMapLayers([layer])
legend.setLayerVisible(layer, False)