Skip to content

Commit

Permalink
Pass wkbType and SRID of loaded views to the provider via URI
Browse files Browse the repository at this point in the history
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 56bebb1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
13 changes: 11 additions & 2 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -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)
Expand Down
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 56bebb1

Please sign in to comment.