本文整理汇总了Python中qgis.core.QgsRasterBlock类的典型用法代码示例。如果您正苦于以下问题:Python QgsRasterBlock类的具体用法?Python QgsRasterBlock怎么用?Python QgsRasterBlock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QgsRasterBlock类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: processAlgorithm
def processAlgorithm(self, parameters, context, feedback):
crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
extent = self.parameterAsExtent(parameters, self.EXTENT, context, crs)
value = self.parameterAsDouble(parameters, self.NUMBER, context)
pixelSize = self.parameterAsDouble(parameters, self.PIXEL_SIZE, context)
outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
outputFormat = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])
rows = max([math.ceil(extent.height() / pixelSize) + 1, 1.0])
cols = max([math.ceil(extent.width() / pixelSize) + 1, 1.0])
writer = QgsRasterFileWriter(outputFile)
writer.setOutputProviderKey('gdal')
writer.setOutputFormat(outputFormat)
provider = writer.createOneBandRaster(Qgis.Float32, cols, rows, extent, crs)
provider.setNoDataValue(1, -9999)
data = [value] * cols
block = QgsRasterBlock(Qgis.Float32, cols, 1)
block.setData(struct.pack('{}f'.format(len(data)), *data))
total = 100.0 / rows if rows else 0
for i in range(rows):
if feedback.isCanceled():
break
provider.writeBlock(block, 1, 0, i)
feedback.setProgress(int(i * rows))
provider.setEditable(False)
return {self.OUTPUT: outputFile}
示例2: processAlgorithm
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}