34
34
# @param db is the selected database
35
35
# @param mainwindow is the DBManager mainwindow
36
36
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)
39
39
sql = u"""SELECT count(*)
40
40
FROM pg_class AS cls JOIN pg_namespace AS nsp ON nsp.oid = cls.relnamespace
41
41
WHERE cls.relname = 'topology' AND nsp.nspname = 'topology'"""
@@ -72,16 +72,25 @@ def run(item, action, mainwindow):
72
72
return False
73
73
74
74
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 )
76
76
c = db .connector ._get_cursor ()
77
77
db .connector ._execute ( c , sql )
78
78
res = db .connector ._fetchone ( c )
79
- isTopoSchema = res != None and int ( res [ 0 ]) > 0
79
+ isTopoSchema = res != None
80
80
81
81
if not isTopoSchema :
82
82
QMessageBox .critical (mainwindow , "Invalid topology" , u'Schema "%s" is not registered in topology.topology.' % item .schema ().name )
83
83
return False
84
84
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
+
85
94
# load layers into the current project
86
95
toponame = item .schema ().name
87
96
template_dir = os .path .join (current_path , 'templates' )
@@ -105,8 +114,11 @@ def run(item, action, mainwindow):
105
114
legend .setGroupVisible (group , False )
106
115
107
116
# 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 )),
110
122
'geom' , 'face_id' , u'%s.face' % toponame )
111
123
layer .loadNamedStyle (os .path .join (template_dir , 'face.qml' ))
112
124
registry .addMapLayers ([layer ])
@@ -115,8 +127,11 @@ def run(item, action, mainwindow):
115
127
legend .moveLayer (layer , group )
116
128
117
129
# 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 )),
120
135
'geom' , 'face_id' , u'%s.face_seed' % toponame )
121
136
layer .loadNamedStyle (os .path .join (template_dir , 'face_seed.qml' ))
122
137
registry .addMapLayers ([layer ])
0 commit comments