本文整理汇总了Python中safe_qgis.safe_interface.temp_dir函数的典型用法代码示例。如果您正苦于以下问题:Python temp_dir函数的具体用法?Python temp_dir怎么用?Python temp_dir使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了temp_dir函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: test_addClassToLegend
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
示例3: test_addSymbolToLegend
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
示例4: test_getVectorLegend
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
示例5: test_printImpactTable
def test_printImpactTable(self):
"""Test that we can render html from impact table keywords."""
LOGGER.debug('InaSAFE HtmlRenderer testing printImpactTable')
myFilename = 'test_floodimpact.tif'
myLayer, _ = load_layer(myFilename)
myMessage = 'Layer is not valid: %s' % myFilename
assert myLayer.isValid(), myMessage
myPageDpi = 300
myHtmlRenderer = HtmlRenderer(myPageDpi)
myPath = unique_filename(prefix='impactTable',
suffix='.pdf',
dir=temp_dir('test'))
myKeywordIO = KeywordIO()
myKeywords = myKeywordIO.read_keywords(myLayer)
myPath = myHtmlRenderer.print_impact_table(myKeywords,
filename=myPath)
myMessage = 'Rendered output does not exist: %s' % myPath
assert os.path.exists(myPath), myMessage
# pdf rendering is non deterministic so we can't do a hash check
# test_renderComposition renders just the image instead of pdf
# so we hash check there and here we just do a basic minimum file
# size check.
mySize = os.stat(myPath).st_size
myExpectedSizes = [20936, # as rendered on linux ub 12.04 64
21523, # as rendered on linux ub 12.10 64
20605, # as rendered on linux ub 13.04 64
21527, # as rendered on Jenkins post 22 June 2013
377191, # as rendered on OSX
252699L, # as rendered on Windows 7 64 bit
251782L, # as rendered on Windows 8 64 bit amd
21491, # as rendered on Slackware64 14.0
]
print 'Output pdf to %s' % myPath
self.assertIn(mySize, myExpectedSizes)
示例6: make_pdf
def make_pdf(self, filename):
"""Generate the printout for our final map.
:param filename: Path on the file system to which the pdf should be
saved. If None, a generated file name will be used.
:type filename: str
:returns: File name of the output file (equivalent to filename if
provided).
:rtype: str
"""
LOGGER.debug('InaSAFE Map printToPdf called')
if filename is None:
myMapPdfPath = unique_filename(
prefix='report', suffix='.pdf', dir=temp_dir('work'))
else:
# We need to cast to python string in case we receive a QString
myMapPdfPath = str(filename)
self.compose_map()
self.printer = setup_printer(myMapPdfPath)
_, myImage, myRectangle = self.render()
myPainter = QtGui.QPainter(self.printer)
myPainter.drawImage(myRectangle, myImage, myRectangle)
myPainter.end()
return myMapPdfPath
示例7: test_renderHtmlToImage
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
示例8: test_defaultTemplate
def test_defaultTemplate(self):
"""Test that loading default template works"""
LOGGER.info('Testing defaultTemplate')
myLayer, _ = load_layer('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.set_impact_layer(myLayer)
myPath = unique_filename(prefix='mapDefaultTemplateTest',
suffix='.pdf',
dir=temp_dir('test'))
myMap.make_pdf(myPath)
LOGGER.debug(myPath)
myMessage = 'Rendered output does not exist: %s' % myPath
assert os.path.exists(myPath), myMessage
# pdf rendering is non deterministic so we can't do a hash check
# test_renderComposition renders just the image instead of pdf
# so we hash check there and here we just do a basic minimum file
# size check.
mySize = os.stat(myPath).st_size
# Note: You should replace, not append the numbers for a given
# platform. Also note that this test will break every time the
# version number of InaSAFE changes so we should ultimately come up
# with a lower maintenance test strategy.
myExpectedSizes = [
400350, # Slackware64 14.0
]
myMessage = '%s\nExpected rendered map pdf to be in %s, got %s' % (
myPath, myExpectedSizes, mySize)
self.assertIn(mySize, myExpectedSizes, myMessage)
示例9: test_printImpactTable
def test_printImpactTable(self):
"""Test that we can render html from impact table keywords."""
LOGGER.debug('InaSAFE HtmlRenderer testing printImpactTable')
myFilename = 'test_floodimpact.tif'
myLayer, _ = loadLayer(myFilename)
myMessage = 'Layer is not valid: %s' % myFilename
assert myLayer.isValid(), myMessage
myPageDpi = 300
myHtmlRenderer = HtmlRenderer(myPageDpi)
myPath = unique_filename(prefix='impactTable',
suffix='.pdf',
dir=temp_dir('test'))
myKeywordIO = KeywordIO()
myKeywords = myKeywordIO.readKeywords(myLayer)
myPath = myHtmlRenderer.printImpactTable(myKeywords,
theFilename=myPath)
myMessage = 'Rendered output does not exist: %s' % myPath
assert os.path.exists(myPath), myMessage
# pdf rendering is non deterministic so we can't do a hash check
# test_renderComposition renders just the image instead of pdf
# so we hash check there and here we just do a basic minimum file
# size check.
mySize = os.stat(myPath).st_size
myExpectedSize = 20936 # as rendered on linux ub 12.04 64
myMessage = ('Expected rendered table pdf to be at least %s, got %s'
% (myExpectedSize, mySize))
assert mySize >= myExpectedSize, myMessage
示例10: test_add_class_to_legend
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)
示例11: test_getVectorLegend
def test_getVectorLegend(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.
"""
myLayer, _ = load_layer('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')
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 = check_images(
'getVectorLegend', 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
示例12: test_printToPdf
def test_printToPdf(self):
"""Test making a pdf of the map - this is the most typical use of map.
"""
LOGGER.info('Testing printToPdf')
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()
myPath = unique_filename(prefix='mapPdfTest',
suffix='.pdf',
dir=temp_dir('test'))
myMap.printToPdf(myPath)
LOGGER.debug(myPath)
myMessage = 'Rendered output does not exist: %s' % myPath
assert os.path.exists(myPath), myMessage
# pdf rendering is non deterministic so we can't do a hash check
# test_renderComposition renders just the image instead of pdf
# so we hash check there and here we just do a basic minimum file
# size check.
mySize = os.stat(myPath).st_size
myExpectedSize = 352798 # as rendered on linux ub 12.04 64
myMessage = 'Expected rendered map pdf to be at least %s, got %s' % (
myExpectedSize, mySize)
assert mySize >= myExpectedSize, myMessage
示例13: test_getVectorLegend
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
示例14: printToPdf
def printToPdf(self, theHtml, theFilename=None):
"""Render an html snippet into the printer, paginating as needed.
Args:
* theHtml: str A string containing an html snippet. It will have a
header and footer appended to it in order to make it a valid
html document. The header will also apply the bootstrap theme
to the document.
* theFilename: str String containing a pdf file path that the
output will be written to.
Returns:
str: The file path of the output pdf (which is the same as the
theFilename parameter if it was specified.
Raises:
None
"""
LOGGER.info('InaSAFE Map printToPdf called')
if theFilename is None:
myHtmlPdfPath = unique_filename(
prefix='table', suffix='.pdf', dir=temp_dir('work'))
else:
# We need to cast to python string in case we receive a QString
myHtmlPdfPath = str(theFilename)
self.printer = setup_printer(myHtmlPdfPath)
self.loadAndWait(theHtmlSnippet=theHtml)
self.webView.print_(self.printer)
return myHtmlPdfPath
示例15: clone_shp_layer
def clone_shp_layer(
name='tsunami_polygon',
include_keywords=False,
source_directory=TESTDATA,
target_directory='testing'):
"""Helper function that copies a test shp layer and returns it.
:param name: The default name for the shp layer.
:type name: str
:param include_keywords: Include keywords file if True.
:type include_keywords: bool
:param source_directory: Directory where the file is located.
:type source_directory: str
:param target_directory: Subdirectory in InaSAFE temp dir that we want to
put the files into. Default to 'testing'.
:type target_directory: str
"""
extensions = ['.shp', '.shx', '.dbf', '.prj']
if include_keywords:
extensions.append('.keywords')
temp_path = unique_filename(dir=temp_dir(target_directory))
# copy to temp file
for ext in extensions:
src_path = os.path.join(source_directory, name + ext)
if os.path.exists(src_path):
target_path = temp_path + ext
shutil.copy2(src_path, target_path)
shp_path = '%s.shp' % temp_path
layer = QgsVectorLayer(shp_path, os.path.basename(shp_path), 'ogr')
return layer