Skip to content

Instantly share code, notes, and snippets.

@rafdouglas
Created May 5, 2019 14:53
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 rafdouglas/9a5548b4a259ec2c7823826b65feeafd to your computer and use it in GitHub Desktop.
Save rafdouglas/9a5548b4a259ec2c7823826b65feeafd to your computer and use it in GitHub Desktop.
Script to correct the vertical mirror error of ASC files exported from QGIS through the "Raster->Conversion->Translate" command (which in turn invokes gdal_translate)
#!/bin/bash
#
# corrects the vertical mirror error of ASC files exported from QGIS
# through the "Raster->Conversion->Translate" command
# (which in turn invokes gdal_translate)
#
# RafDouglas - 190505
#
#
# example header section of the ASC file:
#
#ncols 600
#nrows 300
#xllcorner 565200.000000000000
#yllcorner 4797400.000000000000
#cellsize 10.000000000000
#NODATA_value 0
if [ -z $1 ]; then
echo "Usage: $0 input_file.asc"
exit
fi
if [ ! -e $1 ]; then
echo "Usage: $0 input_file.asc"
echo "The specified input file ($1) does not exist."
exit
fi
mypid=$$
mytmp_file='temp_'"$mypid"'.asc'
mytmp_prj='temp_'"$mypid"'.prj'
infile=$1
inprj=$(echo $infile|sed 's/.asc/.prj/g')
outfile=$(echo $infile|sed 's/.asc/_proper.asc/g')
outprj=$(echo $infile|sed 's/.asc/_proper.prj/g')
head -6 "$infile"|awk '{h[$1]=$2}END{
yllcorner_proper=h["yllcorner"]+h["nrows"]*h["cellsize"]
print "ncols " h["ncols"]
print "nrows " h["nrows"]
print "xllcorner " h["xllcorner"]
#print "yllcorner " h["yllcorner"]
print "yllcorner " yllcorner_proper
print "cellsize " h["cellsize"]
print "NODATA_value " h["NODATA_value"]
}' > "$mytmp_file"
#add the data rows in reversed order
tail -n+7 "$infile"|tac >> "$mytmp_file"
if [ ! -e $outfile ]; then
mv -n "$mytmp_file" "$outfile"
cp -n "$inprj" "$outprj"
echo
echo "Output file: $outfile is ready"
else
cp -n "$inprj" "$mytmp_prj"
echo
echo "Waring: $outfile is already present. Not overwriting it."
echo "Output file: $mytmp_file is ready"
fi
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment