Skip to content

Instantly share code, notes, and snippets.

@liamtoney
Last active July 5, 2022 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liamtoney/01f97217aeba26bb7cd87d62fbbdda01 to your computer and use it in GitHub Desktop.
Save liamtoney/01f97217aeba26bb7cd87d62fbbdda01 to your computer and use it in GitHub Desktop.
Using GDAL to process Planet Labs satellite imagery

Using GDAL to process Planet Labs satellite imagery

This tutorial shows how to install and use the command-line tool GDAL to process satellite imagery from Planet Labs. These instructions are for macOS.

Installing GDAL

  1. Open Terminal.

  2. Install Homebrew. Homebrew is a "package manager" for macOS that allows for easy installation and maintenance of command-line tools such as GDAL. Run the following command in the Terminal prompt:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    

    Follow the instructions (if any) that appear.

  3. Install GDAL. Run the following command in the Terminal prompt:

    brew install gdal
    
  4. Verify that GDAL is installed by running the following command in the Terminal prompt:

    gdal_translate --version
    

    If the installation was successful, you'll get a message similar to this:

    GDAL 2.4.2, released 2019/06/28
    

Using GDAL commands

GDAL offers a large number of programs, but we'll only be using two: gdalinfo and gdal_translate. gdalinfo provides information on an input image file, such as the spatial extent, resolution, and number/type of bands. gdal_translate modifies images by changing their data format, re-scaling them, and/or removing bands.

To run these commands on Planet Labs images:

  1. Download an order from Planet Labs.

  2. Unzip the *.zip file and, using Finder, navigate to the folder containing the target GeoTIFF image. The image should have a filename of the form *_3B_AnalyticMS.tif.

  3. Drag this Finder folder onto the Terminal icon in your Dock to open up a new Terminal window in this directory (or, cd there from Terminal). Type ls and confirm that your target file is listed in the output.

  4. Enter the command that you want to use (see below).

GDAL command anatomy and examples

gdalinfo

Anatomy:

gdalinfo <input_file>
  • gdalinfo
    Base commmand. Just entering this command will display usage instructions.

  • <input_file>
    The filename of the image you'd like to view information for.

Example:

gdalinfo 20191110_213832_1038_3B_AnalyticMS.tif

displays information about the image file 20191110_213832_1038_3B_AnalyticMS.tif. We see that there are four bands available: blue, green, red, and near-infrared (NIR).

Click here to view the command's output.
Driver: GTiff/GeoTIFF
Files: 20191110_213832_1038_3B_AnalyticMS.tif
Size is 9055, 4704
Coordinate System is:
PROJCS["WGS 84 / UTM zone 3N",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",-165],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AXIS["Easting",EAST],
    AXIS["Northing",NORTH],
    AUTHORITY["EPSG","32603"]]
Origin = (563823.000000000000000,6071793.000000000000000)
Pixel Size = (3.000000000000000,-3.000000000000000)
Metadata:
  AREA_OR_POINT=Area
  TIFFTAG_DATETIME=2019:11:10 21:38:32
Image Structure Metadata:
  COMPRESSION=LZW
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (  563823.000, 6071793.000) (164d 0'26.83"W, 54d47'21.35"N)
Lower Left  (  563823.000, 6057681.000) (164d 0'37.97"W, 54d39'44.86"N)
Upper Right (  590988.000, 6071793.000) (163d35' 6.42"W, 54d47' 6.27"N)
Lower Right (  590988.000, 6057681.000) (163d35'22.30"W, 54d39'29.84"N)
Center      (  577405.500, 6064737.000) (163d47'53.35"W, 54d43'26.24"N)
Band 1 Block=256x256 Type=UInt16, ColorInterp=Blue
  Description = blue
  NoData Value=0
  Overviews: 3019x1568, 1007x523, 336x175
Band 2 Block=256x256 Type=UInt16, ColorInterp=Green
  Description = green
  NoData Value=0
  Overviews: 3019x1568, 1007x523, 336x175
Band 3 Block=256x256 Type=UInt16, ColorInterp=Red
  Description = red
  NoData Value=0
  Overviews: 3019x1568, 1007x523, 336x175
Band 4 Block=256x256 Type=UInt16, ColorInterp=Undefined
  Description = nir
  NoData Value=0
  Overviews: 3019x1568, 1007x523, 336x175

gdal_translate

Anatomy:

gdal_translate -b <band_no> -ot "Byte" -scale -exponent <exp> <input_file> <output_file>
  • gdal_translate
    Base commmand. Just entering this command will display usage instructions.

  • -b <band_no>
    Which image bands to include in the output file. Can be repeated to select multiple bands. Use gdalinfo to view which bands are available and what their band numbers are.

  • -ot "Byte"
    Selects the "Byte" output data format. This makes the file viewable in e.g. Preview and Google Earth.

  • -scale
    Scales the image pixel values to match the "Byte" data format.

  • -exponent <exp>
    Specify a positive number here, <exp>, to use for exponential re-scaling. This will raise all pixel values in the image to this power. For images that seem too dark, use a value less than 1. For images that seem too light, use a value greater than 1. Specify 1 if you don't want to use exponential re-scaling at all (since x1 = x).

  • <input_file>
    The filename of the image you'd like to process.

  • <output_file>
    The filename, including the .tif extension, of the output image that will be created. The image will be placed in the directory where the command was run.

Example:

gdal_translate -b 1 -b 2 -b 3 -ot "Byte" -scale -exponent 1 20191110_213832_1038_3B_AnalyticMS.tif rgb.tif

creates a new image, rgb.tif, from the file 20191110_213832_1038_3B_AnalyticMS.tif, using only band numbers 1–3 (RGB) and changing/scaling the data to be viewable in Preview and Google Earth. No image darkness changes are applied (i.e. image pixel values are raised to a power of 1).

gdal_translate -b 4 -ot "Byte" -scale -scale -exponent 0.25 20191110_213832_1038_3B_AnalyticMS.tif nir.tif

creates a new image, nir.tif, from the file 20191110_213832_1038_3B_AnalyticMS.tif, using only band number 4 (NIR) and changing/scaling the data to be viewable in Preview and Google Earth. The image is made lighter by raising the image pixel values to a power of 1/4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment