Skip to content

Commit 73b4986

Browse files
author
Sandro Santilli
committedFeb 8, 2014
Speed up topology loading
When loading topology viewer on a PostGIS-2.0+ database, it includes type and srid typmod in the view layers, avoiding a full scan of the datasets. Fix #9510
1 parent 1d09f43 commit 73b4986

File tree

1 file changed

+23
-8
lines changed
  • python/plugins/db_manager/db_plugins/postgis/plugins/qgis_topoview

1 file changed

+23
-8
lines changed
 

‎python/plugins/db_manager/db_plugins/postgis/plugins/qgis_topoview/__init__.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
# @param db is the selected database
3535
# @param mainwindow is the DBManager mainwindow
3636
def load(db, mainwindow):
37-
# check whether the selected database has topology enabled
38-
# (search for topology.topology)
37+
# check whether the selected database supports topology
38+
# (search for topology.topology)
3939
sql = u"""SELECT count(*)
4040
FROM pg_class AS cls JOIN pg_namespace AS nsp ON nsp.oid = cls.relnamespace
4141
WHERE cls.relname = 'topology' AND nsp.nspname = 'topology'"""
@@ -72,16 +72,25 @@ def run(item, action, mainwindow):
7272
return False
7373

7474
if item.schema() != None:
75-
sql = u"SELECT count(*) FROM topology.topology WHERE name = %s" % quoteStr(item.schema().name)
75+
sql = u"SELECT srid FROM topology.topology WHERE name = %s" % quoteStr(item.schema().name)
7676
c = db.connector._get_cursor()
7777
db.connector._execute( c, sql )
7878
res = db.connector._fetchone( c )
79-
isTopoSchema = res != None and int(res[0]) > 0
79+
isTopoSchema = res != None
8080

8181
if not isTopoSchema:
8282
QMessageBox.critical(mainwindow, "Invalid topology", u'Schema "%s" is not registered in topology.topology.' % item.schema().name)
8383
return False
8484

85+
toposrid = int(res[0])
86+
87+
# Check if postgis supports typmod geometries
88+
sql = u"SELECT typmodin FROM pg_type WHERE typname = 'geometry' AND typmodin::int > 0"
89+
c = db.connector._get_cursor()
90+
db.connector._execute( c, sql )
91+
res = db.connector._fetchone( c )
92+
supportsTypmod = res != None
93+
8594
# load layers into the current project
8695
toponame = item.schema().name
8796
template_dir = os.path.join(current_path, 'templates')
@@ -105,8 +114,11 @@ def run(item, action, mainwindow):
105114
legend.setGroupVisible(group, False)
106115

107116
# face
108-
layer = db.toSqlLayer(u'SELECT face_id, topology.ST_GetFaceGeometry(%s, face_id) as geom ' \
109-
'FROM %s.face WHERE face_id > 0' % (quoteStr(toponame), quoteId(toponame)),
117+
geomcast = ''
118+
if supportsTypmod:
119+
geomcast = '::geometry(multipolygon,%s)' % toposrid
120+
layer = db.toSqlLayer(u'SELECT face_id, topology.ST_GetFaceGeometry(%s, face_id)%s as geom ' \
121+
'FROM %s.face WHERE face_id > 0' % (geomcast, quoteStr(toponame), quoteId(toponame)),
110122
'geom', 'face_id', u'%s.face' % toponame)
111123
layer.loadNamedStyle(os.path.join(template_dir, 'face.qml'))
112124
registry.addMapLayers([layer])
@@ -115,8 +127,11 @@ def run(item, action, mainwindow):
115127
legend.moveLayer(layer, group)
116128

117129
# face_seed
118-
layer = db.toSqlLayer(u'SELECT face_id, ST_PointOnSurface(topology.ST_GetFaceGeometry(%s, face_id)) as geom ' \
119-
'FROM %s.face WHERE face_id > 0' % (quoteStr(toponame), quoteId(toponame)),
130+
geomcast = ''
131+
if supportsTypmod:
132+
geomcast = '::geometry(point,%s)' % toposrid
133+
layer = db.toSqlLayer(u'SELECT face_id, ST_PointOnSurface(topology.ST_GetFaceGeometry(%s, face_id))%s as geom ' \
134+
'FROM %s.face WHERE face_id > 0' % (geomcast ,quoteStr(toponame), quoteId(toponame)),
120135
'geom', 'face_id', u'%s.face_seed' % toponame)
121136
layer.loadNamedStyle(os.path.join(template_dir, 'face_seed.qml'))
122137
registry.addMapLayers([layer])

0 commit comments

Comments
 (0)
Please sign in to comment.