QGIS插件式开发(三)---插件开发

        本篇文章着重讲述插件开发的主要流程,揭开QGIS插件式开发的真面目,正所谓万事开头难,掌握了第一步,后面就可以把主要精力放在功能开发上,而不必再为怎么加载插件、__init__()函数的功能是什么等问题所困扰。
一、自动创建
自动创建很简单,比较适合对QGIS插件开发有一定了解的人,而对于初学者并不是一种很好很好的方式,现有的自动创建的方法主要有两种:
QGIS Plugin Creator Plugin Builder ,由于是自动创建,没有太多需要讲的,就主要讲一下注意事项:
(1)插件位置 :下载的文件是一个压缩包,解压后,将其放到QGIS可以检测到的地方:Windows:一般在C:\Users\maxuemeng\.qgis2\python\plugins下,或者将其路径设置到环境变量QGIS_PLUGINPATH,这样,在QGIS打开的时候,系统会自动的搜索,然后加载我们的插件。
(2)metadata.txt:在QGIS>=1.8.0的版本都是必须的,但是用
QGIS Plugin Creator自动创建的时候并没有,这时候需要我们手动的添加。
二、手动创建
1、
主要文件
__init__.py:是插件的入口,其中必须有一个classFactory()函数用来实例化。
mainPlugin.py:插件的功能代码都在这里实现。
resources.qrc:是一个xml格式的资源配置文件,可以手动创建,也可以在designer中添加。
resources.py:不是手动创建的,是通过PyQt4中的pyrcc工具将.qrc转换成python语言所得。
form.ui:由PyQt4中的qtdesigner工具创建,主要用来设计我们的界面。
form.py:不是手动创建,是通过PyQt4中的pyuic工具将form.ui转换成python语言所得。
metadata.txt:在QGIS>=1.8.0版本之后都是必须的,主要包含插件的版本、名称等元数据,之前这些信息一般都在__init__.py中,但是从QGIS 2.0开始,__init__.py中的元数据不再被接受,metadata.txt必须含有。
2、主要步骤
(1)明确目的:在开发插件之前,一定要明确这个插件要解决什么问题,是否已经有类似的插件,现有的插件基本上都是https://pypi.python.org/pypi,只有明确了目的,然后去开发,才有意义。
(2)创建文件:上面列举了插件包含的主要文件,有一些是自动转换而得,并不需要手动创建,这里需要创建的是:__init__.py,mainPlugin.py,resources.qrc,form.ui,metadata.txt。
(3)代码编写:完成了上面的准备工作之后,现在到了编写代码的时候,主要代码都在mainPlugin.py中。
(4)界面设计:在form.ui中设计插件的界面,同时可以添加我们的资源,这里支持拖拽式操作,相对简单。
(5)文件转换:将界面设计后,产生的.ui和.qrc文件转换得到其对应的.py文件。
(6)测试:完成了上面的工作,就可以测试了,打开QGIS,加载插件,看插件是否可用,不可用进行调试。
(7)发布:如果你感觉自己的插件写的不错,并且希望被更多人使用,可以把插件发布到QGIS的插件仓库中。
3、详细步骤
(1)创建文件
第一步:添加环境变量QGIS_PLUGINPATH,将其设置为自己想要放置插件的路径,我这里为:D:\work\DATA\QGIS\CSDN\Plugins。
第二步:创建一个文件夹,给自己的插件起一个名字,以便识别,我这里为:QuintaBestRoad
第三步:在上面文件夹中,创建__init__.py,mainPlugin.py,metadata.txt文件。
注意:文件路径中不能含有非法字符,只能是ASCII字符(A~Z或a~z)。
(2)代码编写
__init__.py:由于python在安装时,默认为ASCII编码,而我们经常使用utf-8,所以在编码是要首先指明编码方式,以便编译:#coding: utf-8。
代码如下:

#coding:utf-8
def classFactory(iface):
    #Load QuintaBestRoad class from file QuintaBestRoad.py
    from QuintaBestRoad import QuintaBestRoad
    return QuintaBestRoad(iface)
metadata.txt:元数据在编写时,可以参考 官方文档,也可以参考下表:

