Coverage for grm\Watershed_dialog.py : 0%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3/***************************************************************************
4 FillSinkDialog
5 A QGIS plugin
6 FillSink plug-in
7 -------------------
8 begin : 2017-03-13
9 git sha : $Format:%H$
10 copyright : (C) 2017 by Hermesys
11 email : shpark@hermesys.co.kr
12 ***************************************************************************/
14/***************************************************************************
15 * *
16 * This program is free software; you can redistribute it and/or modify *
17 * it under the terms of the GNU General Public License as published by *
18 * the Free Software Foundation; either version 2 of the License, or *
19 * (at your option) any later version. *
20 * *
21 ***************************************************************************/
22"""
23import os
25from qgis.core import QgsProject
26from qgis.PyQt import uic
27from qgis.PyQt.QtCore import Qt
28from qgis.PyQt.QtWidgets import QDialog, QMessageBox
30from grm.lib.File_Class import GetFile_Name
31from grm.lib.Util import (
32 GetcomboSelectedLayerPath,
33 GetTxtToLayerPath,
34 MsInfo,
35 SetCommbox,
36)
38FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), "Watershed.ui"))
41class WatershedDialog(QDialog, FORM_CLASS):
42 def __init__(self, _xmltodict={}, _grmDialogFlag=0, parent=None):
43 super(WatershedDialog, self).__init__(parent)
44 self.setupUi(self)
46 self._xmltodict = _xmltodict
47 self._grmDialogFlag = _grmDialogFlag # Setup / Run GRM 창 실행 중인지 확인
49 # 콤보 박스 레이어 목록 셋팅
50 self.SetComboxLayerList()
52 # 2017-06-22 프로젝트 파일에 콤보박스 레이어 목록과 QGIS 레이어 목록 비교하여
53 # 둘이 동일한 파일이 있을시에 콤보 박스에 셋팅 기능
54 self.SetProjectToCombobox()
56 # chkStreamLayer 체크 박스 체크시 이벤트 처리
57 self.chkStreamLayer.setChecked(True) # 기본 설정은 체크
59 # 체크 박스 체크시 비활성화 체크 박스
60 self.chkStreamLayer.stateChanged.connect(self.chkStreamLayer_CheckedChanged)
62 # 체크 박스 클릭 이벤트 연동
63 self.chkiniSoilRatioLayer.stateChanged.connect(
64 self.chkiniSoilRatioLayer_CheckedChanged
65 )
66 self.chkChannelWidthLayer.stateChanged.connect(
67 self.chkChannelWidthLayer_CheckedChanged
68 )
69 self.chkIniFlow.stateChanged.connect(self.chkIniFlow_CheckedChanged)
71 # OK 버튼 처리 이벤트
72 self.btnOK.clicked.connect(self.ClickOK)
74 # 폼 종료 버튼 이벤트 처리
75 self.btnCancel.clicked.connect(self.Close_Form)
77 def SetComboxLayerList(self):
78 # QGIS 레이어 레전드 목록 받아오기
79 layers = QgsProject.instance().mapLayers().values()
80 cmbList = [
81 self.cmbWatershedArea,
82 self.cmbWatershedSlope,
83 self.cmbFdir,
84 self.cmbFac,
85 self.cmbStream,
86 self.cmbChannelWidthLayer,
87 self.cmbiniSoilRatioLayer,
88 self.cmbiniFlowLayer,
89 ]
91 if self._grmDialogFlag == 1:
92 lly = list(layers)
93 remove_set = QgsProject.instance().mapLayersByName("WSFAlayerload")
94 layers = [i for i in lly if i not in remove_set]
96 for cmb in cmbList:
97 SetCommbox(layers, cmb, "tif")
99 # chkStreamLayer 체크 박스 이벤트
100 def chkStreamLayer_CheckedChanged(self):
101 self.cmbStream.setEnabled(self.chkStreamLayer.checkState())
102 self.chkChannelWidthLayer.setEnabled(self.chkStreamLayer.checkState())
103 self.chkIniFlow.setEnabled(self.chkStreamLayer.checkState())
104 self.cmbiniFlowLayer.setEnabled(self.chkIniFlow.checkState())
106 if self.chkStreamLayer.checkState() == False:
107 self.chkChannelWidthLayer.setChecked(False)
108 self.cmbChannelWidthLayer.setEnabled(False)
110 self.chkIniFlow.setChecked(False)
111 self.cmbiniFlowLayer.setEnabled(False)
113 # 2020-01-09 박:이거 뺀이유를 모르겠음(확인 요망)
114 # 2022.01.14 동 : 최박사님께 문의 결과, soil ratio는 stream과 상관없기에 아래 코드는 주석처리하는게 맞다고 함.
115 # self.chkiniSoilRatioLayer.setChecked(False)
116 # self.cmbiniSoilRatioLayer.setEnabled(False)
118 # chkChannelWidthLayer 체크 박스 이벤트
119 def chkChannelWidthLayer_CheckedChanged(self):
120 if self.chkChannelWidthLayer.isChecked():
121 self.cmbChannelWidthLayer.setEnabled(True)
122 else:
123 self.cmbChannelWidthLayer.setEnabled(False)
125 def chkiniSoilRatioLayer_CheckedChanged(self):
126 if self.chkiniSoilRatioLayer.isChecked():
127 self.cmbiniSoilRatioLayer.setEnabled(True)
128 else:
129 self.cmbiniSoilRatioLayer.setEnabled(False)
131 def chkIniFlow_CheckedChanged(self):
132 self.cmbiniFlowLayer.setEnabled(self.chkIniFlow.checkState())
134 def SetProjectToCombobox(self):
135 # 각각의 변수에 XML 값 할당
136 self.mGridWSFPN = self._xmltodict["GRMProject"]["ProjectSettings"]["DomainFile"]
137 if self.mGridWSFPN != "" and self.mGridWSFPN is not None:
138 mGridNameWS = GetFile_Name(self.mGridWSFPN)
139 self.Setcombobox(self.cmbWatershedArea, mGridNameWS, self.mGridWSFPN)
141 # Slop
142 self.mGridSlopeFPN = self._xmltodict["GRMProject"]["ProjectSettings"][
143 "SlopeFile"
144 ]
145 if self.mGridSlopeFPN != "" and self.mGridSlopeFPN is not None:
146 mGridNameSlope = GetFile_Name(self.mGridSlopeFPN)
147 self.Setcombobox(self.cmbWatershedSlope, mGridNameSlope, self.mGridSlopeFPN)
149 # Flowdirection
150 self.mGridFdirFPN = self._xmltodict["GRMProject"]["ProjectSettings"][
151 "FlowDirectionFile"
152 ]
153 if self.mGridFdirFPN != "" and self.mGridFdirFPN is not None:
154 mGridNameFdir = GetFile_Name(self.mGridFdirFPN)
155 self.Setcombobox(self.cmbFdir, mGridNameFdir, self.mGridFdirFPN)
157 # FlowAC
158 self.mGridFacFPN = self._xmltodict["GRMProject"]["ProjectSettings"][
159 "FlowAccumFile"
160 ]
161 if self.mGridFacFPN != "" and self.mGridFacFPN is not None:
162 mGridNameFac = GetFile_Name(self.mGridFacFPN)
163 self.Setcombobox(self.cmbFac, mGridNameFac, self.mGridFacFPN)
165 # Stream
166 self.mGridStreamFPN = self._xmltodict["GRMProject"]["ProjectSettings"][
167 "StreamFile"
168 ]
169 if self.mGridStreamFPN != "" and self.mGridStreamFPN is not None:
170 mGridNameStream = GetFile_Name(self.mGridStreamFPN)
171 self.Setcombobox(self.cmbStream, mGridNameStream, self.mGridStreamFPN)
173 # Channel
174 # 파싱 방법을 바꿔야 하는데 임시로 처리 흠 흠.....
175 # 프로젝트 파일에서 null 처리
176 self.mGridChannelWidthFPN = self._xmltodict["GRMProject"]["ProjectSettings"][
177 "ChannelWidthFile"
178 ]
180 if self.mGridChannelWidthFPN is None or self.mGridChannelWidthFPN == "":
181 self.chkChannelWidthLayer.setChecked(False)
182 self.cmbChannelWidthLayer.setEnabled(False)
183 else:
184 self.chkChannelWidthLayer.setChecked(True)
185 self.cmbChannelWidthLayer.setEnabled(True)
186 mGridNameChannelWidth = GetFile_Name(self.mGridChannelWidthFPN)
187 self.Setcombobox(
188 self.cmbChannelWidthLayer,
189 mGridNameChannelWidth,
190 self.mGridChannelWidthFPN,
191 )
193 self.miniSoilRatio = self._xmltodict["GRMProject"]["ProjectSettings"][
194 "InitialSoilSaturationRatioFile"
195 ]
196 if self.miniSoilRatio is not None and self.miniSoilRatio != "":
197 self.chkiniSoilRatioLayer.setChecked(True)
198 self.cmbiniSoilRatioLayer.setEnabled(True)
199 name = GetFile_Name(self.miniSoilRatio)
200 self.Setcombobox(self.cmbiniSoilRatioLayer, name, self.miniSoilRatio)
201 else:
202 self.chkiniSoilRatioLayer.setChecked(False)
203 self.cmbiniSoilRatioLayer.setEnabled(False)
205 self.InitialChannelFlowFile = self._xmltodict["GRMProject"]["ProjectSettings"][
206 "InitialChannelFlowFile"
207 ]
208 if (
209 self.InitialChannelFlowFile is not None
210 and self.InitialChannelFlowFile != ""
211 ):
212 self.chkIniFlow.setChecked(True)
213 self.cmbiniFlowLayer.setEnabled(True)
214 name = GetFile_Name(self.InitialChannelFlowFile)
215 self.Setcombobox(self.cmbiniFlowLayer, name, self.InitialChannelFlowFile)
216 else:
217 self.chkIniFlow.setChecked(False)
218 self.cmbiniFlowLayer.setEnabled(False)
220 # FD
221 self.mFDType = self._xmltodict["GRMProject"]["ProjectSettings"][
222 "FlowDirectionType"
223 ]
225 if self.mFDType == "StartsFromN":
226 self.rbFDIndexTypeN.setChecked(True)
227 elif self.mFDType == "StartsFromNE":
228 self.rbFDIndexTypeNE.setChecked(True)
229 elif self.mFDType == "StartsFromE":
230 self.rdioEAST.setChecked(True)
231 else:
232 self.rdioTaudem.setChecked(True)
234 # 콤보 박스에 있는 목록중에 프로젝트 상에 있는 레이어 비교 하여 셋팅
235 def Setcombobox(self, commbox, layername, filepath):
236 index = commbox.findText(str(layername), Qt.MatchFixedString)
237 if GetTxtToLayerPath(layername) == filepath:
238 if index >= 0:
239 commbox.setCurrentIndex(index)
241 # OK 버튼 이벤트
242 def ClickOK(self):
243 # 콤보 박스와 체크 박스등 기본적인 선택 내용 확인 하여 오류 메시지 출력
244 try:
245 flag = self.ValidateInputs()
246 if flag:
247 raise Exception
248 # 선택된 값들 글로벌 변수에 값을 넣음 나중에 값을 XML에 넣을 것임
249 self.mGridWSFPN = GetcomboSelectedLayerPath(self.cmbWatershedArea)
250 self.mGridSlopeFPN = GetcomboSelectedLayerPath(self.cmbWatershedSlope)
251 self.mGridFdirFPN = GetcomboSelectedLayerPath(self.cmbFdir)
252 self.mGridFacFPN = GetcomboSelectedLayerPath(self.cmbFac)
254 if self.rbFDIndexTypeN.isChecked() == True:
255 self.mFDType = "StartsFromN"
256 elif self.rbFDIndexTypeNE.isChecked() == True:
257 self.mFDType = "StartsFromNE"
258 elif self.rdioEAST.isChecked() == True:
259 self.mFDType = "StartsFromE"
260 else:
261 self.mFDType = "StartsFromE_TauDEM"
263 if self.chkiniSoilRatioLayer.checkState():
264 if self.cmbiniSoilRatioLayer.currentIndex() != 0:
265 self.miniSoilRatio = GetcomboSelectedLayerPath(
266 self.cmbiniSoilRatioLayer
267 )
268 else:
269 self.miniSoilRatio = ""
271 if self.chkStreamLayer.checkState():
272 self.mGridStreamFPN = GetcomboSelectedLayerPath(self.cmbStream)
273 if self.chkChannelWidthLayer.checkState():
274 if self.cmbChannelWidthLayer.currentIndex() != 0:
275 self.mGridChannelWidthFPN = GetcomboSelectedLayerPath(
276 self.cmbChannelWidthLayer
277 )
278 else:
279 self.mGridChannelWidthFPN = ""
281 if self.chkIniFlow.checkState():
282 if self.cmbiniFlowLayer.currentIndex() != 0:
283 self.InitialChannelFlowFile = GetcomboSelectedLayerPath(
284 self.cmbiniFlowLayer
285 )
286 else:
287 self.InitialChannelFlowFile = ""
289 self.SetXML()
290 except Exception:
291 pass
292 else:
293 quit_msg = " Watershed setup is completed. "
294 reply = QMessageBox.information(self, "Watershed", quit_msg, QMessageBox.Ok)
295 if reply == QMessageBox.Ok:
296 self.close()
298 def ValidateInputs(self):
299 # 각각 콥보 박스중 선택된것이 없을때 메시지 출력및 포커스 이동
300 try:
301 if self.cmbWatershedArea.currentIndex() == 0:
302 self.cmbWatershedArea.setFocus()
303 raise Exception("\n No layer selected. \n")
304 elif self.cmbWatershedSlope.currentIndex() == 0:
305 self.cmbWatershedSlope.setFocus()
306 raise Exception("\n No layer selected. \n")
307 elif self.cmbFdir.currentIndex() == 0:
308 self.cmbFdir.setFocus()
309 raise Exception("\n No layer selected. \n")
310 elif self.cmbFac.currentIndex() == 0:
311 self.cmbFac.setFocus()
312 raise Exception("\n No layer selected. \n")
313 elif self.cmbStream.currentIndex() == 0:
314 self.cmbStream.setFocus()
315 raise Exception("\n No layer selected. \n")
317 except Exception as exce:
318 MsInfo(exce.args[0])
319 return True
321 def SetXML(self):
322 # WatershedFile
323 self._xmltodict["GRMProject"]["ProjectSettings"]["DomainFile"] = self.mGridWSFPN
324 # Slop
325 self._xmltodict["GRMProject"]["ProjectSettings"][
326 "SlopeFile"
327 ] = self.mGridSlopeFPN
328 # Flowdirection
329 self._xmltodict["GRMProject"]["ProjectSettings"][
330 "FlowDirectionFile"
331 ] = self.mGridFdirFPN
332 # FlowAccu
333 self._xmltodict["GRMProject"]["ProjectSettings"][
334 "FlowAccumFile"
335 ] = self.mGridFacFPN
336 # FDType
337 self._xmltodict["GRMProject"]["ProjectSettings"][
338 "FlowDirectionType"
339 ] = self.mFDType
340 # Stream
342 if self.chkIniFlow.checkState():
343 self._xmltodict["GRMProject"]["ProjectSettings"][
344 "InitialChannelFlowFile"
345 ] = self.InitialChannelFlowFile
346 else:
347 self._xmltodict["GRMProject"]["ProjectSettings"][
348 "InitialChannelFlowFile"
349 ] = ""
351 if self.chkStreamLayer.checkState():
352 self._xmltodict["GRMProject"]["ProjectSettings"][
353 "StreamFile"
354 ] = self.mGridStreamFPN
355 else:
356 self._xmltodict["GRMProject"]["ProjectSettings"]["StreamFile"] = ""
358 # ChannelWidthFile
359 if self.chkChannelWidthLayer.checkState() and self.chkStreamLayer.checkState():
360 self._xmltodict["GRMProject"]["ProjectSettings"][
361 "ChannelWidthFile"
362 ] = self.mGridChannelWidthFPN
363 else:
364 self._xmltodict["GRMProject"]["ProjectSettings"]["ChannelWidthFile"] = ""
366 if self.chkiniSoilRatioLayer.checkState() and self.chkStreamLayer.checkState():
367 self._xmltodict["GRMProject"]["ProjectSettings"][
368 "InitialSoilSaturationRatioFile"
369 ] = self.miniSoilRatio
370 else:
371 self._xmltodict["GRMProject"]["ProjectSettings"][
372 "InitialSoilSaturationRatioFile"
373 ] = ""
375 # 프로그램 종료
376 def Close_Form(self):
377 self.close()