Skip to content

Commit

Permalink
Merge pull request #281 from strk/db_manager_addlayer
Browse files Browse the repository at this point in the history
dbmananger topoviewer: addlayer instead of creating a new project
  • Loading branch information
brushtyler committed Oct 9, 2012
2 parents 1796178 + cb2230e commit 0796871
Show file tree
Hide file tree
Showing 11 changed files with 1,157 additions and 1,155 deletions.
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)
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,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})

0 comments on commit 0796871

Please sign in to comment.