Extracting Raster Values using Points, Polygons & Lines | QGIS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to the channel in this video i will be going over few techniques that you can use to extract data from rasters using points polygons and lines now to give you a bit of a context on what i mean by extracting data from rastus let's say i have russ like this which is basically a digital elevation model so each of these pixels of this raster basically corresponds to an elevation value now you can check this by heading over to this identify features tool and if you happen to click over one of these pixels right over here on the right side you will see that it displays or it gives us the corresponding elevation value in this case it's 391 meters above the mean sea level so similarly if i were to just click somewhere over here you can see that it's basically giving us a lower value but i guess you guys can understand how inefficient this method actually can be if you wanted to extract data from a raster so let's say for an example without extracting the individual pixel values of different random locations let's say if you wanted to just average out maybe a number of pixels just so that you can get the average elevation value of an area which is specified by a given boundary well the manual method to do that would be to extract the pixel value of each of these which are contained by the pyramid of that boundary and then basically to add them all up and then divide it by the number of pixels but as you can imagine that's going to be a very tedious task and similarly if you want to extract the pixel values along a given line let's say for an example if i were to draw a line starting from here until here i could manually read all the pixels that the line cuts through well technically we can do that but again you can imagine that that's actually going to be quite a laborious task if you would do it manually so that's why in this tutorial i'm going to make use of some of the inbuilt tools that comes with qgis that you can actually make use of in order to make the aforementioned tasks much easier and it's just going to take only a couple of seconds to accomplish everything that i mentioned just now all right guys so to get started first i'm going to show you guys how you can extract raster values using a bunch of points so i do not have a pre-prepared shape file which contains some random points so what i'm going to do is i'm going to just head over to my folder and i'm going to create a new shape file and let's just name this as points and when you're creating this point shape file just make sure that you're using the same coordinate reference system as the coordinate reference system of your raster now in my case it happens to be wgs 1984 utm zone 30 not projected coordinate reference system so i'm going to make my point shape file to be an equivalent of that and after that we can just double click over here on this newly created empty shape file maybe i might change the symbology to make it a bit more visible by choosing a color like yellow and after that i'm going to put this into editable mode and using the add point feature i'm just going to add some random points like this so i have added eight different points randomly now in your case if you guys already have this kind of a point shape file noting the locations at which you would like to extract the raster value or the pixel value you guys can directly go ahead and use that just again make sure that both the raster file and your point shape file are having the same coordinate reference system now after i have digitized the points like this i'm going to save my edits and after that i'm going to exit the editable mode now if i go over to the attributes table you will be able to see that what we have right over here is basically seven eight different points just like this so our objective would be to extract the underlying pixel value at each of these locations demarcated by these points so that's going to be our objective and to do that you can simply use this tool called sample raster values which is under raster analysis now in case if you cannot see this processing toolbox you guys can head over to processing and activate it from here so i'm going to double click over here and we basically have to give two inputs so the first input is obviously going to be the locations of our points so you can see that it's already been selected and the second input is going to be the rust from which we expect to extract those pixel values and that raster is going to be this elevation raster so that should be it basically so after that if i were to run this command you can see that it created a new shape file called sample points and if i right click and go to attributes table now you can see that in this newly created point shape file in addition to this id column that we had previously now it has one additional column called r value which basically stands for raster value and each of these raster values are basically corresponding to the pixel value underlying each of these different points for example if i select this first point you can see that it highlights and shows me and it's saying that the the pixel value behind this point is 328 meters now i can always do a random check like this simply by taking this identify features tool and if i were to right click over here and select the elevation raster you can see that the corresponding raster value is 328 so basically it extracted that value correctly so the case is actually quite similar for the others as well so you guys can imagine how fast this method actually is compared to doing this manually one by one so guys feel free to try this method out it's going to make your life much easier especially if you're working with a large data set where you have to extract multiple pixel values from a number of locations and by using this ra sample raster values tool you would be able to do that just in a matter of few seconds all right guys so now let's move on to the next part and that's how to extract data from a raster using a polygon now as i explained to you guys at the beginning of this video you have to understand what's the meaning of extracting raster data based on a polygon because it makes sense to use points like this to extract raster values at different individual locations because that would actually give you the exact pixel value however when it comes to polygons if i were to draw a polygon maybe covering this area you can see that that polygon is basically covering a number of pixels so the type of things that you that you can extract using a polygon would be maybe the average value of all the pixels that are bounded by that polygon that will actually give you the average elevation value in this particular case or if you would like to know within a given boundary what's the maximum elevation or the maximum raster value or the minimum raster value the standard deviation of all the given raster values you can compute this kind of statistical information using the next tool that i'm going to discuss with you guys all right so to demonstrate that i'm going to uh create a couple of polygons so i think for the time being i will get rid of this these two points shape files and now what i'm going to do is i'm going to right click over here and go to new shape file and i'm going to name this as areas and now the geometry type is going to be polygon and over here you have to select the same coordinate reference system as the raster file all right now i'll double click over here and i'll get started with demarcating the areas i think for this particular case i would like to [Music] have only the outline so at least you guys would be able to see what's going to get bounded by the boundary of that particular polygon so after that i will just put this to be on the editable mode and now let's get started with the digitizing so the polygons would be something like this i'm just going to create some random polygons so this is going to be my polygon one and maybe another polygon like this polygon two and finally another polygon like this that's going to be polygon 3. all right after that we can save the edits and exit ourselves out from the editable mode all right so the next tool that i'm going to use is actually called zonal statistics which is under raster analysis as well so if i go over here and double click zonal statistics uh we can see that we will have to provide the raster layer in this case my raster layer is the elevation layer and we basically have just one band and the vector layer containing the zones are basically going to be my polygons which is this uh area shape file and over here under the statistics to calculate you can expand this and you can actually get a bunch of things that you can calculate using this tool so first one will calculate the number of pixels within the demarcated polygon and the second one will calculate the sum of all the pixel values and the third one will get the average and we have the median standard deviation min max and so on so we might not necessarily need everything in this case i think i'm going to go with the average the minimum and the maximum i think that should be good enough click ok and now if i run the tool we can exit out of this window if i head over to the area shape file and now if i open the attributes table you can see that for the three different polygons that we had before we have the average value if i select over here you can see that for this particular polygon we have the average elevation so that's going to be the addition of all the pixel values which are bounded by this boundary and then dividing it by the number of pixels so that's going to give you 474 so on average we can expect an elevation of 474.3 meters above mean sea level for this particular area and the minimum pixel value was 279 and the maximum pixel value was 606 so you can see it's actually quite a big range so the minimum pixel values probably might correspond to areas like this which are shown in these darker colors and the higher pixel values probably might be coming from somewhere over here and we have the same things for this polygon as well as this polygon so guys i think you guys get the basic idea this can be quite helpful especially if you want to do some quick extraction of data from rasters by the way guys this does not apply only to elevation rasters you can basically load any kind of raster with the proper numerical values and you will be able to perform this analysis on those rasters without a hassle alright guys so now let's move on to the next part and that's going to be how to extract data from a raster using a line feature so to demonstrate that i'm going to get rid of this shape file as well and what i'm going to do is i'm just going to randomly create one line so i'm going to name this as line the geometry type is going to be line click ok after that we can double click over here turn on editing mode and we can create just a random line like this now as you can see we have a line over here so the idea would be to extract the pixel values starting from this end all the way up until this end that this line basically cuts through so it's basically going to pick up all these pixel values and what that's going to give us is basically a profile graph starting from this end up until this end so you would be able to see the elevation variation if you were to actually travel along this line from one corner to another corner so for that i'm going to actually make use of these saga tools which is also included in this processing toolbox so if i were to type profile over here there is one tool under terrain analysis called profiles from lines it will be quite self-explanatory actually if you double click over here the first input that we need would be the dem and the second input would be the line shape file and that's pretty much it guys after that we can just click run over here now this might actually take a while compared to the previous two processes so after the process is done you will be able to see that a new shape file a new point shape file appears right on top of this line shape file that we had previously and as you can see at equal distances it has created a single point starting from the left corner of the line all the way up until the right corner of the line so if i right click over here on this new shape file and go to open attributes table and if i click over here on this distance column just so that we can start sorting the values from the lower value to the higher value if i highlight this point you can see that that point basically highlights the very first point let me go ahead and open that up again and we can simply ignore the first two columns the line id and the id but what's denoted by this distance column is basically the distance of each of these points as you successively pass one after another to the right side with respect to the very first point so you can see that if you denote this to be zero meters then second point it has computed the distance to be 24.5 and the third point would be 49.18 meters and the fourth point would be 73.77 meters and so on and these x and y columns are basically extractions of the x and y coordinates of each of these different points and what's important for us would be this z value which is the elevation value of each of these different points now if you want to do a quick check on the distance between these two different points for example if i go over here and take the measure tool i can measure the line distance between the first point and the second point is about 24.5 and and the distance between the first point and the third point would be about 49 meters i'm just doing a rough estimation and that should pretty much line up with whatever they have mentioned over here yeah so it's 24 and 49 and so on and as you can see the z column basically gives us the elevation values along this line from the left corner to the right corner now i think it would be pretty interesting if you can plot the elevation values against the horizontal distance so that you can actually generate an actual profile graph which you can simply do by selecting all of this and you can use a simple spreadsheet processing software like microsoft excel and you can basically copy and paste that data table and since we are not really interested in the first three columns i'm going to get rid of these first three columns and we would also not need the x y values as well and finally what we are left with is basically the distance and the z value so if i highlight everything and try to display this as a graph we can go to xy scatter and maybe select a graph like this so this is how that would look if you were to move starting from the left corner of the line all the way to the right corner of the line this is how the elevation actually would vary so the y-axis is basically showing us the variation of the elevation and the x-axis is showing us the distance from the left corner all the way up until the right corner of the of the line all right guys so that wraps up this tutorial if you do have any questions don't forget to add a comment down below and if you think that this tutorial was helpful for you guys show your support by hitting that like button and if you like to stay tuned for tutorials like this on a weekly basis you can even consider subscribing as well so i'll see you guys again with another tutorial very soon
Info
Channel: GeoDelta Labs
Views: 33,660
Rating: undefined out of 5
Keywords: zonal statistics, spatial statistics, extracting raster values, how to extract raster values, qgis tutorials, extracting raster values using qgis
Id: oPTMhWX3y2A
Channel Id: undefined
Length: 17min 45sec (1065 seconds)
Published: Sat Aug 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.