- QGIS Python Programming Cookbook
- Joel Lawhead
- 363字
- 2021-07-23 19:48:49
Using the Python ScriptRunner plugin
The QGIS Python ScriptRunner plugin provides a middle ground for QGIS automation, between the interactive console and the overhead of plugins. It provides a script management dialog that allows you to easily load, create, edit, and run scripts for large-scale QGIS automation.
Getting ready
Install the ScriptRunner plugin using the QGIS plugin manager. Then, run the plugin from the Plugin menu to open the ScriptRunner dialog. Configure a default editor to edit scripts using the following steps:
- Find the gear icon that represents the ScriptRunner Preferences settings dialog box and click on it.
- In the General Options section, check the Edit Scripts Using: checkbox.
- Click on the … button to browse to the location of a text editor on your system.
- Click on the Open button.
- Click on the OK button in the Preferences dialog.
How to do it…
- In the ScriptRunner dialog, click on the New Script icon, as shown in the following screenshot:
- Browse to the directory where you can save your script, name the script, and save it.
- Verify that the new script is loaded in ScriptRunner.
- Right-click (or control-click on a Mac) on the script name in ScriptRunner and select Edit Script in External Editor.
- In the editor, replace the template code with the following code:
from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * from qgis.gui import * def run_script(iface): layer = QgsVectorLayer('Polygon?crs=epsg:4326', 'Mississippi' , "memory") pr = layer.dataProvider() poly = QgsFeature() geom = QgsGeometry.fromWkt("POLYGON ((-88.82 34.99,-88.09 34.89,-88.39 30.34,-89.57 30.18,-89.73 31,-91.63 30.99,-90.87 32.37,-91.23 33.44,-90.93 34.23,-90.30 34.99,-88.82 34.99))") poly.setGeometry(geom) pr.addFeatures([poly]) layer.updateExtents() QgsMapLayerRegistry.instance().addMapLayers([layer])
- Click on the Run Script icon, which is represented by a green-colored arrow.
- Close the ScriptRunner plugin.
- Verify that the memory layer polygon was added to the QGIS map, as shown in the following screenshot:
How it works…
ScriptRunner is a simple but powerful idea. It allows you to build a library of automation scripts and use them from within QGIS, but without the overhead of building a plugin or a standalone application. All the Python and system path variables are set correctly and inherited from QGIS; however, you must still import the QGIS and Qt libraries.
- C# 7 and .NET Core Cookbook
- Facebook Application Development with Graph API Cookbook
- Learning Chef
- Groovy for Domain:specific Languages(Second Edition)
- C語言程序設(shè)計
- Python圖形化編程(微課版)
- 精通MySQL 8(視頻教學版)
- App Inventor少兒趣味編程動手做
- ASP.NET求職寶典
- Java 9 with JShell
- SQL Server實例教程(2008版)
- Mapping with ArcGIS Pro
- C語言程序設(shè)計
- Effective Python:編寫高質(zhì)量Python代碼的90個有效方法(原書第2版)
- 給產(chǎn)品經(jīng)理講技術(shù)