Skip to content

Instantly share code, notes, and snippets.

@meyer1994
Last active September 21, 2020 13:35
Show Gist options
  • Save meyer1994/9c8cdb9deb9310d5e95676aa58dbb554 to your computer and use it in GitHub Desktop.
Save meyer1994/9c8cdb9deb9310d5e95676aa58dbb554 to your computer and use it in GitHub Desktop.
Downloads a cropped portion of each band from a sentinel 2 tile, generates NDVI and RGB images from the cropped downloads.
-1.0 200 0 0
-0.3 255 40 0
-0.1 255 150 0
0.1 255 240 0
0.3 0 230 0
0.5 0 190 0
1.0 0 130 0
nv 0 0 0 0
#!/bin/bash
set -e
# Usage:
# $ bash crop.sh GEOJSON_FILE BUCKET/TILE_PATH
#
# Example:
# $ bash crop.sh field.geojson sentinel-s2-l1c/tiles/22/J/GQ/2019/11/16/0
export AWS_REQUEST_PAYER=requester
FIELD=$1
TILE=$2
OUTP=$3
seq -f "B%02g" 1 12 | xargs -L1 -I@ -P12 -t \
gdalwarp -of GTiff \
-cutline ${FIELD} \
-crop_to_cutline \
-ot UInt16 \
-t_srs EPSG:4326 \
-srcnodata 0 \
-overwrite \
/vsis3/${TILE}/@.jp2 ${OUTP}_@.tif
gdal_merge.py \
-of GTiff \
-o ${OUTP}_RGB.tif \
-a_nodata 0 \
-ot UInt16 \
-separate \
${OUTP}_B04.tif ${OUTP}_B03.tif ${OUTP}_B02.tif
gdal_translate \
-of PNG \
-ot Byte \
-scale \
-a_nodata 0 \
-outsize 800% 800% \
${OUTP}_RGB.tif ${OUTP}_RGB.png
gdal_calc.py \
-A ${OUTP}_B04.tif \
-B ${OUTP}_B08.tif \
--calc "(B*1.-A*1.)/(B*1.+A*1.)" \
--type Float32 \
--format GTiff \
--overwrite \
--outfile ${OUTP}_NDVI.tif
gdaldem color-relief ${OUTP}_NDVI.tif rel.txt ${OUTP}_NDVI_color.tif -alpha
gdal_translate \
-of PNG \
-ot Byte \
-scale \
-a_nodata 0 \
-outsize 800% 800% \
${OUTP}_NDVI_color.tif ${OUTP}_NDVI_color.png
gdaldem color-relief ${OUTP}_NDVI.tif abs.txt ${OUTP}_NDVI_color_absoulte.tif -alpha
gdal_translate \
-of PNG \
-ot Byte \
-scale \
-a_nodata 0 \
-outsize 800% 800% \
${OUTP}_NDVI_color_absolute.tif ${OUTP}_NDVI_color_absolute.png
0% 200 0 0
35% 255 40 0
45% 255 150 0
55% 255 240 0
65% 0 230 0
75% 0 190 0
100% 0 130 0
nv 0 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment