本文整理汇总了Python中safe_qgis.utilities.keyword_io.KeywordIO.write_keywords_for_uri方法的典型用法代码示例。如果您正苦于以下问题:Python KeywordIO.write_keywords_for_uri方法的具体用法?Python KeywordIO.write_keywords_for_uri怎么用?Python KeywordIO.write_keywords_for_uri使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe_qgis.utilities.keyword_io.KeywordIO
的用法示例。
在下文中一共展示了KeywordIO.write_keywords_for_uri方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: KeywordIOTest
# 需要导入模块: from safe_qgis.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe_qgis.utilities.keyword_io.KeywordIO import write_keywords_for_uri [as 别名]
class KeywordIOTest(unittest.TestCase):
"""Tests for reading and writing of raster and vector data
"""
def setUp(self):
self.keyword_io = KeywordIO()
uri = QgsDataSourceURI()
uri.setDatabase(os.path.join(TESTDATA, 'jk.sqlite'))
uri.setDataSource('', 'osm_buildings', 'Geometry')
self.sqlite_layer = QgsVectorLayer(
uri.uri(), 'OSM Buildings', 'spatialite')
hazard_path = os.path.join(HAZDATA, 'Shakemap_Padang_2009.asc')
self.raster_layer, layer_type = load_layer(
hazard_path, directory=None)
del layer_type
self.vector_layer, layer_type = load_layer('Padang_WGS84.shp')
del layer_type
self.expected_sqlite_keywords = {
'category': 'exposure',
'datatype': 'OSM',
'subcategory': 'building'}
self.expected_vector_keywords = {
'category': 'exposure',
'datatype': 'itb',
'subcategory': 'structure',
'title': 'Padang WGS84'}
self.expected_raster_keywords = {
'category': 'hazard',
'source': 'USGS',
'subcategory': 'earthquake',
'unit': 'MMI',
'title': ('An earthquake in Padang '
'like in 2009')}
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)
assert 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)
assert 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)
assert keyword == expected_keyword, 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 didnt 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)
#.........这里部分代码省略.........
示例2: KeywordIOTest
# 需要导入模块: from safe_qgis.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe_qgis.utilities.keyword_io.KeywordIO import write_keywords_for_uri [as 别名]
class KeywordIOTest(unittest.TestCase):
"""Tests for reading and writing of raster and vector data
"""
def setUp(self):
self.keywordIO = KeywordIO()
myUri = QgsDataSourceURI()
myUri.setDatabase(os.path.join(TESTDATA, 'jk.sqlite'))
myUri.setDataSource('', 'osm_buildings', 'Geometry')
self.sqliteLayer = QgsVectorLayer(myUri.uri(), 'OSM Buildings',
'spatialite')
myHazardPath = os.path.join(HAZDATA, 'Shakemap_Padang_2009.asc')
self.fileRasterLayer, myType = load_layer(
myHazardPath, directory=None)
del myType
self.fileVectorLayer, myType = load_layer('Padang_WGS84.shp')
del myType
self.expectedSqliteKeywords = {
'category': 'exposure',
'datatype': 'OSM',
'subcategory': 'building'}
self.expectedVectorKeywords = {
'category': 'exposure',
'datatype': 'itb',
'subcategory': 'structure',
'title': 'Padang WGS84'}
self.expectedRasterKeywords = {
'category': 'hazard',
'source': 'USGS',
'subcategory': 'earthquake',
'unit': 'MMI',
'title': ('An earthquake in Padang '
'like in 2009')}
def tearDown(self):
pass
def test_getHashForDatasource(self):
"""Test we can reliably get a hash for a uri"""
myHash = self.keywordIO.hash_for_datasource(PG_URI)
myExpectedHash = '7cc153e1b119ca54a91ddb98a56ea95e'
myMessage = "Got: %s\nExpected: %s" % (myHash, myExpectedHash)
assert myHash == myExpectedHash, myMessage
def test_writeReadKeywordFromUri(self):
"""Test we can set and get keywords for a non local datasource"""
myHandle, myFilename = 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(myHandle)
os.remove(myFilename)
myExpectedKeywords = {'category': 'exposure',
'datatype': 'itb',
'subcategory': 'building'}
# SQL insert test
# On first write schema is empty and there is no matching hash
self.keywordIO.set_keyword_db_path(myFilename)
self.keywordIO.write_keywords_for_uri(PG_URI, myExpectedKeywords)
# SQL Update test
# On second write schema is populated and we update matching hash
myExpectedKeywords = {'category': 'exposure',
'datatype': 'OSM', # <--note the change here!
'subcategory': 'building'}
self.keywordIO.write_keywords_for_uri(PG_URI, myExpectedKeywords)
# Test getting all keywords
myKeywords = self.keywordIO.readKeywordFromUri(PG_URI)
myMessage = 'Got: %s\n\nExpected %s\n\nDB: %s' % (
myKeywords, myExpectedKeywords, myFilename)
assert myKeywords == myExpectedKeywords, myMessage
# Test getting just a single keyword
myKeyword = self.keywordIO.readKeywordFromUri(PG_URI, 'datatype')
myExpectedKeyword = 'OSM'
myMessage = 'Got: %s\n\nExpected %s\n\nDB: %s' % (
myKeyword, myExpectedKeyword, myFilename)
assert myKeyword == myExpectedKeyword, myMessage
# Test deleting keywords actually does delete
self.keywordIO.delete_keywords_for_uri(PG_URI)
try:
myKeyword = self.keywordIO.readKeywordFromUri(PG_URI, 'datatype')
#if the above didnt cause an exception then bad
myMessage = 'Expected a HashNotFoundError to be raised'
assert myMessage
except HashNotFoundError:
#we expect this outcome so good!
pass
def test_areKeywordsFileBased(self):
"""Can we correctly determine if keywords should be written to file or
to database?"""
assert not self.keywordIO.are_keywords_file_based(self.sqliteLayer)
assert self.keywordIO.are_keywords_file_based(self.fileRasterLayer)
assert self.keywordIO.are_keywords_file_based(self.fileVectorLayer)
def test_readRasterFileKeywords(self):
"""Can we read raster file keywords using generic readKeywords method
#.........这里部分代码省略.........