Skip to content

Commit

Permalink
re-center TIFFs that cross the dateline
Browse files Browse the repository at this point in the history
  • Loading branch information
dmannarino committed Nov 3, 2024
1 parent 10b7765 commit 6634b8f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
42 changes: 42 additions & 0 deletions batch/scripts/_tiff_crosses_dateline.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
#
# Small Script to check if input raster will
# cross dateline when converting to EPSG:4326
#
# USAGE: ./crosses_dateline.sh infile [outfile]
#
# if no outfile is given, the script returns "true" or "false"
#
# Needs gdal 2.0+ and Python
#
# Credit: Slightly modified from https://gis.stackexchange.com/a/222341


if [ -z "${1}" ]; then
echo -e "Error: No input rasterfile given.\n> USAGE: ./crosses_dateline.sh infile"
exit 1
fi

# Get information, save it to variable as we need it several times
gdalinfo=$(gdalinfo "${1}" -json)

# If -json switch is not available exit!
if [ ! -z $(echo $gdalinfo | grep "^Usage:") ]; then
echo -e "Error: GDAL command failed, Version 2.0+ is needed"
exit 1
fi

function jsonq {
echo "${1}" | python -c "import json,sys; jdata = sys.stdin.read(); data = json.loads(jdata); print(data${2});"
}

ulx=$(jsonq "$gdalinfo" "['wgs84Extent']['coordinates'][0][0][0]")
llx=$(jsonq "$gdalinfo" "['wgs84Extent']['coordinates'][0][1][0]")
lrx=$(jsonq "$gdalinfo" "['wgs84Extent']['coordinates'][0][3][0]")
urx=$(jsonq "$gdalinfo" "['wgs84Extent']['coordinates'][0][2][0]")

crossing_dateline=false
test $(echo "${ulx}>${lrx}" | bc) -eq 1 && crossing_dateline=true
test $(echo "${llx}>${urx}" | bc) -eq 1 && crossing_dateline=true

echo -n "${crossing_dateline}"
13 changes: 12 additions & 1 deletion batch/scripts/_warp_and_upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ if aws s3 ls "$4"; then
exit 0
fi

warp_options=("-co COMPRESS=DEFLATE" "-co TILED=yes")

echo "Seeing if TIFF crosses the dateline"
crosses="$(./_tiff_crosses_dateline.sh $1)"
if [ "${crosses}" = "true" ]; then
echo "$1 crosses the dateline"
warp_options+=("--config CENTER_LONG 180")
else
echo "$1 does not cross the dateline"
fi

echo "Now warping $1 to $2"
gdalwarp "$1" "$2" -t_srs "$3" -co COMPRESS=DEFLATE -co TILED=yes
gdalwarp "$1" "$2" -t_srs "$3" "${warp_options[@]}"
echo "Done warping $1 to $2"

echo "Now uploading $2 to $4"
Expand Down

0 comments on commit 6634b8f

Please sign in to comment.