/
qgis_edb_dockwidget.py
278 lines (244 loc) · 9.84 KB
/
qgis_edb_dockwidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
"""
/***************************************************************************
AirviroOfflineEdbDockWidget
A QGIS plugin
Editing Airviro emission databases offline
-------------------
begin : 2015-11-10
git sha : $Format:%H$
copyright : (C) 2015 by David Segersson
email : david.segersson@smhi.se
***************************************************************************/
/***************************************************************************
* *
* 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 __future__ import unicode_literals
from __future__ import division
import os
# import qgis first to ensure sip.api is set for QString and QVariant
from qgis.core import (
QgsVectorLayer,
QgsProject,
QgsDataSourceURI,
QgsMapLayerRegistry,
QgsMapLayer,
QgsRelation,
QgsCoordinateReferenceSystem,
QgsMessageLog
)
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from PyQt4 import uic
from PyQt4.QtCore import pyqtSignal
from PyQt4.QtGui import QFileDialog, QDockWidget
# from pyAirviro.edb.edb import Edb, SerialEdb, is_serial_edb
from pyAirviro.edb.sqliteapi import (
connect,
get_epsg,
get_foreign_keys,
load_edb,
initdb,
create_emission_views,
create_emission_tables,
TABLES,
GEOMETRY_TABLES_COLUMNS,
table_in_db
)
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'qgis_edb_dockwidget_base.ui'))
class AirviroOfflineEdbDockWidget(QDockWidget, FORM_CLASS):
closingPlugin = pyqtSignal()
def __init__(self, parent=None):
"""Constructor."""
super(AirviroOfflineEdbDockWidget, self).__init__(parent)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
self.create_db_lineedit.clear()
self.create_db_browse_btn.clicked.connect(
self.select_save_db_filename
)
self.open_db_lineedit.clear()
self.open_db_browse_btn.clicked.connect(
self.select_open_db_filename
)
self.open_edb_btn.clicked.connect(
self.open_db
)
self.db_uri = QgsDataSourceURI()
self.con = None
self.cur = None
self.epsg = None
self.layers = {}
def select_save_db_filename(self):
filename = QFileDialog.getSaveFileName(
self,
"Select offline edb",
"",
'*.sqlite'
)
self.create_db_lineedit.setText(filename)
def select_open_db_filename(self):
filename = QFileDialog.getOpenFileName(
self,
"Select offline edb",
"",
'*.sqlite'
)
self.open_db_lineedit.setText(filename)
def open_db(self):
edb_filename = self.open_db_lineedit.text()
edb_name, ext = os.path.splitext(os.path.basename(str(edb_filename)))
QgsMessageLog.logMessage(
"Loading edb %s" % edb_filename,
'AirviroOfflineEdb',
QgsMessageLog.INFO
)
self.db_uri.setDatabase(edb_filename)
self.con, self.cur = connect(str(self.db_uri.database()))
self.epsg = get_epsg(self.con)
root = QgsProject.instance().layerTreeRoot()
edb_increment = 1
while root.findGroup(edb_name) is not None:
edb_name = edb_name + unicode(edb_increment)
edb_increment += 1
QgsMessageLog.logMessage(
"Adding edb layers in %s" % edb_name,
'AirviroOfflineEdb',
QgsMessageLog.INFO
)
edb_group = root.addGroup(edb_name)
point_group = edb_group.addGroup('Point sources')
area_group = edb_group.addGroup('Area sources')
grid_group = edb_group.addGroup('Grid sources')
road_group = edb_group.addGroup('Road sources')
subtable_group = edb_group.addGroup('Subtables')
company_group = edb_group.addGroup('Companies')
facility_group = edb_group.addGroup('Facilities')
emis_group = edb_group.addGroup('Emissions')
point_support_group = point_group.addGroup('Support tables')
area_support_group = area_group.addGroup('Support tables')
grid_support_group = grid_group.addGroup('Support tables')
road_support_group = road_group.addGroup('Support tables')
facility_support_group = facility_group.addGroup('Support tables')
company_support_group = company_group.addGroup('Support tables')
unit_group = subtable_group.addGroup('Units')
road_vehicle_group = subtable_group.addGroup('Road vehicles')
road_vehicle_support_group = road_vehicle_group.addGroup(
'Support tables'
)
roadtype_group = subtable_group.addGroup('Roadtypes')
emis_func_group = subtable_group.addGroup('Emission functions')
searchkey_group = subtable_group.addGroup('Searchkeys')
timevar_group = subtable_group.addGroup('Time variations')
subgrp_group = subtable_group.addGroup('Substance groups')
self.layers = {}
schema = ''
geom_table_column_dict = dict(GEOMETRY_TABLES_COLUMNS)
for table in TABLES:
if not table_in_db(self.cur, table):
iface.messageBar().pushMessage(
"Warning",
"Table %s not found in edb" % table,
level=QgsMessageBar.WARNING,
duration=3
)
continue
geom_col = geom_table_column_dict.get(table, None)
self.db_uri.setDataSource(
schema,
table,
geom_col or ''
)
layer_uri = self.db_uri.uri() # + "&crs=EPSG:4326"
layer = QgsVectorLayer(layer_uri, table, 'spatialite')
layer.setCrs(QgsCoordinateReferenceSystem(
self.epsg,
QgsCoordinateReferenceSystem.EpsgCrsId)
)
if not layer.isValid():
raise ValueError(edb_filename)
map_layer = QgsMapLayerRegistry.instance().addMapLayer(
layer, False
)
if 'timevar' in table:
group = timevar_group
elif 'emission_function' in table:
group = emis_func_group
elif 'searchkey' in table:
group = searchkey_group
elif 'unit' in table:
group = unit_group
elif 'subgrp' in table:
group = subgrp_group
elif table == 'substances':
group = subtable_group
elif table.endswith('_emis'):
group = emis_group
elif table == 'points':
group = point_group
elif 'point_' in table:
group = point_support_group
elif table == 'areas':
group = area_group
elif 'area_' in table:
group = area_support_group
elif table == 'roads':
group = road_group
elif table in ('road_vehicle_link', 'road_alobs'):
group = road_support_group
elif 'road_' in table:
group = road_vehicle_group
elif 'roadtype' in table:
group = roadtype_group
elif table == 'facilties':
group = facility_group
elif 'facility' in table:
group = facility_support_group
elif 'companies' == table:
group = company_group
elif 'company' in table:
group = company_support_group
elif 'traffic_situation' in table:
group = road_vehicle_support_group
group.setVisible(False)
group.setExpanded(False)
group.addLayer(map_layer)
self.layers[table] = map_layer.id()
for table in TABLES:
foreign_keys = get_foreign_keys(self.con, table)
referencing_layer = self.layers[table]
for row in foreign_keys:
referenced_layer = self.layers[row['table']]
from_column = row['from']
to_column = row['to']
rel = QgsRelation()
rel.setReferencingLayer(referencing_layer)
rel.setReferencedLayer(referenced_layer)
rel.addFieldPair(from_column, to_column)
rel_name = 'fk_%s_%s-%s_%s' % (
table, from_column, row['table'], to_column
)
rel.setRelationId(rel_name)
rel.setRelationName(
'fk_%s_%s-%s_%s' % (
table, from_column, row['table'], to_column)
)
if not rel.isValid():
raise ValueError(
'Reference %s is invalid' % rel_name
)
QgsProject.instance().relationManager().addRelation(rel)
def closeEvent(self, event):
self.closingPlugin.emit()
event.accept()