def test_ResultStyling(self): """Test that ouputs from a model are correctly styled (colours and opacity. """ # Push OK with the left mouse button print '--------------------' print combosToString(DOCK) myResult, myMessage = setupScenario( theHazard=('A flood in Jakarta like in 2007'), theExposure='People', theFunction='Need evacuation', theFunctionId='Flood Evacuation Function') assert myResult, myMessage # Enable on-the-fly reprojection setCanvasCrs(GEOCRS, True) setJakartaGeoExtent() # Run manually so we can get the output layer DOCK.setupCalculator() myRunner = DOCK.calculator.getRunner() myRunner.run() # Run in same thread myEngineImpactLayer = myRunner.impactLayer() myQgisImpactLayer = DOCK.readImpactLayer(myEngineImpactLayer) myStyle = myEngineImpactLayer.get_style_info() #print myStyle setRasterStyle(myQgisImpactLayer, myStyle) # simple test for now - we could test explicity for style state # later if needed. myMessage = ('Raster layer was not assigned a ColorRampShader' ' as expected.') assert myQgisImpactLayer.colorShadingAlgorithm() == \ QgsRasterLayer.ColorRampShader, myMessage myMessage = ('Raster layer was not assigned transparency' 'classes as expected.') myTransparencyList = (myQgisImpactLayer.rasterTransparency(). transparentSingleValuePixelList()) #print "Transparency list:" + str(myTransparencyList) assert (len(myTransparencyList) > 0)
def test_transparency_of_minimum_value(self): """Test that transparency of minimum value works when set to 100% """ # This dataset has all cells with value 1.3 myLayer, _ = loadLayer('issue126.tif') # Note the float quantity values below myStyleInfo = {} myStyleInfo['style_classes'] = [ {'colour': '#FFFFFF', 'transparency': 100, 'quantity': 0.0}, {'colour': '#38A800', 'quantity': 0.038362596547925065, 'transparency': 0, 'label': u'Rendah [0 orang/sel]'}, {'colour': '#79C900', 'transparency': 0, 'quantity': 0.07672519309585013}, {'colour': '#CEED00', 'transparency': 0, 'quantity': 0.1150877896437752}, {'colour': '#FFCC00', 'quantity': 0.15345038619170026, 'transparency': 0, 'label': u'Sedang [0 orang/sel]'}, {'colour': '#FF6600', 'transparency': 0, 'quantity': 0.19181298273962533}, {'colour': '#FF0000', 'transparency': 0, 'quantity': 0.23017557928755039}, {'colour': '#7A0000', 'quantity': 0.26853817583547546, 'transparency': 0, 'label': u'Tinggi [0 orang/sel]'}] myMessage = 'Could not create raster style' try: setRasterStyle(myLayer, myStyleInfo) except: raise Exception(myMessage) myMessage = ('Should get a single transparency class for first style ' 'class') myTransparencyList = (myLayer.rasterTransparency(). transparentSingleValuePixelList()) self.assertEqual(len(myTransparencyList), 1)
def test_issue126(self): """Test that non integer transparency ranges fail gracefully. .. seealso:: https://github.com/AIFDR/inasafe/issues/126 """ # This dataset has all cells with value 1.3 myLayer, _ = loadLayer('issue126.tif') # Note the float quantity values below myStyleInfo = {} myStyleInfo['style_classes'] = [ dict(colour='#38A800', quantity=1.1, transparency=100), dict(colour='#38A800', quantity=1.4, transparency=0), dict(colour='#79C900', quantity=10.1, transparency=0)] myMessage = ('Setting style info with float based ranges should fail ' 'gracefully.') try: setRasterStyle(myLayer, myStyleInfo) except: raise Exception(myMessage) # Now validate the transparency values were set to 255 because # they are floats and we cant specify pixel ranges to floats # Note we don't test on the exact interval because 464c6171dd55 myValue1 = myLayer.rasterTransparency().alphaValue(1.2) myValue2 = myLayer.rasterTransparency().alphaValue(1.5) myMessage = ('Transparency should be ignored when style class' ' quantities are floats') assert myValue1 == myValue2 == 255, myMessage # Now run the same test again for int intervals myStyleInfo['style_classes'] = [ dict(colour='#38A800', quantity=2, transparency=100), dict(colour='#38A800', quantity=4, transparency=0), dict(colour='#79C900', quantity=10, transparency=0)] myMessage = ('Setting style info with generate valid transparent ' 'pixel entries.') try: setRasterStyle(myLayer, myStyleInfo) except: raise Exception(myMessage) # Now validate the transparency values were set to 255 because # they are floats and we cant specify pixel ranges to floats myValue1 = myLayer.rasterTransparency().alphaValue(1) myValue2 = myLayer.rasterTransparency().alphaValue(3) myMessage1 = myMessage + 'Expected 0 got %i' % myValue1 myMessage2 = myMessage + 'Expected 255 got %i' % myValue2 assert myValue1 == 0, myMessage1 assert myValue2 == 255, myMessage2 # Verify that setRasterStyle doesn't break when floats coincide with # integers # See https://github.com/AIFDR/inasafe/issues/126#issuecomment-5978416 myStyleInfo['style_classes'] = [ dict(colour='#38A800', quantity=2.0, transparency=100), dict(colour='#38A800', quantity=4.0, transparency=0), dict(colour='#79C900', quantity=10.0, transparency=0)] myMessage = ('Broken: Setting style info with generate valid ' 'transparent ' 'floating point pixel entries such as 2.0, 3.0') try: setRasterStyle(myLayer, myStyleInfo) except Exception, e: raise Exception(myMessage + ': ' + str(e))
def test_issue126(self): """Test that non integer transparency ranges fail gracefully. .. seealso:: https://github.com/AIFDR/inasafe/issues/126 """ # This dataset has all cells with value 1.3 myLayer, myType = loadLayer('issue126.tif') del myType # Note the float quantity values below myStyleInfo = {} myStyleInfo['style_classes'] = [ dict(colour='#38A800', quantity=1.1, transparency=100), dict(colour='#38A800', quantity=1.4, transparency=0), dict(colour='#79C900', quantity=10.1, transparency=0) ] myMessage = ('Setting style info with float based ranges should fail ' 'gracefully.') try: setRasterStyle(myLayer, myStyleInfo) except: raise Exception(myMessage) # Now validate the transparency values were set to 255 because # they are floats and we cant specify pixel ranges to floats myValue1 = myLayer.rasterTransparency().alphaValue(1.1) myValue2 = myLayer.rasterTransparency().alphaValue(1.4) myMessage = ('Transparency should be ignored when style class' ' quantities are floats') assert myValue1 == myValue2 == 255, myMessage # Now run the same test again myStyleInfo['style_classes'] = [ dict(colour='#38A800', quantity=2, transparency=100), dict(colour='#38A800', quantity=4, transparency=0), dict(colour='#79C900', quantity=10, transparency=0) ] myMessage = ('Setting style info with generate valid transparent ' 'pixel entries.') try: setRasterStyle(myLayer, myStyleInfo) except: raise Exception(myMessage) # Now validate the transparency values were set to 255 because # they are floats and we cant specify pixel ranges to floats myValue1 = myLayer.rasterTransparency().alphaValue(1) myValue2 = myLayer.rasterTransparency().alphaValue(3) myMessage1 = myMessage + 'Expected 0 got %i' % myValue1 myMessage2 = myMessage + 'Expected 255 got %i' % myValue2 assert myValue1 == 0, myMessage1 assert myValue2 == 255, myMessage2 # Verify that setRasterStyle doesn't break when floats coincide with # integers # See https://github.com/AIFDR/inasafe/issues/126#issuecomment-5978416 myStyleInfo['style_classes'] = [ dict(colour='#38A800', quantity=2.0, transparency=100), dict(colour='#38A800', quantity=4.0, transparency=0), dict(colour='#79C900', quantity=10.0, transparency=0) ] myMessage = ('Broken: Setting style info with generate valid ' 'transparent ' 'floating point pixel entries such as 2.0, 3.0') try: setRasterStyle(myLayer, myStyleInfo) except Exception, e: raise Exception(myMessage + ': ' + str(e))