147x Filetype PDF File size 1.29 MB Source: docs.qgis.org
PyQGIS3.16developercookbook QGISProject Apr02,2022 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 Thestartup.pyfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3.2 ThePYQGIS_STARTUPenvironmentvariable . . . . . . . . . . . . . . . . . . . . . . 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 LoadingProjects 7 2.1 Resolving bad paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3 LoadingLayers 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 TheQgsProject 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 AddFeatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 TheQgsVectorLayerUtils class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 6.7 Creating Vector Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 6.7.1 Fromaninstance of QgsVectorFileWriter . . . . . . . . . . . . . . . . . . . . . 32 6.7.2 Directly from features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 6.7.3 Fromaninstance 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 GeometryHandling 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 CRSTransformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 9 Using the Map Canvas 57 9.1 EmbeddingMapCanvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 MapRenderingandPrinting 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 ReadingAndStoringSettings 75 13 Communicatingwiththeuser 77 13.1 Showingmessages. The QgsMessageBar class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 13.2 Showingprogress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 13.3 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 13.3.1 QgsMessageLog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 13.3.2 Thepython built in logging module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 14 Authentication infrastructure 83 14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 14.2 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 14.3 QgsAuthManagertheentry point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 14.3.1 Init the manager and set the master password . . . . . . . . . . . . . . . . . . . . . . . . 84 ii
no reviews yet
Please Login to review.