Skip to content

Commit

Permalink
Merge pull request #28 from QGEP/refreshTopology
Browse files Browse the repository at this point in the history
Use QgsTransaction to refresh network topology
  • Loading branch information
m-kuhn authored Nov 2, 2018
2 parents 32627d3 + a522b3f commit 264a4d8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
57 changes: 32 additions & 25 deletions src/tools/qgepnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
from collections import defaultdict
import time
import re
from qgis.PyQt.QtCore import pyqtSignal, QObject
from qgis.PyQt.QtSql import QSqlDatabase, QSqlQuery
from qgis.PyQt.QtCore import (
pyqtSignal,
QObject,
Qt
)

from qgis.core import (
Qgis,
Expand All @@ -47,6 +50,7 @@
NULL
)
from qgis.gui import QgsMessageBar
from qgepplugin.utils.qt_utils import OverrideCursor
import networkx as nx


Expand Down Expand Up @@ -163,34 +167,37 @@ def refresh(self):
Refreshes the network graph. It will force a refresh of the materialized views in the database and then reload
and recreate the graph.
"""
uri = QgsDataSourceUri(self.nodeLayer.dataProvider().dataSourceUri())
with OverrideCursor(Qt.WaitCursor):
transaction = self.nodeLayer.dataProvider().transaction()
temporary_edit_session = False
if not transaction:
self.nodeLayer.startEditing()
temporary_edit_session = True
transaction = self.nodeLayer.dataProvider().transaction()

db = QSqlDatabase.addDatabase("QPSQL") # Name of the driver -- doesn't change
if not transaction:
self.message_emitted.emit(self.tr("Error"), self.tr("Could not initialize transaction"), Qgis.Critical)
return

str_connect_option = "requiressl=0;service=" + uri.service()
db.setConnectOptions(str_connect_option)
query_template = "REFRESH MATERIALIZED VIEW qgep_od.vw_network_segment;"
edge_res, error = transaction.executeSql(query_template)
if not edge_res:
self.message_emitted.emit(self.tr("Error"), error, Qgis.Critical)

if not db.open():
self.message_emitted.emit(self.tr("Warning"), db.lastError().text(), Qgis.Critical)
query_template = "REFRESH MATERIALIZED VIEW qgep_od.vw_network_node;"
node_res, error = transaction.executeSql(query_template)
if not node_res:
self.message_emitted.emit(self.tr("Error"), error, Qgis.Critical)

query_template = "REFRESH MATERIALIZED VIEW qgep_od.vw_network_segment;"
query = QSqlQuery(db)
if not query.exec_(query_template):
str_result = query.lastError().text()
self.message_emitted.emit(self.tr("Warning"), str_result, Qgis.Critical)
else:
self.message_emitted.emit(self.tr("Success"), "vw_network_segment successfully updated", Qgis.Success)
if node_res and edge_res:
self.message_emitted.emit(self.tr("Success"), self.tr("Network successfully updated"), Qgis.Success)

query_template = "REFRESH MATERIALIZED VIEW qgep_od.vw_network_node;"
query = QSqlQuery(db)
if not query.exec_(query_template):
str_result = query.lastError().text()
self.message_emitted.emit(self.tr("Warning"), str_result, Qgis.Critical)
else:
self.message_emitted.emit(self.tr("Success"), "vw_network_node successfully updated", Qgis.Success)
# recreate networkx graph
self.graph.clear()
self.createGraph()
if temporary_edit_session:
self.nodeLayer.commitChanges()

# recreate networkx graph
self.graph.clear()
self.createGraph()

def _profile(self, name):
"""
Expand Down
32 changes: 32 additions & 0 deletions src/utils/qt_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-

"""
/***************************************************************************
-------------------
begin : 2016
copyright : (C) 2016 by OPENGIS.ch
email : info@opengis.ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""

from PyQt5.QtWidgets import QApplication

class OverrideCursor():

def __init__(self, cursor):
self.cursor = cursor

def __enter__(self):
QApplication.setOverrideCursor(self.cursor)

def __exit__(self, exc_type, exc_val, exc_tb):
QApplication.restoreOverrideCursor()

0 comments on commit 264a4d8

Please sign in to comment.