/
boundingbox_sample.py
124 lines (100 loc) · 5.22 KB
/
boundingbox_sample.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
BoundingBox Sample
copyright : (C) 2021 by Chiakikun
email : chiakikungm@gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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 qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction
# Initialize Qt resources from file resources.py
from .resources import *
import os.path
import qgis
from qgis.core import *
from qgis.gui import *
class BoundingBoxSample(QgsMapTool):
def start(self):
# バウンディングボックス作成元
srclayer = self.iface.activeLayer()
if (srclayer == None) or (type(srclayer) is not QgsVectorLayer):
QMessageBox.about(None, '警告', 'ベクタレイヤを選択してから実行してください')
self.action.setChecked(False)
return
# バウンディングボックス投入先
fieldstr = ''
for field in srclayer.fields():
fieldstr = fieldstr + '&field=' + str(field.name()) + ':' + str(field.typeName())
crsstr = srclayer.sourceCrs().authid()
dstlayer = QgsVectorLayer("Polygon?crs=" + crsstr + fieldstr, "サンプルレイヤ", "memory")
for f in srclayer.getFeatures():
# 属性
qf = QgsFields()
## フィールド
for field in srclayer.fields():
qf.append(QgsField(str(field.name()), typeName=field.typeName()))
record = QgsFeature(qf)
## 値投入
for i in range(0, f.fields().count()):
record[i] = f[i]
# オブジェクト
mpol = f.geometry().asMultiPolygon()
bnds = []
for i in range(0, len(mpol)):
bnds.append(QgsGeometry.fromRect(QgsGeometry().fromPolygonXY(mpol[i]).boundingBox()).asPolygon())
newobj = QgsGeometry.fromMultiPolygonXY(bnds)
record.setGeometry(newobj)
# レイヤに追加
dstlayer.dataProvider().addFeatures([record])
dstlayer.updateExtents()
# キャンバスにオブジェクトを表示する
QgsProject.instance().addMapLayers([dstlayer])
self.canvas.refreshAllLayers()
def finish(self):
self.canvas.mapToolSet.disconnect(self.unsetTool)
def __init__(self, iface):
self.plugin_name = 'バウンディングボックス作成サンプル' # プラグイン名
self.menu_pos = 'サンプル' # プラグインの登録場所
self.toolbar = True # Trueならツールバーにアイコンを表示する
self.checkable = False # Trueならプラグイン実行中はアイコンが凹んだままになる
self.iface = iface
self.canvas = self.iface.mapCanvas()
QgsMapTool.__init__(self, self.canvas)
# このプラグイン実行中に他のアイコンが押された場合、アイコンを元の状態に戻す
def unsetTool(self, tool):
if not isinstance(tool, BoundingBoxSample):
self.finish()
self.action.setChecked(False)
def initGui(self):
icon = QIcon(os.path.dirname(__file__)+'/icon.png')
self.action = QAction(icon, self.plugin_name, self.iface.mainWindow())
self.action.triggered.connect(self.execSample) # アイコンを押下した時に実行されるメソッドを登録
self.action.setCheckable(self.checkable) # Trueだとアイコンを押下したら次に押下するまで凹んだままになる
if self.toolbar:
self.iface.addToolBarIcon(self.action) # ツールバーにこのツールのアイコンを表示する
self.iface.addPluginToMenu(self.menu_pos, self.action)
# このプラグインを無効にしたときに呼ばれる
def unload(self):
self.iface.removePluginMenu(self.menu_pos, self.action)
self.iface.removeToolBarIcon(self.action)
# このツールのアイコンを押下したときに呼ばれる
def execSample(self):
if self.checkable:
if self.action.isChecked(): # 凹状態になった
self.previousMapTool = self.canvas.mapTool() # 現在のマップツールを退避
self.start()
else: # 凸状態になった
self.finish()
self.canvas.setMapTool(self.previousMapTool) # このツール実行前に戻す
else:
self.start()