Skip to content

Commit

Permalink
Add topoview layers to existing project
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Santilli committed Oct 8, 2012
1 parent 358c638 commit 2e74722
Show file tree
Hide file tree
Showing 9 changed files with 1,151 additions and 1,154 deletions.
Expand Up @@ -23,6 +23,7 @@

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

import os
current_path = os.path.dirname(__file__)
Expand Down Expand Up @@ -58,6 +59,7 @@ def run(item, action, mainwindow):
db = item.database()
uri = db.uri()
conninfo = uri.connectionInfo()
iface = mainwindow.iface

# check if the selected item is a topology schema
isTopoSchema = False
Expand All @@ -72,40 +74,62 @@ def run(item, action, mainwindow):
QMessageBox.critical(mainwindow, "Invalid topology", u'Schema "%s" is not registered in topology.topology.' % item.schema().name)
return False

# create the new project from the template one
tpl_name = u'topoview_template.qgs'
# load layers into the current project
toponame = item.schema().name
project_name = u'topoview_%s_%s.qgs' % (uri.database(), toponame)

template_file = os.path.join(current_path, tpl_name)
inf = QFile( template_file )
if not inf.exists():
QMessageBox.critical(mainwindow, "Error", u'Template "%s" not found!' % template_file)
return False

project_file = os.path.join(current_path, project_name)
outf = QFile( project_file )
if not outf.open( QIODevice.WriteOnly ):
QMessageBox.critical(mainwindow, "Error", u'Unable to open "%s"' % project_file)
return False
template_dir = os.path.join(current_path, 'templates')
registry = QgsMapLayerRegistry.instance()
legend = iface.legendInterface()

group = legend.addGroup(toponame + ' topology')

# node
layer = db.toSqlLayer('SELECT * FROM "' + toponame + '".node', 'geom', 'node_id', toponame + '.nodes')
layer.loadNamedStyle(os.path.join(template_dir, 'node.qml'))
registry.addMapLayer(layer)
legend.moveLayer(layer, group)

# edge
layer = db.toSqlLayer('SELECT * FROM "' + toponame + '".edge_data', 'geom', 'edge_id', toponame + '.edges')
layer.loadNamedStyle(os.path.join(template_dir, 'edge_style.qml'))
registry.addMapLayer(layer)
legend.moveLayer(layer, group)

# face_left
layer = db.toSqlLayer('SELECT * FROM "' + toponame + '".edge_data', 'geom', 'edge_id', toponame + '.face_left')
layer.loadNamedStyle(os.path.join(template_dir, 'face_left.qml'))
registry.addMapLayer(layer)
legend.moveLayer(layer, group)

# face_right
layer = db.toSqlLayer('SELECT * FROM "' + toponame + '".edge_data', 'geom', 'edge_id', toponame + '.face_right')
layer.loadNamedStyle(os.path.join(template_dir, 'face_right.qml'))
registry.addMapLayer(layer)
legend.moveLayer(layer, group)

# next_left
layer = db.toSqlLayer('SELECT * FROM "' + toponame + '".edge_data', 'geom', 'edge_id', toponame + '.next_left')
layer.loadNamedStyle(os.path.join(template_dir, 'next_left.qml'))
registry.addMapLayer(layer)
legend.setLayerVisible(layer, False)
legend.moveLayer(layer, group)

# next_right
layer = db.toSqlLayer('SELECT * FROM "' + toponame + '".edge_data', 'geom', 'edge_id', toponame + '.next_right')
layer.loadNamedStyle(os.path.join(template_dir, 'next_right.qml'))
registry.addMapLayer(layer)
legend.setLayerVisible(layer, False)
legend.moveLayer(layer, group)

# face_seed
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')
layer.loadNamedStyle(os.path.join(template_dir, 'face_seed.qml'))
registry.addMapLayer(layer)
legend.setLayerVisible(layer, False)
legend.moveLayer(layer, group)

# TODO: add full faces ?
# TODO: add polygon0, polygon1 and polygon2 ?
# TODO: disable signals while adding all layers, then send a single one

if not inf.open( QIODevice.ReadOnly ):
QMessageBox.critical(mainwindow, "Error", u'Unable to open "%s"' % template_file)
return False

while not inf.atEnd():
l = inf.readLine()
l = l.replace( u"dbname='@@DBNAME@@'", conninfo.toUtf8() )
l = l.replace( u'@@TOPONAME@@', toponame )
outf.write( l )

inf.close()
outf.close()

# load the project on QGis canvas
iface = mainwindow.iface
iface.newProject( True )
if iface.mapCanvas().layerCount() == 0:
iface.addProject( project_file )
return True

0 comments on commit 2e74722

Please sign in to comment.