本文整理汇总了Python中safe.utilities.keyword_io.KeywordIO._dict_to_row方法的典型用法代码示例。如果您正苦于以下问题:Python KeywordIO._dict_to_row方法的具体用法?Python KeywordIO._dict_to_row怎么用?Python KeywordIO._dict_to_row使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe.utilities.keyword_io.KeywordIO
的用法示例。
在下文中一共展示了KeywordIO._dict_to_row方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resolve_dict_keywords
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import _dict_to_row [as 别名]
def resolve_dict_keywords(keywords):
"""Replace dictionary content with html.
:param keywords: The keywords.
:type keywords: dict
:return: New keywords with updated content.
:rtype: dict
"""
for keyword in ['value_map', 'inasafe_fields', 'inasafe_default_values']:
value = keywords.get(keyword)
if value:
value = value.get('content')
value = KeywordIO._dict_to_row(value).to_html()
keywords[keyword]['content'] = value
value_maps = keywords.get('value_maps')
thresholds = keywords.get('thresholds')
if value_maps:
value_maps = value_maps.get('content')
value_maps = KeywordIO._value_maps_row(value_maps).to_html()
keywords['value_maps']['content'] = value_maps
if thresholds:
thresholds = thresholds.get('content')
thresholds = KeywordIO._threshold_to_row(thresholds).to_html()
keywords['thresholds']['content'] = thresholds
return keywords
示例2: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import _dict_to_row [as 别名]
#.........这里部分代码省略.........
'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()
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())
示例3: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import _dict_to_row [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())
示例4: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import _dict_to_row [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)
示例5: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import _dict_to_row [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)