23
23
24
24
from PyQt4 .QtCore import *
25
25
from PyQt4 .QtGui import *
26
+ from qgis .core import *
26
27
27
28
import os
28
29
current_path = os .path .dirname (__file__ )
@@ -58,6 +59,7 @@ def run(item, action, mainwindow):
58
59
db = item .database ()
59
60
uri = db .uri ()
60
61
conninfo = uri .connectionInfo ()
62
+ iface = mainwindow .iface
61
63
62
64
# check if the selected item is a topology schema
63
65
isTopoSchema = False
@@ -72,40 +74,62 @@ def run(item, action, mainwindow):
72
74
QMessageBox .critical (mainwindow , "Invalid topology" , u'Schema "%s" is not registered in topology.topology.' % item .schema ().name )
73
75
return False
74
76
75
- # create the new project from the template one
76
- tpl_name = u'topoview_template.qgs'
77
+ # load layers into the current project
77
78
toponame = item .schema ().name
78
- project_name = u'topoview_%s_%s.qgs' % (uri .database (), toponame )
79
-
80
- template_file = os .path .join (current_path , tpl_name )
81
- inf = QFile ( template_file )
82
- if not inf .exists ():
83
- QMessageBox .critical (mainwindow , "Error" , u'Template "%s" not found!' % template_file )
84
- return False
85
-
86
- project_file = os .path .join (current_path , project_name )
87
- outf = QFile ( project_file )
88
- if not outf .open ( QIODevice .WriteOnly ):
89
- QMessageBox .critical (mainwindow , "Error" , u'Unable to open "%s"' % project_file )
90
- return False
79
+ template_dir = os .path .join (current_path , 'templates' )
80
+ registry = QgsMapLayerRegistry .instance ()
81
+ legend = iface .legendInterface ()
82
+
83
+ group = legend .addGroup (toponame + ' topology' )
84
+
85
+ # node
86
+ layer = db .toSqlLayer ('SELECT * FROM "' + toponame + '".node' , 'geom' , 'node_id' , toponame + '.nodes' )
87
+ layer .loadNamedStyle (os .path .join (template_dir , 'node.qml' ))
88
+ registry .addMapLayer (layer )
89
+ legend .moveLayer (layer , group )
90
+
91
+ # edge
92
+ layer = db .toSqlLayer ('SELECT * FROM "' + toponame + '".edge_data' , 'geom' , 'edge_id' , toponame + '.edges' )
93
+ layer .loadNamedStyle (os .path .join (template_dir , 'edge_style.qml' ))
94
+ registry .addMapLayer (layer )
95
+ legend .moveLayer (layer , group )
96
+
97
+ # face_left
98
+ layer = db .toSqlLayer ('SELECT * FROM "' + toponame + '".edge_data' , 'geom' , 'edge_id' , toponame + '.face_left' )
99
+ layer .loadNamedStyle (os .path .join (template_dir , 'face_left.qml' ))
100
+ registry .addMapLayer (layer )
101
+ legend .moveLayer (layer , group )
102
+
103
+ # face_right
104
+ layer = db .toSqlLayer ('SELECT * FROM "' + toponame + '".edge_data' , 'geom' , 'edge_id' , toponame + '.face_right' )
105
+ layer .loadNamedStyle (os .path .join (template_dir , 'face_right.qml' ))
106
+ registry .addMapLayer (layer )
107
+ legend .moveLayer (layer , group )
108
+
109
+ # next_left
110
+ layer = db .toSqlLayer ('SELECT * FROM "' + toponame + '".edge_data' , 'geom' , 'edge_id' , toponame + '.next_left' )
111
+ layer .loadNamedStyle (os .path .join (template_dir , 'next_left.qml' ))
112
+ registry .addMapLayer (layer )
113
+ legend .setLayerVisible (layer , False )
114
+ legend .moveLayer (layer , group )
115
+
116
+ # next_right
117
+ layer = db .toSqlLayer ('SELECT * FROM "' + toponame + '".edge_data' , 'geom' , 'edge_id' , toponame + '.next_right' )
118
+ layer .loadNamedStyle (os .path .join (template_dir , 'next_right.qml' ))
119
+ registry .addMapLayer (layer )
120
+ legend .setLayerVisible (layer , False )
121
+ legend .moveLayer (layer , group )
122
+
123
+ # face_seed
124
+ layer = db .toSqlLayer ('SELECT face_id, ST_PointOnSurface(topology.ST_GetFaceGeometry(\' ' + toponame + '\' , face_id)) as geom FROM "' + toponame + '".face WHERE face_id > 0' , 'geom' , 'face_id' , toponame + '.face_seed' )
125
+ layer .loadNamedStyle (os .path .join (template_dir , 'face_seed.qml' ))
126
+ registry .addMapLayer (layer )
127
+ legend .setLayerVisible (layer , False )
128
+ legend .moveLayer (layer , group )
129
+
130
+ # TODO: add full faces ?
131
+ # TODO: add polygon0, polygon1 and polygon2 ?
132
+ # TODO: disable signals while adding all layers, then send a single one
91
133
92
- if not inf .open ( QIODevice .ReadOnly ):
93
- QMessageBox .critical (mainwindow , "Error" , u'Unable to open "%s"' % template_file )
94
- return False
95
-
96
- while not inf .atEnd ():
97
- l = inf .readLine ()
98
- l = l .replace ( u"dbname='@@DBNAME@@'" , conninfo .toUtf8 () )
99
- l = l .replace ( u'@@TOPONAME@@' , toponame )
100
- outf .write ( l )
101
-
102
- inf .close ()
103
- outf .close ()
104
-
105
- # load the project on QGis canvas
106
- iface = mainwindow .iface
107
- iface .newProject ( True )
108
- if iface .mapCanvas ().layerCount () == 0 :
109
- iface .addProject ( project_file )
110
134
return True
111
135
0 commit comments