Metadata nameRequiredNotes
nameTruea short string containing the name of the plugin
qgisMinimumVersionTruedotted notation of minimum QGIS version
qgisMaximumVersionFalsedotted notation of maximum QGIS version
descriptionTrueshort text which describes the plugin, no HTML allowed
aboutTruelonger text which describes the plugin in details, no HTML allowed
versionTrueshort string with the version dotted notation
authorTrueauthor name
emailTrueemail of the author, not shown in the QGIS plugin manager or in the website unless by a registered logged in user, so only visible to other plugin authors and plugin website administrators
changelogFalsestring, can be multiline, no HTML allowed
experimentalFalseboolean flag, True or False
deprecatedFalseboolean flag, True or False, applies to the whole plugin and not just to the uploaded version
tagsFalsecomma separated list, spaces are allowed inside individual tags
homepageFalsea valid URL pointing to the homepage of your plugin
repositoryTruea valid URL for the source code repository
trackerFalsea valid URL for tickets and bug reports
iconFalsea file name or a relative path (relative to the base folder of the plugin’s compressed package) of a web friendly image (PNG, JPEG)
categoryFalseone of Raster, Vector, Database and Web
代码如下:

[general]
name = QuintaBestRoad
qgisMinimumVersion = 2.14.17
qgisMaximumVersion = 2.18.15
description = Get the best road between two postion
about = choose a starting point and ending point ,through analysis ,get the best road
version = 1.0
author = Quinta
email = 2249757991@qq.com
repository = http://blog.csdn.net/quinta_2018_01_09/article/details/79016749
category = Vector
QuintaBestRoad.py:这个文件相当于之前说的mianPlugin.py文件,是编写插件的核心,完成了插件的主要功能。
代码如下:
#coding:utf-8
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

#initialize Qt resources from file resources.py
import resources
from QuintaBestRoadDialog import QuintaBestRoadDialog

class QuintaBestRoad:
    def __init__(self, iface):
        # save reference to the QGIS interface
        self.iface = iface

    def initGui(self):
        # Create action that will start plugin configuration
        self.action = QAction(QIcon(":/QuintaBestRoad/icon.png"), \
        "QuintaBestRoad", self.iface.mainWindow())
        # connect the action to the run method
        QObject.connect(self.action, SIGNAL("activated()"), self.run)
        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.action)
        self.iface.addPluginToMenu("&Menu Item", self.action)
    def unload(self):
        # Remove the plugin menu item and icon
        self.iface.removePluginMenu("&Menu Item",self.action)
        self.iface.removeToolBarIcon(self.action)
    def run(self):
         # create and show the dialog
      dlg = QuintaBestRoadDialog()
      # show the dialog
      dlg.show()
      result = dlg.exec_() 
      # See if OK was pressed
      if result == 1: 
      # do something useful (delete the line containing pass and
      # substitute with your code
         pass 
QuintaBestRoadDialog.py:为了QuintaBestRoad.py使用UIQuitaBestRoad.py,我在这里添加了QuitaBestRoadDialog.py文件。
代码如下:

#coding:utf-8
from PyQt4 import QtCore, QtGui 
from UIQuintaBestRoad import Ui_Form

class QuintaBestRoadDialog(QtGui.QDialog):

  def __init__(self): 
    QtGui.QDialog.__init__(self) 
    # Set up the user interface from Designer. 
    self.ui = Ui_Form()
    self.ui.setupUi(self)
(3)界面设计
第一步:Pycharm->tools->External tools->Qtdesigner,进入设计界面,其中Qtdesigner的设置为


第二步:在Qtdesigner中创建资源文件命名为:resources.qrc,这里主要存放UI文件中用到的图标、图片等资源。


第三步:拖拽控件,完成界面设计。


第四步:将文件保存到插件文件夹中。
(4)配置pyuic和pyrcc
在我的第一篇文章中介绍了这一部分,但当时是PyQt5,而现在我们用的是PyQt4,所以要重新再Pycharm中进行设置,由于篇幅问题,这里只给出结果,不再详细讲述。


(5)文件转换
第一步:在pycharm中找到UIQuintaBestRoad.ui,右键->External tools->pyuic4,可以看到在project下多了一个UIQuitaBestRoad.py文件。
第二步:找到resources.qrc文件,右键->External tools ->pyrcc4,可以看到在project中多了一个resources.py文件。
(6)测试
QGIS->Plugins->Manage and Install Plugins,找到我们的插件,测试,如果成功,那么恭喜你,如果不成功,肯定是其中哪一步出了问题,可以根据提示,来调试。


注意:在拷贝代码时,一定要注意格式问题,有的看上去对齐了,实际上并没有,常见的错误为indentationerror unexpected indent,如果出现这种现象,肯定是缩进问题,解决方法:Notepad++->视图->显示符号->显示空格与指标符,仔细观察,将不正确的格式修改过来。
  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值