Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This is workflow for downloading, processing, and mosaicing Landsat scenes, as a Makefile
# lansatutil directory
LANDSAT = ~/landsat
# scenes to target
LC81220442016038LGN00 \
LC81220452016038LGN00 \
LC81210442014281LGN00 \
# where to put processed files
LOCAL_PROCESSED = ./processed
# Define projection for the output
PROJECTION = +proj=utm +zone=50 +datum=WGS84
# if you run `make` create a stitched auto contrasted mosaic using bands 4, 3, and 2 for the specified IDs
all:: $(LOCAL_PROCESSED)/stiched_autocontrast_432.tiff
# create a function to use while iterating over the IDs
# the variable $1 is always the current ID
define landsat-workflow
download_all:: $(LANDSAT)/downloads/$1/$1_MTL.txt
# download bands 4, 3, 2, and 8 of the specified ID
landsat download $1 --bands 4328
process432:: $(LOCAL_PROCESSED)/$1_rgb.tif
$(LOCAL_PROCESSED)/$1_rgb.tif: ~/landsat/downloads/$1/$1_MTL.txt $(LOCAL_PROCESSED)
# create a 432 composite of the specified IDs bands
# $$@ is the target for the rule i.e. $(LOCAL_PROCESSED)/$1_rgb.tif \
-separate ~/landsat/downloads/$1/$1_B{4,3,2}.TIF \
-o $$@
warptoutm50:: $(LOCAL_PROCESSED)/$1_rgb_resized_utm50.tif
$(LOCAL_PROCESSED)/$1_rgb_resized_utm50.tif: $(LOCAL_PROCESSED)/$1_rgb.tif
# remove an existing file of the target name because gdal will fail if it's there
rm -f $(LOCAL_PROCESSED)/$1_rgb_resized_utm50.tif
# reproject the specifed scene to UTM 50 and resize to to be 1000px wide using cubic sampling
# $$< are the required files for the rule $$@ is the target for the rule
gdalwarp \
-t_srs '$(PROJECTION)' \
-ts 1000 0 \
-r cubic \
$$< \
# if you run `make` create all the resized reprojected scenes
all:: $(LOCAL_PROCESSED)/$1_rgb_resized_utm50.tif
stich_432: $(LOCAL_PROCESSED)/stiched_432.tiff
$(LOCAL_PROCESSED)/stiched_432.tiff: $(addprefix $(LOCAL_PROCESSED)/,$(addsuffix _rgb_resized_utm50.tif, $(LANDSAT_IDS)))
# stitch together the resized reprojected scenes into one mosaic
# $@ is the target for the rule $^ are the requirements
schooner-stitch \
$^ \
autocontrast432: $(LOCAL_PROCESSED)/stiched_contrast_432.tiff
$(LOCAL_PROCESSED)/stiched_autocontrast_432.tiff: $(LOCAL_PROCESSED)/stiched_432.tiff
# create a new version of the mosaic with enhanced contrast
schooner-contrast $(LOCAL_PROCESSED)/stiched_432.tiff $(LOCAL_PROCESSED)/stiched_autocontrast_432.tiff
#make sure you have your folder to hold processed images
mkdir $@
# iterate over the IDs passing each into the landsat-workflow function to create the rules it contains contained
$(foreach 1, $(LANDSAT_IDS),$(eval $(call landsat-workflow,$1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment