Python QGIS Tutorial: Working with Vector Layer

In this Python QGIS tutorial we will explore more about how to work with vector layer like getting fields name, layer extent, getting feature, and many more. To work with vector layer with python we are using QgsVectorLayer class. In this tutorial we will use some methods that available in this class.

Adding Vector Layer 

Let's start this tutorial with adding a vector layer. (See the tutorial how to add vector layer with python to get more explanation about adding vector layer in QGIS with python)

park_layer=iface.addVectorLayer("F:\park_polygons.shp","park","ogr")

Checking Coordinate System

When getting a vector layer, usually we want to know the coordinate system of the layer. We can do this with crs method.

#Check Layer's Coordinate System
crs=park_layer.crs()
print(crs.description())

Output: NAD83 / UTM zone 10N

Get The Layer Extent

Next let's check the the extent of the layer. For this we use the extent method. It will return a QgsRectangle class. Then to get the extent coordinate we use minimum and maximum coordinate method.

#Check Layer's Extent
extent=park_layer.extent()
min_x=extent.xMinimum()
max_x=extent.xMaximum()
min_y=extent.yMinimum()
max_y=extent.yMaximum()
print (min_x,min_y,max_x,max_y)

Output:
483719.3637926 5449712.914727 498270.49187981 5462386.1670929

Counting Feature Number

To count number featureCount method is used. It will return the number of feature in the layer.

#Counting Feature
n_feature=park_layer.featureCount()
print("number feature: ", n_feature)

Output: number feature:  264

Getting Attributes Column

Next, we want to know what information that can be found in the layer by checking its attribute's column name. For this we use fields method in a loop and print the column name and field type.

#Get Fields Name and Type
for field in park_layer.fields():
    print (field.name(),field.typeName())

Output:
PARK_NAME String
PARK_ID Integer64
PARK_URL String
AREA_HEC String

Layer Metadata

Metadata is information about the data. We can see the metadata using htmlMetada method. The result of metada in html format will be displayed in the console.

#Get HTML Metadata
metadata=park_layer.htmlMetadata()
print (metadata) 

Output: (Something like this)
<html>
<body>
<h1>Information from provider</h1>
<hr>
<table class="list-view">
<tr><td class="highlight">Original</td><td>park</td></tr>
<tr><td class="highlight">Name</td><td>park</td></tr>
<tr><td class="highlight">Source</td><td>F:\park_polygons.shp</td></tr>
<tr><td class="highlight">Storage</td><td>ESRI Shapefile</td></tr>
<tr><td class="highlight">Comment</td><td></td></tr>
<tr><td class="highlight">Encoding</td><td>UTF-8</td></tr>
<tr><td class="highlight">Geometry</td><td>Polygon (MultiPolygon)</td></tr>
<tr><td class="highlight">CRS</td><td>EPSG:26910 - NAD83 / UTM zone 10N - Projected</td></tr>
<tr><td class="highlight">Extent</td><td>483719.3637926569208503,5449712.9147277921438217 : 498270.4918798131402582,5462386.1670929444953799</td></tr>
<tr><td class="highlight">Unit</td><td>meters</td></tr>
<tr><td class="highlight">Feature count</td><td>264</td></tr>
</table>
<br><br><h1>Identification</h1>
<hr>
................
................
</body>
</html>

Getting Attribute Value

To get attribute values of the layer firstly we have have to get the feature. Then in a loop we use attributes method to get each feature's attribute. The output will return as a list for each feature.

#Getting Attribute Value
features=park_layer.getFeatures()
for f in features:
    attr=f.attributes()
    print (attr)

Sample Outoput:
['Yaletown Park', 237, 'http://covapp.vancouver.ca/parkfinder/parkdetail.aspx?inparkid=237', '0.17']

There are many methods still available for QgsVectorLayer  class. If you want to explore more, take a look at QGIS Python API website. Visit other topics in QGIS Python Programming

Related Posts

Disqus Comments