Skip to content

Instantly share code, notes, and snippets.

@brookisme
Last active August 1, 2016 14:50
Show Gist options
  • Save brookisme/739f182e0d8b169bad670cb1a094ac87 to your computer and use it in GitHub Desktop.
Save brookisme/739f182e0d8b169bad670cb1a094ac87 to your computer and use it in GitHub Desktop.
Create CSV from Multiband-TIFF
#!/usr/bin/env python
function set_dir {
SOURCE_DIR=$1
if [ -z "$2" ]
then
TARGET_DIR=$1
else
TARGET_DIR=$2
fi
# create directories if needed
if [ ! -d "$SOURCE_DIR" ]
then
mkdir -p $SOURCE_DIR
fi
if [ ! -d "$TARGET_DIR" ]
then
mkdir -p $TARGET_DIR
fi
echo "DIRECTORIES SET: " $SOURCE_DIR, $TARGET_DIR
}
function _set_band_string {
bstring=""
for i in $(seq 1 $1)
do
bstring=$bstring" -band "$i
done
}
function _convert {
if [ -z "$2" ]
then
TARGET_NAME=$1
else
TARGET_NAME=$2
fi
convert_cmd="gdal2xyz.py "$bstring" "$SOURCE_DIR/$1.tif" "$TARGET_DIR/$TARGET_NAME.csv
echo " "
echo "==================================="
echo " "
echo "CONVERTING TIF TO CSV WITH:"
echo " "
echo $convert_cmd
echo " "
echo "==================================="
echo " "
echo " "
$($convert_cmd)
}
function tocsv {
_set_band_string $2
_convert $1 $3
}
# call arguments verbatim:
$@
@brookisme
Copy link
Author

brookisme commented May 11, 2016

$  . tif2csv.sh
$  set_dir source/dir/path target/dir/path  # will create directories if need. target will default to source if not passed
$  tocsv source_file_name_without_tif_ext nbands target_file_name_without_csv_ext # nbands defaults to 1, target defaults to source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment