Pyqgis 3.16 Developer Cookbook
Total Page:16
File Type:pdf, Size:1020Kb
PyQGIS 3.16 developer cookbook QGIS Project Sep 28, 2021 CONTENTS 1 Introduction 1 1.1 Scripting in the Python Console ................................... 1 1.2 Python Plugins ............................................ 2 1.3 Running Python code when QGIS starts ............................... 3 1.3.1 The startup.py file ................................... 3 1.3.2 The PYQGIS_STARTUP environment variable ...................... 3 1.4 Python Applications ......................................... 3 1.4.1 Using PyQGIS in standalone scripts ............................. 4 1.4.2 Using PyQGIS in custom applications ............................ 4 1.4.3 Running Custom Applications ................................ 5 1.5 Technical notes on PyQt and SIP ................................... 6 2 Loading Projects 7 2.1 Resolving bad paths .......................................... 8 3 Loading Layers 9 3.1 Vector Layers ............................................. 9 3.2 Raster Layers ............................................. 12 3.3 QgsProject instance .......................................... 14 4 Accessing the Table Of Contents (TOC) 15 4.1 The QgsProject class ......................................... 15 4.2 QgsLayerTreeGroup class ...................................... 16 5 Using Raster Layers 19 5.1 Layer Details ............................................. 19 5.2 Renderer ............................................... 20 5.2.1 Single Band Rasters ..................................... 20 5.2.2 Multi Band Rasters ..................................... 21 5.3 Query Values ............................................. 21 6 Using Vector Layers 23 6.1 Retrieving information about attributes ................................ 24 6.2 Iterating over Vector Layer ...................................... 24 6.3 Selecting features ........................................... 25 6.3.1 Accessing attributes ..................................... 26 6.3.2 Iterating over selected features ............................... 26 6.3.3 Iterating over a subset of features .............................. 26 6.4 Modifying Vector Layers ....................................... 27 6.4.1 Add Features ........................................ 28 6.4.2 Delete Features ....................................... 28 6.4.3 Modify Features ....................................... 28 6.4.4 Modifying Vector Layers with an Editing Buffer ...................... 29 6.4.5 Adding and Removing Fields ................................ 30 6.5 Using Spatial Index .......................................... 31 i 6.6 The QgsVectorLayerUtils class .................................... 32 6.7 Creating Vector Layers ........................................ 32 6.7.1 From an instance of QgsVectorFileWriter ..................... 32 6.7.2 Directly from features .................................... 34 6.7.3 From an instance of QgsVectorLayer ......................... 35 6.8 Appearance (Symbology) of Vector Layers .............................. 36 6.8.1 Single Symbol Renderer ................................... 37 6.8.2 Categorized Symbol Renderer ................................ 38 6.8.3 Graduated Symbol Renderer ................................ 38 6.8.4 Working with Symbols ................................... 39 6.8.5 Creating Custom Renderers ................................. 42 6.9 Further Topics ............................................ 44 7 Geometry Handling 47 7.1 Geometry Construction ........................................ 47 7.2 Access to Geometry ......................................... 48 7.3 Geometry Predicates and Operations ................................. 49 8 Projections Support 53 8.1 Coordinate reference systems ..................................... 53 8.2 CRS Transformation ......................................... 54 9 Using the Map Canvas 57 9.1 Embedding Map Canvas ....................................... 58 9.2 Rubber Bands and Vertex Markers .................................. 58 9.3 Using Map Tools with Canvas .................................... 59 9.3.1 Select a feature using QgsMapToolIdentifyFeature ..................... 60 9.4 Writing Custom Map Tools ...................................... 61 9.5 Writing Custom Map Canvas Items .................................. 62 10 Map Rendering and Printing 65 10.1 Simple Rendering ........................................... 65 10.2 Rendering layers with different CRS ................................. 66 10.3 Output using print layout ....................................... 66 10.3.1 Exporting the layout ..................................... 68 10.3.2 Exporting a layout atlas ................................... 68 11 Expressions, Filtering and Calculating Values 69 11.1 Parsing Expressions .......................................... 70 11.2 Evaluating Expressions ........................................ 70 11.2.1 Basic Expressions ...................................... 70 11.2.2 Expressions with features .................................. 71 11.2.3 Filtering a layer with expressions .............................. 72 11.3 Handling expression errors ...................................... 72 12 Reading And Storing Settings 75 13 Communicating with the user 77 13.1 Showing messages. The QgsMessageBar class ............................ 77 13.2 Showing progress ........................................... 80 13.3 Logging ................................................ 80 13.3.1 QgsMessageLog ....................................... 81 13.3.2 The python built in logging module ............................. 81 14 Authentication infrastructure 83 14.1 Introduction .............................................. 83 14.2 Glossary ............................................... 84 14.3 QgsAuthManager the entry point ................................... 84 14.3.1 Init the manager and set the master password ........................ 84 ii 14.3.2 Populate authdb with a new Authentication Configuration entry .............. 85 14.3.3 Remove an entry from authdb ................................ 86 14.3.4 Leave authcfg expansion to QgsAuthManager ........................ 86 14.4 Adapt plugins to use Authentication infrastructure .......................... 87 14.5 Authentication GUIs ......................................... 88 14.5.1 GUI to select credentials .................................. 88 14.5.2 Authentication Editor GUI ................................. 88 14.5.3 Authorities Editor GUI ................................... 89 15 Tasks - doing heavy work in the background 91 15.1 Introduction .............................................. 91 15.2 Examples ............................................... 92 15.2.1 Extending QgsTask ..................................... 92 15.2.2 Task from function ..................................... 94 15.2.3 Task from a processing algorithm .............................. 96 16 Developing Python Plugins 97 16.1 Structuring Python Plugins ...................................... 97 16.1.1 Writing a plugin ....................................... 98 16.1.2 Plugin content ........................................ 99 16.1.3 Documentation ....................................... 103 16.1.4 Translation .......................................... 103 16.1.5 Tips and Tricks ....................................... 105 16.2 Code Snippets ............................................ 106 16.2.1 How to call a method by a key shortcut ........................... 106 16.2.2 How to toggle Layers .................................... 106 16.2.3 How to access attribute table of selected features ...................... 107 16.2.4 Interface for plugin in the options dialog .......................... 107 16.3 Using Plugin Layers ......................................... 108 16.3.1 Subclassing QgsPluginLayer ................................. 108 16.4 IDE settings for writing and debugging plugins ............................ 110 16.4.1 Useful plugins for writing Python plugins .......................... 110 16.4.2 A note on configuring your IDE on Linux and Windows .................. 110 16.4.3 Debugging using Pyscripter IDE (Windows) ........................ 111 16.4.4 Debugging using Eclipse and PyDev ............................ 111 16.4.5 Debugging with PyCharm on Ubuntu with a compiled QGIS ................ 115 16.4.6 Debugging using PDB .................................... 116 16.5 Releasing your plugin ......................................... 117 16.5.1 Metadata and names ..................................... 117 16.5.2 Code and help ........................................ 118 16.5.3 Official Python plugin repository .............................. 118 17 Writing a Processing plugin 121 17.1 Creating from scratch ......................................... 121 17.2 Updating a plugin ........................................... 121 18 Network analysis library 125 18.1 General information ......................................... 125 18.2 Building a graph ........................................... 125 18.3 Graph analysis ............................................ 127 18.3.1 Finding shortest paths .................................... 129 18.3.2 Areas of availability ..................................... 131 19 QGIS Server and Python 133 19.1 Introduction .............................................. 133 19.2 Server API basics ........................................... 133 19.3 Standalone or embedding ....................................... 134 19.4 Server plugins ............................................. 135 19.4.1 Server filter plugins