Raster Calculation in Qgis using Python ScriptClipper in QGIS with Python ScriptNumber inputs as raster calculator variables in QGIS ModelerPython script for raster calculation using GDALQGIS Script for Raster EquationRaster calculation qgis >=Does not accept in raster calculator variable s the number QGISError in raster Analysis in QGISRaster Calculation Using GDALraster calculation using rasterioImproving QGIS raster calculation performance?SAGA Raster Calculator in QGIS Algorithm script produces 'NoneType' object has no attribute 'crs'QGIS Processing Script Stops Working After First Run
How could indestructible materials be used in power generation?
Python: return float 1.0 as int 1 but float 1.5 as float 1.5
How to draw the figure with four pentagons?
Cronab fails because shell path not found
Watching something be written to a file live with tail
Where does SFDX store details about scratch orgs?
Why do I get two different answers for this counting problem?
intersection of two sorted vectors in C++
Etiquette around loan refinance - decision is going to cost first broker a lot of money
Has there ever been an airliner design involving reducing generator load by installing solar panels?
Can I make "comment-region" comment empty lines?
Is the Joker left-handed?
Should I tell management that I intend to leave due to bad software development practices?
Brothers & sisters
90's TV series where a boy goes to another dimension through portal near power lines
Were any external disk drives stacked vertically?
What does it mean to describe someone as a butt steak?
Took a trip to a parallel universe, need help deciphering
Why does Kotter return in Welcome Back Kotter
Why doesn't H₄O²⁺ exist?
Why is the ratio of two extensive quantities always intensive?
Can a virus destroy the BIOS of a modern computer?
Plain language with long required phrases
Why does Arabsat 6A need a Falcon Heavy to launch
Raster Calculation in Qgis using Python Script
Clipper in QGIS with Python ScriptNumber inputs as raster calculator variables in QGIS ModelerPython script for raster calculation using GDALQGIS Script for Raster EquationRaster calculation qgis >=Does not accept in raster calculator variable s the number QGISError in raster Analysis in QGISRaster Calculation Using GDALraster calculation using rasterioImproving QGIS raster calculation performance?SAGA Raster Calculator in QGIS Algorithm script produces 'NoneType' object has no attribute 'crs'QGIS Processing Script Stops Working After First Run
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to do raster calculation over raster layer for which i am using python script as:
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
bohLayer = qgis.utils.iface.activeLayer()
entries = []
# Define band1
boh1 = QgsRasterCalculatorEntry()
boh1.ref = 'boh@1'
boh1.raster = bohLayer
boh1.bandNumber = 1
entries.append( boh1 )
# Process calculation with input extent and resolution
calc = QgsRasterCalculator( 'boh@1/10000',
'E:/data/abc.tif',
'GTiff',
bohLayer.extent(),
bohLayer.width(),
bohLayer.height(),
entries )
calc.processCalculation()
Sometimes this script run successfully and gives output but mostly it give error as
AttributeError: 'NoneType' object has no attribute 'extent'
Is there any other way to do raster calculation over raster layer with python script.
pyqgis raster-calculator
|
show 1 more comment
I want to do raster calculation over raster layer for which i am using python script as:
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
bohLayer = qgis.utils.iface.activeLayer()
entries = []
# Define band1
boh1 = QgsRasterCalculatorEntry()
boh1.ref = 'boh@1'
boh1.raster = bohLayer
boh1.bandNumber = 1
entries.append( boh1 )
# Process calculation with input extent and resolution
calc = QgsRasterCalculator( 'boh@1/10000',
'E:/data/abc.tif',
'GTiff',
bohLayer.extent(),
bohLayer.width(),
bohLayer.height(),
entries )
calc.processCalculation()
Sometimes this script run successfully and gives output but mostly it give error as
AttributeError: 'NoneType' object has no attribute 'extent'
Is there any other way to do raster calculation over raster layer with python script.
pyqgis raster-calculator
You could call the GDAL or SAGA raster calculator from the Processing plugin such as described in this post: Number inputs as raster calculator variables in QGIS Modeler
– Joseph
Nov 23 '16 at 12:38
Note that you're initializingbohLayerwith the QGIS Layer Tree's current layer (aka. active layer or selected layer). I guess the script fails when you have not selected yourbohLayerin the Layer Tree. Let me know if that's right to tell you how to avoid relying on a selected layer.
– Germán Carrillo
Nov 23 '16 at 13:11
@German Carrillo : yeah while executing script from c# program it gives an error but while i load layer in QGIS and then execute script it run. It is not finding the current layer . and if i pass the path to the input layer for BohLayer then it do not find its extent . Is any other way to do so by script ?
– User18
Nov 30 '16 at 6:36
@Joseph : With GDAL i am using the following code : gdal_calc.py -A abc.tif --outfile=result.tif --calc="(A)/10000" but it is not working .it shows an error ImportError : numpy.core.multiarray failed to import
– User18
Nov 30 '16 at 6:40
@User18 - What QGIS version and Processing version are you using?
– Joseph
Nov 30 '16 at 10:37
|
show 1 more comment
I want to do raster calculation over raster layer for which i am using python script as:
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
bohLayer = qgis.utils.iface.activeLayer()
entries = []
# Define band1
boh1 = QgsRasterCalculatorEntry()
boh1.ref = 'boh@1'
boh1.raster = bohLayer
boh1.bandNumber = 1
entries.append( boh1 )
# Process calculation with input extent and resolution
calc = QgsRasterCalculator( 'boh@1/10000',
'E:/data/abc.tif',
'GTiff',
bohLayer.extent(),
bohLayer.width(),
bohLayer.height(),
entries )
calc.processCalculation()
Sometimes this script run successfully and gives output but mostly it give error as
AttributeError: 'NoneType' object has no attribute 'extent'
Is there any other way to do raster calculation over raster layer with python script.
pyqgis raster-calculator
I want to do raster calculation over raster layer for which i am using python script as:
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
bohLayer = qgis.utils.iface.activeLayer()
entries = []
# Define band1
boh1 = QgsRasterCalculatorEntry()
boh1.ref = 'boh@1'
boh1.raster = bohLayer
boh1.bandNumber = 1
entries.append( boh1 )
# Process calculation with input extent and resolution
calc = QgsRasterCalculator( 'boh@1/10000',
'E:/data/abc.tif',
'GTiff',
bohLayer.extent(),
bohLayer.width(),
bohLayer.height(),
entries )
calc.processCalculation()
Sometimes this script run successfully and gives output but mostly it give error as
AttributeError: 'NoneType' object has no attribute 'extent'
Is there any other way to do raster calculation over raster layer with python script.
pyqgis raster-calculator
pyqgis raster-calculator
edited Dec 8 '16 at 0:19
mgri
13k42959
13k42959
asked Nov 23 '16 at 10:09
User18User18
18912
18912
You could call the GDAL or SAGA raster calculator from the Processing plugin such as described in this post: Number inputs as raster calculator variables in QGIS Modeler
– Joseph
Nov 23 '16 at 12:38
Note that you're initializingbohLayerwith the QGIS Layer Tree's current layer (aka. active layer or selected layer). I guess the script fails when you have not selected yourbohLayerin the Layer Tree. Let me know if that's right to tell you how to avoid relying on a selected layer.
– Germán Carrillo
Nov 23 '16 at 13:11
@German Carrillo : yeah while executing script from c# program it gives an error but while i load layer in QGIS and then execute script it run. It is not finding the current layer . and if i pass the path to the input layer for BohLayer then it do not find its extent . Is any other way to do so by script ?
– User18
Nov 30 '16 at 6:36
@Joseph : With GDAL i am using the following code : gdal_calc.py -A abc.tif --outfile=result.tif --calc="(A)/10000" but it is not working .it shows an error ImportError : numpy.core.multiarray failed to import
– User18
Nov 30 '16 at 6:40
@User18 - What QGIS version and Processing version are you using?
– Joseph
Nov 30 '16 at 10:37
|
show 1 more comment
You could call the GDAL or SAGA raster calculator from the Processing plugin such as described in this post: Number inputs as raster calculator variables in QGIS Modeler
– Joseph
Nov 23 '16 at 12:38
Note that you're initializingbohLayerwith the QGIS Layer Tree's current layer (aka. active layer or selected layer). I guess the script fails when you have not selected yourbohLayerin the Layer Tree. Let me know if that's right to tell you how to avoid relying on a selected layer.
– Germán Carrillo
Nov 23 '16 at 13:11
@German Carrillo : yeah while executing script from c# program it gives an error but while i load layer in QGIS and then execute script it run. It is not finding the current layer . and if i pass the path to the input layer for BohLayer then it do not find its extent . Is any other way to do so by script ?
– User18
Nov 30 '16 at 6:36
@Joseph : With GDAL i am using the following code : gdal_calc.py -A abc.tif --outfile=result.tif --calc="(A)/10000" but it is not working .it shows an error ImportError : numpy.core.multiarray failed to import
– User18
Nov 30 '16 at 6:40
@User18 - What QGIS version and Processing version are you using?
– Joseph
Nov 30 '16 at 10:37
You could call the GDAL or SAGA raster calculator from the Processing plugin such as described in this post: Number inputs as raster calculator variables in QGIS Modeler
– Joseph
Nov 23 '16 at 12:38
You could call the GDAL or SAGA raster calculator from the Processing plugin such as described in this post: Number inputs as raster calculator variables in QGIS Modeler
– Joseph
Nov 23 '16 at 12:38
Note that you're initializing
bohLayer with the QGIS Layer Tree's current layer (aka. active layer or selected layer). I guess the script fails when you have not selected your bohLayer in the Layer Tree. Let me know if that's right to tell you how to avoid relying on a selected layer.– Germán Carrillo
Nov 23 '16 at 13:11
Note that you're initializing
bohLayer with the QGIS Layer Tree's current layer (aka. active layer or selected layer). I guess the script fails when you have not selected your bohLayer in the Layer Tree. Let me know if that's right to tell you how to avoid relying on a selected layer.– Germán Carrillo
Nov 23 '16 at 13:11
@German Carrillo : yeah while executing script from c# program it gives an error but while i load layer in QGIS and then execute script it run. It is not finding the current layer . and if i pass the path to the input layer for BohLayer then it do not find its extent . Is any other way to do so by script ?
– User18
Nov 30 '16 at 6:36
@German Carrillo : yeah while executing script from c# program it gives an error but while i load layer in QGIS and then execute script it run. It is not finding the current layer . and if i pass the path to the input layer for BohLayer then it do not find its extent . Is any other way to do so by script ?
– User18
Nov 30 '16 at 6:36
@Joseph : With GDAL i am using the following code : gdal_calc.py -A abc.tif --outfile=result.tif --calc="(A)/10000" but it is not working .it shows an error ImportError : numpy.core.multiarray failed to import
– User18
Nov 30 '16 at 6:40
@Joseph : With GDAL i am using the following code : gdal_calc.py -A abc.tif --outfile=result.tif --calc="(A)/10000" but it is not working .it shows an error ImportError : numpy.core.multiarray failed to import
– User18
Nov 30 '16 at 6:40
@User18 - What QGIS version and Processing version are you using?
– Joseph
Nov 30 '16 at 10:37
@User18 - What QGIS version and Processing version are you using?
– Joseph
Nov 30 '16 at 10:37
|
show 1 more comment
1 Answer
1
active
oldest
votes
I'd like to provide an alternative answer/example using GDAL: raster calculator, updated for QGIS 3. If you look in the 'processing' toolbox then GDAL > Raster Miscellaneous > Raster calculator you'll find the GUI tool. I like to test using the GUI tool first before I write the code.
First, you can call the help docs for this tool using:
processing.algorithmHelp('gdal:rastercalculator')
I find the help docs very straight forward and helpful. Here is an example of what it looks like as a standalone script:
import processing
input_raster = QgsRasterLayer('path/to/your/input/raster', 'raster')
output_raster = 'path/to/your/output/raster'
#I find it nice to create parameters as a dictionary
parameters = 'INPUT_A' : input_raster,
'BAND_A' : 1,
'FORMULA' : '(A > 100)', #your expression here. Mine finds all cells with value > 100. Experiment in the GUI if needed. You can copy and paste exactly the same expression to into your code here
'OUTPUT' : output_raster
processing.runAndLoadResults('gdal:rastercalculator', parameters) #feed in the parameters dictionary cleanly as one argument. You can also write the parameters here as individual arguments if you want.
As you might notice, I left out a lot of optional parameters. But you if you read the help docs, just follow along as you would using the GUI tool. Lastly, make sure to capitalize the parameter names as I did in my example.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f218835%2fraster-calculation-in-qgis-using-python-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'd like to provide an alternative answer/example using GDAL: raster calculator, updated for QGIS 3. If you look in the 'processing' toolbox then GDAL > Raster Miscellaneous > Raster calculator you'll find the GUI tool. I like to test using the GUI tool first before I write the code.
First, you can call the help docs for this tool using:
processing.algorithmHelp('gdal:rastercalculator')
I find the help docs very straight forward and helpful. Here is an example of what it looks like as a standalone script:
import processing
input_raster = QgsRasterLayer('path/to/your/input/raster', 'raster')
output_raster = 'path/to/your/output/raster'
#I find it nice to create parameters as a dictionary
parameters = 'INPUT_A' : input_raster,
'BAND_A' : 1,
'FORMULA' : '(A > 100)', #your expression here. Mine finds all cells with value > 100. Experiment in the GUI if needed. You can copy and paste exactly the same expression to into your code here
'OUTPUT' : output_raster
processing.runAndLoadResults('gdal:rastercalculator', parameters) #feed in the parameters dictionary cleanly as one argument. You can also write the parameters here as individual arguments if you want.
As you might notice, I left out a lot of optional parameters. But you if you read the help docs, just follow along as you would using the GUI tool. Lastly, make sure to capitalize the parameter names as I did in my example.
add a comment |
I'd like to provide an alternative answer/example using GDAL: raster calculator, updated for QGIS 3. If you look in the 'processing' toolbox then GDAL > Raster Miscellaneous > Raster calculator you'll find the GUI tool. I like to test using the GUI tool first before I write the code.
First, you can call the help docs for this tool using:
processing.algorithmHelp('gdal:rastercalculator')
I find the help docs very straight forward and helpful. Here is an example of what it looks like as a standalone script:
import processing
input_raster = QgsRasterLayer('path/to/your/input/raster', 'raster')
output_raster = 'path/to/your/output/raster'
#I find it nice to create parameters as a dictionary
parameters = 'INPUT_A' : input_raster,
'BAND_A' : 1,
'FORMULA' : '(A > 100)', #your expression here. Mine finds all cells with value > 100. Experiment in the GUI if needed. You can copy and paste exactly the same expression to into your code here
'OUTPUT' : output_raster
processing.runAndLoadResults('gdal:rastercalculator', parameters) #feed in the parameters dictionary cleanly as one argument. You can also write the parameters here as individual arguments if you want.
As you might notice, I left out a lot of optional parameters. But you if you read the help docs, just follow along as you would using the GUI tool. Lastly, make sure to capitalize the parameter names as I did in my example.
add a comment |
I'd like to provide an alternative answer/example using GDAL: raster calculator, updated for QGIS 3. If you look in the 'processing' toolbox then GDAL > Raster Miscellaneous > Raster calculator you'll find the GUI tool. I like to test using the GUI tool first before I write the code.
First, you can call the help docs for this tool using:
processing.algorithmHelp('gdal:rastercalculator')
I find the help docs very straight forward and helpful. Here is an example of what it looks like as a standalone script:
import processing
input_raster = QgsRasterLayer('path/to/your/input/raster', 'raster')
output_raster = 'path/to/your/output/raster'
#I find it nice to create parameters as a dictionary
parameters = 'INPUT_A' : input_raster,
'BAND_A' : 1,
'FORMULA' : '(A > 100)', #your expression here. Mine finds all cells with value > 100. Experiment in the GUI if needed. You can copy and paste exactly the same expression to into your code here
'OUTPUT' : output_raster
processing.runAndLoadResults('gdal:rastercalculator', parameters) #feed in the parameters dictionary cleanly as one argument. You can also write the parameters here as individual arguments if you want.
As you might notice, I left out a lot of optional parameters. But you if you read the help docs, just follow along as you would using the GUI tool. Lastly, make sure to capitalize the parameter names as I did in my example.
I'd like to provide an alternative answer/example using GDAL: raster calculator, updated for QGIS 3. If you look in the 'processing' toolbox then GDAL > Raster Miscellaneous > Raster calculator you'll find the GUI tool. I like to test using the GUI tool first before I write the code.
First, you can call the help docs for this tool using:
processing.algorithmHelp('gdal:rastercalculator')
I find the help docs very straight forward and helpful. Here is an example of what it looks like as a standalone script:
import processing
input_raster = QgsRasterLayer('path/to/your/input/raster', 'raster')
output_raster = 'path/to/your/output/raster'
#I find it nice to create parameters as a dictionary
parameters = 'INPUT_A' : input_raster,
'BAND_A' : 1,
'FORMULA' : '(A > 100)', #your expression here. Mine finds all cells with value > 100. Experiment in the GUI if needed. You can copy and paste exactly the same expression to into your code here
'OUTPUT' : output_raster
processing.runAndLoadResults('gdal:rastercalculator', parameters) #feed in the parameters dictionary cleanly as one argument. You can also write the parameters here as individual arguments if you want.
As you might notice, I left out a lot of optional parameters. But you if you read the help docs, just follow along as you would using the GUI tool. Lastly, make sure to capitalize the parameter names as I did in my example.
answered Mar 2 at 15:41
Erich PurpurErich Purpur
301214
301214
add a comment |
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f218835%2fraster-calculation-in-qgis-using-python-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You could call the GDAL or SAGA raster calculator from the Processing plugin such as described in this post: Number inputs as raster calculator variables in QGIS Modeler
– Joseph
Nov 23 '16 at 12:38
Note that you're initializing
bohLayerwith the QGIS Layer Tree's current layer (aka. active layer or selected layer). I guess the script fails when you have not selected yourbohLayerin the Layer Tree. Let me know if that's right to tell you how to avoid relying on a selected layer.– Germán Carrillo
Nov 23 '16 at 13:11
@German Carrillo : yeah while executing script from c# program it gives an error but while i load layer in QGIS and then execute script it run. It is not finding the current layer . and if i pass the path to the input layer for BohLayer then it do not find its extent . Is any other way to do so by script ?
– User18
Nov 30 '16 at 6:36
@Joseph : With GDAL i am using the following code : gdal_calc.py -A abc.tif --outfile=result.tif --calc="(A)/10000" but it is not working .it shows an error ImportError : numpy.core.multiarray failed to import
– User18
Nov 30 '16 at 6:40
@User18 - What QGIS version and Processing version are you using?
– Joseph
Nov 30 '16 at 10:37