In the previous post I had discussed how to compute raster statistics by vector polygon in QGIS. This post will discuss more or less the same thing, but using Google Earth Engine (GEE) platform. We will compute some statistics figures like mean, median, mode, minimum and maximum. And at the end all the statistics figures will be plotted into a one graph as seen in the following figure 1.
Figure 1. Statistics Figures Plot
In this tutorial I applied the same scenario that used NDVI and the local area boundary of Vancouver City. Therefore we will start this tutorial with NDVI calculation.
To do NDVI calculation can be done with the following code.
In the code above, firstly we define a variable for local area boundary data which is called local_area. The local_area variable is a feature collection from the shared assets. So you can use it immediately. Then in the dataset variable, we define the image collection from Sentinel-2 satellite imagery within a date range of June - December 2021, filter the cloud coverage below 20% and select all Bands.
Image collection comprises all filtered images. In line 12 we only take a single image from the image collection based on median value and clip it with local area boundary. The calculation of NDVI is taking place in line 15 using the normalizedDifference method. The rest of the code is adding NDVI and the local area boundary into the map. After running the code you should get the result as shown in the figure 2.
Figure 2. Data Added to GEE Map
Next we will do computation for some statistics parameters as mentioned
above. For this we have to aggregate the data using Reducer. As shown in the following code, there are some reducers method are applied such as: mean, min, max, mode and median. All reducers are combined into a variable reducers.
Before plotting the graph, maybe you want to check the statistics result. For that we can use the image.reduceRegions() to get image statistics for multiple regions at once. In the code below the reduceRegions was applied on the NDVI image and print the result to the console as shown in figure 3.
|Figure 3. Printed Statistics Result in the Console|
Lastly we create a graph to plot all statistics variables using ui.Chart.image.byRegion() as in the code below. The code mainly consists of two parts: The first one is the part for image and regions properties where we define image's name, vector regions, reducers, scale and the property for x axis. The second part we specify the chart's type and its options like the chart's title including x and y axis title, line and point size and define color for the graph. Lastly to show the graph the print command is used.
After running the code you should get a graph as in figure 1.
That's all the tutorial on how to compute and plot image statistics by region in Google Earth Engine. We had discussed how to create a NDVI image from Sentinel-2 satellite imagery, compute some statistics parameters using image reducer and plot all statistics variables into a graph. Hope this tutorial is useful. Thanks for reading!
To see the code in action. Check out the following video!