Ejemplo n.º 1
0
 def test_getVectorLegend(self):
     """Getting a legend for a vector layer works."""
     myLayer, _ = loadLayer('test_shakeimpact.shp')
     myMapLegend = MapLegend(
         myLayer,
         theLegendNotes='Thousand separator represented by \'.\'',
         theLegendUnits='(people per cell)')
     myImage = myMapLegend.getVectorLegend()
     myPath = unique_filename(prefix='getVectorLegend',
                              suffix='.png',
                              dir=temp_dir('test'))
     myImage.save(myPath, 'PNG')
     print myMapLegend.legendUnits
     print myMapLegend.legendNotes
     print myPath
     LOGGER.debug(myPath)
     # As we have discovered, different versions of Qt and
     # OS platforms cause different output, so myControlImages is a list
     # of 'known good' renders.
     myControlImages = ['getVectorLegend.png',
                        'getVectorLegend-variantWindowsVistaSP2-32.png',
                        'getVectorLegend-variantWindowsXPSP3-32.png',
                        'getVectorLegend-variantOSXml.png',
                        'getVectorLegend-variantUB12.04-64.png',
                        'getVectorLegend-variantUB11.04-64.png',
                        'getVectorLegend-variantLinuxMint-14-x86_64.png',
                        'getVectorLegend-variantWindows7-SP1-AMD64.png',
                        'getVectorLegend-variantJenkins.png']
     myTolerance = 0  # to allow for version number changes in disclaimer
     myFlag, myMessage = checkImages(myControlImages, myPath, myTolerance)
     myMessage += ('\nWe want these images to match, if they do already '
                   'copy the test image generated to create a new control '
                   'image.')
     assert myFlag, myMessage
Ejemplo n.º 2
0
 def test_getRasterLegend(self):
     """Getting a legend for a raster layer works."""
     myLayer, _ = loadLayer('test_floodimpact.tif')
     myMapLegend = MapLegend(myLayer)
     myImage = myMapLegend.getRasterLegend()
     myPath = unique_filename(prefix='getRasterLegend',
                              suffix='.png',
                              dir=temp_dir('test'))
     myImage.save(myPath, 'PNG')
     LOGGER.debug(myPath)
     # As we have discovered, different versions of Qt and
     # OS platforms cause different output, so myControlImages is a list
     # of 'known good' renders.
     myControlImages = [
         'getRasterLegend.png',
         'getRasterLegend-variantWindosVistaSP2-32.png',
         'getRasterLegend-variantUB12.04-64.png',
         'getRasterLegend-variantUB11.04-64.png',
         'getRasterLegend-variantJenkins.png'
     ]
     myTolerance = 0  # to allow for version number changes in disclaimer
     myFlag, myMessage = checkImages(myControlImages, myPath, myTolerance)
     myMessage += (
         '\nWe want these images to match, if they do already '
         'copy the test image generated to create a new control image.')
     assert myFlag, myMessage
Ejemplo n.º 3
0
    def test_renderHtmlToImage(self):
        """Test that we can render html to a pixmap."""
        LOGGER.debug('InaSAFE HtmlRenderer testing renderHtmlToImage')
        myHtml = self.sampleHtml(20)
        LOGGER.debug(myHtml)
        myPageDpi = 300
        myRenderer = HtmlRenderer(myPageDpi)
        myPath = unique_filename(prefix='testHtmlToImage',
                                 suffix='.png',
                                 dir=temp_dir('test'))
        LOGGER.debug(myPath)
        myWidth = 250
        myPixmap = myRenderer.renderHtmlToImage(myHtml, myWidth)
        assert not myPixmap.isNull()
        LOGGER.debug(myPixmap.__class__)
        myPixmap.save(myPath)
        myMessage = 'Rendered output does not exist: %s' % myPath
        assert os.path.exists(myPath), myMessage

        myControlImages = ['renderHtmlToImage.png',
                           'renderHtmlToImage-variantOSX10.8.png',
                           'renderHtmlToImage-variantWindosVistaSP2-32.png',
                           'renderHtmlToImage-variantWindowsXPSP3-32.png',
                           'renderHtmlToImage-variantUB11.04-64.png',
                           'renderHtmlToImage-variantLinuxMint-14-x86_64.png',
                           'renderHtmlToImage-variantWindows7-SP1-AMD64.png',
                           'renderHtmlToImage-variantUB11.10-64.png']
        myTolerance = 1000  # to allow for version number changes in disclaimer
        myFlag, myMessage = checkImages(myControlImages, myPath, myTolerance)
        assert myFlag, myMessage
