Learning Custom Scripts to make Useful and Beautiful Satellite images
When I first discovered EO Browser custom scripts, I didn’t know how useful and easy to use they are. I love remote sensing, geography, and beautiful satellite images, but I wasn’t very familiar with coding so the task seemed as impossible as rocket (satellite) science. This changed when I decided to participate in the Sentinel Hub Custom Script Contest. I learned that I can create a beautiful and informative image with a single line of code (like the following image of Lena river delta).
return [B08 * 4.5, B04 * 4.5, B01 * 3.5]
At first, it was difficult to understand even the line above. All the available tutorials were made for seasoned programmers and it took me a lot of hard work to eventually figure things out. If you are like me, this blog should spare you that trouble and help you start exploring satellite images today.
The biggest secret to creating beautiful images is using the EO Browser or the Sentinel Hub Playground; they take care of technical complexity and let you focus on the interpretation of data. All you need to do is open a scripting window in one of them and try out visualizing different satellite bands. Then you multiply them by a certain factor to manipulate their brightness. Each band shows the reflective features with the red, green or blue color assigned to it (the first band will appear red, second green and third blue).
return [B11 * 2.5, B08 * 2.5, B02 * 2.5]
How did I know which bands to use though? The Sentinel-2 satellite we’re using only has 13 of them, so you can simply go ahead and try them out to see which ones make an image you like!
But it’s also possible to create an image that is useful, as well as being beautiful. To do that, we only need to know a tiny bit more about satellites. This is the most exciting thing about satellite imagery!
To learn all about custom scripts, which bands you can use, what they mean and how they work, check out the Custom Script tutorial. See the Sentinel Hub data product page to learn about band uses and get some ideas on color composites.
Let me demonstrate how useful custom scripts can be — if you take just a little more time — by showing you the Madagascar deforestation issue. We will create multiple visualizations of increasing complexity and learn how to use the already premade ones while exploring the issue together. We will be working in EO Browser, using the Sentinel-2 satellite data.
Playing an EO Detective to Analyze Madagascar Deforestation
Madagascar suffers from severe deforestation and soil erosion. We will focus on the general area around the beautiful Betsiboka river delta and try to find out whether we can identify the problem areas using custom scripts.
Let’s first take a look at a true-color time-lapse, created in EO Browser.
This time-lapse does not show us much about vegetation, apart from seasonal changes.
To get a better idea of what’s happening, we will make a custom composite based on bands B04, B08, and B02, which can be used to detect vegetation, bare ground, and water. If you’d like, you can read a step by step guide on how you can create this composite.
return [B04 * 5.5, B08 * 2.5, B02 * 3.5]
Comparing the result above to the true-color image, we see that vegetation is displayed green and bare soil in brownish shades in the composite. Water is displayed with the whole range of colors between deep blue and red. We‘ll use it in the time-lapse to see how vegetation cover changes over time. Since we have read, that the area is heavily deforested, we’ll focus on any interesting changes that support this. Don’t miss the date in the upper right corner.
Do you see the reappearing dark brown patches of land? They seem to appear every autumn. It seems they are not forested since they are not colored green. We can expect to find those areas bare, save for dry grass or similar vegetation.
They also appear in large chunks at once, in similar shapes, meaning they are likely human-made. Could this be deforestation? Let’s make the hypothesis that those areas are deliberately deforested and test it.
First, we should figure out what those patches of land are. Is there uncovered soil there, or maybe water?
Here, we are making a big step forward in learning custom scripts, by implementing our first index.
We will use the Barren soil script I created for the latest Sentinel Hub Custom Script Contest and which is freely available in the custom script repository.
It is calculated based on the Bare soil index as follows:
The combination of bands B11 and B04 indicates bare soil areas, band B08 indicates vegetation for contrast and band B02 additionally discriminates between vegetated and non-vegetated areas.
First, we calculate the index and then return it in the red channel, so that bare ground areas display red.
let index = ((B11 + B04)-(B08 + B02))/((B11 + B04)+(B08 + B02))
return [index * 6.25, B08, B04];
The green areas display lush vegetation (forest), and red those areas, where soil or rock is exposed. It does not mean that vegetation there is nonexistent, but that it is scarce and the soil shows through.
After zooming in, we can see, that, interestingly, the suspected deforestation areas appear black. If people cut trees down, the exposed ground should appear either red, should there be exposed soil or green, should it be covered in grass. Black color means that the reflectance was low for all the input bands.
If we look at the previous, large scale image, we can see, that water in the Bare soil script is also colored black. Does that mean, those areas could somehow be wet? Irrigation maybe?
To confirm that, we will look at the moisture index. It is already available in the visualization tab of EO Browser, so you don’t have to do all the work yourself.
The moisture index uses a continuous color scale, which is also made using custom scripts. “Continuous” means, that the colors are stretched between the border values, as opposed to being classified. This way, we get as many hues and value differences as possible.
Apparently, the patches are not wet, since the moisture index (based on the color scale) is low in those areas. The area is extremely dry and there is little to no vegetation present, so I’m suspecting a fire.
If we take a look at the bands that are used in the moisture index above the color scale, we can see, it’s made of band B8A, which can be used to detect vegetation, and short wave infrared band B11. Since vegetated areas shouldn’t have extremely low moisture values, we know that it’s band B11, which is being reflected in the dry areas. This makes sense since bands B11 and B12 can be used to detect heat. Based on this information, it is reasonable to consider, that the extremely dry areas were burned.
To check if we’re correct, we can search for a custom script online. A very useful place to search for them is the Custom script repository. We’re in luck! There is a Wildfire Visualization script by Pierre Markuse. We should click on “EO Browser” to open it. If you would like to recreate a similar script, note, that bands B11 and B12 can be used to detect heated areas.
We can clearly see heat (not flames) and even smoke. The area was burned that day, which is evident by the heat on the border of our dark patch. If we look around Madagascar, we can see, there are many fires every summer and autumn all around the surrounding area.
What we could do next is try to highlight the burnt areas, to learn the extent of it. To do that, we can create custom color classes. The following script is a bit more complicated, but it shows that even classification can be done directly using custom scripts in EO Browser, showcasing their usefulness even more.
First, we calculate the bare soil index, the NDVI index for vegetation and the NDWI index for water. Then we use conditional if statements to define colors. The if statements define our index values by specifying that each index should be larger than the specified threshold. We assign colors to our water, vegetation, burnt areas, and other pixels. The indices and threshold values are obtained from understanding the satellite bands and experimenting by trying out different thresholds, until, for example, our vegetation index matches vegetation, as seen in the true-color composite. The values could also be obtained by understanding the index histogram distribution for a specific area.
This classification does a good job of highlighting the areas we believe to be deforested. However, it is not perfect. Urban areas and some bare soil fields have a similar spectral signature as the burnt areas. If you zoom in to the Mahajanga settlement at the coast, you will see it colored red. Due to similar spectral signatures, it is often difficult to find indices, bands or thresholds, that would correspond only to the phenomena you are interested in and nothing else. It‘s always good to be critical of your results and realize their limitations.
The extent of land burnt is too large to be reasonably attributed to wildfires. However, we should do research to see if burning is the common cause of deforestation in Madagascar before confirming our hypothesis.
As we can see in this article, massive amounts of forest are burnt down every year. This is due to the slash and burn agriculture, also called tavy, where the forest is burnt and then farmed in the following months. The forest grows back, but the soil is vulnerable to erosion until then. Moreover, this is not the only cause of deforestation in Madagascar; others include deforestation for grazing, logging, and production of coal. As a consequence, heavy rains erode large amounts of soil, since there is no forest cover to protect it. Madagascar has lost 80 % of its primary forests, causing many indigenous species, such as lemurs, to be endangered.
Have we explored a different area of Madagascar, on the east, for example, deforestation for logging might be more prevalent than deforestation by tavy and our results would differ.
I hope you found this exploration interesting and that one day you enjoy custom scripts as much as I do. I hope to see your submission in the next Sentinel Hub Custom Script Contest where you can earn a nice prize while creating beautiful and useful satellite images!