forked from apryet/Qgridder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgridder.py
363 lines (306 loc) · 11.3 KB
/
qgridder.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
"""
/***************************************************************************
qgridder.py
Qgridder - A QGIS plugin
Plugin main file.
Qgridder Builds 2D regular and unstructured grids and comes together with
pre- and post-processing capabilities for spatially distributed modeling.
-------------------
begin : 2013-04-08
copyright : (C) 2013 by Pryet
email : alexandre.pryet@ensegid.fr
***************************************************************************/
This plugin uses functions from fTools
Copyright (C) 2008-2011 Carson Farmer
EMAIL: carson.farmer (at) gmail.com
WEB : http://www.ftools.ca/fTools.html
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
# Import PyQt and QGIS modules
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialogs
from qgridder_dialog_new import QGridderDialogNew
from qgridder_dialog_refinement import QGridderDialogRefinement
from qgridder_dialog_check3D import QGridderDialogCheck3D
from qgridder_dialog_preproc import QGridderDialogPreProc
from qgridder_dialog_plot import QGridderDialogPlot
from qgridder_dialog_export import QGridderDialogExport
from qgridder_dialog_settings import QGridderDialogSettings
# ---------------------------------------------
class QGridder:
"""
Description
-----------
Qgridder plugin class
"""
def __init__(self, iface):
"""
Description
-----------
Initialize Qgridder plugin
"""
# Save reference to the QGIS interface
self.iface = iface
# load settings
self.settings = QgridderSettings()
# Create the dialogs and keep reference
self.dlg_new = QGridderDialogNew(self.iface, self.settings)
self.dlg_refinement = QGridderDialogRefinement(self.iface, self.settings)
self.dlg_check3D = QGridderDialogCheck3D(self.iface, self.settings)
self.dlg_plot = QGridderDialogPlot(self.iface, self.settings)
self.dlg_export = QGridderDialogExport(self.iface, self.settings)
self.dlg_settings = QGridderDialogSettings(self.iface, self.settings)
self.dlg_preproc = QGridderDialogPreProc(self.iface, self.settings)
# Initialize menu
self.qgridder_menu = None
# initialize plugin directory
self.plugin_dir = QFileInfo(QgsApplication.qgisUserDbFilePath()).path() + "/python/plugins/Qgridder"
# initialize locale
localePath = ""
locale = QSettings().value("locale/userLocale")[0:2]
if QFileInfo(self.plugin_dir).exists():
localePath = self.plugin_dir + "/i18n/Qgridder_" + locale + ".qm"
if QFileInfo(localePath).exists():
self.translator = QTranslator()
self.translator.load(localePath)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
def initGui(self):
"""
Description
-----------
Initialize plugin Gui : dialogs, icons and menu
"""
#Create actions that will set up plugin
self.action_new = QAction(QIcon(":/plugins/Qgridder/icon_new.png"), \
"New grid", self.iface.mainWindow())
self.action_refinement = QAction(QIcon(":/plugins/Qgridder/icon_refinement.png"), \
"Refine grid", self.iface.mainWindow())
self.action_check3D = QAction(QIcon(":/plugins/Qgridder/icon_check3D.png"), \
"3D grid", self.iface.mainWindow())
self.action_plot = QAction(QIcon(":/plugins/Qgridder/icon_plot.png"), \
"Plot chart", self.iface.mainWindow())
self.action_export = QAction(QIcon(":/plugins/Qgridder/icon_export.png"), \
"Export", self.iface.mainWindow())
self.action_settings = QAction(QIcon(":/plugins/Qgridder/icon_settings.png"), \
"Settings", self.iface.mainWindow())
self.action_preproc = QAction(QIcon(":/plugins/Qgridder/icon_pproc.png"), \
"Settings", self.iface.mainWindow())
# connect the action to the run method
QObject.connect(self.action_new, SIGNAL("activated()"), self.run_new)
QObject.connect(self.action_refinement, SIGNAL("activated()"), self.run_refinement)
QObject.connect(self.action_check3D, SIGNAL("activated()"), self.run_check3D)
QObject.connect(self.action_plot, SIGNAL("activated()"), self.run_plot)
QObject.connect(self.action_export, SIGNAL("activated()"), self.run_export)
QObject.connect(self.action_settings, SIGNAL("activated()"), self.run_settings)
QObject.connect(self.action_preproc, SIGNAL("activated()"), self.run_preproc)
# Add toolbar buttons
self.iface.addToolBarIcon(self.action_new)
self.iface.addToolBarIcon(self.action_refinement)
self.iface.addToolBarIcon(self.action_check3D)
self.iface.addToolBarIcon(self.action_plot)
self.iface.addToolBarIcon(self.action_export)
self.iface.addToolBarIcon(self.action_settings)
self.iface.addToolBarIcon(self.action_preproc)
# Add menu items
self.iface.addPluginToMenu("Qgridder", self.action_new)
self.iface.addPluginToMenu("Qgridder", self.action_refinement)
self.iface.addPluginToMenu("Qgridder", self.action_check3D)
self.iface.addPluginToMenu("Qgridder", self.action_plot)
self.iface.addPluginToMenu("Qgridder", self.action_export)
self.iface.addPluginToMenu("Qgridder", self.action_settings)
self.iface.addPluginToMenu("Qgridder", self.action_preproc)
def unload(self):
"""
Description
-----------
Remove the plugin menu item and icon
"""
self.iface.removeToolBarIcon(self.action_new)
self.iface.removeToolBarIcon(self.action_refinement)
self.iface.removeToolBarIcon(self.action_check3D)
self.iface.removeToolBarIcon(self.action_plot)
self.iface.removeToolBarIcon(self.action_export)
self.iface.removeToolBarIcon(self.action_settings)
self.iface.removeToolBarIcon(self.action_preproc)
self.iface.removePluginMenu("Qgridder", self.action_new)
self.iface.removePluginMenu("Qgridder", self.action_refinement)
self.iface.removePluginMenu("Qgridder", self.action_check3D)
self.iface.removePluginMenu("Qgridder", self.action_plot)
self.iface.removePluginMenu("Qgridder", self.action_export)
self.iface.removePluginMenu("Qgridder", self.action_settings)
self.iface.removePluginMenu("Qgridder", self.action_preproc)
def run_new(self):
"""
Description
-----------
Launch grid creation dialog
"""
# update settings
self.settings.load_settings( QgsProject.instance() )
# Populate layer list
self.dlg_new.populate_layer_list(self.dlg_new.listSourceLayer)
# Get update extents from map canvas
self.dlg_new.update_from_canvas()
# show the dialog
self.dlg_new.show()
# Run the dialog event loop
result = self.dlg_new.exec_()
def run_refinement(self):
"""
Description
-----------
Launch grid refinement dialog
"""
# update settings
self.settings.load_settings( QgsProject.instance() )
# Populate layer list
self.dlg_refinement.populate_layer_list(self.dlg_refinement.listGridLayer)
# show the dialog
self.dlg_refinement.show()
# Run the dialog event loop
result = self.dlg_refinement.exec_()
def run_check3D(self):
"""
Description
-----------
Launch pseudo 3D grid topology check dialog
"""
# update settings
self.settings.load_settings( QgsProject.instance() )
# Populate layer list
self.dlg_check3D.populate_layer_list(self.dlg_check3D.listReferenceGrid)
self.dlg_check3D.populate_layer_list(self.dlg_check3D.listExistingLayer)
# show the dialog
self.dlg_check3D.show()
# Run the dialog event loop
result = self.dlg_check3D.exec_()
def run_plot(self):
"""
Description
-----------
Launch plot chart dialog
"""
# update settings
self.settings.load_settings( QgsProject.instance() )
# show the dialog
self.dlg_plot.show()
# plot data
self.dlg_plot.run_plot()
# Run the dialog event loop
result = self.dlg_plot.exec_()
def run_preproc(self):
"""
Description
-----------
Launch pre-processing dialog
"""
# update settings
self.settings.load_settings( QgsProject.instance() )
# update layer list
self.dlg_preproc.populate_layer_list(self.dlg_preproc.listGridLayer)
# show the dialog
self.dlg_preproc.show()
# Run the dialog event loop
result = self.dlg_preproc.exec_()
def run_export(self):
"""
Description
-----------
Launch grid creation dialog
"""
# update settings
self.settings.load_settings( QgsProject.instance() )
# Populate layer list
self.dlg_export.populate_layer_list(self.dlg_export.listGridLayer)
# show the dialog
self.dlg_export.show()
# Run the dialog event loop
result = self.dlg_export.exec_()
def run_settings(self):
"""
Description
-----------
Launch grid creation dialog
"""
# update settings
self.settings.load_settings( QgsProject.instance() )
# refresh dialog with current settings
self.dlg_settings.load_settings_to_dialog()
# show the dialog
self.dlg_settings.show()
# Run the dialog event loop
result = self.dlg_settings.exec_()
class QgridderSettings :
"""
Description
-----------
Qgridder settings
"""
def __init__(self) :
# link to project instance
self.proj = QgsProject.instance()
# initialize settings dictionary
self.dic_settings = {}
# support model types (grid topology)
self.model_types = ['Modflow','Nested']
# initialize list of grid backups
self.list_grid_bckup = []
# load settings from Qgis project
self.load_settings(self.proj)
def load_settings(self, proj) :
"""
Description
-----------
Load settings from Qgis project
"""
# default settings
dic_default_settings = { 'model_type':'Modflow',
'obs_dir':'./',
'simul_file':'simul.file',
'model_src_dir':'./',
'simul_start_date':'2015-01-01 00:00',
'support_grid_layer_name' : 'grid',
'plot_obs':'True',
'plot_simul':'False',
'grid_backup':'True',
'max_grid_backup':'5'
}
# load settings from Qgis project
for key in dic_default_settings.keys() :
entry_value, valid_entry = self.proj.readEntry('qgridder', str(key))
print(str(entry_value) + str(valid_entry) )
if entry_value == '' :
self.dic_settings[key] = dic_default_settings[key]
else :
self.dic_settings[key] = entry_value
return()
def save_settings(self, proj) :
"""
Description
-----------
Save settings to Qgis project
"""
success = True
for key,val in zip(self.dic_settings.keys(),self.dic_settings.values()) :
success = success*self.proj.writeEntry('qgridder', str(key), str(val))
return(success)
def update_settings(self, dic_settings) :
"""
Description
-----------
Update settings from Qgis project
"""
self.dic_settings = dic_settings