本文整理汇总了Python中safe.utilities.keyword_io.KeywordIO.to_message方法的典型用法代码示例。如果您正苦于以下问题:Python KeywordIO.to_message方法的具体用法?Python KeywordIO.to_message怎么用?Python KeywordIO.to_message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe.utilities.keyword_io.KeywordIO
的用法示例。
在下文中一共展示了KeywordIO.to_message方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: show_current_metadata
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import to_message [as 别名]
def show_current_metadata(self):
"""Show metadata of the current selected layer."""
LOGGER.debug('Showing layer: ' + self.layer.name())
keywords = KeywordIO(self.layer)
content_html = keywords.to_message().to_html()
full_html = html_header() + content_html + html_footer()
self.metadata_preview_web_view.setHtml(full_html)
示例2: test_layer_to_message
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import to_message [as 别名]
def test_layer_to_message(self):
"""Test to show augmented keywords if KeywordsIO ctor passed a layer.
.. versionadded:: 3.3
"""
keywords = KeywordIO(self.vector_layer)
message = keywords.to_message().to_text()
self.assertIn('*Reference system*, ', message)
示例3: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import to_message [as 别名]
class KeywordIOTest(unittest.TestCase):
"""Tests for reading and writing of raster and vector data
"""
def setUp(self):
self.keyword_io = KeywordIO()
# SQLite Layer
uri = QgsDataSourceURI()
sqlite_building_path = standard_data_path(
'exposure', 'exposure.sqlite')
uri.setDatabase(sqlite_building_path)
uri.setDataSource('', 'buildings_osm_4326', 'Geometry')
self.sqlite_layer = QgsVectorLayer(
uri.uri(), 'OSM Buildings', 'spatialite')
self.expected_sqlite_keywords = {
'datatype': 'OSM'
}
# Raster Layer keywords
hazard_path = standard_data_path('hazard', 'tsunami_wgs84.tif')
self.raster_layer, _ = load_layer(hazard_path)
self.expected_raster_keywords = {
'hazard_category': 'single_event',
'title': 'Generic Continuous Flood',
'hazard': 'flood',
'continuous_hazard_unit': 'generic',
'layer_geometry': 'raster',
'layer_purpose': 'hazard',
'layer_mode': 'continuous',
'keyword_version': '3.5'
}
# Vector Layer keywords
vector_path = standard_data_path('exposure', 'buildings_osm_4326.shp')
self.vector_layer, _ = load_layer(vector_path)
self.expected_vector_keywords = {
'keyword_version': '3.5',
'value_map': {},
'title': 'buildings_osm_4326',
'layer_geometry': 'polygon',
'layer_purpose': 'exposure',
'layer_mode': 'classified',
'exposure': 'structure',
}
# Keyword less layer
keywordless_path = standard_data_path('other', 'keywordless_layer.shp')
self.keywordless_layer, _ = load_layer(keywordless_path)
# Keyword file
self.keyword_path = standard_data_path(
'exposure', 'buildings_osm_4326.xml')
def test_read_raster_file_keywords(self):
"""Can we read raster file keywords using generic readKeywords method
"""
layer = clone_raster_layer(
name='generic_continuous_flood',
extension='.asc',
include_keywords=True,
source_directory=standard_data_path('hazard'))
keywords = self.keyword_io.read_keywords(layer)
expected_keywords = self.expected_raster_keywords
self.assertDictEqual(keywords, expected_keywords)
def test_read_vector_file_keywords(self):
"""Test read vector file keywords with the generic readKeywords method.
"""
self.maxDiff = None
keywords = self.keyword_io.read_keywords(self.vector_layer)
expected_keywords = self.expected_vector_keywords
self.assertDictEqual(keywords, expected_keywords)
def test_read_keywordless_layer(self):
"""Test read 'keyword' file from keywordless layer.
"""
self.assertRaises(
NoKeywordsFoundError,
self.keyword_io.read_keywords,
self.keywordless_layer,
)
def test_to_message(self):
"""Test we can convert keywords to a message object.
.. versionadded:: 3.2
"""
keywords = self.keyword_io.read_keywords(self.vector_layer)
message = self.keyword_io.to_message(keywords).to_text()
self.assertIn('*Exposure*, structure------', message)
def test_layer_to_message(self):
"""Test to show augmented keywords if KeywordsIO ctor passed a layer.
.. versionadded:: 3.3
"""
keywords = KeywordIO(self.vector_layer)
message = keywords.to_message().to_text()
#.........这里部分代码省略.........
示例4: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import to_message [as 别名]
#.........这里部分代码省略.........
'test': 'TEST',
'layer_geometry': 'raster',
'layer_purpose': 'hazard',
'layer_mode': 'continuous',
'keyword_version': inasafe_keyword_version
}
message = 'Got:\n%s\nExpected:\n%s' % (keywords, expected_keywords)
self.assertDictEqual(keywords, expected_keywords, message)
def test_read_db_keywords(self):
"""Can we read sqlite kw with the generic read_keywords method
"""
db_path = test_data_path('other', 'test_keywords.db')
self.read_db_keywords(db_path)
def read_db_keywords(self, db_path):
"""Can we read sqlite keywords with the generic readKeywords method
"""
self.keyword_io.set_keyword_db_path(db_path)
# We need to use relative path so that the hash from URI will match
local_path = os.path.join(
os.path.dirname(__file__), 'exposure.sqlite')
sqlite_building_path = test_data_path('exposure', 'exposure.sqlite')
shutil.copy2(sqlite_building_path, local_path)
uri = QgsDataSourceURI()
uri.setDatabase('exposure.sqlite')
uri.setDataSource('', 'buildings_osm_4326', 'Geometry')
sqlite_layer = QgsVectorLayer(uri.uri(), 'OSM Buildings', 'spatialite')
expected_source = (
'dbname=\'exposure.sqlite\' table="buildings_osm_4326" ('
'Geometry) sql=')
message = 'Got source: %s\n\nExpected %s\n' % (
sqlite_layer.source(), expected_source)
self.assertEqual(sqlite_layer.source(), expected_source, message)
keywords = self.keyword_io.read_keywords(sqlite_layer)
expected_keywords = self.expected_sqlite_keywords
message = 'Got: %s\n\nExpected %s\n\nSource: %s' % (
keywords, expected_keywords, self.sqlite_layer.source())
self.assertDictEqual(keywords, expected_keywords, message)
# Delete SQL Layer so that we can delete the file
del sqlite_layer
os.remove(local_path)
def test_copy_keywords(self):
"""Test we can copy the keywords."""
out_path = unique_filename(
prefix='test_copy_keywords', suffix='.keywords')
self.keyword_io.copy_keywords(self.raster_layer, out_path)
copied_keywords = read_file_keywords(out_path)
expected_keywords = self.expected_raster_keywords
message = 'Got:\n%s\nExpected:\n%s\nSource:\n%s' % (
copied_keywords, expected_keywords, out_path)
self.assertDictEqual(copied_keywords, expected_keywords, message)
def test_definition(self):
"""Test we can get definitions for keywords.
.. versionadded:: 3.2
"""
keyword = 'hazards'
definition = self.keyword_io.definition(keyword)
self.assertTrue('description' in definition)
def test_to_message(self):
"""Test we can convert keywords to a message object.
.. versionadded:: 3.2
"""
keywords = self.keyword_io.read_keywords(self.vector_layer)
message = self.keyword_io.to_message(keywords).to_text()
self.assertIn('*Exposure*, structure------', message)
def test_dict_to_row(self):
"""Test the dict to row helper works.
.. versionadded:: 3.2
"""
keyword_value = (
"{'high': ['Kawasan Rawan Bencana III'], "
"'medium': ['Kawasan Rawan Bencana II'], "
"'low': ['Kawasan Rawan Bencana I']}")
table = self.keyword_io._dict_to_row(keyword_value)
self.assertIn(
u'\n---\n*high*, Kawasan Rawan Bencana III------',
table.to_text())
# should also work passing a dict
keyword_value = {
'high': ['Kawasan Rawan Bencana III'],
'medium': ['Kawasan Rawan Bencana II'],
'low': ['Kawasan Rawan Bencana I']}
table = self.keyword_io._dict_to_row(keyword_value)
self.assertIn(
u'\n---\n*high*, Kawasan Rawan Bencana III------',
table.to_text())
示例5: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import to_message [as 别名]
#.........这里部分代码省略.........
source_directory=standard_data_path('hazard'))
new_keywords = {
'hazard_category': 'multiple_event'
}
self.keyword_io.update_keywords(layer, new_keywords)
keywords = self.keyword_io.read_keywords(layer)
expected_keywords = {
'hazard_category': 'multiple_event',
'title': 'Tsunami',
'hazard': 'tsunami',
'continuous_hazard_unit': 'metres',
'layer_geometry': 'raster',
'layer_purpose': 'hazard',
'layer_mode': 'continuous',
'keyword_version': inasafe_keyword_version
}
expected_keywords = {
k: get_unicode(v) for k, v in expected_keywords.iteritems()
}
self.assertDictEqual(keywords, expected_keywords)
def test_copy_keywords(self):
"""Test we can copy the keywords."""
self.maxDiff = None
out_path = unique_filename(
prefix='test_copy_keywords', suffix='.shp')
layer = clone_raster_layer(
name='generic_continuous_flood',
extension='.asc',
include_keywords=True,
source_directory=standard_data_path('hazard'))
self.keyword_io.copy_keywords(layer, out_path)
# copied_keywords = read_file_keywords(out_path.split('.')[0] + 'xml')
copied_keywords = read_iso19115_metadata(out_path)
expected_keywords = self.expected_raster_keywords
expected_keywords['keyword_version'] = inasafe_keyword_version
self.assertDictEqual(copied_keywords, expected_keywords)
def test_definition(self):
"""Test we can get definitions for keywords.
.. versionadded:: 3.2
"""
keyword = 'hazards'
keyword_definition = definition(keyword)
self.assertTrue('description' in keyword_definition)
def test_to_message(self):
"""Test we can convert keywords to a message object.
.. versionadded:: 3.2
"""
keywords = self.keyword_io.read_keywords(self.vector_layer)
message = self.keyword_io.to_message(keywords).to_text()
self.assertIn('*Exposure*, structure------', message)
def test_layer_to_message(self):
"""Test to show augmented keywords if KeywordsIO ctor passed a layer.
.. versionadded:: 3.3
"""
keywords = KeywordIO(self.vector_layer)
message = keywords.to_message().to_text()
self.assertIn('*Reference system*, ', message)
def test_dict_to_row(self):
"""Test the dict to row helper works.
.. versionadded:: 3.2
"""
keyword_value = (
"{'high': ['Kawasan Rawan Bencana III'], "
"'medium': ['Kawasan Rawan Bencana II'], "
"'low': ['Kawasan Rawan Bencana I']}")
table = self.keyword_io._dict_to_row(keyword_value)
self.assertIn(
u'\n---\n*high*, Kawasan Rawan Bencana III------',
table.to_text())
# should also work passing a dict
keyword_value = {
'high': ['Kawasan Rawan Bencana III'],
'medium': ['Kawasan Rawan Bencana II'],
'low': ['Kawasan Rawan Bencana I']}
table = self.keyword_io._dict_to_row(keyword_value)
self.assertIn(
u'\n---\n*high*, Kawasan Rawan Bencana III------',
table.to_text())
def test_keyword_io(self):
"""Test read keywords directly from keywords file
.. versionadded:: 3.2
"""
self.maxDiff = None
keywords = self.keyword_io.read_keywords_file(self.keyword_path)
expected_keywords = self.expected_vector_keywords
self.assertDictEqual(keywords, expected_keywords)
示例6: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import to_message [as 别名]
#.........这里部分代码省略.........
uri = QgsDataSourceURI()
uri.setDatabase('exposure.sqlite')
uri.setDataSource('', 'buildings_osm_4326', 'Geometry')
sqlite_layer = QgsVectorLayer(uri.uri(), 'OSM Buildings', 'spatialite')
expected_source = (
'dbname=\'exposure.sqlite\' table="buildings_osm_4326" ('
'Geometry) sql=')
self.assertEqual(sqlite_layer.source(), expected_source)
keywords = self.keyword_io.read_keywords(sqlite_layer)
expected_keywords = self.expected_sqlite_keywords
self.assertDictEqual(keywords, expected_keywords)
# Delete SQL Layer so that we can delete the file
del sqlite_layer
os.remove(local_path)
def test_copy_keywords(self):
"""Test we can copy the keywords."""
out_path = unique_filename(
prefix='test_copy_keywords', suffix='.shp')
layer = clone_raster_layer(
name='generic_continuous_flood',
extension='.asc',
include_keywords=True,
source_directory=test_data_path('hazard'))
self.keyword_io.copy_keywords(layer, out_path)
# copied_keywords = read_file_keywords(out_path.split('.')[0] + 'xml')
copied_keywords = read_iso19115_metadata(out_path)
expected_keywords = self.expected_raster_keywords
expected_keywords['keyword_version'] = inasafe_keyword_version
self.maxDiff = None
self.assertDictEqual(copied_keywords, expected_keywords)
def test_definition(self):
"""Test we can get definitions for keywords.
.. versionadded:: 3.2
"""
keyword = 'hazards'
keyword_definition = definition(keyword)
self.assertTrue('description' in keyword_definition)
def test_to_message(self):
"""Test we can convert keywords to a message object.
.. versionadded:: 3.2
"""
keywords = self.keyword_io.read_keywords(self.vector_layer)
message = self.keyword_io.to_message(keywords).to_text()
self.assertIn('*Exposure*, structure------', message)
def test_layer_to_message(self):
"""Test to show augmented keywords if KeywordsIO ctor passed a layer.
.. versionadded:: 3.3
"""
keywords = KeywordIO(self.vector_layer)
message = keywords.to_message().to_text()
self.assertIn('*Reference system*, ', message)
def test_dict_to_row(self):
"""Test the dict to row helper works.
.. versionadded:: 3.2
"""
keyword_value = (
"{'high': ['Kawasan Rawan Bencana III'], "
"'medium': ['Kawasan Rawan Bencana II'], "
"'low': ['Kawasan Rawan Bencana I']}")
table = self.keyword_io._dict_to_row(keyword_value)
self.assertIn(
u'\n---\n*high*, Kawasan Rawan Bencana III------',
table.to_text())
# should also work passing a dict
keyword_value = {
'high': ['Kawasan Rawan Bencana III'],
'medium': ['Kawasan Rawan Bencana II'],
'low': ['Kawasan Rawan Bencana I']}
table = self.keyword_io._dict_to_row(keyword_value)
self.assertIn(
u'\n---\n*high*, Kawasan Rawan Bencana III------',
table.to_text())
def test_keyword_io(self):
"""Test read keywords directly from keywords file
.. versionadded:: 3.2
"""
keywords = self.keyword_io.read_keywords_file(self.keyword_path)
expected_keywords = self.expected_vector_keywords
message = 'Got:\n%s\nExpected:\n%s\nSource:\n%s' % (
keywords, expected_keywords, self.keyword_path)
self.assertDictEqual(keywords, expected_keywords, message)
示例7: test_regression_2553_no_resample
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import to_message [as 别名]
def test_regression_2553_no_resample(self):
"""Test for regression 2553 (no resampling).
see :
https://github.com/inasafe/inasafe/issues/2553
We want to verify that population with resampling should produce
a result within a reasonable range of the same analysis but doing
population with no resampling.
"""
hazard_path = test_data_path(
'hazard', 'continuous_flood_unaligned_big_size.tif')
exposure_path = test_data_path(
'exposure', 'people_allow_resampling_false.tif')
hazard_layer, hazard_layer_purpose = load_layer(hazard_path)
# Check if there is a regression about keywords being updated from
# another layer - see #2605
keywords = KeywordIO(hazard_layer)
self.assertIn('flood unaligned', keywords.to_message().to_text())
exposure_layer, exposure_layer_purpose = load_layer(
exposure_path)
keywords = KeywordIO(exposure_layer)
self.assertIn(
'*Allow resampling*, false------',
keywords.to_message().to_text())
QgsMapLayerRegistry.instance().addMapLayers(
[hazard_layer, exposure_layer])
# Count the total value of all exposure pixels
# this is arse about face but width is actually giving height
height = exposure_layer.width()
# this is arse about face but height is actually giving width
width = exposure_layer.height()
provider = exposure_layer.dataProvider()
# Bands count from 1!
block = provider.block(1, provider.extent(), height, width)
# Enable on-the-fly reprojection
set_canvas_crs(GEOCRS, True)
# This is the nicer way but wierdly it gets nan for every cell
total_population = 0.0
cell_count = 0
row = 0
# Iterate down each column to match the layout produced by r.stats
while row < width:
column = 0
while column < height:
cell_count += 1
value = block.value(row, column)
if value > 0:
total_population += value
column += 1
row += 1
print "Total value of all cells is: %d" % total_population
print "Number of cells counted: %d" % cell_count
# 131 computed using r.sum
self.assertAlmostEqual(total_population, 131.0177006121)
result, message = setup_scenario(
self.dock,
hazard='flood unaligned',
exposure='People never resample',
function='Need evacuation',
function_id='FloodEvacuationRasterHazardFunction')
self.assertTrue(result, message)
# Press RUN
self.dock.accept()
safe_layer = self.dock.impact_function.impact
keywords = safe_layer.get_keywords()
evacuated = float(keywords['evacuated'])
self.assertLess(evacuated, total_population)
expected_evacuated = 131.0
self.assertEqual(evacuated, expected_evacuated)