本文整理汇总了Python中safe.utilities.keyword_io.KeywordIO.read_keywords_file方法的典型用法代码示例。如果您正苦于以下问题:Python KeywordIO.read_keywords_file方法的具体用法?Python KeywordIO.read_keywords_file怎么用?Python KeywordIO.read_keywords_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe.utilities.keyword_io.KeywordIO
的用法示例。
在下文中一共展示了KeywordIO.read_keywords_file方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_keywords_iso_metadata
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import read_keywords_file [as 别名]
def read_keywords_iso_metadata(metadata_url, keyword=None):
"""Read xml metadata of a layer"""
filename = download_file(metadata_url)
# add xml extension
new_filename = filename + '.xml'
shutil.move(filename, new_filename)
keyword_io = KeywordIO()
keywords = keyword_io.read_keywords_file(new_filename)
if keyword:
return keywords.get(keyword, None)
return keywords
示例2: read_keywords_iso_metadata
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import read_keywords_file [as 别名]
def read_keywords_iso_metadata(metadata_url, keyword=None):
"""Read xml metadata of a layer
:param keyword: Can be string or tuple containing keywords to search for
:type keyword: str, (str, )
:return: the keywords, or a dictionary with key-value pair
"""
filename = download_file(metadata_url)
# add xml extension
new_filename = filename + '.xml'
shutil.move(filename, new_filename)
keyword_io = KeywordIO()
keywords = keyword_io.read_keywords_file(new_filename)
if keyword:
if isinstance(keyword, tuple) or isinstance(keyword, list):
ret_val = {}
for key in keyword:
ret_val[key] = keywords.get(key, None)
return ret_val
else:
return keywords.get(keyword, None)
return keywords
示例3: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import read_keywords_file [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)
示例4: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import read_keywords_file [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)