Ejemplo n.º 4
0
    def test_renderHtmlToImage(self):
        """Test that we can render html to a pixmap."""
        LOGGER.debug('InaSAFE HtmlRenderer testing renderHtmlToImage')
        myHtml = self.sampleHtml(20)
        LOGGER.debug(myHtml)
        myPageDpi = 300
        myRenderer = HtmlRenderer(myPageDpi)
        myPath = unique_filename(prefix='testHtmlToImage',
                                 suffix='.png',
                                 dir=temp_dir('test'))
        LOGGER.debug(myPath)
        myWidth = 250
        myPixmap = myRenderer.renderHtmlToImage(myHtml, myWidth)
        assert not myPixmap.isNull()
        LOGGER.debug(myPixmap.__class__)
        myPixmap.save(myPath)
        myMessage = 'Rendered output does not exist: %s' % myPath
        assert os.path.exists(myPath), myMessage

        myControlImages = [
            'renderHtmlToImage.png',
            'renderHtmlToImage-variantWindosVistaSP2-32.png',
            'renderHtmlToImage-variantUB11.04-64.png',
            'renderHtmlToImage-variantUB11.10-64.png'
        ]
        myTolerance = 1000  # to allow for version number changes in disclaimer
        myFlag, myMessage = checkImages(myControlImages, myPath, myTolerance)
        assert myFlag, myMessage
Ejemplo n.º 5
0
 def test_addSymbolToLegend(self):
     """Test we can add a symbol to the legend."""
     myLayer, _ = loadLayer('test_floodimpact.tif')
     myMapLegend = MapLegend(myLayer)
     mySymbol = QgsSymbol()
     mySymbol.setColor(QtGui.QColor(12, 34, 56))
     myMapLegend.addSymbolToLegend(mySymbol,
                                   theMin=0,
                                   theMax=2,
                                   theCategory=None,
                                   theLabel='Foo')
     myPath = unique_filename(prefix='addSymblToLegend',
                              suffix='.png',
                              dir=temp_dir('test'))
     myMapLegend.getLegend().save(myPath, 'PNG')
     LOGGER.debug(myPath)
     # As we have discovered, different versions of Qt and
     # OS platforms cause different output, so myControlImages is a list
     # of 'known good' renders.
     myControlImages = [
         'addSymbolToLegend.png',
         'addSymbolToLegend-variantWindosVistaSP2-32.png',
         'addSymbolToLegend-variantUB12.04-64.png',
         'addSymbolToLegend-variantUB11.04-64.png',
         'addSymbolToLegend-variantJenkins.png'
     ]
     myTolerance = 0  # to allow for version number changes in disclaimer
     myFlag, myMessage = checkImages(myControlImages, myPath, myTolerance)
     myMessage += (
         '\nWe want these images to match, if they do already '
         'copy the test image generated to create a new control image.')
     assert myFlag, myMessage
Ejemplo n.º 6
0
    def test_getLegend(self):
        """Getting a legend for a generic layer works."""
        LOGGER.debug('test_getLegend called')
        myLayer, _ = loadLayer('test_shakeimpact.shp')
        myMapLegend = MapLegend(myLayer)
        assert myMapLegend.layer is not None
        myLegend = myMapLegend.getLegend()
        myPath = unique_filename(prefix='getLegend',
                                 suffix='.png',
                                 dir=temp_dir('test'))
        myLegend.save(myPath, 'PNG')
        LOGGER.debug(myPath)
        # As we have discovered, different versions of Qt and
        # OS platforms cause different output, so myControlImages is a list
        # of 'known good' renders.

        myTolerance = 0  # to allow for version number changes in disclaimer

        myFlag, myMessage = checkImages('getLegend.png',
                                        myPath,
                                        myTolerance)
        myMessage += ('\nWe want these images to match, if they do already '
                      'copy the test image generated to create a new control '
                      'image.')
        assert myFlag, myMessage
        LOGGER.debug('test_getLegend done')
