本文整理汇总了Python中safe.common.utilities.unique_filename函数的典型用法代码示例。如果您正苦于以下问题:Python unique_filename函数的具体用法?Python unique_filename怎么用?Python unique_filename使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unique_filename函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_export_import_setting
def test_export_import_setting(self):
"""Test for export_setting method."""
profile_file = unique_filename(
suffix='.json', dir='population_preference')
original_settings = {
'key': 'value',
'key_bool': True,
'population_preference': generate_default_profile(),
'key_int': 1,
'key_float': 2.0
}
# Write
for key, value in list(original_settings.items()):
set_setting(key, value, self.qsettings)
# Export
inasafe_settings = export_setting(profile_file, self.qsettings)
# Check result
self.assertTrue(os.path.exists(profile_file))
self.assertEqual(inasafe_settings['key'], 'value')
self.assertEqual(
inasafe_settings['population_preference'],
generate_default_profile())
# Import
read_setting = import_setting(profile_file, self.qsettings)
self.assertDictEqual(inasafe_settings, read_setting)
self.assertDictEqual(original_settings, read_setting)
示例2: test_relative_path
def test_relative_path(self):
"""Test we calculate the relative paths correctly when saving scenario.
"""
result, message = setup_scenario(
self.DOCK,
hazard='Classified Flood',
exposure='Population')
self.assertTrue(result, message)
fake_dir = standard_data_path()
scenario_file = unique_filename(
prefix='scenarioTest', suffix='.txt', dir=fake_dir)
exposure_layer = self.DOCK.get_exposure_layer().publicSource()
hazard_layer = self.DOCK.get_hazard_layer().publicSource()
relative_exposure = self.save_scenario_dialog.relative_path(
scenario_file, exposure_layer)
relative_hazard = self.save_scenario_dialog.relative_path(
scenario_file, hazard_layer)
if 'win32' in sys.platform:
# windows
self.assertEqual(
'exposure\\pop_binary_raster_20_20.asc',
relative_exposure)
self.assertEqual(
'hazard\\classified_flood_20_20.asc',
relative_hazard)
else:
self.assertEqual(
'exposure/pop_binary_raster_20_20.asc',
relative_exposure)
self.assertEqual(
'hazard/classified_flood_20_20.asc',
relative_hazard)
示例3: 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')
示例4: read_from_qgis_native
def read_from_qgis_native(self, qgis_layer):
"""Read raster data from qgis layer QgsRasterLayer.
A stub is used now:
save all data in a file,
then call safe.read_from_file
Raises:
* TypeError if qgis is not avialable
* IOError if can't store temporary file
* GetDataError if can't create copy of qgis_layer's
dataProvider
"""
base_name = unique_filename()
file_name = base_name + '.tif'
file_writer = QgsRasterFileWriter(file_name)
pipe = QgsRasterPipe()
provider = qgis_layer.dataProvider()
if not pipe.set(provider.clone()):
msg = "Cannot set pipe provider"
raise GetDataError(msg)
file_writer.writeRaster(
pipe,
provider.xSize(),
provider.ySize(),
provider.extent(),
provider.crs())
# Write keywords if any
write_keywords(self.keywords, base_name + '.keywords')
self.read_from_file(file_name)
示例5: 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)
示例6: 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)
示例7: 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)
示例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: clip_raster
def clip_raster(raster, column_count, row_count, output_extent):
"""Clip raster to specified extent, width and height.
Note there is similar utility in safe_qgis.utilities.clipper, but it uses
gdal whereas this one uses native QGIS.
:param raster: Raster
:type raster: QgsRasterLayer
:param column_count: Desired width in pixels of new raster
:type column_count: Int
:param row_count: Desired height in pixels of new raster
:type row_count: Int
:param output_extent: Extent of the clipped region
:type output_extent: QgsRectangle
:returns: Clipped region of the raster
:rtype: QgsRasterLayer
"""
provider = raster.dataProvider()
pipe = QgsRasterPipe()
pipe.set(provider.clone())
base_name = unique_filename()
file_name = base_name + ".tif"
file_writer = QgsRasterFileWriter(file_name)
file_writer.writeRaster(pipe, column_count, row_count, output_extent, raster.crs())
return QgsRasterLayer(file_name, "clipped_raster")
示例10: 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
示例11: test_standard_properties
def test_standard_properties(self):
metadata = AggregationLayerMetadata(unique_filename())
with self.assertRaises(KeyError):
metadata.get_property('non_existing_key')
# from BaseMetadata
metadata.get_property('organisation')
示例12: print_map_to_pdf
def print_map_to_pdf(self, output_path):
"""Generate the printout for our final map as pdf.
:param output_path: Path on the file system to which the pdf should be
saved. If None, a generated file name will be used.
:type output_path: str
:returns: File name of the output file (equivalent to filename if
provided).
:rtype: str
"""
LOGGER.debug('InaSAFE Map print_to_pdf called')
self.setup_composition()
try:
self.load_template()
except TemplateLoadingError:
raise
self.draw_composition()
if output_path is None:
output_path = unique_filename(
prefix='report', suffix='.pdf', dir=temp_dir())
self.composition.exportAsPDF(output_path)
return output_path
示例13: create_grid
def create_grid(size):
"""Create a polygonal grid using Processing.
:param size: The cell size.
:type size: int
:return: The grid layer in memory.
:rtype: QgsVectorLayer
"""
output_filename = unique_filename(prefix='grid', suffix='.shp')
result = processing.runalg(
'qgis:vectorgrid',
'336199.970553,352338.397991,7636164.67975,7648562.41208',
size, # X spacing
size, # Y spacing
0, # Output as polygons
output_filename)
layer = QgsVectorLayer(output_filename, 'grid', 'ogr')
layer.setCrs(QgsCoordinateReferenceSystem(32740))
remove_fields(layer, ['xmin', 'xmax', 'ymin', 'ymax'])
# Make a copy in memory
memory = create_memory_layer(
'grid', layer.geometryType(), layer.crs(), layer.fields())
copy_layer(layer, memory)
print "NB cells : %s" % layer.featureCount()
return memory
示例14: 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'])
示例15: read_from_qgis_native
def read_from_qgis_native(self, qgis_layer):
"""Read and unpack vector data from qgis layer QgsVectorLayer.
A stub is used now:
save all data in a file,
then call safe.read_from_file
Raises:
* TypeError if qgis is not avialable
* IOError if can't store temporary file
"""
# FIXME (DK): this branch isn't covered by test
if not QGIS_IS_AVAILABLE:
msg = ('Used data is QgsVectorLayer instance, '
'but QGIS is not available.')
raise TypeError(msg)
base_name = unique_filename()
file_name = base_name + '.shp'
error = QgsVectorFileWriter.writeAsVectorFormat(
qgis_layer,
file_name,
"UTF8",
qgis_layer.crs(),
"ESRI Shapefile"
)
if error != QgsVectorFileWriter.NoError:
# FIXME (DK): this branch isn't covered by test
msg = ('Can not save data in temporary file.')
raise IOError(msg)
# Write keywords if any
write_keywords(self.keywords, base_name + '.keywords')
self.read_from_file(file_name)