本文整理汇总了Python中safe.common.utilities.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
myMap = Map(IFACE)
myMap.setupComposition()
myPdfPath = os.path.join(temp_dir(), 'outArtifactsTest.pdf')
myMap.setupPrinter(myPdfPath)
myPixmap = QtGui.QPixmap(10, 10)
myPixmap.fill(QtGui.QColor(250, 250, 250))
myFilename = os.path.join(temp_dir(), '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
示例2: test_constructor
def test_constructor(self):
"""Test create shake data."""
local_path = os.path.join(temp_dir('realtime-test'), 'shakemaps')
try:
event_one = ShakeData(working_dir=local_path)
event_two = ShakeData(
working_dir=temp_dir('realtime-test'),
event=SHAKE_ID)
self.assertEqual(event_one.event_id, SHAKE_ID)
self.assertEqual(event_two.event_id, SHAKE_ID)
except:
raise
示例3: test_convert_grid_to_raster
def test_convert_grid_to_raster(self):
"""Test converting grid.xml to raster (tif file)"""
grid_title = 'Earthquake'
grid_source = 'USGS'
output_raster = unique_filename(
prefix='result_grid',
suffix='.tif',
dir=temp_dir('test'))
result = convert_mmi_data(
GRID_PATH, grid_title, grid_source, output_path=output_raster,
algorithm=NEAREST_NEIGHBOUR)
expected_result = output_raster.replace(
'.tif', '-%s.tif' % NEAREST_NEIGHBOUR)
self.assertEqual(
result, expected_result,
'Result path not as expected')
exists = os.path.exists(result)
self.assertTrue(exists, 'File result : %s does not exist' % result)
exists = os.path.exists(result[:-3] + 'xml')
self.assertTrue(
exists,
'File result : %s does not exist' % result[:-3] + 'xml')
exists = os.path.exists(result[:-3] + 'qml')
self.assertTrue(
exists,
'File result : %s does not exist' % result[:-3] + 'qml')
示例4: test_convert_grid_to_raster_with_ascii
def test_convert_grid_to_raster_with_ascii(self):
"""Test converting grid.xml to raster (tif file)"""
grid_title = 'Earthquake'
grid_source = 'USGS'
output_raster = unique_filename(
prefix='result_grid',
suffix='.tif',
dir=temp_dir('test'))
result = convert_mmi_data(
GRID_PATH,
grid_title,
grid_source,
algorithm=USE_ASCII,
output_path=output_raster)
expected_result = output_raster.replace('.tif', '-%s.tif' % USE_ASCII)
self.assertEqual(
result, expected_result,
'Result path not as expected')
exists = os.path.exists(result)
self.assertTrue(exists, 'File result : %s does not exist' % result)
exists = os.path.exists(result[:-3] + 'xml')
self.assertTrue(
exists,
'File result : %s does not exist' % result[:-3] + 'xml')
exists = os.path.exists(result[:-3] + 'qml')
self.assertTrue(
exists,
'File result : %s does not exist' % result[:-3] + 'qml')
tif_file = load_layer(result)[0]
keywords = tif_file.keywords
self.assertEqual(keywords['hazard'], hazard_earthquake['key'])
population_classification = list(keywords['thresholds'][
exposure_population['key']].keys())[0]
self.assertEqual(
population_classification, earthquake_mmi_scale['key'])
示例5: setUp
def setUp(self):
"""Setup before each test."""
# Download files (which are local files) to realtime-test temp folder
local_path = os.path.join(temp_dir('realtime-test'))
shake_data = standard_data_path('hazard', 'shake_data', SHAKE_ID)
shutil.copytree(
shake_data, os.path.join(local_path, 'shakemaps', SHAKE_ID))
示例6: testSqliteWriting
def testSqliteWriting(self):
"""Test that writing a dataset to sqlite works."""
keywords = read_keywords(SHP_BASE + '.keywords')
layer = Vector(data=SHP_BASE + '.shp', keywords=keywords)
test_dir = temp_dir(sub_dir='test')
test_file = unique_filename(suffix='.sqlite', dir=test_dir)
layer.write_to_file(test_file, sublayer='foo')
示例7: clone_shp_layer
def clone_shp_layer(
name,
include_keywords,
source_directory,
target_directory='test'):
"""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 'test'.
: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
示例8: test_converting
def test_converting(self):
"""Test converting grif file to tiff."""
dialog = ShakemapConverterDialog(PARENT, IFACE)
dialog.use_output_default.setEnabled(False)
grid_path = standard_data_path(
'hazard',
'shake_data',
'20131105060809',
'output',
'grid.xml')
output_raster = unique_filename(
prefix='result_grid',
suffix='.tif',
dir=temp_dir('test'))
dialog.load_result.setEnabled(True)
dialog.load_result.setChecked(False)
dialog.input_path.setText(grid_path)
dialog.nearest_mode.setChecked(True)
dialog.output_path.setText(output_raster)
button = dialog.button_box.button(QDialogButtonBox.Ok)
button.click()
msg = 'Raster is not created'
output_path = '%s-nearest.tif' % output_raster[:-4]
self.assertTrue(os.path.exists(output_path), msg)
示例9: test_generate_html_reports
def test_generate_html_reports(self):
"""Test generate_html_reports function."""
self.mock_the_dialog(test_entire_mode=False)
self.impact_merge_dialog.prepare_input()
self.impact_merge_dialog.validate_all_layers()
first_postprocessing_report = self.impact_merge_dialog.first_impact["postprocessing_report"]
second_postprocessing_report = self.impact_merge_dialog.second_impact["postprocessing_report"]
first_report = "<body>" + first_postprocessing_report + "</body>"
second_report = "<body>" + second_postprocessing_report + "</body>"
# Now create a dom document for each
first_document = minidom.parseString(first_report)
second_document = minidom.parseString(second_report)
first_impact_tables = first_document.getElementsByTagName("table")
second_impact_tables = second_document.getElementsByTagName("table")
first_report_dict = self.impact_merge_dialog.generate_report_dictionary_from_dom(first_impact_tables)
second_report_dict = self.impact_merge_dialog.generate_report_dictionary_from_dom(second_impact_tables)
self.impact_merge_dialog.generate_html_reports(first_report_dict, second_report_dict)
# There should be 4 HTML files generated on temp_dir()
html_list = glob(os.path.join(temp_dir(), "*.html"))
expected_html_number = 4
self.assertEqual(len(html_list), expected_html_number)
示例10: Xtest_renderTable
def Xtest_renderTable(self):
"""Test that html renders nicely. Commented out for now until we work
out how to get webkit to do offscreen rendering nicely."""
myFilename = 'test_floodimpact.tif'
myLayer, myType = loadLayer(myFilename)
CANVAS.refresh()
del myType
myMessage = 'Layer is not valid: %s' % myFilename
assert myLayer.isValid(), myMessage
myMap = Map(IFACE)
myMap.setImpactLayer(myLayer)
myPixmap = myMap.renderImpactTable()
assert myPixmap is not None
myExpectedWidth = 500
myExpectedHeight = 300
myMessage = 'Invalid width - got %s expected %s' % (
myPixmap.width(),
myExpectedWidth)
assert myPixmap.width() == myExpectedWidth, myMessage
myMessage = 'Invalid height - got %s expected %s' % (
myPixmap.height(),
myExpectedHeight)
assert myPixmap.height() == myExpectedHeight
myPath = os.path.join(temp_dir(), 'renderImpactTable.png')
myPixmap.save(myPath, 'PNG')
myExpectedHash = 'c9164d5c2bb85c6081905456ab827f3e'
assertHashForFile(myExpectedHash, myPath)
示例11: setUp
def setUp(self):
"""Runs before each test."""
# noinspection PyUnresolvedReferences
self.map_layer_registry = QgsMapLayerRegistry.instance()
self.register_layers()
# Create Impact Merge Dialog
self.impact_merge_dialog = ImpactMergeDialog(PARENT, IFACE)
# Create test dir
test_data_dir = temp_dir('test')
test_impact_merge_dir = os.path.join(
test_data_dir, 'test-impact-merge')
if not os.path.exists(test_impact_merge_dir):
os.makedirs(test_impact_merge_dir)
# Create test dir for aggregated
# noinspection PyUnresolvedReferences
test_aggregated_dir = os.path.join(
test_impact_merge_dir, 'aggregated')
if not os.path.exists(test_aggregated_dir):
os.makedirs(test_aggregated_dir)
# Create test dir for entire
test_entire_dir = os.path.join(
test_impact_merge_dir, 'entire')
if not os.path.exists(test_entire_dir):
os.makedirs(test_entire_dir)
示例12: test_sorted_impacted_cities
def test_sorted_impacted_cities(self):
"""Test getting impacted cities sorted by mmi then population."""
working_dir = shakemap_extract_dir()
shake_event = ShakeEvent(
working_dir=working_dir,
event_id=SHAKE_ID,
data_is_local_flag=True)
table = shake_event.sorted_impacted_cities()
file_path = unique_filename(
prefix='test_sorted_impacted_cities',
suffix='.txt',
dir=temp_dir('test'))
cities_file = file(file_path, 'w')
cities_file.writelines(str(table))
cities_file.close()
table = str(table).replace(', \'', ',\n\'')
table += '\n'
fixture_path = os.path.join(
data_dir(), 'tests', 'test_sorted_impacted_cities.txt')
cities_file = file(fixture_path)
expected_string = cities_file.read()
cities_file.close()
expected_string = expected_string.replace(', \'', ',\n\'')
self.max_diff = None
message = 'Expectation:\n%s, Got\n%s' % (expected_string, table)
self.assertEqual(expected_string, table, message)
示例13: Xtest_print_impact_table
def Xtest_print_impact_table(self):
"""Test print impact table to pdf."""
impact_layer_path = test_data_path(
'impact', 'population_affected_entire_area.shp')
layer, _ = load_layer(impact_layer_path)
# noinspection PyUnresolvedReferences,PyArgumentList
QgsMapLayerRegistry.instance().addMapLayer(layer)
# noinspection PyCallingNonCallable
rect = QgsRectangle(106.8194, -6.2108, 106.8201, -6.1964)
CANVAS.setExtent(rect)
CANVAS.refresh()
template = resources_path(
'qgis-composer-templates', 'a4-portrait-blue.qpt')
report = ImpactReport(IFACE, template, layer)
report.template = template # just to cover set template
out_path = unique_filename(
prefix='test_print_impact_table',
suffix='.pdf',
dir=temp_dir('test'))
report.print_impact_table(out_path)
# Check the file exists
message = 'Rendered output does not exist: %s' % out_path
self.assertTrue(os.path.exists(out_path), message)
# Check the file is not corrupt
message = 'The output file %s is corrupt' % out_path
out_size = os.stat(out_path).st_size
self.assertTrue(out_size > 0, message)
示例14: clone_raster_layer
def clone_raster_layer(name, extension, include_keywords, source_directory, target_directory="test"):
"""Helper function that copies a test raster.
:param name: The default name for the raster layer.
:type name: str
:param extension: The extension of the raster file.
:type extension: 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 = [".prj", ".sld", "qml", ".prj", extension]
if include_keywords:
extensions.append(".xml")
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):
trg_path = temp_path + ext
shutil.copy2(src_path, trg_path)
raster_path = "%s%s" % (temp_path, extension)
layer = QgsRasterLayer(raster_path, os.path.basename(raster_path))
return layer
示例15: test_print_default_template
def test_print_default_template(self):
"""Test printing report to pdf using default template works."""
impact_layer_path = test_data_path(
'impact', 'population_affected_entire_area.shp')
layer, _ = load_layer(impact_layer_path)
# noinspection PyUnresolvedReferences
QgsMapLayerRegistry.instance().addMapLayer(layer)
# noinspection PyCallingNonCallable
rect = QgsRectangle(106.8194, -6.2108, 106.8201, -6.1964)
CANVAS.setExtent(rect)
CANVAS.refresh()
template = resources_path(
'qgis-composer-templates', 'inasafe-portrait-a4.qpt')
report = ImpactReport(IFACE, template, layer)
out_path = unique_filename(
prefix='map_default_template_test',
suffix='.pdf',
dir=temp_dir('test'))
report.print_map_to_pdf(out_path)
# Check the file exists
message = 'Rendered output does not exist: %s' % out_path
self.assertTrue(os.path.exists(out_path), message)
# Check the file is not corrupt
message = 'The output file %s is corrupt' % out_path
out_size = os.stat(out_path).st_size
self.assertTrue(out_size > 0, message)
# Check the components in composition are default components
if qgis_version() < 20500:
safe_logo = report.composition.getComposerItemById(
'safe-logo').pictureFile()
north_arrow = report.composition.getComposerItemById(
'north-arrow').pictureFile()
org_logo = report.composition.getComposerItemById(
'organisation-logo').pictureFile()
else:
safe_logo = report.composition.getComposerItemById(
'safe-logo').picturePath()
north_arrow = report.composition.getComposerItemById(
'north-arrow').picturePath()
org_logo = report.composition.getComposerItemById(
'organisation-logo').picturePath()
expected_safe_logo = resources_path(
'img', 'logos', 'inasafe-logo-url.svg')
expected_north_arrow = resources_path(
'img', 'north_arrows', 'simple_north_arrow.png')
expected_org_logo = resources_path('img', 'logos', 'supporters.png')
message = 'The safe logo path is not the default one'
self.assertEqual(expected_safe_logo, safe_logo, message)
message = 'The north arrow path is not the default one'
self.assertEqual(expected_north_arrow, north_arrow, message)
message = 'The organisation logo path is not the default one'
self.assertEqual(expected_org_logo, org_logo, message)