Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
136 lines (106 sloc) 5.29 KB
# -*- coding: utf-8 -*-
"""
/***************************************************************************
ScreenShotSample2Dialog
A QGIS plugin
少しマシになったスクリーンショットサンプルです。
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2019-11-29
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 os
from qgis.PyQt import uic
from qgis.PyQt import QtWidgets
from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox
from qgis.PyQt.QtGui import QColor
import qgis.core
import time
# 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__), 'screenShot_Sample2_dialog_base.ui'))
class ScreenShotSample2Dialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(ScreenShotSample2Dialog, 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)
self.isrun = False
# 縮尺リスト
self.scaleList.addItem('2500')
self.scaleList.addItem('10000')
self.scaleList.addItem('50000')
self.scaleList.addItem('100000')
self.scaleList.addItem('250000')
def closeEvent(self, e):
if self.isrun:
self.layer.setRenderer(self.prerenderer)
self.layer.triggerRepaint()
self.isrun = False
def pushSelectDir(self):
path = QFileDialog.getExistingDirectory(None, "", "")
self.saveDirPath.setText(path)
def pushClose(self):
self.close()
def exportMap(self):
qgis.utils.iface.mapCanvas().saveAsImage(self.saveDirPath.text() + "\{}.png".format(self.ids.pop()) )
if self.ids:
self.setNextFeatureExtent()
else: # We're done
qgis.utils.iface.mapCanvas().mapCanvasRefreshed.disconnect( self.exportMap )
self.close()
def setNextFeatureExtent(self):
rule = self.root_rule.children()[0].clone()
rule.setFilterExpression('$id = ' + str(self.ids[-1]))
self.root_rule.insertChild(1,rule)
self.root_rule.removeChildAt(0)
self.layer.setRenderer(self.renderer)
self.layer.triggerRepaint()
qgis.utils.iface.mapCanvas().zoomToFeatureIds( self.layer, [self.ids[-1]] )
qgis.utils.iface.mapCanvas().zoomScale(int(self.scaleList.currentText()))
def pushExec(self):
if self.isrun:
return
if not (os.path.exists(self.saveDirPath.text()) and (os.path.isdir(self.saveDirPath.text()))):
QtWidgets.QMessageBox.information(None, "エラー", "指定したフォルダが見つかりませんでした。;"+ self.saveDirPath.text() , QMessageBox.Ok)
self.done(-1)
return
self.isrun = True
self.layer = qgis.utils.iface.activeLayer()
self.ids = self.layer.allFeatureIds()
self.prerenderer = self.layer.renderer().clone() # 後で元に戻したいので現在のスタイル設定を退避しておく
# 画像にするフューチャーを赤くしたいので... 「https://gis.stackovernet.com/ja/q/54438」を参考にしました
symbol = qgis.core.QgsSymbol.defaultSymbol(self.layer.geometryType())
self.renderer = qgis.core.QgsRuleBasedRenderer(symbol)
style_rules = (
('Target', '$id = 0', '#ff0000'),
('Other', 'ELSE', self.prerenderer.symbol().color().name()),
)
self.root_rule = self.renderer.rootRule()
for label, expression, color_name in style_rules:
rule = self.root_rule.children()[0].clone()
rule.setLabel(label)
rule.setFilterExpression(expression)
rule.symbol().setColor(QColor(color_name))
self.root_rule.appendChild(rule)
self.root_rule.removeChildAt(0)
self.layer.setRenderer(self.renderer)
self.layer.triggerRepaint()
# スクリーンショット開始
qgis.utils.iface.mapCanvas().mapCanvasRefreshed.connect( self.exportMap )
self.setNextFeatureExtent() # Let's start
You can’t perform that action at this time.