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.
Download the following zipped shapefile from https://geospatialpython.googlecode.com/files/MSCities_Geo_Pts.zip.
Extract the shapefile to a directory named ms
in your qgis_data
shapefile.
We will load the point shapefile layer, create a label object, set its properties, apply it to the layer, and then add the layer to the map. To do this, we need to perform the following steps:
src = "/Users/joellawhead/qgis_data/ms/MSCities_Geo_Pts.shp"
lyr = QgsVectorLayer(src, "Museums", "ogr")
label = QgsPalLayerSettings()
label.readFromLayer(lyr) label.enabled = True
label.fieldName = 'NAME10'
label.placement= QgsPalLayerSettings.AroundPoint label.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'8','')
label.writeToLayer(lyr)
QgsMapLayerRegistry.instance().addMapLayers([lyr])