Labeling and Map Transparency: QGIS Python Programming CookBook

Ad:

Learn how to label features and set map transparency in QGIS using Python from this section as part of this preview chapter, Creating Dynamic Maps from QGIS Python Programming CookBook. With 140 short, reusable recipes to automate geospatial processes in QGIS, the QGIS Python Programming CookBook teaches readers how to use Python and QGIS to create and transform data, produce appealing GIS visualizations, and build complex map layouts.

Label Features

Once your map layers are styled, the next step to creating a complete map is labeling features. We’ll explore the basics of labeling in this recipe.

Getting ready

Download the following zipped shapefile from the following URL:


Ad:


https://geospatialpython.googlecode.com/files/MSCities_Geo_Pts.zip

Extract the shapefile to a directory named ms in our qgis_data shapefile.

How to do it…

We will load the point shapefile layer, then create a label object, set its properties, apply it to the layer, and add the layer to the map.

  1. First, to save space, we’ll specify the path to the shapefile:

src = “/Users/joellawhead/qgis_data/ms/MSCities_Geo_Pts.shp”

  1. Next we’ll load the layer:

lyr = QgsVectorLayer(src, “Museums”, “ogr”)

  1. Then we’ll create the labeling object:

label = QgsPalLayerSettings()

  1. Now we’ll configure the labels starting with reading the current layer settings:

label.readFromLayer(lyr)

label.enabled = True

  1. Then we specify the attribute for the label data:

label.fieldName = ‘NAME10’

  1. Then we can set the placement and size options:

label.placement= QgsPalLayerSettings.AroundPoint

label.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,’8′,”)

  1. And then we commit the changes to the layer:

label.writeToLayer(lyr)

  1. Finally we can add the layer to the map to view the labels:

QgsMapLayerRegistry.instance().addMapLayers([lyr])

How it works…

The interesting part of labeling is the round trip read and write process to access the layer data and the assign the labeling properties. Labeling can become quite complex but this recipe covers the basics needed to get started.

Change Map Layer Transparency

Map layer transparency allows you to change the opacity of a layer so items behind it are visible to some degree. A common technique is to make a vector layer polygon partially transparent to allow underlying imagery or elevation data to add texture to the data.

Getting ready

In a directory called ms, in your qgis_data directory, download and extract the following shapefile:

https://geospatialpython.googlecode.com/files/Mississippi.zip

How to do it…

This process is extremely simple. The transparency is just a method

  1. First we load the shapefile layer:

lyr = QgsVectorLayer(“/Users/joellawhead/qgis_data/ms/mississippi.shp”, “Mississippi”, “ogr”)

  1. Next we set the layer transparency to 50%:

lyr.setLayerTransparency(50)

  1. Finally we add the layer to the map:

QgsMapLayerRegistry.instance().addMapLayer(lyr)

How it works…

If you set the transparency to 100% the layer is completely opaque. If you set it to 0, the layer becomes completely invisible.


QGIS Python Programming CookBook

<– Previous: Real-Time Weather Data  |     Next: Working with Tools –>


Advertising:



Like this article and want more?

Enter your email to receive the weekly GIS Lounge newsletter:

Advertising