本文整理汇总了Python中safe.utilities.keyword_io.KeywordIO.are_keywords_file_based方法的典型用法代码示例。如果您正苦于以下问题:Python KeywordIO.are_keywords_file_based方法的具体用法?Python KeywordIO.are_keywords_file_based怎么用?Python KeywordIO.are_keywords_file_based使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe.utilities.keyword_io.KeywordIO
的用法示例。
在下文中一共展示了KeywordIO.are_keywords_file_based方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import are_keywords_file_based [as 别名]
#.........这里部分代码省略.........
# SQL insert test
# On first write schema is empty and there is no matching hash
self.keyword_io.set_keyword_db_path(filename)
self.keyword_io.write_keywords_for_uri(PG_URI, expected_keywords)
# SQL Update test
# On second write schema is populated and we update matching hash
expected_keywords = {
'category': 'exposure',
'datatype': 'OSM', # <--note the change here!
'subcategory': 'building'}
self.keyword_io.write_keywords_for_uri(PG_URI, expected_keywords)
# Test getting all keywords
keywords = self.keyword_io.read_keyword_from_uri(PG_URI)
message = 'Got: %s\n\nExpected %s\n\nDB: %s' % (
keywords, expected_keywords, filename)
self.assertDictEqual(keywords, expected_keywords, message)
# Test getting just a single keyword
keyword = self.keyword_io.read_keyword_from_uri(PG_URI, 'datatype')
expected_keyword = 'OSM'
message = 'Got: %s\n\nExpected %s\n\nDB: %s' % (
keyword, expected_keyword, filename)
self.assertDictEqual(keywords, expected_keywords, message)
# Test deleting keywords actually does delete
self.keyword_io.delete_keywords_for_uri(PG_URI)
try:
_ = self.keyword_io.read_keyword_from_uri(PG_URI, 'datatype')
# if the above didn't cause an exception then bad
message = 'Expected a HashNotFoundError to be raised'
assert message
except HashNotFoundError:
# we expect this outcome so good!
pass
def test_are_keywords_file_based(self):
"""Can we correctly determine if keywords should be written to file or
to database?"""
assert not self.keyword_io.are_keywords_file_based(self.sqlite_layer)
assert self.keyword_io.are_keywords_file_based(self.raster_layer)
assert self.keyword_io.are_keywords_file_based(self.vector_layer)
def test_read_raster_file_keywords(self):
"""Can we read raster file keywords using generic readKeywords method
"""
keywords = self.keyword_io.read_keywords(self.raster_layer)
expected_keywords = self.expected_raster_keywords
source = self.raster_layer.source()
message = 'Got:\n%s\nExpected:\n%s\nSource:\n%s' % (
keywords, expected_keywords, source)
self.assertDictEqual(keywords, expected_keywords, message)
def test_read_vector_file_keywords(self):
"""Test read vector file keywords with the generic readKeywords method.
"""
keywords = self.keyword_io.read_keywords(self.vector_layer)
expected_keywords = self.expected_vector_keywords
source = self.vector_layer.source()
message = 'Got: %s\n\nExpected %s\n\nSource: %s' % (
keywords, expected_keywords, source)
self.assertDictEqual(keywords, expected_keywords, message)
def test_read_keywordless_layer(self):
"""Test read 'keyword' file from keywordless layer.
"""
self.assertRaises(
NoKeywordsFoundError,
self.keyword_io.read_keywords,
示例2: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import are_keywords_file_based [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 = test_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 = test_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.2'
}
# Vector Layer keywords
vector_path = test_data_path('exposure', 'buildings_osm_4326.shp')
self.vector_layer, _ = load_layer(vector_path)
self.expected_vector_keywords = {
'keyword_version': '3.3',
'structure_class_field': 'FLOODED',
'title': 'buildings_osm_4326',
'layer_geometry': 'polygon',
'layer_purpose': 'exposure',
'layer_mode': 'classified',
'exposure': 'structure'
}
# Keyword less layer
keywordless_path = test_data_path('other', 'keywordless_layer.shp')
self.keywordless_layer, _ = load_layer(keywordless_path)
# Keyword file
self.keyword_path = test_data_path(
'exposure', 'buildings_osm_4326.xml')
def tearDown(self):
pass
def test_get_hash_for_datasource(self):
"""Test we can reliably get a hash for a uri"""
hash_value = self.keyword_io.hash_for_datasource(PG_URI)
expected_hash = '7cc153e1b119ca54a91ddb98a56ea95e'
message = "Got: %s\nExpected: %s" % (hash_value, expected_hash)
self.assertEqual(hash_value, expected_hash, message)
def test_are_keywords_file_based(self):
"""Can we correctly determine if keywords should be written to file or
to database?"""
assert not self.keyword_io.are_keywords_file_based(self.sqlite_layer)
assert self.keyword_io.are_keywords_file_based(self.raster_layer)
assert self.keyword_io.are_keywords_file_based(self.vector_layer)
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=test_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.
"""
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_update_keywords(self):
"""Test append file keywords with update_keywords method."""
#.........这里部分代码省略.........