本文整理汇总了Python中qgis.core.QgsComposerPicture类的典型用法代码示例。如果您正苦于以下问题:Python QgsComposerPicture类的具体用法?Python QgsComposerPicture怎么用?Python QgsComposerPicture使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QgsComposerPicture类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_windowsDrawingArtifacts
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)
setup_printer(myPath)
myMap.setup_composition()
myImage = QtGui.QImage(10, 10, QtGui.QImage.Format_RGB32)
myImage.setDotsPerMeterX(dpi_to_meters(300))
myImage.setDotsPerMeterY(dpi_to_meters(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 qgis_version() >= 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
# noinspection PyCallByClass,PyTypeChecker,PyArgumentList
myPixmapItem = myMap.composition.addPixmap(
QtGui.QPixmap.fromImage(myImage))
myPixmapItem.setOffset(i, i + 20)
# Same drawing using our drawImage Helper
myWidthMM = 1
myMap.draw_image(myImage, myWidthMM, i, i + 40)
myImagePath, _, _ = myMap.render()
# when this test no longer matches our broken render hash
# we know the issue is fixed
myTolerance = 0
myFlag, myMessage = check_images(
'windowsArtifacts',
myImagePath,
myTolerance)
myMessage += ('\nWe want these images to match, if they do not '
'there may be rendering artifacts in windows.\n')
assert myFlag, myMessage
示例2: draw_logo
def draw_logo(self, top_offset):
"""Add a picture containing the logo to the map top left corner
:param top_offset: Vertical offset at which the logo should be drawn.
:type top_offset: int
"""
myLogo = QgsComposerPicture(self.composition)
myLogo.setPictureFile(':/plugins/inasafe/bnpb_logo.png')
myLogo.setItemPosition(self.pageMargin, top_offset, 10, 10)
myLogo.setFrameEnabled(self.showFramesFlag)
myLogo.setZValue(1) # To ensure it overlays graticule markers
self.composition.addItem(myLogo)
示例3: test_windowsDrawingArtifacts
def test_windowsDrawingArtifacts(self):
"""Test that windows rendering does not make artifacts"""
# sometimes spurious lines are drawn on the layout
myMap = ISMap(IFACE)
myMap.setupComposition()
myPdfPath = os.path.join(getTempDir(), 'outArtifactsTest.pdf')
myMap.setupPrinter(myPdfPath)
myPixmap = QtGui.QPixmap(10, 10)
myPixmap.fill(QtGui.QColor(250, 250, 250))
myFilename = os.path.join(getTempDir(), 'greyBox')
myPixmap.save(myFilename, 'PNG')
for i in range(10, 190, 10):
myPicture = QgsComposerPicture(myMap.composition)
myPicture.setPictureFile(myFilename)
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(myPixmap)
myPixmapItem.setOffset(i, i + 20)
# Same drawing using our drawPixmap Helper
myWidthMM = 1
myMap.drawPixmap(myPixmap, myWidthMM, i, i + 40)
myMap.renderPrintout()
myUnwantedHash = 'd05e9223d50baf8bb147475aa96d6ba3'
myHash = hashForFile(myPdfPath)
# when this test no longer matches our broken render hash
# we know the issue is fixed
myMessage = 'Windows map render still draws with artifacts.'
assert myHash != myUnwantedHash, myMessage
示例4: draw_legend
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)
示例5: drawLegend
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)
示例6: drawLegend
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)
示例7: testGridNorth
def testGridNorth(self):
"""Test syncing picture to grid north"""
composition = QgsComposition(QgsProject.instance())
composerMap = QgsComposerMap(composition)
composerMap.setNewExtent(QgsRectangle(0, -256, 256, 0))
composition.addComposerMap(composerMap)
composerPicture = QgsComposerPicture(composition)
composition.addComposerPicture(composerPicture)
composerPicture.setRotationMap(composerMap.id())
self.assertTrue(composerPicture.rotationMap() >= 0)
composerPicture.setNorthMode(QgsComposerPicture.GridNorth)
composerMap.setMapRotation(45)
self.assertEqual(composerPicture.pictureRotation(), 45)
# add an offset
composerPicture.setNorthOffset(-10)
self.assertEqual(composerPicture.pictureRotation(), 35)
示例8: test_windowsDrawingArtifacts
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, i, 10, 10) # x # y # width # 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
示例9: drawLogo
def drawLogo(self, theTopOffset):
"""Add a picture containing the logo to the map top left corner
Args:
theTopOffset - vertical offset at which the logo shoudl be drawn
Returns:
None
Raises:
None
"""
myLogo = QgsComposerPicture(self.composition)
myLogo.setPictureFile(':/plugins/inasafe/bnpb_logo.png')
myLogo.setItemPosition(self.pageMargin,
theTopOffset,
10,
10)
myLogo.setFrame(self.showFramesFlag)
myLogo.setZValue(1) # To ensure it overlays graticule markers
self.composition.addItem(myLogo)
示例10: __init__
def __init__(self, methodName):
"""Run once on class initialization."""
unittest.TestCase.__init__(self, methodName)
TEST_DATA_DIR = unitTestDataPath()
self.pngImage = TEST_DATA_DIR + "/sample_image.png"
# create composition
self.composition = QgsComposition(QgsProject.instance())
self.composition.setPaperSize(297, 210)
self.composerPicture = QgsComposerPicture(self.composition)
self.composerPicture.setPicturePath(self.pngImage)
self.composerPicture.setSceneRect(QRectF(70, 70, 100, 100))
self.composerPicture.setFrameEnabled(True)
self.composition.addComposerPicture(self.composerPicture)
示例11: TestQgsComposerPicture
class TestQgsComposerPicture(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Bring up a simple HTTP server, for remote picture tests
os.chdir(unitTestDataPath() + '')
handler = http.server.SimpleHTTPRequestHandler
cls.httpd = socketserver.TCPServer(('localhost', 0), handler)
cls.port = cls.httpd.server_address[1]
cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever)
cls.httpd_thread.setDaemon(True)
cls.httpd_thread.start()
def __init__(self, methodName):
"""Run once on class initialization."""
unittest.TestCase.__init__(self, methodName)
TEST_DATA_DIR = unitTestDataPath()
self.pngImage = TEST_DATA_DIR + "/sample_image.png"
# create composition
self.mapSettings = QgsMapSettings()
self.composition = QgsComposition(self.mapSettings, QgsProject.instance())
self.composition.setPaperSize(297, 210)
self.composerPicture = QgsComposerPicture(self.composition)
self.composerPicture.setPicturePath(self.pngImage)
self.composerPicture.setSceneRect(QRectF(70, 70, 100, 100))
self.composerPicture.setFrameEnabled(True)
self.composition.addComposerPicture(self.composerPicture)
def testResizeZoom(self):
"""Test picture resize zoom mode."""
self.composerPicture.setResizeMode(QgsComposerPicture.Zoom)
checker = QgsCompositionChecker('composerpicture_resize_zoom', self.composition)
checker.setControlPathPrefix("composer_picture")
testResult, message = checker.testComposition()
assert testResult, message
@unittest.skip('test is broken for qt5/python3 - feature works')
def testRemoteImage(self):
"""Test fetching remote picture."""
self.composerPicture.setPicturePath('http://localhost:' + str(TestQgsComposerPicture.port) + '/qgis_local_server/logo.png')
checker = QgsCompositionChecker('composerpicture_remote', self.composition)
checker.setControlPathPrefix("composer_picture")
testResult, message = checker.testComposition()
self.composerPicture.setPicturePath(self.pngImage)
assert testResult, message
def testGridNorth(self):
"""Test syncing picture to grid north"""
mapSettings = QgsMapSettings()
composition = QgsComposition(mapSettings, QgsProject.instance())
composerMap = QgsComposerMap(composition)
composerMap.setNewExtent(QgsRectangle(0, -256, 256, 0))
composition.addComposerMap(composerMap)
composerPicture = QgsComposerPicture(composition)
composition.addComposerPicture(composerPicture)
composerPicture.setRotationMap(composerMap.id())
self.assertTrue(composerPicture.rotationMap() >= 0)
composerPicture.setNorthMode(QgsComposerPicture.GridNorth)
composerMap.setMapRotation(45)
self.assertEqual(composerPicture.pictureRotation(), 45)
# add an offset
composerPicture.setNorthOffset(-10)
self.assertEqual(composerPicture.pictureRotation(), 35)
def testTrueNorth(self):
"""Test syncing picture to true north"""
mapSettings = QgsMapSettings()
composition = QgsComposition(mapSettings, QgsProject.instance())
composerMap = QgsComposerMap(composition)
composerMap.setCrs(QgsCoordinateReferenceSystem.fromEpsgId(3575))
composerMap.setNewExtent(QgsRectangle(-2126029.962, -2200807.749, -119078.102, -757031.156))
composition.addComposerMap(composerMap)
composerPicture = QgsComposerPicture(composition)
composition.addComposerPicture(composerPicture)
composerPicture.setRotationMap(composerMap.id())
self.assertTrue(composerPicture.rotationMap() >= 0)
composerPicture.setNorthMode(QgsComposerPicture.TrueNorth)
self.assertAlmostEqual(composerPicture.pictureRotation(), 37.20, 1)
# shift map
#.........这里部分代码省略.........
示例12: testTrueNorth
def testTrueNorth(self):
"""Test syncing picture to true north"""
mapSettings = QgsMapSettings()
composition = QgsComposition(mapSettings, QgsProject.instance())
composerMap = QgsComposerMap(composition)
composerMap.setCrs(QgsCoordinateReferenceSystem.fromEpsgId(3575))
composerMap.setNewExtent(QgsRectangle(-2126029.962, -2200807.749, -119078.102, -757031.156))
composition.addComposerMap(composerMap)
composerPicture = QgsComposerPicture(composition)
composition.addComposerPicture(composerPicture)
composerPicture.setRotationMap(composerMap.id())
self.assertTrue(composerPicture.rotationMap() >= 0)
composerPicture.setNorthMode(QgsComposerPicture.TrueNorth)
self.assertAlmostEqual(composerPicture.pictureRotation(), 37.20, 1)
# shift map
composerMap.setNewExtent(QgsRectangle(2120672.293, -3056394.691, 2481640.226, -2796718.780))
self.assertAlmostEqual(composerPicture.pictureRotation(), -38.18, 1)
# rotate map
composerMap.setMapRotation(45)
self.assertAlmostEqual(composerPicture.pictureRotation(), -38.18 + 45, 1)
# add an offset
composerPicture.setNorthOffset(-10)
self.assertAlmostEqual(composerPicture.pictureRotation(), -38.18 + 35, 1)
示例13: TestQgsComposerPicture
class TestQgsComposerPicture(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Bring up a simple HTTP server, for remote picture tests
os.chdir(unitTestDataPath() + '')
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
cls.httpd = socketserver.TCPServer(('localhost', 0), handler)
cls.port = cls.httpd.server_address[1]
cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever)
cls.httpd_thread.setDaemon(True)
cls.httpd_thread.start()
def __init__(self, methodName):
"""Run once on class initialization."""
unittest.TestCase.__init__(self, methodName)
TEST_DATA_DIR = unitTestDataPath()
self.pngImage = TEST_DATA_DIR + "/sample_image.png"
# create composition
self.mapSettings = QgsMapSettings()
self.composition = QgsComposition(self.mapSettings)
self.composition.setPaperSize(297, 210)
self.composerPicture = QgsComposerPicture(self.composition)
self.composerPicture.setPicturePath(self.pngImage)
self.composerPicture.setSceneRect(QRectF(70, 70, 100, 100))
self.composerPicture.setFrameEnabled(True)
self.composition.addComposerPicture(self.composerPicture)
def testResizeZoom(self):
"""Test picture resize zoom mode."""
self.composerPicture.setResizeMode(QgsComposerPicture.Zoom)
checker = QgsCompositionChecker('composerpicture_resize_zoom', self.composition)
checker.setControlPathPrefix("composer_picture")
testResult, message = checker.testComposition()
assert testResult, message
def testRemoteImage(self):
"""Test fetching remote picture."""
self.composerPicture.setPicturePath('http://localhost:' + str(TestQgsComposerPicture.port) + '/qgis_local_server/logo.png')
checker = QgsCompositionChecker('composerpicture_remote', self.composition)
checker.setControlPathPrefix("composer_picture")
testResult, message = checker.testComposition()
self.composerPicture.setPicturePath(self.pngImage)
assert testResult, message