本文整理汇总了Python中safe.utilities.keyword_io.KeywordIO.set_keyword_db_path方法的典型用法代码示例。如果您正苦于以下问题:Python KeywordIO.set_keyword_db_path方法的具体用法?Python KeywordIO.set_keyword_db_path怎么用?Python KeywordIO.set_keyword_db_path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe.utilities.keyword_io.KeywordIO
的用法示例。
在下文中一共展示了KeywordIO.set_keyword_db_path方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import set_keyword_db_path [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 = {
'category': 'exposure',
'datatype': 'OSM',
'subcategory': 'building'}
# 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': 'Tsunami',
'hazard': 'tsunami',
'continuous_hazard_unit': 'metres',
'layer_geometry': 'raster',
'layer_purpose': 'hazard',
'layer_mode': 'continuous',
'keyword_version': inasafe_keyword_version
}
# 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': inasafe_keyword_version,
'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)
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_write_read_keyword_from_uri(self):
"""Test we can set and get keywords for a non local datasource"""
handle, filename = tempfile.mkstemp(
'.db', 'keywords_', temp_dir())
# Ensure the file is deleted before we try to write to it
# fixes windows specific issue where you get a message like this
# ERROR 1: c:\temp\inasafe\clip_jpxjnt.shp is not a directory.
# This is because mkstemp creates the file handle and leaves
# the file open.
os.close(handle)
os.remove(filename)
expected_keywords = {
'category': 'exposure',
'datatype': 'itb',
'subcategory': 'building'}
# 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)
#.........这里部分代码省略.........
示例2: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import set_keyword_db_path [as 别名]
#.........这里部分代码省略.........
include_keywords=True,
source_directory=test_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.maxDiff = None
self.assertDictEqual(keywords, expected_keywords)
@unittest.skip('No longer used in the new metadata.')
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=')
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',