-
Notifications
You must be signed in to change notification settings - Fork 6
/
load-friction-raster
executable file
·65 lines (56 loc) · 1.93 KB
/
load-friction-raster
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
set -euo pipefail
DATA_PATH=${DATA_PATH:-/data}
OUTPUT_PATH=${DATA_PATH}/friction/regions
export PGPASSWORD=$POSTGRES_PASSWORD;
if [ ! -d $DATA_PATH ]; then
echo Data path ${DATA_PATH} does not exist
exit 1
fi
raster_file=
force=
while [ $# -gt 0 ]; do
case $1 in
-h|--help)
echo "Usage: $0 [-f] raster_file"
echo "Only missing regions will be processed, unless the option -f is present"
exit 0
;;
-f|--force)
force=1
;;
*)
if [ ! -z "$raster_file" ]; then
echo Multiple file arguments specified
exit 1
fi
raster_file=$1
;;
esac
shift
done
if [ -z "$raster_file" -o ! -f "$raster_file" ]; then
echo Friction raster file $raster_file does not exist
exit 1
fi
echo "Clipping friction raster to country-level regions (admin_level = 0)"
echo "Using raster file ${raster_file}"
mkdir -p $OUTPUT_PATH
regions="$(psql -d $POSTGRES_DB -U $POSTGRES_USER -h $POSTGRES_HOST -p $POSTGRES_PORT -t -A -c 'SELECT id FROM regions WHERE admin_level = 0;')"
for region in $regions; do
echo -n Clipping for region $region ...
output_file=$OUTPUT_PATH/$region.tif
if [ ! -f "$output_file" ] || [ "$force" = "1" ]; then
rm -f $output_file
echo
# TODO: we may want to cut by the envelope (even buffer it a little) instead
# of the region itself to avoid errors and abrupt cuts in the region border
gdalwarp -co COMPRESS=DEFLATE \
-dstnodata -3.4e+38 \
-crop_to_cutline -csql "SELECT the_geom FROM regions WHERE id = ${region}" \
-cutline PG:"dbname=${POSTGRES_DB} host=${POSTGRES_HOST} port=${POSTGRES_PORT} user=${POSTGRES_USER} password=${POSTGRES_PASSWORD}" \
$raster_file $output_file
else
echo Clipped friction exists for region
fi
done