Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ SET (DB_MANAGER_POSTGIS_TOPOVIEW_DIR ${DB_MANAGER_POSTGIS_DIR}/plugins/qgis_topo
FILE(GLOB PY_FILES *.py)

INSTALL(FILES ${PY_FILES} DESTINATION ${DB_MANAGER_POSTGIS_TOPOVIEW_DIR})
INSTALL(FILES topoview_template.qgs DESTINATION ${DB_MANAGER_POSTGIS_TOPOVIEW_DIR})

ADD_SUBDIRECTORY(templates)
Original file line number Diff line number Diff line change
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,11 +59,20 @@ def run(item, action, mainwindow):
db = item.database()
uri = db.uri()
conninfo = uri.connectionInfo()
iface = mainwindow.iface

quoteId = db.connector.quoteId
quoteStr = db.connector.quoteString

# check if the selected item is a topology schema
isTopoSchema = False
if hasattr(item, 'schema') and item.schema() != None:
sql = u"SELECT count(*) FROM topology.topology WHERE name = '%s'" % item.schema().name

if not hasattr(item, 'schema'):
QMessageBox.critical(mainwindow, "Invalid topology", u'Select a topology schema to continue.')
return False

if item.schema() != None:
sql = u"SELECT count(*) FROM topology.topology WHERE name = %s" % quoteStr(item.schema().name)
c = db.connector._get_cursor()
db.connector._execute( c, sql )
res = db.connector._fetchone( c )
Expand All @@ -72,40 +82,77 @@ 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
template_dir = os.path.join(current_path, 'templates')
registry = QgsMapLayerRegistry.instance()
legend = iface.legendInterface()

# do not refresh the canvas until all the layers are added
prevRenderFlagState = iface.mapCanvas().renderFlag()
iface.mapCanvas().setRenderFlag( False )
try:
group = legend.addGroup(u'%s topology' % toponame)

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

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

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

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

# next_left
layer = db.toSqlLayer(u'SELECT * FROM %s.edge_data' % quoteId(toponame),
'geom', 'edge_id', u'%s.next_left' % toponame)
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(u'SELECT * FROM %s.edge_data' % toponame,
'geom', 'edge_id', u'%s.next_right' % toponame)
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(u'SELECT face_id, ST_PointOnSurface(topology.ST_GetFaceGeometry(%s, face_id)) as geom ' \
'FROM %s.face WHERE face_id > 0' % (quoteStr(toponame), quoteId(toponame)),
'geom', 'face_id', u'%s.face_seed' % toponame)
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 ?

finally:
# restore canvas render flag
iface.mapCanvas().setRenderFlag( prevRenderFlagState )

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SET (DB_MANAGER_POSTGIS_TOPOVIEW_TEMPLATE_DIR ${DB_MANAGER_POSTGIS_DIR}/plugins/qgis_topoview/templates)

FILE(GLOB QML_FILES *.qml)

INSTALL(FILES ${QML_FILES} DESTINATION ${DB_MANAGER_POSTGIS_TOPOVIEW_TEMPLATE_DIR})
Loading