Skip to content

Instantly share code, notes, and snippets.

@martgnz
Forked from yanofsky/Makefile
Created July 20, 2016 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martgnz/5ebf9e99479bef860f6d91c4a1d634c8 to your computer and use it in GitHub Desktop.
Save martgnz/5ebf9e99479bef860f6d91c4a1d634c8 to your computer and use it in GitHub Desktop.
This is workflow for downloading, processing, and mosaicing Landsat scenes, as a Makefile
# lansatutil directory
LANDSAT = ~/landsat
# scenes to target
LANDSAT_IDS = \
LC81220442016038LGN00 \
LC81220452016038LGN00 \
LC81210442014281LGN00 \
LC81210452014281LGN00
# 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
$(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
gdal_merge.py \
-co "PHOTOMETRIC=RGB" \
-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 \
-co "PHOTOMETRIC=RGB" \
-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
endef
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
$(LOCAL_PROCESSED):
#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