Ejemplo n.º 7
0
    def test_getVectorLegend(self):
        """Getting a legend for a vector layer works."""
        myLayer, _ = loadLayer('test_shakeimpact.shp')
        myMapLegend = MapLegend(
            myLayer,
            theLegendNotes='Thousand separator represented by \'.\'',
            theLegendUnits='(people per cell)')
        myImage = myMapLegend.getVectorLegend()
        myPath = unique_filename(prefix='getVectorLegend',
                                 suffix='.png',
                                 dir=temp_dir('test'))
        myImage.save(myPath, 'PNG')
        print myMapLegend.legendUnits
        print myMapLegend.legendNotes
        print myPath
        LOGGER.debug(myPath)
        # As we have discovered, different versions of Qt and
        # OS platforms cause different output, so myControlImages is a list
        # of 'known good' renders.

        myTolerance = 0  # to allow for version number changes in disclaimer
        myFlag, myMessage = checkImages(
            'getVectorLegend.png', myPath, myTolerance)
        myMessage += ('\nWe want these images to match, if they do already '
                      'copy the test image generated to create a new control '
                      'image.')
        assert myFlag, myMessage
Ejemplo n.º 8
0
 def test_addClassToLegend(self):
     """Test we can add a class to the map legend."""
     myLayer, _ = loadLayer('test_shakeimpact.shp')
     myMapLegend = MapLegend(myLayer)
     myColour = QtGui.QColor(12, 34, 126)
     myMapLegend.addClassToLegend(myColour,
                                  theMin=None,
                                  theMax=None,
                                  theCategory=None,
                                  theLabel='bar')
     myMapLegend.addClassToLegend(myColour,
                                  theMin=None,
                                  theMax=None,
                                  theCategory=None,
                                  theLabel='foo')
     myPath = unique_filename(prefix='addClassToLegend',
                              suffix='.png',
                              dir=temp_dir('test'))
     myMapLegend.getLegend().save(myPath, 'PNG')
     LOGGER.debug(myPath)
     # As we have discovered, different versions of Qt and
     # OS platforms cause different output, so place any other possible
     # variants in the safe_qgis/test_data/test_images/ dir e.g.
     # addClassToLegend-variantUbuntu13.04.png
     myTolerance = 0  # to allow for version number changes in disclaimer
     myFlag, myMessage = checkImages('addClassToLegend.png',
                                     myPath,
                                     myTolerance)
     myMessage += ('\nWe want these images to match, if they do already '
                   'copy the test image generated to create a new control '
                   'image.')
     assert myFlag, myMessage
Ejemplo n.º 9
0
    def test_addSymbolToLegend(self):
        """Test we can add a symbol to the legend."""
        myLayer, _ = loadLayer('test_floodimpact.tif')
        myMapLegend = MapLegend(myLayer)
        mySymbol = QgsSymbol()
        mySymbol.setColor(QtGui.QColor(12, 34, 56))
        myMapLegend.addSymbolToLegend(mySymbol,
                                      theMin=0,
                                      # expect 2.0303 in legend
                                      theMax=2.02030,
                                      theCategory=None,
                                      theLabel='Foo')
        myPath = unique_filename(prefix='addSymbolToLegend',
                                 suffix='.png',
                                 dir=temp_dir('test'))
        myMapLegend.getLegend().save(myPath, 'PNG')
        LOGGER.debug(myPath)
        # As we have discovered, different versions of Qt and
        # OS platforms cause different output, so myControlImages is a list
        # of 'known good' renders.

        myTolerance = 0  # to allow for version number changes in disclaimer
        myFlag, myMessage = checkImages('addSymbolToLegend.png',
                                        myPath,
                                        myTolerance)
        myMessage += ('\nWe want these images to match, if they do already '
                      'copy the test image generated to create a new control '
                      'image.')
        assert myFlag, myMessage
