Adding a polygon feature to a vector layer
In this recipe, we'll add a polygon to a layer. A polygon is the most complex kind of geometry; however, in QGIS the API is very similar to a line.
Getting ready
For this recipe, we'll use a simple polygon shapefile which you can download as a ZIP file from the following URL:
https://github.com/GeospatialPython/Learn/raw/master/polygon.zip
Extract this shapefile to a folder called polygon
in your /qgis_data
directory.
How to do it...
This recipe will follow the standard PyQGIS process of loading a layer, building a feature, and adding it to the layer's data provider:
- Start QGIS.
- From the Plugins menu, select Python Console.
- First, load the layer and validate it:
vectorLyr= QgsVectorLayer('/qgis_data/polygon/polygon.shp', ...
Get QGIS Python Programming Cookbook - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.