本文整理汇总了Python中safe.storage.utilities.write_keywords函数的典型用法代码示例。如果您正苦于以下问题:Python write_keywords函数的具体用法?Python write_keywords怎么用?Python write_keywords使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_keywords函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_write_keywords
def test_write_keywords(self):
"""Test writing keywords with no sublayer, no existing file."""
keywords = DKI_KEYWORDS
filename = self.make_temp_file()
write_keywords(keywords, filename)
# Read back contents and check against control dataset
control_keywords = read_keywords(SIMPLE_PATH, all_blocks=True)
actual_keywords = read_keywords(filename, all_blocks=True)
msg = 'Expected:\n%s\nGot:\n%s\n' % (control_keywords, actual_keywords)
assert control_keywords == actual_keywords, msg
示例2: test_write_keywords_singlesublayer
def test_write_keywords_singlesublayer(self):
"""Test writing keywords for single sublayer, no existing file."""
filename = self.make_temp_file()
write_keywords(OSM_KEYWORDS, filename=filename, sublayer='osm')
# read back contents and check against control dataset
control_path = test_data_path('other', 'expected_singlelayer.keywords')
control_keywords = read_keywords(control_path, all_blocks=True)
actual_keywords = read_keywords(filename, all_blocks=True)
msg = 'Expected:\n%s\nGot:\n%s\n' % (control_keywords, actual_keywords)
assert control_keywords == actual_keywords, msg
示例3: test_write_keywords_singlesublayer
def test_write_keywords_singlesublayer(self):
"""Test writing keywords for single sublayer, no existing file."""
filename = self.make_temp_file()
write_keywords(OSM_KEYWORDS, filename=filename, sublayer="osm")
# read back contents and check against control dataset
control_path = os.path.abspath(os.path.join(UNITDATA, "other", "expected_singlelayer.keywords"))
control_keywords = read_keywords(control_path, all_blocks=True)
actual_keywords = read_keywords(filename, all_blocks=True)
msg = "Expected:\n%s\nGot:\n%s\n" % (control_keywords, actual_keywords)
assert control_keywords == actual_keywords, msg
示例4: test_write_keywords_multisublayer
def test_write_keywords_multisublayer(self):
"""Test writing keywords for named sublayer, no existing file."""
filename = self.make_temp_file()
write_keywords(DKI_KEYWORDS, filename=filename, sublayer="dki")
write_keywords(OSM_KEYWORDS, filename=filename, sublayer="osm")
# Read back contents and check against control dataset
control_path = test_data_path("other", "expected_multilayer.keywords")
control_keywords = read_keywords(control_path, all_blocks=True)
actual_keywords = read_keywords(filename, all_blocks=True)
msg = "Expected:\n%s\nGot:\n%s\n" % (control_keywords, actual_keywords)
assert control_keywords == actual_keywords, msg
示例5: test_write_keywords_multisublayer
def test_write_keywords_multisublayer(self):
"""Test writing keywords for named sublayer, no existing file."""
filename = self.make_temp_file()
write_keywords(DKI_KEYWORDS, filename=filename, sublayer='dki')
write_keywords(OSM_KEYWORDS, filename=filename, sublayer='osm')
# Read back contents and check against control dataset
control_path = os.path.abspath(
os.path.join(UNITDATA, 'other', 'expected_multilayer.keywords'))
control_keywords = read_keywords(control_path, all_blocks=True)
actual_keywords = read_keywords(filename, all_blocks=True)
msg = 'Expected:\n%s\nGot:\n%s\n' % (control_keywords, actual_keywords)
assert control_keywords == actual_keywords, msg
示例6: write_to_file
def write_to_file(self, filename):
"""Save raster data to file
Input
filename: filename with extension .tif
"""
# Check file format
basename, extension = os.path.splitext(filename)
msg = ('Invalid file type for file %s. Only extension '
'tif allowed.' % filename)
verify(extension in ['.tif', '.asc'], msg)
format = DRIVER_MAP[extension]
# Get raster data
A = self.get_data()
# Get Dimensions. Note numpy and Gdal swap order
N, M = A.shape
# Create empty file.
# FIXME (Ole): It appears that this is created as single
# precision even though Float64 is specified
# - see issue #17
driver = gdal.GetDriverByName(format)
fid = driver.Create(filename, M, N, 1, gdal.GDT_Float64)
if fid is None:
msg = ('Gdal could not create filename %s using '
'format %s' % (filename, format))
raise Exception(msg)
# Write metada
fid.SetProjection(str(self.projection))
fid.SetGeoTransform(self.geotransform)
# Write data
fid.GetRasterBand(1).WriteArray(A)
# Write keywords if any
write_keywords(self.keywords, basename + '.keywords')
示例7: write_to_file
#.........这里部分代码省略.........
except:
msg = ('Input parameter "attributes" was specified '
'but it does not contain dictionaries with '
'field information as expected. The first'
'element is %s' % data[0])
raise Exception(msg)
else:
# Establish OGR types for each element
ogrtypes = {}
for name in fields:
att = data[0][name]
py_type = type(att)
msg = ('Unknown type for storing vector '
'data: %s, %s' % (name, str(py_type)[1:-1]))
verify(py_type in TYPE_MAP, msg)
ogrtypes[name] = TYPE_MAP[py_type]
else:
msg = ('Input parameter "data" was specified '
'but appears to be empty')
raise Exception(msg)
# Create attribute fields in layer
store_attributes = True
for name in fields:
fd = ogr.FieldDefn(name, ogrtypes[name])
# FIXME (Ole): Trying to address issue #16
# But it doesn't work and
# somehow changes the values of MMI in test
#width = max(128, len(name))
#print name, width
#fd.SetWidth(width)
# Silent handling of warnings like
# Warning 6: Normalized/laundered field name:
#'CONTENTS_LOSS_AUD' to 'CONTENTS_L'
gdal.PushErrorHandler('CPLQuietErrorHandler')
if lyr.CreateField(fd) != 0:
msg = 'Could not create field %s' % name
raise Exception(msg)
# Restore error handler
gdal.PopErrorHandler()
# Store geometry
geom = ogr.Geometry(self.geometry_type)
layer_def = lyr.GetLayerDefn()
for i in range(N):
# Create new feature instance
feature = ogr.Feature(layer_def)
# Store geometry and check
if self.geometry_type == ogr.wkbPoint:
x = float(geometry[i][0])
y = float(geometry[i][1])
geom.SetPoint_2D(0, x, y)
elif self.geometry_type == ogr.wkbPolygon:
wkt = array2wkt(geometry[i], geom_type='POLYGON')
geom = ogr.CreateGeometryFromWkt(wkt)
elif self.geometry_type == ogr.wkbLineString:
wkt = array2wkt(geometry[i], geom_type='LINESTRING')
geom = ogr.CreateGeometryFromWkt(wkt)
else:
msg = 'Geometry type %s not implemented' % self.geometry_type
raise Exception(msg)
feature.SetGeometry(geom)
G = feature.GetGeometryRef()
if G is None:
msg = 'Could not create GeometryRef for file %s' % filename
raise Exception(msg)
# Store attributes
if store_attributes:
for j, name in enumerate(fields):
actual_field_name = layer_def.GetFieldDefn(j).GetNameRef()
val = data[i][name]
if type(val) == numpy.ndarray:
# A singleton of type <type 'numpy.ndarray'> works
# for gdal version 1.6 but fails for version 1.8
# in SetField with error: NotImplementedError:
# Wrong number of arguments for overloaded function
val = float(val)
elif val is None:
val = ''
feature.SetField(actual_field_name, val)
# Save this feature
if lyr.CreateFeature(feature) != 0:
msg = 'Failed to create feature %i in file %s' % (i, filename)
raise Exception(msg)
feature.Destroy()
# Write keywords if any
write_keywords(self.keywords, basename + '.keywords')