Ejemplo n.º 10
0
 def test_getVectorLegend(self):
     """Getting a legend for a vector layer works."""
     myLayer, _ = loadLayer('test_shakeimpact.shp')
     myMapLegend = MapLegend(myLayer)
     myImage = myMapLegend.getVectorLegend()
     myPath = unique_filename(prefix='getVectorLegend',
                              suffix='.png',
                              dir=temp_dir('test'))
     myImage.save(myPath, 'PNG')
     LOGGER.debug(myPath)
     # As we have discovered, different versions of Qt and
     # OS platforms cause different output, so myControlImages is a list
     # of 'known good' renders.
     myControlImages = ['getVectorLegend.png',
                        'getVectorLegend-variantWindosVistaSP2-32.png',
                        'getVectorLegend-variantUB12.04-64.png',
                        'getVectorLegend-variantUB11.04-64.png',
                        'getVectorLegend-variantJenkins.png']
     myTolerance = 0  # to allow for version number changes in disclaimer
     myFlag, myMessage = checkImages(myControlImages,
                                 myPath,
                                 myTolerance)
     myMessage += ('\nWe want these images to match, if they do already '
             'copy the test image generated to create a new control image.')
     assert myFlag, myMessage
Ejemplo n.º 11
0
 def test_addClassToLegend(self):
     """Test we can add a class to the map legend."""
     myLayer, _ = loadLayer('test_shakeimpact.shp')
     myMapLegend = MapLegend(myLayer)
     myColour = QtGui.QColor(12, 34, 126)
     myMapLegend.addClassToLegend(myColour,
                            theMin=None,
                            theMax=None,
                            theCategory=None,
                            theLabel='bar')
     myMapLegend.addClassToLegend(myColour,
                            theMin=None,
                            theMax=None,
                            theCategory=None,
                            theLabel='foo')
     myPath = unique_filename(prefix='addClassToLegend',
                              suffix='.png',
                              dir=temp_dir('test'))
     myMapLegend.getLegend().save(myPath, 'PNG')
     LOGGER.debug(myPath)
     # As we have discovered, different versions of Qt and
     # OS platforms cause different output, so myControlImages is a list
     # of 'known good' renders.
     myControlImages = ['getClassToLegend.png',
                        'getClassToLegend-variantWindosVistaSP2-32.png',
                        'getClassToLegend-variantUB12.04-64.png',
                        'getClassToLegend-variantUB11.04-64.png',
                        'getClassToLegend-variantJenkins.png']
     myTolerance = 0  # to allow for version number changes in disclaimer
     myFlag, myMessage = checkImages(myControlImages,
                                     myPath,
                                     myTolerance)
     myMessage += ('\nWe want these images to match, if they do already '
             'copy the test image generated to create a new control image.')
     assert myFlag, myMessage
Ejemplo n.º 12
0
    def test_windowsDrawingArtifacts(self):
        """Test that windows rendering does not make artifacts"""
        # sometimes spurious lines are drawn on the layout
        LOGGER.info('Testing windowsDrawingArtifacts')
        myPath = unique_filename(prefix='artifacts',
                                 suffix='.pdf',
                                 dir=temp_dir('test'))
        myMap = Map(IFACE)
        setupPrinter(myPath)
        myMap.setupComposition()

        myImage = QtGui.QImage(10, 10, QtGui.QImage.Format_RGB32)
        myImage.setDotsPerMeterX(dpiToMeters(300))
        myImage.setDotsPerMeterY(dpiToMeters(300))
        #myImage.fill(QtGui.QColor(250, 250, 250))
        # Look at the output, you will see antialiasing issues around some
        # of the boxes drawn...
        # myImage.fill(QtGui.QColor(200, 200, 200))
        myImage.fill(200 + 200 * 256 + 200 * 256 * 256)
        myFilename = os.path.join(temp_dir(), 'greyBox')
        myImage.save(myFilename, 'PNG')
        for i in range(10, 190, 10):
            myPicture = QgsComposerPicture(myMap.composition)
            myPicture.setPictureFile(myFilename)
            if qgisVersion() >= 10800:  # 1.8 or newer
                myPicture.setFrameEnabled(False)
            else:
                myPicture.setFrame(False)
            myPicture.setItemPosition(i,  # x
                                      i,  # y
                                      10,  # width
                                      10)  # height
            myMap.composition.addItem(myPicture)
            # Same drawing drawn directly as a pixmap
            myPixmapItem = myMap.composition.addPixmap(
                QtGui.QPixmap.fromImage(myImage))
            myPixmapItem.setOffset(i, i + 20)
            # Same drawing using our drawImage Helper
            myWidthMM = 1
            myMap.drawImage(myImage, myWidthMM, i, i + 40)

        myImagePath, _, _ = myMap.renderComposition()
        # when this test no longer matches our broken render hash
        # we know the issue is fixed

        myControlImages = ['windowsArtifacts.png']
        myTolerance = 0
        myFlag, myMessage = checkImages(myControlImages,
                                        myImagePath,
                                        myTolerance)
        myMessage += ('\nWe want these images to match, if they do not '
                      'there may be rendering artifacts in windows.\n')
        assert myFlag, myMessage
