Tip: Generate Image Footprints Using GDAL/OGR

I often need to generate polygons that trace the outline of the valid data area of an image. I used to use gdal_trace_outline from dans-gdal-scripts (https://github.com/gina-alaska/dans-gdal-scripts), but that package isn’t actively maintained and I’ve had trouble getting it to compile against recent versions of GDAL installed from conda-forge.

Instead, I’ve figured out how to use GDAL to make a virtual raster mask and then use GDAL’s own gdal_polygonize.py to draw the footprint.

Here’s an example:

# Say you have an 8-bit image with a NoData value of 0,
#  use gdal_translate to stretch values [1,255] to [1,1], 
# and assign 0 to represent the NoData value on output,
#  save result as VRT rather than writing a new raster to disk.
gdal_translate -scale 1 255 1 1 -ot Byte -of vrt -a_nodata 0 input_ortho.tif input_ortho_mask.vrt

# Create a polygon shapefile that outlines the valid (DN==1) 
# regions of the VRT just created
gdal_polygonize.py -8 input_ortho_mask.vrt -f "ESRI Shapefile" mask_footprint.shp mask_footprint DN