本文整理汇总了Python中safe.utilities.keyword_io.KeywordIO.hash_for_datasource方法的典型用法代码示例。如果您正苦于以下问题:Python KeywordIO.hash_for_datasource方法的具体用法?Python KeywordIO.hash_for_datasource怎么用?Python KeywordIO.hash_for_datasource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe.utilities.keyword_io.KeywordIO
的用法示例。
在下文中一共展示了KeywordIO.hash_for_datasource方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: KeywordIOTest
# 需要导入模块: from safe.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe.utilities.keyword_io.KeywordIO import hash_for_datasource [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 hash_for_datasource [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."""
#.........这里部分代码省略.........