forked from apryet/Qgridder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgridder_dialog_check3D.py
168 lines (135 loc) · 6.06 KB
/
qgridder_dialog_check3D.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
qgridder_dialog_check3D.py
Qgridder - A QGIS plugin
This file handles Qgridder graphical user interface
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 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 PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgridder_dialog_base import QGridderDialog
from ui_qgridder_check3D import Ui_QGridderCheck3D
import ftools_utils
import qgridder_utils
import numpy as np
class QGridderDialogCheck3D(QGridderDialog, Ui_QGridderCheck3D):
"""
Grid creation dialog
"""
def __init__(self,iface, settings):
# Set up the user interface from Designer.
QDialog.__init__(self)
self.iface = iface
self.settings = settings
self.setupUi(self)
# Connect buttons
QObject.connect(self.buttonLayer3DUp, SIGNAL("clicked()"), self.layer3D_up)
QObject.connect(self.buttonLayer3DDown, SIGNAL("clicked()"), self.layer3D_down)
QObject.connect(self.buttonLayer3DRemove, SIGNAL("clicked()"), self.remove_layer3D)
QObject.connect(self.buttonAddNewLayer3D, SIGNAL("clicked()"), self.add_new_layer3D)
QObject.connect(self.buttonAddExistingLayer3D, SIGNAL("clicked()"), self.add_existing_layer3D)
QObject.connect(self.buttonCheck3D, SIGNAL("clicked()"), self.run_check3D)
# Populate model name list
self.populate_layer_list(self.listReferenceGrid)
self.populate_layer_list(self.listExistingLayer)
# ========== update listLayers3D
def update_listLayers3D(self) :
NamesofLayersInMapCanvas = []
# load list of every layers in map canvas
for layer in self.iface.mapCanvas().layers() :
NamesofLayersInMapCanvas.append(layer.name())
# check whether each layer of the listLayers3D list is loaded
itemRemoved = True
while( itemRemoved == True ) :
itemRemoved = False
for row in range(self.listLayers3D.count()) :
if self.listLayers3D.item(row) != None :
if self.listLayers3D.item(row).text() not in NamesofLayersInMapCanvas :
self.listLayers3D.takeItem(row)
itemRemoved = True
break
# ---------- Pseudo 3D grid management --------------------------------
def layer3D_up(self):
itemRow = self.listLayers3D.currentRow()
if itemRow > 0 :
item = self.listLayers3D.takeItem(itemRow)
self.listLayers3D.insertItem(itemRow-1,item)
self.listLayers3D.setCurrentRow(itemRow-1)
def layer3D_down(self):
itemRow = self.listLayers3D.currentRow()
if itemRow < self.listLayers3D.count() - 1 :
item = self.listLayers3D.takeItem(itemRow)
self.listLayers3D.insertItem(itemRow+1,item)
self.listLayers3D.setCurrentRow(itemRow+1)
def remove_layer3D(self):
itemRow = self.listLayers3D.currentRow()
removedItem = self.listLayers3D.takeItem(itemRow)
def add_new_layer3D(self):
# fetch reference grid from listReferenceGrid
ReferenceGridLayerName = self.listReferenceGrid.currentText()
if not ReferenceGridLayerName == "":
ReferenceGridLayer = ftools_utils.getMapLayerByName( unicode( ReferenceGridLayerName ) )
# Copy reference grid to shapefile to user defined location
OutFileName, Encoding = ftools_utils.saveDialog( self )
if (OutFileName, OutFileName) == (None, None) :
return()
# write new layer to shapefile
ftools_utils.writeVectorLayerToShape( ReferenceGridLayer, OutFileName, self.encoding )
# get new layer name
file_info = QFileInfo( OutFileName )
if file_info.exists():
newLayerName = file_info.completeBaseName()
# check whether this layer name is already in the 3D list
if len(self.listLayers3D.findItems(newLayerName,Qt.MatchFixedString)) != 0 :
QMessageBox.information(self, self.tr("Gridder"),
self.tr("This layer is already in the list."))
return()
# Load new layer into map canvas ...
for (name,layer) in QgsMapLayerRegistry.instance().mapLayers().iteritems():
# Note : reload() doesn't work.
if layer.source()==self.OutFileName:
QgsMapLayerRegistry.instance().removeMapLayers( layer.id() )
ftools_utils.addShapeToCanvas( OutFileName )
# add new layer to listLayers3D
self.listLayers3D.addItem(newLayerName)
def add_existing_layer3D(self):
# get layer name
existingGridLayerName = self.listExistingLayer.currentText()
# check whether current layer is not already in the list
if len(self.listLayers3D.findItems(existingGridLayerName,Qt.MatchFixedString)) == 0 :
# add new layer
self.listLayers3D.addItem(existingGridLayerName)
else :
QMessageBox.information(self, self.tr("Gridder"),
self.tr("This layer is already in the list.")
)
def run_check3D(self):
allLayers = []
topoRules = {'model':'newsam', 'nmax':2,'pmax':4}
for row in range( self.listLayers3D.count() ) :
vLayerName = self.listLayers3D.item(row).text()
vLayer = ftools_utils.getMapLayerByName( unicode( vLayerName ) )
allLayers.append(vLayer)
qgridder_utils.correct_pseudo3D_grid(allLayers, topoRules)
QMessageBox.information(self, self.tr("Qgridder"),
self.tr('pseudo-3D grid topology successfully checked and corrected')
)
# Refresh map canvas
self.iface.mapCanvas().refresh()
return()