Making Sense of Satellite Data, An Open Source Workflow: Pre-processing Data with QGIS

Manually color-corrected image of the Alps on July 4, 2017, from Sentinel 2B.

Data vs. Images

Each file is a single slice of color—what you’d get if you separated the red, green, and blue bands of a digital photograph into separate channels. You can think of each band as a grayscale image, but they’re not simply images—they’re data. Each pixel is carefully calibrated to represent a real-world quantity from a narrow slice of the color spectrum.

Installing QGIS

Installing QGIS is beyond the scope of this post, but I had success on Mac OSX with this QGIS3 installer. The instructions are fairly straightforward with the exception of a few tricky bits:

  • A specific version of Python 3.6 is required. Follow the download link given with the QGIS installer (don’t worry—it won’t interfere with your system Python).
  • You’ll need to edit (or even create) a hidden file called a bash profile. To do so, navigate to your home folder (the one with Applications, Desktop, Documents, etc.) and press shift-command-period to show hidden files. If you see something called .bash_profile you can add the following line with a text editor:
export PATH=/Library/Frameworks/GDAL.framework/Programs:/Library/Frameworks/PROJ.framework/Programs:/Library/Frameworks/SQLite3.framework/Programs:/Library/Frameworks/UnixImageIO.framework/Programs:$PATH
  • If not, create a new file, copy and paste in the above text, and name the file .bash_profile. You’ll likely be warned that you’re about to create a system file—correct! Just make sure you don’t overwrite an existing file.
  • After installing QGIS it should launch, but you’ll need to do one more thing in QGIS itself:
  • Go to Settings→Options from the menu bar.
  • Select System from the options on the left.
  • Scroll down to Environment and check Use Custom Variables.
  • Under the Apply option select Prepend, under Variable enter PATH, and under Value copy/paste:
  • Click OK and restart QGIS.

Using QGIS

Here’s what we’ll do: combine the 3 bands of Sentinel-2 data into one full-color image (derived from this Stack Overflow post), then stretch the 16 bits of data into an 8-bit range to export for further processing.

  • Launch QGIS, then go to Raster → Miscellaneous → Build Virtual Raster from the menu bar.
  • This brings up the Build Virtual Raster window:
  • Make sure Layer Stack is checked to put each input file into its own channel (otherwise they will be merged spatially and all put into a single band). Likewise, select Open output file after running algorithm to import into a QGIS raster layer—otherwise the merged output won’t be imported (in other words, you won’t see anything).
  • Click the button just to the right of the Input Layers field to select the source files. Press Add file(s)…. This will open a system file dialog—navigate to the directory with the .jp2 files then select and open all three of them. This will add the paths to the files to the QGIS Multiple selection dialog box. In the Sentinel data linked above B02.jp2 is blue, B03.jp2 is green, and B04.jp2 is red, but they need to be in reverse order: red, green, blue. Re-arrange the bands by dragging them. Then hit OK.
  • The Build Virtual Raster windows might disappear at this point—don’t despair! It’s just hidden behind the main QGIS window. Find it again, and hit Run in Background.
  • QGIS is nothing if not verbose, so it’ll tell you exactly what it’s doing. When you see Algorithm ‘Build Virtual Raster’ finished click Close. Which will return you to the main GIS window, this time with an image in it!
True color (red, green, blue) surface reflectance image of the Alps on July 4, 2017, from Sentinel 2B.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Robert Simmon

Robert Simmon

Data Visualization, Planet Labs. Ex-NASA. Blue Marble, Earth at Night, color.