2

I'm trying to apply a graduated renderer on a point layer. I found this code here (the first in the solution) and slightly modified it to my project.

It seem's to work beautifully (in the layer legend I get all different point colors listed), but on the map canvas all points are gone/aren't shown anymore! And if I press "show feature count", I can see the points are still there but somehow aren't listed in the new added colors:the layer legend here my code:

def validatedDefaultSymbol( geometryType ):
  symbol = QgsSymbolV2.defaultSymbol( geometryType )
  if symbol is None:
    if geometryType == QGis.Point:
        symbol = QgsMarkerSymbolV2()
        print ("yes point")
    elif geometryType == QGis.Line:
        symbol =  QgsLineSymbolV2 ()
    elif geometryType == QGis.Polygon:
        symbol = QgsFillSymbolV2 ()
  return symbol

def makeSymbologyForRange( layer, min , max, title, color):
  symbol = validatedDefaultSymbol( layer.geometryType() )
  symbol.setColor( color )
  range = QgsRendererRangeV2( min, max, symbol, title )
  return range

def applySymbologyFixedDivisions():
  targetField = 'score'
  try:
    layer = QgsMapLayerRegistry.instance().mapLayersByName('itpoint')[0]
    print ('itpoint-layer found')
    rangeList = []
    rangeList.append( makeSymbologyForRange( layer,0.0, 0.1, '---', QColor(38, 166, 154) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.1, 0.2,'--o',  QColor(76, 175, 80) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.2, 0.3,'--', QColor(139, 195, 74) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.3, 0.4,'-o', QColor(212, 225, 87) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.4, 0.5,'-', QColor(255, 238, 88) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.5, 0.6,'o', QColor(255, 193, 7) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.6, 0.7,'+', QColor(255, 152, 0) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.7, 0.8,'+o', QColor(255, 87, 34) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.8, 0.9,'++', QColor(244, 67, 54) ) )
    rangeList.append( makeSymbologyForRange( layer, 0.9, 1.0,'++o', QColor(183, 28, 28) ) )
    rangeList.append( makeSymbologyForRange( layer,1.0, 2,'+++', QColor("Brown") ) )
    renderer = QgsGraduatedSymbolRendererV2( targetField, rangeList )
    renderer.setMode( QgsGraduatedSymbolRendererV2.Custom )
    layer.setRendererV2( renderer )
    layer.triggerRepaint()
  except:
    print ("failed")

Has someone an idea why?

(Note: I know that because it is a point layer I actually don't need the "validatedDefaultSymbol", but who knows if another layer is following... ;-))

Joseph
  • 75,746
  • 7
  • 171
  • 282
Nico
  • 317
  • 1
  • 9

1 Answers1

1

That's an interesting method to use for the graduated style, although not sure why it doesn't work (can't test it at the moment). I tend to use something like the following which hopefully will work for you (and applies to any geometry type):

from PyQt4.QtGui import QColor

layer = QgsMapLayerRegistry.instance().mapLayersByName('itpoint')[0]

symbols = (('---', 0.0, 0.1, QColor(38, 166, 154)),
            ('--o', 0.1, 0.2, QColor(76, 175, 80)),
            ('--', 0.2, 0.3, QColor(139, 195, 74)),
            ('-o', 0.3, 0.4, QColor(212, 225, 87)),
            ('-', 0.4, 0.5, QColor(255, 238, 88)),
            ('o', 0.5, 0.6, QColor(255, 193, 7)),
            ('+', 0.6, 0.7, QColor(255, 152, 0)),
            ('+o', 0.7, 0.8, QColor(255, 87, 34)),
            ('++', 0.8, 0.9, QColor(244, 67, 54)),
            ('++o', 0.9, 1.0, QColor(183, 28, 28)),
            ('+++', 1.0, 2, QColor("Brown")))

ranges = []
for label, lower, upper, color in symbols:
    sym = QgsSymbolV2.defaultSymbol(layer.geometryType())
    sym.setColor(QColor(color))
    rng = QgsRendererRangeV2(lower, upper, sym, label)
    ranges.append(rng)

field = "score"
renderer = QgsGraduatedSymbolRendererV2(field, ranges)
layer.setRendererV2(renderer)
layer.triggerRepaint()
Joseph
  • 75,746
  • 7
  • 171
  • 282
  • also looks great but the result is still the same for me: "itpoint" layer with 43 points but without any one shown on the map canvas or the sub layer panel... I checked the geometry of "itpoint", because I thought it could be without any and therefore might giving trouble, but 'print (layer.geometryType())' gives "0" as wished. – Nico Aug 16 '17 at 15:56
  • any idea what could be the matter? – Nico Aug 16 '17 at 18:43
  • @Nico - Hard to determine, could be a CRS issue or the field name is not correct etc. Would it be possible to share your data so that others could test it? – Joseph Aug 17 '17 at 09:26
  • 1
    I'm reeeaaally, really confused! Started my computer 10 minutes ago, tested (without having changed anything in the code) again and now it works perfectly now (both versions!). I don't get it at all! QGIS seemed a bit buggy yesterday, but something like this!? I'm sorry, if I stole your time Joseph, although big thanks for your help! – Nico Aug 17 '17 at 10:17
  • @Nico - Ha! Most welcome and don't worry, these things happen to everyone! Glad you got it working :) – Joseph Aug 17 '17 at 10:21