Ejemplo n.º 13
0
    def test_windowsDrawingArtifacts(self):
        """Test that windows rendering does not make artifacts"""
        # sometimes spurious lines are drawn on the layout
        LOGGER.info('Testing windowsDrawingArtifacts')
        myPath = unique_filename(prefix='artifacts',
                                 suffix='.pdf',
                                 dir=temp_dir('test'))
        myMap = Map(IFACE)
        setupPrinter(myPath)
        myMap.setupComposition()

        myImage = QtGui.QImage(10, 10, QtGui.QImage.Format_RGB32)
        myImage.setDotsPerMeterX(dpiToMeters(300))
        myImage.setDotsPerMeterY(dpiToMeters(300))
        #myImage.fill(QtGui.QColor(250, 250, 250))
        # Look at the output, you will see antialiasing issues around some
        # of the boxes drawn...
        # myImage.fill(QtGui.QColor(200, 200, 200))
        myImage.fill(200 + 200 * 256 + 200 * 256 * 256)
        myFilename = os.path.join(temp_dir(), 'greyBox')
        myImage.save(myFilename, 'PNG')
        for i in range(10, 190, 10):
            myPicture = QgsComposerPicture(myMap.composition)
            myPicture.setPictureFile(myFilename)
            if qgisVersion() >= 10800:  # 1.8 or newer
                myPicture.setFrameEnabled(False)
            else:
                myPicture.setFrame(False)
            myPicture.setItemPosition(i,  # x
                                      i,  # y
                                      10,  # width
                                      10)  # height
            myMap.composition.addItem(myPicture)
            # Same drawing drawn directly as a pixmap
            myPixmapItem = myMap.composition.addPixmap(
                QtGui.QPixmap.fromImage(myImage))
            myPixmapItem.setOffset(i, i + 20)
            # Same drawing using our drawImage Helper
            myWidthMM = 1
            myMap.drawImage(myImage, myWidthMM, i, i + 40)

        myImagePath, _, _ = myMap.renderComposition()
        # when this test no longer matches our broken render hash
        # we know the issue is fixed

        myTolerance = 0
        myFlag, myMessage = checkImages('windowsArtifacts.png',
                                        myImagePath,
                                        myTolerance)
        myMessage += ('\nWe want these images to match, if they do not '
                      'there may be rendering artifacts in windows.\n')
        assert myFlag, myMessage
