def draw_legend(self, top_offset): """Add a legend to the map using our custom legend renderer. .. note:: getLegend generates a pixmap in 150dpi so if you set the map to a higher dpi it will appear undersized. :param top_offset: Vertical offset at which the logo should be drawn. :type top_offset: int """ LOGGER.debug('InaSAFE Map drawLegend called') mapLegendAttributes = self.map_legend_attributes() legendNotes = mapLegendAttributes.get('legend_notes', None) legendUnits = mapLegendAttributes.get('legend_units', None) legendTitle = mapLegendAttributes.get('legend_title', None) LOGGER.debug(mapLegendAttributes) myLegend = MapLegend(self.layer, self.pageDpi, legendTitle, legendNotes, legendUnits) self.legend = myLegend.get_legend() myPicture1 = QgsComposerPicture(self.composition) myLegendFilePath = unique_filename( prefix='legend', suffix='.png', dir='work') self.legend.save(myLegendFilePath, 'PNG') myPicture1.setPictureFile(myLegendFilePath) myLegendHeight = points_to_mm(self.legend.height(), self.pageDpi) myLegendWidth = points_to_mm(self.legend.width(), self.pageDpi) myPicture1.setItemPosition(self.pageMargin, top_offset, myLegendWidth, myLegendHeight) myPicture1.setFrameEnabled(False) self.composition.addItem(myPicture1) os.remove(myLegendFilePath)
def test_get_vector_legend(self): """Getting a legend for a vector layer works. .. note:: This test is not related do thousand separator since we insert our own legend notes and our own layer. """ layer, _ = load_layer('test_shakeimpact.shp') map_legend = MapLegend( layer, legend_notes='Thousand separator represented by \',\'', legend_units='(people per cell)') image = map_legend.vector_legend() path = unique_filename( prefix='getVectorLegend', suffix='.png', dir=temp_dir('test')) image.save(path, 'PNG') LOGGER.debug(path) # As we have discovered, different versions of Qt and # OS platforms cause different output, so myControlImages is a list # of 'known good' renders. tolerance = 0 # to allow for version number changes in disclaimer flag, message = check_images( 'getVectorLegend', path, tolerance) message += ( '\nWe want these images to match, if they do already ' 'copy the test image generated to create a new control image.') self.assertTrue(flag, message)
def test_get_legend(self): """Getting a legend for a generic layer works.""" LOGGER.debug('test_get_legend called') layer, _ = load_layer('test_shakeimpact.shp') map_legend = MapLegend(layer) self.assertTrue(map_legend.layer is not None) legend = map_legend.get_legend() path = unique_filename( prefix='getLegend', suffix='.png', dir=temp_dir('test')) legend.save(path, 'PNG') LOGGER.debug(path) # As we have discovered, different versions of Qt and # OS platforms cause different output, so myControlImages is a list # of 'known good' renders. tolerance = 0 # to allow for version number changes in disclaimer flag, message = check_images('getLegend', path, tolerance) message += ( '\nWe want these images to match, if they do already ' 'copy the test image generated to create a new control ' 'image.') self.assertTrue(flag, message) LOGGER.debug('test_getLegend done')
def draw_legend(self, top_offset): """Add a legend to the map using our custom legend renderer. .. note:: getLegend generates a pixmap in 150dpi so if you set the map to a higher dpi it will appear undersized. :param top_offset: Vertical offset at which the logo should be drawn. :type top_offset: int """ LOGGER.debug('InaSAFE Map drawLegend called') legend_attributes = self.map_legend_attributes() legend_notes = legend_attributes.get('legend_notes', None) legend_units = legend_attributes.get('legend_units', None) legend_title = legend_attributes.get('legend_title', None) LOGGER.debug(legend_attributes) legend = MapLegend(self.layer, self.page_dpi, legend_title, legend_notes, legend_units) self.legend = legend.get_legend() picture1 = QgsComposerPicture(self.composition) legend_file_path = unique_filename(prefix='legend', suffix='.png', dir='work') self.legend.save(legend_file_path, 'PNG') picture1.setPictureFile(legend_file_path) legend_height = points_to_mm(self.legend.height(), self.page_dpi) legend_width = points_to_mm(self.legend.width(), self.page_dpi) picture1.setItemPosition(self.page_margin, top_offset, legend_width, legend_height) picture1.setFrameEnabled(False) self.composition.addItem(picture1) os.remove(legend_file_path)
def test_add_class_to_legend(self): """Test we can add a class to the map legend.""" layer, _ = load_layer('test_shakeimpact.shp') map_legend = MapLegend(layer) colour = QtGui.QColor(12, 34, 126) map_legend.add_class( colour, label='bar') map_legend.add_class( colour, label='foo') path = unique_filename( prefix='addClassToLegend', suffix='.png', dir=temp_dir('test')) map_legend.get_legend().save(path, 'PNG') LOGGER.debug(path) # 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 tolerance = 0 # to allow for version number changes in disclaimer flag, message = check_images( 'addClassToLegend', path, tolerance) message += ( '\nWe want these images to match, if they do already copy the test' ' image generated to create a new control image.') self.assertTrue(flag, message)
def test_add_symbol_to_legend(self): """Test we can add a symbol to the legend.""" layer, _ = load_layer('test_floodimpact.tif') map_legend = MapLegend(layer) symbol = QgsFillSymbolV2() symbol.setColor(QtGui.QColor(12, 34, 56)) map_legend.add_symbol( symbol, minimum=0, # expect 2.0303 in legend maximum=2.02030, label='Foo') path = unique_filename( prefix='addSymbolToLegend', suffix='.png', dir=temp_dir('test')) map_legend.get_legend().save(path, 'PNG') LOGGER.debug(path) # As we have discovered, different versions of Qt and # OS platforms cause different output, so myControlImages is a list # of 'known good' renders. tolerance = 0 # to allow for version number changes in disclaimer flag, message = check_images( 'addSymbolToLegend', path, tolerance) message += ( '\nWe want these images to match, if they do already, copy the ' 'test image generated to create a new control image.') self.assertTrue(flag, message)