Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
131 lines (104 sloc) 5.61 KB
# -*- coding: utf-8 -*-
"""
/***************************************************************************
RubberBandSampleDialog
A QGIS plugin
RubberBandのSampleです
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2019-11-23
git sha : $Format:%H$
copyright : (C) 2019 by unemployed
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. *
* *
***************************************************************************/
"""
import qgis.core
from qgis.PyQt.QtGui import QColor
from qgis.PyQt import QtCore, QtGui
import os
from qgis.PyQt import uic
from qgis.PyQt import QtWidgets
from datetime import datetime
# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'maptoolemitpoint_sample_dialog_base.ui'))
class RubberBandSampleDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(RubberBandSampleDialog, self).__init__(parent)
# Set up the user interface from Designer through FORM_CLASS.
# After self.setupUi() you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
# Create new virtual layer
self.vlyr = qgis.core.QgsVectorLayer("Polygon?&crs=epsg:4326&field=name:string&field=size:double", "サンプルレイヤ", "memory")
self.mouseEventSample = qgis.gui.QgsMapToolEmitPoint( qgis.utils.iface.mapCanvas() )
def showEvent(self, e):
self.isrun = False
self.myRubberBand = None
def closeEvent(self, e):
if self.isrun:
self.isrun = False
self.mouseEventSample.canvasClicked.disconnect(self.mouseClick)
qgis.utils.iface.mapCanvas().unsetMapTool(self.mouseEventSample)
if not self.myRubberBand == None:
qgis.utils.iface.mapCanvas().scene().removeItem(self.myRubberBand)
self.myRubberBand = None
def pushExec(self):
self.mouseEventSample.canvasClicked.connect(self.mouseClick)
self.mouseEventSample.canvasMoveEvent = self.canvasMoveEvent
qgis.utils.iface.mapCanvas().setMapTool(self.mouseEventSample)
self.isrun = True
def pushClose(self):
self.close()
def canvasMoveEvent(self, event):
# 一度も左ボタンをクリックしてない
if self.myRubberBand == None:
return
pos = event.pos()
point = qgis.utils.iface.mapCanvas().getCoordinateTransform().toMapCoordinates(pos)
point_count = self.myRubberBand.numberOfVertices()
if point_count == 0:
return
self.myRubberBand.movePoint(point)
def mouseClick(self, currentPos, clickedButton ):
if clickedButton == QtCore.Qt.LeftButton and self.myRubberBand == None: #len(self.coordinates) == 0:
# create the polygon rubber band associated to the current canvas
self.myRubberBand = qgis.gui.QgsRubberBand( qgis.utils.iface.mapCanvas(), qgis.core.QgsWkbTypes.PolygonGeometry )
# set rubber band style
color = QColor(78, 97, 114)
color.setAlpha(190)
self.myRubberBand.setColor(color)
#Draw rubberband
self.myRubberBand.addPoint( qgis.core.QgsPointXY(currentPos) )
if clickedButton == QtCore.Qt.LeftButton and self.myRubberBand.numberOfVertices() > 0:
self.myRubberBand.addPoint( qgis.core.QgsPointXY(currentPos) )
if clickedButton == QtCore.Qt.RightButton:
# フューチャー作成
qf = qgis.core.QgsFields()
for field in self.vlyr.fields():
qf.append(qgis.core.QgsField(str(field.name()), typeName=field.typeName()))
poly = qgis.core.QgsFeature(qf)
# easier solution using simply the geometry of self.myRubberBand
geomP = self.myRubberBand.asGeometry()
poly.setGeometry(geomP)
# add feature
# set attributes
poly.setAttributes([str(datetime.now()), geomP.area()])
self.vlyr.dataProvider().addFeatures([poly])
self.vlyr.updateExtents() # これが無いと『レイヤの領域にズーム』した時に、レイヤの最初のオブジェクト部分しかズームされない
# キャンバスにオブジェクトを追加して、パンする。setExtentが無いと、画面に表示されない(少しでも画面動かせば表示されるので)
qgis.core.QgsProject.instance().addMapLayers([self.vlyr])
qgis.utils.iface.mapCanvas().setExtent(geomP.boundingBox())
qgis.utils.iface.mapCanvas().refresh()
self.close()
You can’t perform that action at this time.