Ejemplo n.º 14
0
    def test_renderComposition(self):
        """Test making an image of the map only."""
        LOGGER.info('Testing renderComposition')
        myLayer, _ = loadLayer('test_shakeimpact.shp')
        myCanvasLayer = QgsMapCanvasLayer(myLayer)
        CANVAS.setLayerSet([myCanvasLayer])

        myRect = QgsRectangle(106.7894, -6.2308, 106.8004, -6.2264)
        CANVAS.setExtent(myRect)
        CANVAS.refresh()
        myMap = Map(IFACE)
        myMap.setImpactLayer(myLayer)
        myMap.composeMap()
        myImagePath, myControlImage, myTargetArea = myMap.renderComposition()
        LOGGER.debug(myImagePath)

        assert myControlImage is not None

        myDimensions = [myTargetArea.left(),
                        myTargetArea.top(),
                        myTargetArea.bottom(),
                        myTargetArea.right()]
        myExpectedDimensions = [0.0, 0.0, 3507.0, 2480.0]
        myMessage = 'Expected target area to be %s, got %s' % (
            str(myExpectedDimensions), str(myDimensions))
        assert myExpectedDimensions == myDimensions, myMessage

        myMessage = 'Rendered output does not exist'
        assert os.path.exists(myImagePath), myMessage

        myAcceptableImages = [
            'renderComposition.png',
            'renderComposition-variantUB12.04.png',
            'renderComposition-variantUB12.10.png',
            'renderComposition-variantOSXml.png',
            'renderComposition-variantWindowsVistaSP2-32.png',
            'renderComposition-variantJenkins.png',
            'renderComposition-variantUB11.10-64.png',
            'renderComposition-variantLinuxMint-14-x86_64.png',
            'renderComposition-variantWindows7-SP1-AMD64.png',
            'renderComposition-variantUB11.04-64.png']
        # Beta version and version changes  can introduce a few extra chars
        # into the metadata section so we set a reasonable tolerance to cope
        # with this.
        myTolerance = 8000
        print myImagePath
        print myAcceptableImages
        myFlag, myMessage = checkImages(myAcceptableImages,
                                        myImagePath,
                                        myTolerance)
        assert myFlag, myMessage
Ejemplo n.º 15
0
    def test_renderComposition(self):
        """Test making an image of the map only."""
        LOGGER.info('Testing renderComposition')
        myLayer, _ = loadLayer('test_shakeimpact.shp')
        myCanvasLayer = QgsMapCanvasLayer(myLayer)
        CANVAS.setLayerSet([myCanvasLayer])

        myRect = QgsRectangle(106.7894, -6.2308, 106.8004, -6.2264)
        CANVAS.setExtent(myRect)
        CANVAS.refresh()
        myMap = Map(IFACE)
        myMap.setImpactLayer(myLayer)
        myMap.composeMap()
        myImagePath, myControlImage, myTargetArea = myMap.renderComposition()
        LOGGER.debug(myImagePath)

        assert myControlImage is not None

        myDimensions = [
            myTargetArea.left(),
            myTargetArea.top(),
            myTargetArea.bottom(),
            myTargetArea.right()
        ]
        myExpectedDimensions = [0.0, 0.0, 3507.0, 2480.0]
        myMessage = 'Expected target area to be %s, got %s' % (
            str(myExpectedDimensions), str(myDimensions))
        assert myExpectedDimensions == myDimensions, myMessage

        myMessage = 'Rendered output does not exist'
        assert os.path.exists(myImagePath), myMessage

        myAcceptableImages = [
            'renderComposition.png', 'renderComposition-variantUB12.04.png',
            'renderComposition-variantWindosVistaSP2-32.png',
            'renderComposition-variantJenkins.png',
            'renderComposition-variantUB11.10-64.png',
            'renderComposition-variantUB11.04-64.png'
        ]
        # Beta version and version changes  can introduce a few extra chars
        # into the metadata section so we set a reasonable tolerance to cope
        # with this.
        myTolerance = 8000
        myFlag, myMessage = checkImages(myAcceptableImages, myImagePath,
                                        myTolerance)
        assert myFlag, myMessage
Ejemplo n.º 16
0
    def test_renderHtmlToImage(self):
        """Test that we can render html to a pixmap."""
        LOGGER.debug('InaSAFE HtmlRenderer testing renderHtmlToImage')
        myHtml = self.sampleHtml(20)
        LOGGER.debug(myHtml)
        myPageDpi = 300
        myRenderer = HtmlRenderer(myPageDpi)
        myPath = unique_filename(prefix='testHtmlToImage',
                                 suffix='.png',
                                 dir=temp_dir('test'))
        LOGGER.debug(myPath)
        myWidth = 250
        myPixmap = myRenderer.renderHtmlToImage(myHtml, myWidth)
        assert not myPixmap.isNull()
        LOGGER.debug(myPixmap.__class__)
        myPixmap.save(myPath)
        myMessage = 'Rendered output does not exist: %s' % myPath
        assert os.path.exists(myPath), myMessage

        myTolerance = 1000  # to allow for version number changes in disclaimer
        myFlag, myMessage = checkImages(
            'renderHtmlToImage', myPath, myTolerance)
        assert myFlag, myMessage