Python for Geosciences: Spectral Analysis (Step by Step)

Third post in a series that will teach non-programmers how to use Python to handle and analyze geospatial data

Maurício Cordeiro
Analytics Vidhya
Published in
11 min readMar 21, 2021

--

For information about the course Introduction to Python for Scientists (available on YouTube) and other articles like this, please visit my website cordmaur.carrd.co.

Introduction

Hello and welcome back. This is the third story of the series Python for Geosciences, which has the objective to help non-programmers to start using Python for spatial data analysis and to automate it’s geospatial related processes. In the first post of the series (here), we learned how to prepare the Python environment on Windows using the Anaconda package manager and how to open a GeoTiff image from a Jupyter notebook. Next, on the second post (here), we saw the basics of matrix manipulation and we also learned how to created a flexible function to calculate Normalized Difference Indexes (such as NDVI, MNDWI, etc.), given two bands.

For this third post, we will continue to analyse the image’s data, and our goal is to plot some spectral data for different targets. In remote sensing, target is usually referred to what type of coverage is present that we want to select. For example, by the end if today's post we shall be able to display the mean spectral curve of the water pixels, or vegetation. To achieve this, we will first learn:

  • Array slicing: how to access part of the image;
  • Boolean Array Indexing: to mask the portions of the image we don't want;
  • Compute mean (or other arithmetic) along a specific dimension; and
  • How to plot the spectral data.

Let's go!

Step 1- Understanding array slicing

On the first post we saw how to access single pixel values by passing indexes into square brackets to the array variable, like so: img[3000, 3000]. However, accessing single values is not of much use in satellite images, where we can easily have millions of values do deal with. So, the first concept we have to learn before moving on to the spectral analysis itself is how to crop just a part of the…

--

--

Maurício Cordeiro
Analytics Vidhya

Ph.D. Geospatial Data Scientist and water specialist at Brazilian National Water and Sanitation Agency. To get in touch: https://www.linkedin.com/in/cordmaur/