pyqgis二次开发教程
pyqgis二次开发教程
ytkzhttps://zhuanlan.zhihu.com/p/629483254
安装好QGIS后,使用pycharm作为开发项目的编辑平台。现在在pycharm中进行配置。
新建项目,如下图所示。
坑:
报错1:
用pyuic5将.ui文件转化为.py文件出现报错,如下:
最终解决办法是:.ui文件名不能有空格。路径名称当中的文件夹名称也不能有空格;
比如你的.ui文件在 project program文件夹下面就不能实现转化,得把文件夹名称改为projectprogram
报错2:
不知道为什么由ui文件生成的py文件会带有C++的语法
报错3:
第一次运行python qt时显示:
This application failed to start because no Qt platform plugin could beinitialized Reinstalling the application may fix this problem
Available platform plugins are: direct2d, minimal, offscreen, windows。
from qgis.core import *
from qgis.gui import *
from qgis.PyQt.QtWidgets import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtCore import *
from os.path import *
import sys
class MyWnd(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.resize(1200, 800)
self.setWindowTitle("GIS软件")
self.canvas = QgsMapCanvas()
self.canvas.setCanvasColor(Qt.white)
self.canvas.setVisible(True)
self.canvas.xyCoordinates.connect(self.showXY)
self.actionZoomIn = QAction("放大工具", self)
self.actionZoomOut = QAction("缩小工具", self)
self.actionPan = QAction("平移工具", self)
self.actionOpenRas = QAction("打开影像", self)
self.actionZoomIn.setCheckable(True)
self.actionZoomOut.setCheckable(True)
self.actionPan.setCheckable(True)
self.actionZoomIn.triggered.connect(self.zoomIn)
self.actionZoomOut.triggered.connect(self.zoomOut)
self.actionPan.triggered.connect(self.pan)
self.actionOpenRas.triggered.connect(self.openDialogRas)
self.toolbar = QToolBar("Canvas actions")
self.addToolBar(self.toolbar)
self.toolbar.addAction(self.actionZoomIn)
self.toolbar.addAction(self.actionZoomOut)
self.toolbar.addAction(self.actionPan)
self.toolbar.addAction(self.actionOpenRas)
self.root = QgsProject.instance().layerTreeRoot()
self.model = QgsLayerTreeModel(self.root, self)
self.model.setFlag(QgsLayerTreeModel.AllowNodeReorder)
self.model.setFlag(QgsLayerTreeModel.AllowNodeChangeVisibility)
self.tocView = QgsLayerTreeView()
self.tocView.setModel(self.model)
self.tocView.setFixedWidth(200)
self.bridge = QgsLayerTreeMapCanvasBridge(self.root, self.canvas, self)
# create the map tools
self.toolPan = QgsMapToolPan(self.canvas)
self.toolPan.setAction(self.actionPan)
self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in
self.toolZoomIn.setAction(self.actionZoomIn)
self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out
self.toolZoomOut.setAction(self.actionZoomOut)
centralWidget = QWidget()
hLayout = QHBoxLayout()
hLayout.addWidget(self.canvas)
hLayout.addWidget(self.tocView)
centralWidget.setLayout(hLayout)
self.setCentralWidget(centralWidget)
self.pan()
def zoomIn(self):
self.canvas.setMapTool(self.toolZoomIn)
def zoomOut(self):
self.canvas.setMapTool(self.toolZoomOut)
def pan(self):
self.canvas.setMapTool(self.toolPan)
def openDialogRas(self):
path_to_tif = QFileDialog.getOpenFileName()[0]
if path_to_tif == "":
return
self.addRaster(path_to_tif)
def openDialogVec(self):
path_to_vec = QFileDialog.getOpenFileName()[0]
if path_to_vec == "":
return
self.addVector(path_to_vec)
def addRaster(self, path):
layer = QgsRasterLayer(path, basename(path))
if not layer.isValid():
QMessageBox.information(self, '提示', '文件打开失败', QMessageBox.Ok)
return
layer.dataProvider().setNoDataValue(1, 0)
QgsProject.instance().addMapLayers([layer])
self.canvas.setLayers([layer])
self.canvas.setExtent(layer.extent())
self.canvas.refresh()
def showXY(self, point):
m_statusBar = self.statusBar()
m_statusBar.showMessage("Coordinate:" + str(point.x()) + "," + str(point.y()))
if __name__ == "__main__":
QgsApplication.setPrefixPath("./qgis_init", True)
qgs = QgsApplication([], False)
qgs.initQgis()
w = MyWnd()
w.show()
sys.exit(qgs.exec_())
qgs.exitQgis()