-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlateauStatisticsVisualizationPlugin.py
More file actions
383 lines (321 loc) · 17.2 KB
/
Copy pathPlateauStatisticsVisualizationPlugin.py
File metadata and controls
383 lines (321 loc) · 17.2 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""
/***************************************************************************
PlateauStatisticsVisualizationPlugin
A QGIS plugin
Urban Structure Assessment Dashboard
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2024-08-29
git sha : $Format:%H$
copyright : (C) 2024 by Author
email : mail
***************************************************************************/
/***************************************************************************
* *
* 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 os.path
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt # pylint: disable=import-error
from qgis.PyQt.QtGui import QIcon # pylint: disable=import-error
from qgis.PyQt.QtWidgets import QAction # pylint: disable=import-error
from qgis.core import QgsApplication
from .resources import * # pylint: disable=wildcard-import, unused-wildcard-import
from .PlateauStatisticsVisualizationPlugin_dockwidget import (
PlateauStatisticsVisualizationPluginDockWidget
)
from .processing_provider import PlateauProcessingProvider
from .functions.create_directory import CreateDirectory
from .functions.metric_calculation import MetricCalculation
from .functions.visualization import ControlDock, GraphDock, MainGraphDock, RevisedAreaGraphDock
from .functions.output import Output
class PlateauStatisticsVisualizationPlugin:
"""
QGISプラグインの実装
属性:
iface (QgisInterface): QGISインターフェースへの参照。
plugin_dir (str): プラグインのディレクトリパス。
translator (QTranslator): 翻訳用のQTranslatorオブジェクト。
actions (list): プラグインで作成したアクションのリスト。
menu (str): プラグインのメニュー名。
toolbar (QToolBar): プラグインのツールバー。
pluginIsActive (bool): プラグインがアクティブかどうかを示すフラグ。
dockwidget (PlateauStatisticsVisualizationPluginDockWidget):
プラグインのDockWidget。
control_dock (ControlDock): 可視化制御のDockWidget。
main_graph_dock (GraphDock): メイングラフ表示のDockWidget。
revised_area_graph_dock (GraphDock): 修正区域グラフ表示のDockWidget。
sub_graph_dock (GraphDock): 比較用グラフ表示のDockWidget。
control_dock_called (bool): control_dockが呼ばれたかどうかを示すフラグ。
メソッド:
__init__(self, iface): QGISプラグインを初期化します。
tr(self, message): Qtの翻訳APIを使用して文字列の翻訳を取得します。
add_action(self, icon_path, text, callback, enabled_flag=True,
add_to_menu=True, add_to_toolbar=True, status_tip=None,
whats_this=None, parent=None): ツールバーにツールバーアイコンを追加します。
initGui(self): QGIS GUI内でメニューエントリとツールバーアイコンを作成します。
onClosePlugin(self): プラグインが閉じられたときに必要なクリーンアップを行います。
unload(self): プラグインをQGIS GUIから削除します。
run(self): プラグインをロードして起動する関数。
_createDir(self): CreateDirectoryダイアログを使用してディレクトリ構造を作成する関数。
_calcMetric(self): MetricCalculationダイアログを使用して指標計算を実行する関数。
_visualizing(self): ControlDockおよびGraphDockを開き、可視化を行う関数。
_output(self): データをエクスポートするためのOutputダイアログを開く関数。
"""
def __init__(self, iface):
# QGISインターフェースへの参照を保存
self.iface = iface
# プラグインディレクトリを初期化
self.plugin_dir = os.path.dirname(__file__)
# ロケールを初期化
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
f'PlateauStatisticsVisualizationPlugin_{locale}.qm')
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
# インスタンス属性を宣言
self.actions = []
self.menu = self.tr('&Plateau Statistics Visualization Plugin')
self.toolbar = self.iface.addToolBar(
'PlateauStatisticsVisualizationPlugin'
)
self.toolbar.setObjectName('PlateauStatisticsVisualizationPlugin')
#print "** INITIALIZING PlateauStatisticsVisualizationPlugin"
self.pluginIsActive = False
self.dockwidget = None
self.control_dock = None
self.main_graph_dock = None
self.revised_area_graph_dock = None
self.sub_graph_dock = None
self.control_dock_called = False
# noinspection PyMethodMayBeStatic
def tr(self, message):
"""
Qtの翻訳APIを使用して文字列の翻訳を取得します。
QObjectを継承していないため、これを独自に実装しています。
:param message: 翻訳する文字列。
:type message: str
:returns: 翻訳されたメッセージ。
:rtype: str
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate(
'PlateauStatisticsVisualizationPlugin', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
"""
ツールバーにツールバーアイコンを追加します
:param icon_path: このアクションのアイコンのパス。
リソースパス(例:':/plugins/foo/bar.png')または通常のファイルシステムパスを指定可能です。
:type icon_path: str
:param text: このアクションのメニュー項目に表示されるテキスト。
:type text: str
:param callback: アクションがトリガーされたときに呼び出される関数。
:type callback: function
:param enabled_flag: アクションがデフォルトで有効であるべきかを示すフラグ。デフォルトはTrueです。
:type enabled_flag: bool
:param add_to_menu: アクションをメニューにも追加するかどうかを示すフラグ。デフォルトはTrueです。
:type add_to_menu: bool
:param add_to_toolbar: アクションをツールバーにも追加するかどうかを示すフラグ。デフォルトはTrueです。
:type add_to_toolbar: bool
:param status_tip: マウスポインタがアクション上にあるときにポップアップで表示されるオプションのテキスト。
:type status_tip: str
:param parent: 新しいアクションの親ウィジェット。デフォルトはNoneです。
:type parent: QWidget
:param whats_this: マウスポインタがアクション上にあるときにステータスバーに表示されるオプションのテキスト。
:returns: 作成されたアクション。作成されたアクションはself.actionsリストにも追加されます。
:rtype: QAction
"""
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
self.toolbar.addAction(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""QGIS GUI内でメニューエントリとツールバーアイコンを作成します"""
# プロセシングプロバイダーを登録
self.provider = PlateauProcessingProvider()
QgsApplication.processingRegistry().addProvider(self.provider)
icon_path = ':/plugins/PlateauStatisticsVisualizationPlugin/icon.png'
self.add_action(
icon_path,
text=self.tr('Plateau Statistics Visualization Plugin'),
callback=self.run,
parent=self.iface.mainWindow())
#--------------------------------------------------------------------------
def onClosePlugin(self):
"""プラグインのダイアログウィジェットが閉じられたときに必要なクリーンアップを行います"""
#print "** CLOSING PlateauStatisticsVisualizationPlugin"
# 接続を解除
self.dockwidget.closingPlugin.disconnect(self.onClosePlugin)
# dockwidgetを残す場合は、このステートメントを削除する。
# プラグインが再び開かれた場合に再利用するため
# # ドッキングされたウィンドウを閉じるとQGISがクラッシュするため、次のステートメントはコメントアウトされています
# self.dockwidget = None
self.pluginIsActive = False
def unload(self):
"""プラグインのメニュー項目とアイコンをQGIS GUIから削除します"""
#print "** UNLOAD PlateauStatisticsVisualizationPlugin"
# プロセシングプロバイダーを削除
if hasattr(self, 'provider'):
QgsApplication.processingRegistry().removeProvider(self.provider)
for action in self.actions:
self.iface.removePluginMenu(
self.tr('&Plateau Statistics Visualization Plugin'),
action)
self.iface.removeToolBarIcon(action)
# ツールバーを削除する
del self.toolbar
#--------------------------------------------------------------------------
def run(self):
"""プラグインをロードして起動する関数"""
if not self.pluginIsActive:
self.pluginIsActive = True
#print "** STARTING PlateauStatisticsVisualizationPlugin"
# dockwidgetが存在しない場合があります:
# - プラグインの最初の実行
# - 閉じると削除される(self.onClosePlugin メソッドを参照)
if self.dockwidget is None:
# dockwidgetを作成(翻訳後)して参照を保持します
self.dockwidget = (
PlateauStatisticsVisualizationPluginDockWidget()
)
self.dockwidget.buttonCreateDirectory.clicked.connect(
self._createDir
)
self.dockwidget.buttonMetricCalculation.clicked.connect(
self._calcMetric
)
self.dockwidget.buttonVisualize.clicked.connect(
self._visualizing
)
self.dockwidget.buttonOutput.clicked.connect(self._output)
# dockwidgetを閉じたときにクリーンアップを提供するために接続します
self.dockwidget.closingPlugin.connect(self.onClosePlugin)
# dockwidgetを表示
self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dockwidget)
self.dockwidget.show()
def _createDir(self):
"""CreateDirectoryダイアログを使用してディレクトリ構造を作成する関数"""
createDirectory = CreateDirectory(self.translator)
createDirectory.exec_()
def _calcMetric(self):
"""MetricCalculationダイアログを使用して指標計算を実行する関数"""
metricCalculation = MetricCalculation(self.translator)
metricCalculation.calculationFinished.connect(self._on_metric_calculation_finished)
metricCalculation.exec_()
def _on_metric_calculation_finished(self):
"""評価指標算出完了時にRevisedAreaGraphDockの設定を再読み込みする"""
if self.revised_area_graph_dock:
self.revised_area_graph_dock.reload_revised_area_config()
def _visualizing(self):
"""ControlDockおよびGraphDockを開き、可視化を行う関数"""
if not self.control_dock:
self.control_dock = ControlDock(self.iface.mainWindow(),
self.translator)
self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.control_dock)
else:
self.control_dock.show()
if not self.main_graph_dock:
self.main_graph_dock = MainGraphDock(self.iface.mainWindow(),
self.translator)
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.main_graph_dock)
else:
self.main_graph_dock.show()
if not self.revised_area_graph_dock:
self.revised_area_graph_dock = RevisedAreaGraphDock(self.iface.mainWindow(),
self.translator)
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.revised_area_graph_dock)
# Tabify the main graph dock and revised area graph dock
self.iface.mainWindow().tabifyDockWidget(self.main_graph_dock, self.revised_area_graph_dock)
self.main_graph_dock.raise_()
else:
self.revised_area_graph_dock.show()
if not self.sub_graph_dock:
self.sub_graph_dock = GraphDock(self.iface.mainWindow(),
self.translator,
1)
self.iface.addDockWidget(Qt.RightDockWidgetArea,
self.sub_graph_dock)
else:
self.sub_graph_dock.show()
if not self.control_dock_called:
print("Debug: Setting up signal connections...")
self.control_dock.plotSignal.connect(
self._update_main_and_revised_graphs
)
print("Debug: Connected plotSignal to _update_main_and_revised_graphs")
self.control_dock.plotSignal_sub.connect(
self.sub_graph_dock.update_plots
)
print("Debug: Connected plotSignal_sub to sub_graph_dock.update_plots")
self.control_dock_called = True
def _update_main_and_revised_graphs(self, data_item, year):
"""メイングラフと修正区域グラフを同時に更新する関数"""
print(f"Debug: _update_main_and_revised_graphs called with data_item={data_item}, year={year}")
print(f"Debug: main_graph_dock exists: {self.main_graph_dock is not None}")
print(f"Debug: revised_area_graph_dock exists: {self.revised_area_graph_dock is not None}")
# メイングラフのプロット
if self.main_graph_dock:
print("Debug: Updating main graph dock (plot only)...")
try:
self.main_graph_dock.update_plots(data_item)
print("Debug: Main graph dock plot updated")
except Exception as e:
print(f"Error: Failed to update main graph dock plot: {e}")
import traceback
traceback.print_exc()
# 修正区域グラフのプロット
if self.revised_area_graph_dock:
print("Debug: Updating revised area graph dock...")
try:
self.revised_area_graph_dock.update_plots(data_item)
print("Debug: Revised area graph dock updated")
except Exception as e:
print(f"Error: Failed to update revised area graph dock: {e}")
import traceback
traceback.print_exc()
else:
print("Debug: revised_area_graph_dock is None!")
# レイヤの色付け
if self.main_graph_dock:
print("Debug: Updating layer coloring...")
try:
self.main_graph_dock.layer_coloring.coloring(data_item, year)
print("Debug: Layer coloring completed")
except Exception as e:
print(f"Error: Failed to update layer coloring: {e}")
import traceback
traceback.print_exc()
def _output(self):
"""データをエクスポートするためのOutputダイアログを開く関数"""
outputDialog = Output(translator=self.translator)
outputDialog.exec_()