本文整理汇总了Python中qgis.core.QgsRasterBlock.setValue方法的典型用法代码示例。如果您正苦于以下问题:Python QgsRasterBlock.setValue方法的具体用法?Python QgsRasterBlock.setValue怎么用?Python QgsRasterBlock.setValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsRasterBlock
的用法示例。
在下文中一共展示了QgsRasterBlock.setValue方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: processAlgorithm
# 需要导入模块: from qgis.core import QgsRasterBlock [as 别名]
# 或者: from qgis.core.QgsRasterBlock import setValue [as 别名]
def processAlgorithm(self, parameters, context, feedback):
inp_rast = self.parameterAsRasterLayer(parameters, self.INPUT, context)
grib_filename = self.parameterAsString(parameters, self.OUTPUT, context)
if not grib_filename:
raise QgsProcessingException(self.tr('You need to specify output filename.'))
idp = inp_rast.dataProvider()
map_settings = iface.mapCanvas().mapSettings()
crs = map_settings.destinationCrs()
outputFormat = QgsRasterFileWriter.driverForExtension('.tif')
height = inp_rast.height()
width = inp_rast.width()
inp_block = idp.block(1, idp.extent(), width, height)
rfw = QgsRasterFileWriter(grib_filename + '.tif')
rfw.setOutputProviderKey('gdal')
rfw.setOutputFormat(outputFormat)
rdp = rfw.createMultiBandRaster(
Qgis.Float32,
width,
height,
idp.extent(),
crs,
2
)
rdp.setEditable(True)
x_block = QgsRasterBlock(Qgis.Float32, width, height)
y_block = QgsRasterBlock(Qgis.Float32, width, height)
diag = 1. / sqrt(2)
# resulting raster has no NODATA value set, which
# is not treated correctly in MDAL 0.2.0. See
# see https://github.com/lutraconsulting/MDAL/issues/104
# therefore set some small value to overcome the issue
dir_map = {
0: (1e-7, 1),
1: (diag, diag),
2: (1, 1e-7),
3: (diag, -diag),
4: (1e-7, -1),
5: (-diag, -diag),
6: (-1, 1e-7),
7: (-diag, diag),
255: (0, 0)
}
for row in range(height):
for col in range(width):
dir_ind = inp_block.value(row, col)
try:
x_val, y_val = dir_map.get(dir_ind, 255)
except TypeError:
x_val, y_val = (0, 0)
x_block.setValue(row, col, x_val)
y_block.setValue(row, col, y_val)
rdp.writeBlock(x_block, 1)
rdp.writeBlock(y_block, 2)
rdp.setNoDataValue(1, inp_block.noDataValue())
rdp.setNoDataValue(2, inp_block.noDataValue())
rdp.setEditable(False)
# rewrite the resulting raster as GRIB using GDAL for setting metadata
res_tif = gdal.Open(grib_filename + '.tif')
grib_driver = gdal.GetDriverByName('GRIB')
grib = grib_driver.CreateCopy(grib_filename, res_tif)
band_names = ('x-flow', 'y-flow')
for i in range(2):
band_nr = i + 1
band_name = band_names[i]
grib_band = grib.GetRasterBand(band_nr)
grib_band.SetMetadataItem('grib_comment', band_name)
grib_band.SetNoDataValue(255)
grib_band.SetDescription(band_name)
res_tif_band_array = res_tif.GetRasterBand(band_nr).ReadAsArray()
grib_band.WriteArray(res_tif_band_array)
feedback.setProgress(band_nr * 50)
grib = None
return {self.OUTPUT: grib_filename}