1.使用mamba加速conda下载qgis
conda install -c conda-forge -n base mamba
2.创建qgis虚拟环境
conda create -n qgis python=3.11 conda activate qgis(管理员CMD) mamba install -c conda-forge qgis=3.30.0
mamba install -c conda-forge rasterio mamba install -c conda-forge geopandas
安装命令,需要rasterio、geopandas的可以使用
如果这样:
(qgis) PS C:\Users\anita> python
>>> import qgis
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'qgis'
3.设置环境变量
打开Qgis:
(base) C:\Users\user>conda activate qgis (qgis) C:\Users\user>qgis
Qgis的python控制台
import sys sys.path
复制输出的结果数组,在base环境下输入以下命令,这样环境就配置好了:
conda activate base mamba install conda-build -c conda-forge conda develop [你复制的数组] -n qgis
4.代码测试
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()View Code
标签:canvas,qgis,self,python,conda,3.30,import,path From: https://www.cnblogs.com/shiningleo007/p/18057027