QGIS安装
版本:QGIS 3.4.2
环境配置
开发工具:pycharm
File-->settings...中Project interpreter添加python开发环境,选择C:\Program Files\QGIS 3.4\bin\python-qgis.bat,如下图:
界面设计
可使用qt designer设计,本文主要实现了工具栏功能(放大、缩小、抓手)
写代码
主要讲一下main函数实现逻辑,qgs.setPrefixPath('qgis', True),这一行比较重要(后边会用到):
def main():
# 应用入口,使用GUI
qgs = QgsApplication([], True)
# 设置,qgis安装路径,这里写相对路径,如果是源码运行,这行可不写
qgs.setPrefixPath('qgis', True)
# 初始化
qgs.initQgis()
window = MapExplorer()
window.show()
exit_code = qgs.exec_()
# 退出
qgs.exitQgis()
sys.exit(exit_code)
# -*- coding: utf-8 -*-
# @Time : 2018/11/26 9:36
# @Author : llc
# @File : standalone.py
import os, sys
from qgis.core import QgsProject, QgsApplication, QgsVectorLayer
from qgis.gui import QgsMapCanvas, QgsMapToolPan, QgsMapToolZoom, QgsMapToolIdentify
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QFileDialog
from ui.main_ui import Ui_MainWindow
class MapExplorer(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MapExplorer, self).__init__()
self.setupUi(self)
self.init_mapcanvas()
self.slot_connect()
def slot_connect(self):
self.action_open.triggered.connect(self.action_open_triggered)
self.action_basemap.triggered.connect(self.action_basemap_triggered)
self.action_mark.triggered.connect(self.action_mark_triggered)
self.action_zoomin.triggered.connect(self.action_zoomin_triggered)
self.action_zoomout.triggered.connect(self.action_zoomout_triggered)
self.action_pan.triggered.connect(self.action_pan_triggered)
self.action_identify.triggered.connect(self.action_identify_triggered)
def init_mapcanvas(self):
self.mapCanvas = QgsMapCanvas()
self.mapCanvas.xyCoordinates.connect(self.show_lonlat)
self.mapCanvas.setCanvasColor(Qt.white)
# self.mapCanvas.show()
layout = QVBoxLayout(self.centralwidget)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.mapCanvas)
def loadMap(self, fullpath):
print(fullpath)
self.layer = QgsVectorLayer(fullpath, "shp", "ogr")
QgsProject.instance().addMapLayer(self.layer)
self.mapCanvas.setLayers([self.layer])
self.mapCanvas.setExtent(self.layer.extent())
self.mapCanvas.refresh()
def action_open_triggered(self):
fullpath, format = QFileDialog.getOpenFileName(self, '打开数据', '', '*.shp')
if os.path.exists(fullpath):
self.loadMap(fullpath)
def action_basemap_triggered(self):
pass
def action_mark_triggered(self):
pass
def action_zoomin_triggered(self):
self.maptool = QgsMapToolZoom(self.mapCanvas, False)
self.mapCanvas.setMapTool(self.maptool)
def action_zoomout_triggered(self):
self.maptool = QgsMapToolZoom(self.mapCanvas, True)
self.mapCanvas.setMapTool(self.maptool)
def action_pan_triggered(self):
self.maptool = QgsMapToolPan(self.mapCanvas)
self.mapCanvas.setMapTool(self.maptool)
def action_identify_triggered(self):
self.maptool = QgsMapToolIdentify(self.mapCanvas)
self.mapCanvas.setMapTool(self.maptool)
def show_lonlat(self, point):
x = point.x()
y = point.y()
self.statusbar.showMessage(f'经度:{x},纬度:{y}')
def main():
qgs = QgsApplication([], True)
qgs.setPrefixPath('qgis', True)
qgs.initQgis()
window = MapExplorer()
window.show()
exit_code = qgs.exec_()
qgs.exitQgis()
sys.exit(exit_code)
if __name__ == '__main__':
main()
编译
源码调试没有错误后编译程序,编译步骤比较麻烦,需要用到pyinstaller。
安装pyinstaller
管理员启动cmd,进入到C:\Program Files\QGIS 3.4\bin,执行命令:
python-qgis.bat -m easy_install pyinstaller
环境变量配置
分析 python-qgis.bat后得知,该bat先添加了一些环境变量(不用关心那些环境变量),然后启动了python,如下:
@echo off
call "%~dp0\o4w_env.bat"
call qt5_env.bat
call py3_env.bat
@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%
"%PYTHONHOME%\python" %*
复制python-qgis.bat重命名为python-pyinstaller.bat,并修改最后一行启动命令,如下:
@echo off
call "%~dp0\o4w_env.bat"
call qt5_env.bat
call py3_env.bat
@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%
"%PYTHONHOME%\Scripts\pyinstaller" %*
开始编译
进入到脚本standalone.py所在目录,执行(请自行百度pyinstaller用法):
"C:\Program Files\QGIS 3.4\bin\python-pyinstaller.bat" -w standalone.py
报错,提示插件目录不存在(可能是上边环境变量的问题),我的解决方案是:新建C:\src\osgeo4w\qt5\osgeo4w\qt5\5.11.2\msvc2015_64路径,将C:\Program Files\QGIS 3.4\apps\qt5目录下的plugins拷贝过来:
再次编译(时间稍长),成功;在standalone.py所在目录下生成dist文件夹,进入到.\dist\standalone目录运行standalone.exe,依然报错:
修改参数后重新编译(-w是窗口程序,-c是控制台程序:可打印出相关错误),找到报错原因,是因为缺少好多pyd依赖,我的解决方案是:复制C:\Program Files\QGIS 3.4\apps\Python37\Lib\site-packages\PyQt5目录下所有的pyd文件到.\dist\standalone\PyQt5目录
"C:\Program Files\QGIS 3.4\bin\python-pyinstaller.bat" -c standalone.py
终于可以正确打开程序了,但当加载shp数据的时候,发现数据没有显示,也没有报错,这就是前面提到的qgs.setPrefixPath('qgis', True)这一行代码,可以发现.\dist\standalone\qgis目录下只有_core.pyd和_gui.pyd两个文件,经分析是缺少plugins引起的,解决方案:将C:\Program Files\QGIS 3.4\apps\qgis目录下的plugins文件夹拷贝到.\dist\standalone\qgis目录下,此时可以正常加载数据了,并且可以发布自己的程序到其它计算机上(不需要python和QGIS环境)。