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)
39
- sql = u"""SELECT count(*)
40
- FROM pg_class AS cls JOIN pg_namespace AS nsp ON nsp.oid = cls.relnamespace
41
- WHERE cls.relname = 'topology' AND nsp.nspname = 'topology'"""
42
- c = db .connector ._get_cursor ()
43
- db .connector ._execute ( c , sql )
44
- res = db .connector ._fetchone ( c )
45
- if res == None or int (res [0 ]) <= 0 :
46
- return
47
-
48
- # add the action to the DBManager menu
49
- action = QAction ( QIcon (), "&TopoViewer" , db )
50
- mainwindow .registerAction ( action , "&Schema" , run )
37
+ # check whether the selected database has topology enabled
38
+ # (search for topology.topology)
39
+ sql = u"""SELECT count(*)
40
+ FROM pg_class AS cls JOIN pg_namespace AS nsp ON nsp.oid = cls.relnamespace
41
+ WHERE cls.relname = 'topology' AND nsp.nspname = 'topology'"""
42
+ c = db .connector ._get_cursor ()
43
+ db .connector ._execute ( c , sql )
44
+ res = db .connector ._fetchone ( c )
45
+ if res == None or int (res [0 ]) <= 0 :
46
+ return
47
+
48
+ # add the action to the DBManager menu
49
+ action = QAction ( QIcon (), "&TopoViewer" , db )
50
+ mainwindow .registerAction ( action , "&Schema" , run )
51
51
52
52
53
53
# The run function is called once the user clicks on the action TopoViewer
@@ -56,146 +56,146 @@ def load(db, mainwindow):
56
56
# @param action is the clicked action on the DBManager menu/toolbar
57
57
# @param mainwindow is the DBManager mainwindow
58
58
def run (item , action , mainwindow ):
59
- db = item .database ()
60
- uri = db .uri ()
61
- conninfo = uri .connectionInfo ()
62
- iface = mainwindow .iface
63
-
64
- quoteId = db .connector .quoteId
65
- quoteStr = db .connector .quoteString
66
-
67
- # check if the selected item is a topology schema
68
- isTopoSchema = False
69
-
70
- if not hasattr (item , 'schema' ):
71
- QMessageBox .critical (mainwindow , "Invalid topology" , u'Select a topology schema to continue.' )
72
- return False
73
-
74
- if item .schema () != None :
75
- sql = u"SELECT count(*) FROM topology.topology WHERE name = %s" % quoteStr (item .schema ().name )
76
- c = db .connector ._get_cursor ()
77
- db .connector ._execute ( c , sql )
78
- res = db .connector ._fetchone ( c )
79
- isTopoSchema = res != None and int (res [0 ]) > 0
80
-
81
- if not isTopoSchema :
82
- QMessageBox .critical (mainwindow , "Invalid topology" , u'Schema "%s" is not registered in topology.topology.' % item .schema ().name )
83
- return False
84
-
85
- # load layers into the current project
86
- toponame = item .schema ().name
87
- template_dir = os .path .join (current_path , 'templates' )
88
- registry = QgsMapLayerRegistry .instance ()
89
- legend = iface .legendInterface ()
90
-
91
- # do not refresh the canvas until all the layers are added
92
- prevRenderFlagState = iface .mapCanvas ().renderFlag ()
93
- iface .mapCanvas ().setRenderFlag ( False )
94
- try :
95
- # NOTE: -1 parent is an attempt to always add to the root, but
96
- # it is currently broken: http://hub.qgis.org/issues/6879
97
- group = legend .addGroup (u'%s topology' % toponame , False , - 1 )
98
-
99
- provider = db .dbplugin ().providerName ()
100
- uri = db .uri ();
101
-
102
- # face
103
- layer = db .toSqlLayer (u'SELECT face_id, topology.ST_GetFaceGeometry(%s, face_id) as geom ' \
104
- 'FROM %s.face WHERE face_id > 0' % (quoteStr (toponame ), quoteId (toponame )),
105
- 'geom' , 'face_id' , u'%s.face' % toponame )
106
- layer .loadNamedStyle (os .path .join (template_dir , 'face.qml' ))
107
- registry .addMapLayer ( layer )
108
- legend .setLayerVisible (layer , False )
109
- legend .setLayerExpanded (layer , False )
110
- legend .moveLayer (layer , group )
111
-
112
- # node
113
- uri .setDataSource (toponame , 'node' , 'geom' , '' , 'node_id' )
114
- layer = QgsVectorLayer (uri .uri (), u'%s.nodes' % toponame , provider )
115
- layer .loadNamedStyle (os .path .join (template_dir , 'node.qml' ))
116
- registry .addMapLayer ( layer )
117
- legend .setLayerVisible (layer , False )
118
- legend .setLayerExpanded (layer , False )
119
- legend .moveLayer (layer , group )
120
-
121
- # node labels
122
- uri .setDataSource (toponame , 'node' , 'geom' , '' , 'node_id' )
123
- layer = QgsVectorLayer (uri .uri (), u'%s.node label' % toponame , provider )
124
- layer .loadNamedStyle (os .path .join (template_dir , 'node_label.qml' ))
125
- registry .addMapLayer ( layer )
126
- legend .setLayerVisible (layer , False )
127
- legend .setLayerExpanded (layer , False )
128
- legend .moveLayer (layer , group )
129
-
130
- # edge
131
- uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
132
- layer = QgsVectorLayer (uri .uri (), u'%s.edge' % toponame , provider )
133
- layer .loadNamedStyle (os .path .join (template_dir , 'edge.qml' ))
134
- registry .addMapLayer ( layer )
135
- legend .setLayerVisible (layer , False )
136
- legend .setLayerExpanded (layer , False )
137
- legend .moveLayer (layer , group )
138
-
139
- # edge labels
140
- uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
141
- layer = QgsVectorLayer (uri .uri (), u'%s.edge label' % toponame , provider )
142
- layer .loadNamedStyle (os .path .join (template_dir , 'edge_label.qml' ))
143
- registry .addMapLayer ( layer )
144
- legend .setLayerVisible (layer , False )
145
- legend .setLayerExpanded (layer , False )
146
- legend .moveLayer (layer , group )
147
-
148
- # face_left
149
- uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
150
- layer = QgsVectorLayer (uri .uri (), u'%s.face_left' % toponame , provider )
151
- layer .loadNamedStyle (os .path .join (template_dir , 'face_left.qml' ))
152
- registry .addMapLayer ( layer )
153
- legend .setLayerVisible (layer , False )
154
- legend .setLayerExpanded (layer , False )
155
- legend .moveLayer (layer , group )
156
-
157
- # face_right
158
- uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
159
- layer = QgsVectorLayer (uri .uri (), u'%s.face_right' % toponame , provider )
160
- layer .loadNamedStyle (os .path .join (template_dir , 'face_right.qml' ))
161
- registry .addMapLayer ( layer )
162
- legend .setLayerVisible (layer , False )
163
- legend .setLayerExpanded (layer , False )
164
- legend .moveLayer (layer , group )
165
-
166
- # next_left
167
- uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
168
- layer = QgsVectorLayer (uri .uri (), u'%s.next_left' % toponame , provider )
169
- layer .loadNamedStyle (os .path .join (template_dir , 'next_left.qml' ))
170
- registry .addMapLayer ( layer )
171
- legend .setLayerVisible (layer , False )
172
- legend .setLayerExpanded (layer , False )
173
- legend .moveLayer (layer , group )
174
-
175
- # next_right
176
- uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
177
- layer = QgsVectorLayer (uri .uri (), u'%s.next_right' % toponame , provider )
178
- layer .loadNamedStyle (os .path .join (template_dir , 'next_right.qml' ))
179
- registry .addMapLayer ( layer )
180
- legend .setLayerVisible (layer , False )
181
- legend .setLayerExpanded (layer , False )
182
- legend .moveLayer (layer , group )
183
-
184
- # face_seed
185
- layer = db .toSqlLayer (u'SELECT face_id, ST_PointOnSurface(topology.ST_GetFaceGeometry(%s, face_id)) as geom ' \
186
- 'FROM %s.face WHERE face_id > 0' % (quoteStr (toponame ), quoteId (toponame )),
187
- 'geom' , 'face_id' , u'%s.face_seed' % toponame )
188
- layer .loadNamedStyle (os .path .join (template_dir , 'face_seed.qml' ))
189
- registry .addMapLayer ( layer )
190
- legend .setLayerVisible (layer , False )
191
- legend .setLayerExpanded (layer , False )
192
- legend .moveLayer (layer , group )
193
-
194
- # TODO: add polygon0, polygon1 and polygon2 ?
195
-
196
- finally :
197
- # restore canvas render flag
198
- iface .mapCanvas ().setRenderFlag ( prevRenderFlagState )
199
-
200
- return True
59
+ db = item .database ()
60
+ uri = db .uri ()
61
+ conninfo = uri .connectionInfo ()
62
+ iface = mainwindow .iface
63
+
64
+ quoteId = db .connector .quoteId
65
+ quoteStr = db .connector .quoteString
66
+
67
+ # check if the selected item is a topology schema
68
+ isTopoSchema = False
69
+
70
+ if not hasattr (item , 'schema' ):
71
+ QMessageBox .critical (mainwindow , "Invalid topology" , u'Select a topology schema to continue.' )
72
+ return False
73
+
74
+ if item .schema () != None :
75
+ sql = u"SELECT count(*) FROM topology.topology WHERE name = %s" % quoteStr (item .schema ().name )
76
+ c = db .connector ._get_cursor ()
77
+ db .connector ._execute ( c , sql )
78
+ res = db .connector ._fetchone ( c )
79
+ isTopoSchema = res != None and int (res [0 ]) > 0
80
+
81
+ if not isTopoSchema :
82
+ QMessageBox .critical (mainwindow , "Invalid topology" , u'Schema "%s" is not registered in topology.topology.' % item .schema ().name )
83
+ return False
84
+
85
+ # load layers into the current project
86
+ toponame = item .schema ().name
87
+ template_dir = os .path .join (current_path , 'templates' )
88
+ registry = QgsMapLayerRegistry .instance ()
89
+ legend = iface .legendInterface ()
90
+
91
+ # do not refresh the canvas until all the layers are added
92
+ prevRenderFlagState = iface .mapCanvas ().renderFlag ()
93
+ iface .mapCanvas ().setRenderFlag ( False )
94
+ try :
95
+ # NOTE: -1 parent is an attempt to always add to the root, but
96
+ # it is currently broken: http://hub.qgis.org/issues/6879
97
+ group = legend .addGroup (u'%s topology' % toponame , False , - 1 )
98
+
99
+ provider = db .dbplugin ().providerName ()
100
+ uri = db .uri ();
101
+
102
+ # face
103
+ layer = db .toSqlLayer (u'SELECT face_id, topology.ST_GetFaceGeometry(%s, face_id) as geom ' \
104
+ 'FROM %s.face WHERE face_id > 0' % (quoteStr (toponame ), quoteId (toponame )),
105
+ 'geom' , 'face_id' , u'%s.face' % toponame )
106
+ layer .loadNamedStyle (os .path .join (template_dir , 'face.qml' ))
107
+ registry .addMapLayers ([ layer ] )
108
+ legend .setLayerVisible (layer , False )
109
+ legend .setLayerExpanded (layer , False )
110
+ legend .moveLayer (layer , group )
111
+
112
+ # node
113
+ uri .setDataSource (toponame , 'node' , 'geom' , '' , 'node_id' )
114
+ layer = QgsVectorLayer (uri .uri (), u'%s.nodes' % toponame , provider )
115
+ layer .loadNamedStyle (os .path .join (template_dir , 'node.qml' ))
116
+ registry .addMapLayers ([ layer ] )
117
+ legend .setLayerVisible (layer , False )
118
+ legend .setLayerExpanded (layer , False )
119
+ legend .moveLayer (layer , group )
120
+
121
+ # node labels
122
+ uri .setDataSource (toponame , 'node' , 'geom' , '' , 'node_id' )
123
+ layer = QgsVectorLayer (uri .uri (), u'%s.node label' % toponame , provider )
124
+ layer .loadNamedStyle (os .path .join (template_dir , 'node_label.qml' ))
125
+ registry .addMapLayers ([ layer ] )
126
+ legend .setLayerVisible (layer , False )
127
+ legend .setLayerExpanded (layer , False )
128
+ legend .moveLayer (layer , group )
129
+
130
+ # edge
131
+ uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
132
+ layer = QgsVectorLayer (uri .uri (), u'%s.edge' % toponame , provider )
133
+ layer .loadNamedStyle (os .path .join (template_dir , 'edge.qml' ))
134
+ registry .addMapLayers ([ layer ] )
135
+ legend .setLayerVisible (layer , False )
136
+ legend .setLayerExpanded (layer , False )
137
+ legend .moveLayer (layer , group )
138
+
139
+ # edge labels
140
+ uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
141
+ layer = QgsVectorLayer (uri .uri (), u'%s.edge label' % toponame , provider )
142
+ layer .loadNamedStyle (os .path .join (template_dir , 'edge_label.qml' ))
143
+ registry .addMapLayers ([ layer ] )
144
+ legend .setLayerVisible (layer , False )
145
+ legend .setLayerExpanded (layer , False )
146
+ legend .moveLayer (layer , group )
147
+
148
+ # face_left
149
+ uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
150
+ layer = QgsVectorLayer (uri .uri (), u'%s.face_left' % toponame , provider )
151
+ layer .loadNamedStyle (os .path .join (template_dir , 'face_left.qml' ))
152
+ registry .addMapLayers ([ layer ] )
153
+ legend .setLayerVisible (layer , False )
154
+ legend .setLayerExpanded (layer , False )
155
+ legend .moveLayer (layer , group )
156
+
157
+ # face_right
158
+ uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
159
+ layer = QgsVectorLayer (uri .uri (), u'%s.face_right' % toponame , provider )
160
+ layer .loadNamedStyle (os .path .join (template_dir , 'face_right.qml' ))
161
+ registry .addMapLayers ([ layer ] )
162
+ legend .setLayerVisible (layer , False )
163
+ legend .setLayerExpanded (layer , False )
164
+ legend .moveLayer (layer , group )
165
+
166
+ # next_left
167
+ uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
168
+ layer = QgsVectorLayer (uri .uri (), u'%s.next_left' % toponame , provider )
169
+ layer .loadNamedStyle (os .path .join (template_dir , 'next_left.qml' ))
170
+ registry .addMapLayers ([ layer ] )
171
+ legend .setLayerVisible (layer , False )
172
+ legend .setLayerExpanded (layer , False )
173
+ legend .moveLayer (layer , group )
174
+
175
+ # next_right
176
+ uri .setDataSource (toponame , 'edge_data' , 'geom' , '' , 'edge_id' )
177
+ layer = QgsVectorLayer (uri .uri (), u'%s.next_right' % toponame , provider )
178
+ layer .loadNamedStyle (os .path .join (template_dir , 'next_right.qml' ))
179
+ registry .addMapLayers ([ layer ] )
180
+ legend .setLayerVisible (layer , False )
181
+ legend .setLayerExpanded (layer , False )
182
+ legend .moveLayer (layer , group )
183
+
184
+ # face_seed
185
+ layer = db .toSqlLayer (u'SELECT face_id, ST_PointOnSurface(topology.ST_GetFaceGeometry(%s, face_id)) as geom ' \
186
+ 'FROM %s.face WHERE face_id > 0' % (quoteStr (toponame ), quoteId (toponame )),
187
+ 'geom' , 'face_id' , u'%s.face_seed' % toponame )
188
+ layer .loadNamedStyle (os .path .join (template_dir , 'face_seed.qml' ))
189
+ registry .addMapLayers ([ layer ] )
190
+ legend .setLayerVisible (layer , False )
191
+ legend .setLayerExpanded (layer , False )
192
+ legend .moveLayer (layer , group )
193
+
194
+ # TODO: add polygon0, polygon1 and polygon2 ?
195
+
196
+ finally :
197
+ # restore canvas render flag
198
+ iface .mapCanvas ().setRenderFlag ( prevRenderFlagState )
199
+
200
+ return True
201
201
0 commit comments