PYQGIS独立应用开发

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()

效果图

  • 9
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

llc的足迹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值