2

I have gone through an online tutorial on how to do a pie chart map using PYQGIS.

The following is my code, identical to what was provided.

# -*- coding: utf-8 -*-
from PyQt4.QtGui import*
from PyQt4.QtCore import*
layer = QgsVectorLayer("C:\Users\Aaron\Documents\pygis\County10PopnHou\County10PopnHou.shp","Population","ogr")
categories = [u'PCT_WHT',u'PCT_BLK',u'PCT_AMIND',u'PCT_ASIAN',u'PCT_HAW',u'PCT_ORA',u'PCT_MR',u'PCT_HISP']
colors = ['#3727fa','#01daae','#f849a6','#268605','#6810ff','#453990','#630f2f','#07dd45']
qcolors = map(QColor,colors)
diagram = QgsPieDiagram()
ds = QgsDiagramSettings()
ds.font = QFont("Helvectica", 12)
ds.transparency = 0
ds.categoryColors = qcolors
ds.categoryAttributes = categories
ds.size = QSizeF(100.0,100.0)
ds.sizeType = 0
ds.labelPlacementMethod = 1
ds.scaleByArea = True
ds.minimumSize = 0
ds.BackgroundColor = QColor(255,255,255,0)
ds.penWidth = 0
dr = QgsLinearlyInterpolateDiagramRenderer()
dr.setLowerValue(0.0)
dr.setLowerSize(QSizeF(0.0,0.0))
dr.setUpperValue(2000000)
dr.setUpperSize(QSizeF(40,40))
dr.setClassificationAttribute(6)
dr.setDiagram(diagram)
layer.setDiagramRenderer(dr)
dls = QgsDiagramLayerSettings()
dls.dist = 0
dls.priority = 0
dls.xPosColumn = -1
dls.yPosColumn = -1
dls.placement = 0
layer.setDiagramLayerSettings(dls)
label = QgsPalLayerSettings()
label.readFromLayer(layer)
label.enabled = True
label.writeToLayer(layer)
if hasattr(layer,"setCacheImage"):
    layer.setCacheImage(None)
layer.triggerRepaint()
QgsMapLayerRegistry.instance().addMapLayer(layer)

Upon running on QGIS using the Python console, I get this error.

Traceback (most recent call last): File "", line 1, in File "C:/Users/Aaron/Documents/pygis/UsingPieChartSymbols.py", line 21, in dr = QgsLinearlyInterpolateDiagramRenderer() NameError: name 'QgsLinearlyInterpolateDiagramRenderer' is not defined

I have no idea why this is occuring and what is missing.

  • Correct name for class is QgsLinearlyInterpolatedDiagramRenderer. Please, see my answer. – xunilk Feb 4 '18 at 13:55
2

Excluding whatever another error, I found out that QgsLinearlyInterpolateDiagramRenderer class does not exist. Correct name for class is QgsLinearlyInterpolatedDiagramRenderer. So, your code (with my path to shapefile) can be write as follows:

# -*- coding: utf-8 -*-
from PyQt4.QtGui import*
from PyQt4.QtCore import*
path = "/home/zeito/pyqgis_data/County10PopnHou/County10PopnHou/County10PopnHou.shp"
layer = QgsVectorLayer(path,"Population","ogr")
categories = [u'PCT_WHT',u'PCT_BLK',u'PCT_AMIND',u'PCT_ASIAN',u'PCT_HAW',u'PCT_ORA',u'PCT_MR',u'PCT_HISP']
colors = ['#3727fa','#01daae','#f849a6','#268605','#6810ff','#453990','#630f2f','#07dd45']
qcolors = map(QColor,colors)
diagram = QgsPieDiagram()
ds = QgsDiagramSettings()
ds.font = QFont("Helvectica", 12)
ds.transparency = 0
ds.categoryColors = qcolors
ds.categoryAttributes = categories
ds.size = QSizeF(100.0,100.0)
ds.sizeType = 0
ds.labelPlacementMethod = 1
ds.scaleByArea = True
ds.minimumSize = 0
ds.BackgroundColor = QColor(255,255,255,0)
ds.penWidth = 0
dr = QgsLinearlyInterpolatedDiagramRenderer()
dr.setLowerValue(0.0)
dr.setLowerSize(QSizeF(0.0,0.0))
dr.setUpperValue(2000000)
dr.setUpperSize(QSizeF(40,40))
dr.setClassificationAttribute(6)
dr.setDiagram(diagram)
layer.setDiagramRenderer(dr)
dls = QgsDiagramLayerSettings()
dls.dist = 0
dls.priority = 0
dls.xPosColumn = -1
dls.yPosColumn = -1
dls.placement = 0
layer.setDiagramLayerSettings(dls)
label = QgsPalLayerSettings()
label.readFromLayer(layer)
label.enabled = True
label.writeToLayer(layer)
if hasattr(layer,"setCacheImage"):
    layer.setCacheImage(None)
layer.triggerRepaint()
QgsMapLayerRegistry.instance().addMapLayer(layer)

After running it at Python Console of QGIS, I got:

enter image description here

It can be observed that pie diagrams are not produced as expected. So, there are other issues must be solved.

Editing Note:

Above code does not have some lines (see original here: https://github.com/PacktPublishing/QGIS-Python-Programming-Cookbook-Second-Edition/blob/master/Chapter05/B06246_05_21-pie.py). After putting them at following code:

# -*- coding: utf-8 -*-
from PyQt4.QtGui import*
from PyQt4.QtCore import*

layer = QgsVectorLayer("/home/zeito/pyqgis_data/County10PopnHou/County10PopnHou/County10PopnHou.shp","Population","ogr")

categories = [u'PCT_WHT',u'PCT_BLK',u'PCT_AMIND',u'PCT_ASIAN',u'PCT_HAW',u'PCT_ORA',u'PCT_MR',u'PCT_HISP']

colors = ['#3727fa','#01daae','#f849a6','#268605','#6810ff','#453990','#630f2f','#07dd45']

qcolors = map(QColor,colors)

diagram = QgsPieDiagram()  

ds = QgsDiagramSettings()
ds.font = QFont("Helvetica", 12)
ds.transparency = 0 
ds.categoryColors = qcolors
ds.categoryAttributes = categories
ds.categoryLabels = categories
ds.size = QSizeF(100.0,100.0)
ds.sizeType = 0
ds.labelPlacementMethod = 1
ds.scaleByArea = True
ds.minimumSize = 0
ds.BackgroundColor = QColor(255,255,255,0)
ds.PenColor = QColor("black") 
ds.penWidth = 0    

dr = QgsLinearlyInterpolatedDiagramRenderer()
dr.setLowerValue(0.0)
dr.setLowerSize(QSizeF(0.0,0.0))
dr.setUpperValue(2000000)
dr.setUpperSize(QSizeF(40,40))
dr.setClassificationAttribute(6)
dr.setDiagram(diagram)
dr.setDiagramSettings(ds)
layer.setDiagramRenderer(dr)    

dls = QgsDiagramLayerSettings()
dls.dist = 0
dls.priority = 0
dls.xPosColumn = -1
dls.yPosColumn = -1
dls.placement = 0
layer.setDiagramLayerSettings(dls)   

label = QgsPalLayerSettings()
label.readFromLayer(layer)
label.enabled = True
label.writeToLayer(layer)      

if hasattr(layer,"setCacheImage"):
    layer.setCacheImage(None)

layer.triggerRepaint()

QgsMapLayerRegistry.instance().addMapLayer(layer)

it produces desired result after running it at Python Console of QGIS:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.