def drawLegend(self, theTopOffset): """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. Args: theTopOffset - vertical offset at which to begin drawing Returns: None Raises: None """ LOGGER.debug('InaSAFE Map drawLegend called') myLegend = MapLegend(self.layer, self.pageDpi) self.legend = myLegend.getLegend() myPicture1 = QgsComposerPicture(self.composition) myLegendFilePath = unique_filename(prefix='legend', suffix='.png', dir='work') self.legend.save(myLegendFilePath, 'PNG') myPicture1.setPictureFile(myLegendFilePath) myLegendHeight = pointsToMM(self.legend.height(), self.pageDpi) myLegendWidth = pointsToMM(self.legend.width(), self.pageDpi) myPicture1.setItemPosition(self.pageMargin, theTopOffset, myLegendWidth, myLegendHeight) myPicture1.setFrame(False) self.composition.addItem(myPicture1) os.remove(myLegendFilePath)
def drawLegend(self, theTopOffset): """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. Args: theTopOffset - vertical offset at which to begin drawing Returns: None Raises: None """ LOGGER.debug("InaSAFE Map drawLegend called") mapLegendAttributes = self.getMapLegendAtributes() 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.getLegend() myPicture1 = QgsComposerPicture(self.composition) myLegendFilePath = unique_filename(prefix="legend", suffix=".png", dir="work") self.legend.save(myLegendFilePath, "PNG") myPicture1.setPictureFile(myLegendFilePath) myLegendHeight = pointsToMM(self.legend.height(), self.pageDpi) myLegendWidth = pointsToMM(self.legend.width(), self.pageDpi) myPicture1.setItemPosition(self.pageMargin, theTopOffset, myLegendWidth, myLegendHeight) myPicture1.setFrame(False) self.composition.addItem(myPicture1) os.remove(myLegendFilePath)
def drawLegend(self, theTopOffset): """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. Args: theTopOffset - vertical offset at which to begin drawing Returns: None Raises: None """ LOGGER.debug('InaSAFE Map drawLegend called') mapLegendAttributes = self.getMapLegendAtributes() 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.getLegend() myPicture1 = QgsComposerPicture(self.composition) myLegendFilePath = unique_filename(prefix='legend', suffix='.png', dir='work') self.legend.save(myLegendFilePath, 'PNG') myPicture1.setPictureFile(myLegendFilePath) myLegendHeight = pointsToMM(self.legend.height(), self.pageDpi) myLegendWidth = pointsToMM(self.legend.width(), self.pageDpi) myPicture1.setItemPosition(self.pageMargin, theTopOffset, myLegendWidth, myLegendHeight) myPicture1.setFrame(False) self.composition.addItem(myPicture1) os.remove(myLegendFilePath)
def test_mmPointConversion(self): """Test that conversions between pixel and page dimensions work.""" myDpi = 300 myPixels = 300 myMM = 25.4 # 1 inch myResult = pointsToMM(myPixels, myDpi) myMessage = "Expected: %s\nGot: %s" % (myMM, myResult) assert myResult == myMM, myMessage myResult = mmToPoints(myMM, myDpi) myMessage = "Expected: %s\nGot: %s" % (myPixels, myResult) assert myResult == myPixels, myMessage