QGIS Python (PyQGIS) - Raster Calculator in a Python Script

  Рет қаралды 10,450

Geospatial School

Geospatial School

Күн бұрын

The QGIS Raster Calculator is a powerful tool for creating and altering raster data. This QGIS Python tutorial will teach you how to use the raster calculator in a Python script so you can automate repetitive tasks.
Visit opensourceoptions.com for more content and courses

Пікірлер: 24
@annasam4461
@annasam4461 2 жыл бұрын
Hi, Thank you for the tutorial. Is there any way to combine two rasters (reclassified raster and a binary raster) to find the no of pixels of a binary raster in the each category of the reclassified raster ?
@jaimegarbanzo9083
@jaimegarbanzo9083 Жыл бұрын
awesome, I will add this calc.lastError() to know if something goes wrong
@rubencrespo1720
@rubencrespo1720 3 жыл бұрын
Hi There, thank you for the video. I have a question. If I want to do a subtraction between two raster layers and I want to get only a certain extent of the area. Should I use the "extent" parameter? How do I set the desired bounding box? Thank you!
@Benjamin-ej8oz
@Benjamin-ej8oz 4 жыл бұрын
Would it be possible to do a video about PyQGIS documentation? - where do you go when you check the syntax of these commands, how to read, search and understand the documentation? I'm newish to python and when I look at the docs I feel like part of my brain must be missing
@geospatialschool
@geospatialschool 4 жыл бұрын
Check out this course: opensourceoptions.com/courses/courses/pyqgis-learn-the-qgis-python-api/ The sample (free) lessons will go through documentation for some specific functions. Hopefully you'll find it helpful.
@connor3089
@connor3089 Жыл бұрын
Thanks for the video. For line 12 I get the error message File "", line 13, in AttributeError: 'str' object has no attribute 'extent'. Any idea what's going on there?
@geospatialschool
@geospatialschool Жыл бұрын
The variable that you're calling 'extent' on is of string type
@johannesmay185
@johannesmay185 4 жыл бұрын
have you figured out why qgis crashes (7:25)? The same happens to me over and over again, but simply restarting the program doesn´t solve the problem.
@geospatialschool
@geospatialschool 4 жыл бұрын
Did you change anything in the code? In the video I changed the code slightly after the crash. Make sure your code matches the code in the video from after the crash. If it does, and you still get a crash then it could be something with the files you are using.
@johannesmay185
@johannesmay185 4 жыл бұрын
@@geospatialschool Thanks for this hint. NOTE for everyone: change ras.layer into ras.raster ; Then it all gonna work trouble free.
@Richard-sk3vh
@Richard-sk3vh 4 жыл бұрын
Hey man, I'm trying to run a batch QGIS3 NDVI calculator script on a bunch of ENVI 16 bit unsigned .bil data cubes, and the outputs are all 32bit and dark n grainy. Is there a way to set the output data type?
@geospatialschool
@geospatialschool 4 жыл бұрын
Your outputs are 32 bit because NDVI is going to result in a floating point value, not an integer. You might be able to change the data type by converting NDVI to an integer value. Also, you can probably just adjust the symbology to get the outputs to look the way you want.
@Richard-sk3vh
@Richard-sk3vh 4 жыл бұрын
​@@geospatialschool Okay, that sort of sheds some light on it. But I don't think I provided enough detail. If I run the NDVI equation in the QGIS calculator GUI it yields a decent resolution, image with stats; STATISTICS_MAXIMUM=0.58577406406403 STATISTICS_MEAN=-0.13778701532923 STATISTICS_MINIMUM=-0.59615385532379 STATISTICS_STDDEV=0.30573901223104 STATISTICS_VALID_PERCENT=100 But when I use Python, the output is dark and has lost resolution. The stats are: STATISTICS_MAXIMUM=0 STATISTICS_MEAN=-0.92910058966749 STATISTICS_MINIMUM=-1 STATISTICS_STDDEV=0.05557199898703 STATISTICS_VALID_PERCENT=100 AREA_OR_POINT=Area I can't see any way to adjust it to appear like the GUIs output The for loop code I'm using is: lddLrs = [l for l in QgsProject().instance().mapLayers().values() if isinstance(l, QgsRasterLayer)] I'm trying to figure out if there's some else I need to add? Bearing in mind I don't fully understand what I'm doing yet .
@geospatialschool
@geospatialschool 4 жыл бұрын
@@Richard-sk3vh Maybe the statistics aren't being fully calculated. Even though they look different you could still have the same values in the two NDVI rasters. Check that out first. If the values are different then there's a problem with your calculation.
@eddisonjose
@eddisonjose 4 жыл бұрын
Hola!! puedes dejar aquí los códigos y las imagenes por favor? Gracias!
@geospatialschool
@geospatialschool 4 жыл бұрын
English?
@eddisonjose
@eddisonjose 4 жыл бұрын
@@geospatialschool Hi thanks!! Can you leave here the codes you have made and the data to practice?
@geospatialschool
@geospatialschool 4 жыл бұрын
I will eventually have the code available in a website post. The data can be downloaded online. This video will show you how: kzbin.info/www/bejne/jJ2unHSPpLOiiKc
@umamartinez938
@umamartinez938 4 жыл бұрын
Hi there, I got it to work once applied the change suggested below (ras.raster). Question: 1) I want to multiply bands 1-55 of an image by 400 and bands 56 to 242 by 800. Then 2) I want to rescale the Y value to range between 0 and 1. and then 3) I would like to convert the bands numbers to wavelengths, so that in viper tools wavelength and not band numbers show up. What is the simplest way to do this? I am new to python. I tried raster calculator but cannot figure out how to get the batch mode to work. thanks U
@geospatialschool
@geospatialschool 4 жыл бұрын
That's a very detailed question and would take some time to prepare an answer. The basics are all there in this video. You just need to loop through all the bands and perform the desired operation(s).
@umamartinez938
@umamartinez938 4 жыл бұрын
@@geospatialschool thanks I am working through the various videos and will tackle. Great videos!
@CuriousKitchen
@CuriousKitchen 4 жыл бұрын
Using GUI would have been much faster.
@geospatialschool
@geospatialschool 4 жыл бұрын
For this simple operation, yes. But the point of the video is to show you that raster calculator operations can be automated so if you need to run the same calculation multiple times, or on multiple datasets, you can replicate it easily.
@loganashmore4754
@loganashmore4754 4 жыл бұрын
I just incorporated this into a PyQgis model for 60 geotiff files which recalculated the rasters and hand the results to a conversion function. Easily a day+ of manual GUI labour sorted in 2 hours.
QGIS Python (PyQGIS) - Calculate Field Values (Field Calculator)
18:06
Geospatial School
Рет қаралды 13 М.
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
Cool Parenting Gadget Against Mosquitos! 🦟👶 #gen
00:21
TheSoul Music Family
Рет қаралды 32 МЛН
НИКИТА ПОДСТАВИЛ ДЖОНИ 😡
01:00
HOOOTDOGS
Рет қаралды 2,8 МЛН
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 262 #shorts
00:20
QGIS Python - Use Processing Tools in a Python Script
7:23
Geospatial School
Рет қаралды 24 М.
QGIS Python Tutorial (PyQGIS Tutorial)
32:58
Geospatial School
Рет қаралды 38 М.
QGIS Python (PyQGIS) - Add layer to a map layout
13:50
Geospatial School
Рет қаралды 12 М.
Introducing: the AI Georeferencer for QGIS
5:58
Bunting Labs
Рет қаралды 2,3 М.
QGIS 3 for Absolute Beginners
32:59
Klas Karlsson
Рет қаралды 772 М.
QGIS: Advanced Geoprocessing
22:49
Eric Brelsford
Рет қаралды 11 М.
Create stunning Urban Maps with QGIS and the QuickOSM Plugin
9:03
Open Source for Architects
Рет қаралды 21 М.
QGIS Python (PyQGIS) - Buffer Features
15:10
Geospatial School
Рет қаралды 9 М.
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18