Skip to main content
Adding more information
Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

Editing Note:

Afterward, I see condition for resulting raster. In this case, code is as follows:

l=iface.activeLayer()
a=l.extent()
b=l.width()
c=l.height()
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
rast = QgsRasterCalculatorEntry()
rast.ref = l.name() +'@1'
rast.raster = l
rast.bandNumber = 1
entries=[ rast ]

expression = '( (' + entries[0].ref + ' ) = 1 ) * 200 + ' + '( (' + entries[0].ref + ' ) != 1 ) *  ' + entries[0].ref
print expression

calc = QgsRasterCalculator( expression,
                            "/home/zeito/pyqgis_data/z.tif",
                            'GTiff',
                            a,
                            b,
                            c,
                            entries )

calc.processCalculation()

After running it, results were obtained as expected.

Editing Note:

Afterward, I see condition for resulting raster. In this case, code is as follows:

l=iface.activeLayer()
a=l.extent()
b=l.width()
c=l.height()
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
rast = QgsRasterCalculatorEntry()
rast.ref = l.name() +'@1'
rast.raster = l
rast.bandNumber = 1
entries=[ rast ]

expression = '( (' + entries[0].ref + ' ) = 1 ) * 200 + ' + '( (' + entries[0].ref + ' ) != 1 ) *  ' + entries[0].ref
print expression

calc = QgsRasterCalculator( expression,
                            "/home/zeito/pyqgis_data/z.tif",
                            'GTiff',
                            a,
                            b,
                            c,
                            entries )

calc.processCalculation()

After running it, results were obtained as expected.

deleted 1 character in body
Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

I got my system dump with your code. Issue is because it's necessary a good reference for your layer by using QgsRasterCalculatorEntry class methods (you cannot use athat string for 'expression'). So, with a copy/path of your next modified code at Python Console of QGIS:

l=iface.activeLayer()
a=l.extent()
b=l.width()
c=l.height()
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
rast = QgsRasterCalculatorEntry()
rast.ref = l.name() +'@1'
rast.raster = l
rast.bandNumber = 1
entries=[ rast ]

expression = '( ' + entries[0].ref + ' ) * 200 '
print expression

calc = QgsRasterCalculator( expression,
                            "/home/zeito/pyqgis_data/z.tif",
                            'GTiff',
                            a,
                            b,
                            c,
                            entries )

calc.processCalculation()  

I got 0 execution code, pointed out that it was ran without problems. I tried it out with raster of next image (with only 3 values to make corroboration easier).

enter image description here

By using Value Tool plugin, it was corroborated that values were obtained as expected in resulting raster.

I got my system dump with your code. Issue is because it's necessary a good reference for your layer by using QgsRasterCalculatorEntry class methods (you cannot use a string for 'expression'). So, with a copy/path of your next modified code at Python Console of QGIS:

l=iface.activeLayer()
a=l.extent()
b=l.width()
c=l.height()
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
rast = QgsRasterCalculatorEntry()
rast.ref = l.name() +'@1'
rast.raster = l
rast.bandNumber = 1
entries=[ rast ]

expression = '( ' + entries[0].ref + ' ) * 200 '
print expression

calc = QgsRasterCalculator( expression,
                            "/home/zeito/pyqgis_data/z.tif",
                            'GTiff',
                            a,
                            b,
                            c,
                            entries )

calc.processCalculation()  

I got 0 execution code, pointed out that it was ran without problems. I tried it out with raster of next image (with only 3 values to make corroboration easier).

enter image description here

By using Value Tool plugin, it was corroborated that values were obtained as expected in resulting raster.

I got my system dump with your code. Issue is because it's necessary a good reference for your layer by using QgsRasterCalculatorEntry class methods (you cannot use that string for 'expression'). So, with a copy/path of your next modified code at Python Console of QGIS:

l=iface.activeLayer()
a=l.extent()
b=l.width()
c=l.height()
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
rast = QgsRasterCalculatorEntry()
rast.ref = l.name() +'@1'
rast.raster = l
rast.bandNumber = 1
entries=[ rast ]

expression = '( ' + entries[0].ref + ' ) * 200 '
print expression

calc = QgsRasterCalculator( expression,
                            "/home/zeito/pyqgis_data/z.tif",
                            'GTiff',
                            a,
                            b,
                            c,
                            entries )

calc.processCalculation()  

I got 0 execution code, pointed out that it ran without problems. I tried it out with raster of next image (with only 3 values to make corroboration easier).

enter image description here

By using Value Tool plugin, it was corroborated that values were obtained as expected in resulting raster.

Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

I got my system dump with your code. Issue is because it's necessary a good reference for your layer by using QgsRasterCalculatorEntry class methods (you cannot use a string for 'expression'). So, with a copy/path of your next modified code at Python Console of QGIS:

l=iface.activeLayer()
a=l.extent()
b=l.width()
c=l.height()
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
rast = QgsRasterCalculatorEntry()
rast.ref = l.name() +'@1'
rast.raster = l
rast.bandNumber = 1
entries=[ rast ]

expression = '( ' + entries[0].ref + ' ) * 200 '
print expression

calc = QgsRasterCalculator( expression,
                            "/home/zeito/pyqgis_data/z.tif",
                            'GTiff',
                            a,
                            b,
                            c,
                            entries )

calc.processCalculation()  

I got 0 execution code, pointed out that it was ran without problems. I tried it out with raster of next image (with only 3 values to make corroboration easier).

enter image description here

By using Value Tool plugin, it was corroborated that values were obtained as expected in resulting raster.