Skip to content
Permalink
Browse files
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.
  • Loading branch information
Sandro Santilli committed Feb 8, 2014
1 parent 3701561 commit 56bebb193b568719eeba220d12f0b1d3608b279d
@@ -201,16 +201,25 @@ def sqlResultModel(self, sql, parent):
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)
@@ -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,25 +107,19 @@ 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)
legend.setLayerExpanded(layer, False)
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)

0 comments on commit 56bebb1

Please sign in to comment.